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.FdPersonnel;
import org.jeecg.modules.projectManagement.entity.FdContract;
import org.jeecg.modules.sampleAndConsumableManagement.entity.FdReagentManagement;
import org.jeecg.modules.sampleAndConsumableManagement.entity.FdReagentManagementRecipientRecord;
import org.jeecg.modules.sampleAndConsumableManagement.mapper.FdReagentManagementMapper;
import org.jeecg.modules.basedata.service.IFdPersonnelService;
import org.jeecg.modules.projectManagement.service.IFdProjectFileService;
import org.jeecg.modules.sampleAndConsumableManagement.service.IFdReagentManagementRecipientRecordService;
import org.jeecg.modules.sampleAndConsumableManagement.service.IFdReagentManagementService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
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;

/**
 * @Description: 试剂管理
 * @author： jeecg-boot
 * @date： 2019-06-12
 * @version： V1.0
 */
@Service
@Transactional
public class FdReagentManagementServiceImpl extends ServiceImpl<FdReagentManagementMapper, FdReagentManagement> implements IFdReagentManagementService {

    @Resource
    IFdReagentManagementService fdReagentManagementService;

    @Resource
    IFdProjectFileService fdProjectFileService;

    @Resource
    IFdReagentManagementRecipientRecordService fdReagentManagementRecipientRecordService;

    @Resource
    ISysDepartService sysDepartService;

    @Resource
    IFdPersonnelService fdPersonnelService;

    @Resource
    FdReagentManagementMapper fdReagentManagementMapper;

    /**
     * @return void
     * @Author gurui
     * @Description //TODO 添加 试剂管理同时添加 试剂管理领用记录
     * @Date 10:52 2019/6/12
     * @Param [fdReagentManagement]
     **/
    @Override
    public void addAll(FdReagentManagement fdReagentManagement) {
        if (fdReagentManagement.getId() == "" || fdReagentManagement.getId() == null) {
            fdReagentManagement.setDelFlag("0");
            fdReagentManagementService.save(fdReagentManagement);
        } else {
            fdReagentManagementService.updateById(fdReagentManagement);
            //删除试剂管理领用记录
            LambdaQueryWrapper<FdReagentManagementRecipientRecord> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(FdReagentManagementRecipientRecord::getPkReagentManagement, fdReagentManagement.getId());
            fdReagentManagementRecipientRecordService.remove(queryWrapper2);
        }
        //项目负责人
        LambdaQueryWrapper<FdPersonnel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FdPersonnel::getId, fdReagentManagement.getPkPersonInCharge());
        List<FdPersonnel> personnelList = fdPersonnelService.list(queryWrapper);
        for (FdPersonnel fdPersonnel : personnelList) {
            fdReagentManagement.setPersonInCharge(fdPersonnel.getName());
        }
        //生物分析负责人
        LambdaQueryWrapper<FdPersonnel> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(FdPersonnel::getId, fdReagentManagement.getPkBioanalystLeader());
        List<FdPersonnel> personnelList1 = fdPersonnelService.list(queryWrapper1);
        for (FdPersonnel fdPersonnel : personnelList1) {
            fdReagentManagement.setBioanalystLeader(fdPersonnel.getName());
        }
        //部门
        LambdaQueryWrapper<SysDepart> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(SysDepart::getId,fdReagentManagement.getPkDepart());
        List<SysDepart> departList = sysDepartService.list(queryWrapper2);
        for (SysDepart sysDepart:departList){
            fdReagentManagement.setDepartName(sysDepart.getDepartName());
        }


        for (FdReagentManagementRecipientRecord fdReagentManagementRecipientRecord : fdReagentManagement.getFdReagentManagementRecipientRecordList()) {
            fdReagentManagementRecipientRecord.setId("");
            fdReagentManagementRecipientRecord.setPkReagentManagement(fdReagentManagement.getId());
        }
        //批量保存
        fdReagentManagementRecipientRecordService.saveBatch(fdReagentManagement.getFdReagentManagementRecipientRecordList());


    }

    /**
     * @Author gurui
     * @Description //TODO 根据合同id查询相关数据
     * @Date 15:27 2019/6/27
     * @Param [contractId]
     * @return org.jeecg.modules.sampleAndConsumableManagement.entity.FdReagentManagement
     **/
    @Override
    public FdReagentManagement queryReagentByConId(String contractId) {
        return fdReagentManagementMapper.queryReagentByConId(contractId);
    }

    /**
     * @Author gurui
     * @Description //TODO 删除试剂管理同时删除领用记录
     * @Date 15:27 2019/6/27
     * @Param [id]
     * @return boolean
     **/
    @Override
    public boolean deleteAll(String id) {
        boolean result = true;
        try {
            fdReagentManagementService.removeById(id);
            LambdaQueryWrapper<FdReagentManagementRecipientRecord> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FdReagentManagementRecipientRecord::getPkReagentManagement,id);
            fdReagentManagementRecipientRecordService.remove(queryWrapper);
        } catch (Exception e) {
            result = false;
            e.printStackTrace();
        }
        return result;
    }

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

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