package com.lightinit.hsdataportal.impl;

import com.lightinit.hsdataportal.dictionary.DicRqrmntType;
import com.lightinit.hsdataportal.entity.BaseExample;
import com.lightinit.hsdataportal.entity.DataRequirementData;
import com.lightinit.hsdataportal.entity.Requirement;
import com.lightinit.hsdataportal.entity.RequirementData;
import com.lightinit.hsdataportal.mapper.DataRequirementDataMapper;
import com.lightinit.hsdataportal.mapper.RequirementDataMapper;
import com.lightinit.hsdataportal.mapper.RequirementMapper;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IMyDataRequirementService;
import com.lightinit.hsdataportal.spring.DynamicDataContextHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class MyDataRequirementServiceImpl implements IMyDataRequirementService {
    @Autowired
    private RequirementMapper requirementMapper;
    @Autowired
    private RequirementDataMapper requirementDataMapper;
    @Autowired
    private DataRequirementDataMapper dataRequirementDataMapper;
    @Override
    public List<MyRequirementListModel> selectOpenList(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
            return requirementMapper.selectOpenDataList(id);

    }

    @Override
    public List<MyRequirementListModel> selectShareList(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return requirementMapper.selectShareDataList(id);

    }

    @Override
    public Requirement selectRequirementById(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return requirementMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResultState<Long> offLine(Requirement requirement) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();
        int sqlResult = requirementMapper.updateByPrimaryKey(requirement);

        if (!(sqlResult > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("下架失败");
        }
        resultState.setData(requirement.getId());
        return resultState;
    }

    @Override
    @Transactional
    public ResultState<Long> del(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();
        int sqlResult =0; int sqlResulta1 =0;int sqlResulta2 =0;
        sqlResulta2 = dataRequirementDataMapper.deleteByResourceId(id);
        sqlResulta1 = requirementDataMapper.deleteByPrimaryKey(id);
        sqlResult = requirementMapper.deleteByPrimaryKey(id);

        if (!(sqlResult > 0 &&sqlResulta1>0 &&sqlResulta2>0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("删除失败");
        }
        resultState.setData(id);
        return resultState;
    }

    @Override
    public Requirement selectById(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return requirementMapper.selectByPrimaryKey(id);
    }

    @Override
    public RequirementData selectByRqmentId(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return requirementDataMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<DataRequirementData> selectByDataRqmentId(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return dataRequirementDataMapper.selectByDataRqmentId(id);
    }

    @Override
    public ResultState doEdit(DataRequirementAddModel inputModel) {
       String str = inputModel.getDelIds();
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();
        int sqlResult =0; int sqlResult1 =0;int sqlResult2 =0;int sqlResult3 =0;
        //List<DataRequirementData> list = dataRequirementDataMapper.selectByDataRqmentId(inputModel.getId());
        //删除前台删除的DataRequirementData
        if(str!=null && str!="") {
           String[] strr=str.substring(0, str.length()-1).split(",");
            for (int i = 0; i <strr.length ; i++) {
                sqlResult = dataRequirementDataMapper.deleteByPrimaryKey(Long.valueOf(strr[i]));
            }
            if (!( sqlResult>0 )){
                resultState.setStateCode(ResultStateCode.INVALID_DATA);
                resultState.setMsg("编辑失败");
            }
        }
        List<DataRequirementDataModel>   dataRequirementDataList = inputModel.getDataRequirementDataList();
        for (DataRequirementDataModel model:dataRequirementDataList) {
            if(model!=null){
                if(model.getId()!=null&& ((model.getDataName()!=null && model.getDataName()!="") ||(model.getDataItem()!=null &&model.getDataItem()!="" ))) {
                    DataRequirementData model1 = new DataRequirementData();
                    BeanUtils.copyProperties(model,model1);
                    model1.setDataId(inputModel.getId());
                    model1.setRqrmntId(inputModel.getId());
                    sqlResult1 =  dataRequirementDataMapper.updateByPrimaryKey(model1);
                }else if((model.getDataName()!=null && model.getDataName()!="") ||(model.getDataItem()!=null &&model.getDataItem()!="" )){
                    DataRequirementData model1 = new DataRequirementData();
                    BeanUtils.copyProperties(model,model1);
                    model1.setDataId(inputModel.getId());
                    model1.setRqrmntId(inputModel.getId());
                    sqlResult1 = dataRequirementDataMapper.insert(model1);
                }
            }

        }
        //编辑requirementData表
        RequirementData requirementData = new RequirementData();
        BeanUtils.copyProperties(inputModel,requirementData);
        sqlResult2 = requirementDataMapper.updateByPrimaryKey(requirementData);
        Requirement requirement = new Requirement();
        BeanUtils.copyProperties(inputModel,requirement);
        sqlResult3 = requirementMapper.updateByPrimaryKey(requirement);
        if (!( sqlResult1>0 && sqlResult2>0 && sqlResult3>0 )){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("编辑失败");
        }
        resultState.setData(inputModel.getId());
        return resultState;

    }
}
