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.*;
import org.jeecg.modules.basedata.mapper.FdContractClassMapper;
import org.jeecg.modules.basedata.mapper.FdMatrixTypeMapper;
import org.jeecg.modules.projectManagement.entity.FdContract;
import org.jeecg.modules.sampleAndConsumableManagement.mapper.FdBlankSubstrateMapper;
import org.jeecg.modules.basedata.service.*;
import org.jeecg.modules.projectManagement.service.IFdProjectFileService;
import org.jeecg.modules.sampleAndConsumableManagement.entity.FdBlankSubstrate;
import org.jeecg.modules.sampleAndConsumableManagement.entity.FdBlankSubstrateFileUpload;
import org.jeecg.modules.sampleAndConsumableManagement.entity.FdBlankSubstrateRecipientRecord;
import org.jeecg.modules.sampleAndConsumableManagement.service.IFdBlankSubstrateFileUploadService;
import org.jeecg.modules.sampleAndConsumableManagement.service.IFdBlankSubstrateRecipientRecordService;
import org.jeecg.modules.sampleAndConsumableManagement.service.IFdBlankSubstrateService;
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.Arrays;
import java.util.List;

/**
 * @Description: 空白基质管理
 * @author： jeecg-boot
 * @date： 2019-06-10
 * @version： V1.0
 */
@Service
@Transactional
public class FdBlankSubstrateServiceImpl extends ServiceImpl<FdBlankSubstrateMapper, FdBlankSubstrate> implements IFdBlankSubstrateService {


    @Resource
    IFdBlankSubstrateService fdBlankSubstrateService;

    @Resource
    IFdProjectFileService fdProjectFileService;

    @Resource
    IFdHospitalService fdHospitalService;

    @Resource
    IFdBlankSubstrateFileUploadService fdBlankSubstrateFileUploadService;

    @Resource
    IFdBlankSubstrateRecipientRecordService fdBlankSubstrateRecipientRecordService;

    @Resource
    FdBlankSubstrateMapper fdBlankSubstrateMapper;

    @Resource
    FdMatrixTypeMapper fdMatrixTypeMapper;


    /**
     * @return void
     * @Author gurui
     * @Description //TODO 新增空包基质管理表 同时添加   空白基质文件上传 空白基质领用记录
     * @Date 10:21 2019/6/11
     * @Param [fdBlankSubstrate]
     **/
    @Override
    public void addAll(FdBlankSubstrate fdBlankSubstrate) {
        if (fdBlankSubstrate.getId() == null || fdBlankSubstrate.getId() == "") {
            fdBlankSubstrate.setDelFlag("0");
            fdBlankSubstrateService.save(fdBlankSubstrate);
        } else {
            fdBlankSubstrateService.updateById(fdBlankSubstrate);
            //删除空白基质文件上传
            LambdaQueryWrapper<FdBlankSubstrateFileUpload> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(FdBlankSubstrateFileUpload::getPkBlankSubstrate, fdBlankSubstrate.getId());
            fdBlankSubstrateFileUploadService.remove(queryWrapper2);
            //删除空白基质领用记录
            LambdaQueryWrapper<FdBlankSubstrateRecipientRecord> queryWrapper3 = new LambdaQueryWrapper<>();
            queryWrapper3.eq(FdBlankSubstrateRecipientRecord::getPkBlankSubstrate, fdBlankSubstrate.getId());
            fdBlankSubstrateRecipientRecordService.remove(queryWrapper3);
        }
        if (fdBlankSubstrate.getFdBlankSubstrateFileUploadList()!=null){
            for (FdBlankSubstrateFileUpload fdBlankSubstrateFileUpload : fdBlankSubstrate.getFdBlankSubstrateFileUploadList()) {
                fdBlankSubstrateFileUpload.setPkBlankSubstrate(fdBlankSubstrate.getId());
                fdBlankSubstrateFileUpload.setId(null);
            }
            fdBlankSubstrateFileUploadService.saveBatch(fdBlankSubstrate.getFdBlankSubstrateFileUploadList());
        }
        if (fdBlankSubstrate.getFdBlankSubstrateRecipientRecordList()!=null){
            for (FdBlankSubstrateRecipientRecord fdBlankSubstrateRecipientRecord : fdBlankSubstrate.getFdBlankSubstrateRecipientRecordList()) {
                fdBlankSubstrateRecipientRecord.setId("");
                fdBlankSubstrateRecipientRecord.setPkBlankSubstrate(fdBlankSubstrate.getId());
            }
            fdBlankSubstrateRecipientRecordService.saveBatch(fdBlankSubstrate.getFdBlankSubstrateRecipientRecordList());
        }





    }

    /**
     * @return org.jeecg.modules.sampleAndConsumableManagement.entity.FdBlankSubstrate
     * @Author gurui
     * @Description //TODO 根据合同id查询项目档案相关数据
     * @Date 10:47 2019/6/26
     * @Param [contractId]
     **/
    @Override
    public FdBlankSubstrate queryBlankSubstrateByConId(String pkProjectFile) {
        FdBlankSubstrate fdBlankSubstrate = fdBlankSubstrateMapper.queryBlankSubstrateByConId(pkProjectFile);
        //根据合同id查询基质类型名称z
        queryMatrixType(fdBlankSubstrate);
        return fdBlankSubstrate;
    }

    /**
     * @Author gurui
     * @Description //TODO 删除空白基质 同时删除 领用记录 文件上传
     * @Date 14:30 2019/6/27
     * @Param [id]
     * @return boolean
     **/
    @Override
    public boolean deleteAll(String id) {
        boolean result = true;
        try {
            fdBlankSubstrateService.removeById(id);
            //删除空白基质文件上传
            LambdaQueryWrapper<FdBlankSubstrateFileUpload> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(FdBlankSubstrateFileUpload::getPkBlankSubstrate, id);
            fdBlankSubstrateFileUploadService.remove(queryWrapper2);
            //删除空白基质领用记录
            LambdaQueryWrapper<FdBlankSubstrateRecipientRecord> queryWrapper3 = new LambdaQueryWrapper<>();
            queryWrapper3.eq(FdBlankSubstrateRecipientRecord::getPkBlankSubstrate, id);
            fdBlankSubstrateRecipientRecordService.remove(queryWrapper3);
        }catch (Exception e){
            result = false;
            e.printStackTrace();
        }
        return result;
    }

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

    @Override
    public List<FdContract> getFileContractList() {
        return fdBlankSubstrateMapper.getFileContractList();
    }

    //根据合同id查询基质类型名称
    public void queryMatrixType(FdBlankSubstrate fdBlankSubstrate){
        //根据合同id查询基质类型名称
        if (fdBlankSubstrate.getSubstrateTypeName() != null && fdBlankSubstrate.getSubstrateTypeName() != "") {
            //截取合同类型id
            List<String> list = Arrays.asList(fdBlankSubstrate.getSubstrateTypeName().split(","));
            //根据id获取相关信息
            List<FdMatrixType> matrixTypes = fdMatrixTypeMapper.selectBatchIds(list);
            if (!matrixTypes.isEmpty()) {
                //基质类型名称
                String matrixType = "";
                for (FdMatrixType fdMatrixType : matrixTypes) {
                    if (matrixType == "") {
                        matrixType = fdMatrixType.getName();
                    } else {
                        matrixType += "," + fdMatrixType.getName();
                    }
                }
                fdBlankSubstrate.setSubstrateTypeName(matrixType);
            }
        }
    }


}
