package org.jeecg.modules.sampleAndConsumableManagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.basedata.entity.FdContractClass;
import org.jeecg.modules.basedata.entity.FdMatrixType;
import org.jeecg.modules.basedata.service.IFdContractClassService;
import org.jeecg.modules.basedata.service.IFdMatrixTypeService;
import org.jeecg.modules.financeManage.entity.FdSampleStorageRevenueRecognition;
import org.jeecg.modules.financeManage.service.IFdSampleStorageRevenueRecognitionService;
import org.jeecg.modules.sampleAndConsumableManagement.entity.FdSampleStorage;
import org.jeecg.modules.basedata.entity.FdStoragePlace;
import org.jeecg.modules.sampleAndConsumableManagement.entity.FdStandardProductsRecipientRecord;
import org.jeecg.modules.sampleAndConsumableManagement.mapper.FdSampleStorageMapper;
import org.jeecg.modules.basedata.service.IFdPersonnelService;
import org.jeecg.modules.projectManagement.service.IFdProjectFileService;
import org.jeecg.modules.sampleAndConsumableManagement.service.IFdSampleStorageService;
import org.jeecg.modules.basedata.service.IFdStoragePlaceService;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Description: 样本存储
 * @author： jeecg-boot
 * @date：   2019-06-06
 * @version： V1.0
 */
@Service
@Transactional
public class FdSampleStorageServiceImpl extends ServiceImpl<FdSampleStorageMapper, FdSampleStorage> implements IFdSampleStorageService {

    @Resource
    FdSampleStorageMapper fdSampleStorageMapper;

    @Resource
    IFdSampleStorageService fdSampleStorageService;

    @Resource
    IFdProjectFileService fdProjectFileService;

    @Resource
    IFdStoragePlaceService fdStoragePlaceService;

    @Resource
    IFdPersonnelService fdPersonnelService;
    @Resource
    IFdContractClassService fdContractClassService;
    @Resource
    IFdMatrixTypeService fdMatrixTypeService;

    @Resource
    ISysDictService iSysDictService;

    @Override
    public IPage<FdSampleStorage> findByPage(Page<FdSampleStorage> page, QueryWrapper<FdSampleStorage> queryWrapper) {
        return fdSampleStorageMapper.findByPage(page,queryWrapper);
    }

    /**
     * @Author gurui
     * @Description //TODO 添加样本存储同时 添加  存储位置
     * @Date 14:30 2019/6/6
     * @Param [fdSampleStorage]
     * @return void
     **/
    @Override
    public void addAll(FdSampleStorage fdSampleStorage) {
        if (fdSampleStorage.getId()==null || fdSampleStorage.getId()==""){
            fdSampleStorage.setDelFlag("0");
            fdSampleStorageService.save(fdSampleStorage);
        }else{
            fdSampleStorageService.updateById(fdSampleStorage);
            //删除存储位置
            LambdaQueryWrapper<FdStoragePlace> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(FdStoragePlace::getId,fdSampleStorage.getPkPrimarySampleStoragePlace());
            fdStoragePlaceService.remove(queryWrapper2);

        }
        for (FdStoragePlace fdStoragePlace:fdSampleStorage.getFdStoragePlaceList()){
            fdStoragePlace.setId("");
            fdStoragePlace.setId(fdSampleStorage.getPkPrimarySampleStoragePlace());
        }
        //批量存储
        fdStoragePlaceService.saveBatch(fdSampleStorage.getFdStoragePlaceList());

    }

    /***
     * @Title:根据合同id查询项目档案的相关数据
            * @Description:TODO
            * @param contractId
            * @return org.jeecg.modules.sampleAndConsumableManagement.entity.FdSampleStorage
            * @exception
            * @author 陈创
            * @date 2019/6/25 13:22
            * @updatehistory:
     *   2019/6/25 13:22 新增
            */
    @Override
    public FdSampleStorage getSampleStorageByConId(String projectFileId) {
        FdSampleStorage fdSampleStorage=fdSampleStorageMapper.getSampleStorageByConId(projectFileId);

        //获取合同方名称
        List<Map<String,Object>> dictList=iSysDictService.queryDictItemsByCode("contract_square");
        if(dictList!=null){
            for(Map<String,Object> map:dictList){
                if(map.get("value").equals(fdSampleStorage.getPkContractParty())){
                    fdSampleStorage.setContractPartyName(map.get("text")+"");
                }
            }
        }

        //合同类型名称
        contractTypeName(fdSampleStorage);
        //基质类型名称
        matrixTypeName(fdSampleStorage);

        return fdSampleStorage;
    }


    //根据合同类型id查询合同类型名称
    public void contractTypeName(FdSampleStorage fdSampleStorage){
        if(fdSampleStorage.getContractType()!=null&&fdSampleStorage.getContractType()!=""){
            //截取合同类型id
            String[] conClassIds = fdSampleStorage.getContractType().split(",");
            //根据合同类型id获取相关信息
            List<FdContractClass> fdContractClassList = fdContractClassService.selectBatchIds(conClassIds);
            if(!fdContractClassList.isEmpty()){
                //合同类型名称
                String conClassName = "";
                //循环获取名称
                for (FdContractClass fdContractClass : fdContractClassList) {
                    if (conClassName == "") {
                        conClassName = fdContractClass.getName();
                    } else {
                        conClassName += "、" + fdContractClass.getName();
                    }
                }
                fdSampleStorage.setContractTypeName(conClassName);
            }
        }
    }

    //根据合同类型id查询合同类型名称
    public void matrixTypeName(FdSampleStorage fdSampleStorage){
        if(fdSampleStorage.getSubstrateType()!=null&&fdSampleStorage.getSubstrateType()!=""){
            //截取合同类型id
            String[] matClassIds = fdSampleStorage.getSubstrateType().split(",");
            //根据合同类型id获取相关信息
            List<FdMatrixType> fdMatrixClassList = fdMatrixTypeService.selectBatchIds(matClassIds);
            if(!fdMatrixClassList.isEmpty()){
                //合同类型名称
                String matClassName = "";
                //循环获取名称
                for (FdMatrixType fdMatrixType : fdMatrixClassList) {
                    if (matClassName == "") {
                        matClassName = fdMatrixType.getName();
                    } else {
                        matClassName += "、" + fdMatrixType.getName();
                    }
                }
                fdSampleStorage.setSubstrateTypeName(matClassName);
            }
        }
    }


    @Resource
    IFdSampleStorageRevenueRecognitionService iFdSampleStorageRevenueRecognitionService;

    @Override
    public void addCustom(FdSampleStorage fdSampleStorage) {
        fdSampleStorageMapper.insert(fdSampleStorage);
        //如果样本存储收费开始日期不等于空则样本存储收入确认产生数据
        if(fdSampleStorage.getSampleStorageChargeStartDate()!=null){
            //获取样本存储收入确认的基本数据
            FdSampleStorageRevenueRecognition fdSampleStorageRevenueRecognition=new FdSampleStorageRevenueRecognition();
            //获取样本储存的数据
            getFdSampleStorageRevenueRecognition(fdSampleStorageRevenueRecognition,fdSampleStorage);
            iFdSampleStorageRevenueRecognitionService.sampleStorageTriggerAdd(fdSampleStorageRevenueRecognition);
        }

    }

    @Override
    public void updateCustom(FdSampleStorage fdSampleStorage) {
        fdSampleStorageMapper.updateById(fdSampleStorage);
        //如果样本存储收费开始日期不等于空则样本存储收入确认产生数据
        if(fdSampleStorage.getSampleStorageChargeStartDate()!=null){
            LambdaQueryWrapper<FdSampleStorageRevenueRecognition> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(FdSampleStorageRevenueRecognition::getDelFlag,"0");
            lambdaQueryWrapper.eq(FdSampleStorageRevenueRecognition::getProjectFileId,fdSampleStorage.getPkProjectFile());
            List<FdSampleStorageRevenueRecognition> fdSampleStorageRevenueRecognitionList=iFdSampleStorageRevenueRecognitionService.list(lambdaQueryWrapper);
            if(fdSampleStorageRevenueRecognitionList!=null&&fdSampleStorageRevenueRecognitionList.size()>0){//有对应的确认则修改
                FdSampleStorageRevenueRecognition fdSampleStorageRevenueRecognition=new FdSampleStorageRevenueRecognition();
                for(FdSampleStorageRevenueRecognition storageRevenueRecognition:fdSampleStorageRevenueRecognitionList){
                    fdSampleStorageRevenueRecognition=storageRevenueRecognition;
                }
                //获取样本储存的数据
                getFdSampleStorageRevenueRecognition(fdSampleStorageRevenueRecognition,fdSampleStorage);
                //修改确认信息
                iFdSampleStorageRevenueRecognitionService.sampleStorageTriggerUpdate(fdSampleStorageRevenueRecognition);
            }else{//没有则新增
                //获取样本存储收入确认的基本数据
                FdSampleStorageRevenueRecognition fdSampleStorageRevenueRecognition=new FdSampleStorageRevenueRecognition();
                //获取样本储存的数据
                getFdSampleStorageRevenueRecognition(fdSampleStorageRevenueRecognition,fdSampleStorage);
                //新增确认信息
                iFdSampleStorageRevenueRecognitionService.sampleStorageTriggerAdd(fdSampleStorageRevenueRecognition);
            }
        }

    }

    //获取样本存储信息
    public void getFdSampleStorageRevenueRecognition(FdSampleStorageRevenueRecognition fdSampleStorageRevenueRecognition,FdSampleStorage fdSampleStorage){
        fdSampleStorageRevenueRecognition.setCode(fdSampleStorage.getContractCode());
        fdSampleStorageRevenueRecognition.setContractId(fdSampleStorage.getPkContract());
        fdSampleStorageRevenueRecognition.setProjectFileId(fdSampleStorage.getPkProjectFile());
        fdSampleStorageRevenueRecognition.setContractPartyId(fdSampleStorage.getPkContractParty());
        fdSampleStorageRevenueRecognition.setContractPartyName(fdSampleStorage.getContractPartyName());
        fdSampleStorageRevenueRecognition.setCustomerId(fdSampleStorage.getPkBidParty());
        fdSampleStorageRevenueRecognition.setCustomerName(fdSampleStorage.getBidPartyName());
        fdSampleStorageRevenueRecognition.setPfBioanalysisPiId(fdSampleStorage.getBioanalysisPm());
        fdSampleStorageRevenueRecognition.setPfBioanalysisPiName(fdSampleStorage.getBioanalysisPmName());
        fdSampleStorageRevenueRecognition.setCompoundName(fdSampleStorage.getCompound());
        fdSampleStorageRevenueRecognition.setMatrixTypeId(fdSampleStorage.getSubstrateType());
        fdSampleStorageRevenueRecognition.setMatrixTypeName(fdSampleStorage.getSubstrateTypeName());
        fdSampleStorageRevenueRecognition.setStartConfirmationDate(fdSampleStorage.getSampleStorageChargeStartDate());
        fdSampleStorageRevenueRecognition.setEndConfirmationDate(fdSampleStorage.getSampleStorageChargeExpireDate());
    }
}
