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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.basedata.entity.FdCustomer;
import org.jeecg.modules.basedata.entity.FdCustomerContacts;
import org.jeecg.modules.basedata.entity.FdPersonnel;
import org.jeecg.modules.projectManagement.mapper.FdProjectFileMapper;
import org.jeecg.modules.basedata.service.*;
import org.jeecg.modules.projectManagement.entity.*;
import org.jeecg.modules.projectManagement.service.*;
import org.jeecg.modules.system.entity.SysRole;
import org.jeecg.modules.system.service.ISysRoleService;
import org.jeecg.modules.system.service.impl.SysDictServiceImpl;
import org.springframework.stereotype.Service;

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

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: 项目档案
 * @author： jeecg-boot
 * @date： 2019-05-28
 * @version： V1.0
 */
@Service
@Transactional
public class FdProjectFileServiceImpl extends ServiceImpl<FdProjectFileMapper, FdProjectFile> implements IFdProjectFileService {

    @Resource
    FdProjectFileMapper fdProjectFileMapper;

    @Resource
    IFdProjectFileService fdProjectFileService;

    @Resource
    IFdProjectFileCompoundService fdProjectFileCompoundService;

    @Resource
    IFdProjectFileContactsService fdProjectFileContactsService;

    @Resource
    IFdProjectFileMembersService fdProjectFileMembersService;

    @Resource
    IFdProjectFileMethodService fdProjectFileMethodService;

    @Resource
    IFdProjectFileConfirmService fdProjectFileConfirmService;

    @Resource
    IFdProjectFileValidationService fdProjectFileValidationService;

    @Resource
    IFdProjectFileSampleService fdProjectFileSampleService;

    @Resource
    IFdProjectFileUploadService fdProjectFileUploadService;

    @Resource
    IFdHospitalService fdHospitalService;

    @Resource
    IFdProjectFileBidpartyService fdProjectFileBidpartyService;

    @Resource
    IFdContractCompoundService fdContractCompoundService;

    @Resource
    IFdContractService fdContractService;

    @Resource
    IFdCustomerService fdCustomerService;

    @Resource
    ISysRoleService sysRoleService;

    @Resource
    SysDictServiceImpl sysDictService;
    @Resource
    IFdProjectQuotationService fdProjectQuotationService;


    /**
     * @return com.baomidou.mybatisplus.core.metadata.IPage<org.jeecg.modules.projectManagement.entity.FdProjectFile>
     * @Author gurui
     * @Description //TODO 分页连接查询
     * @Date 10:25 2019/5/29
     * @Param [page, queryWrapper]
     **/
    @Override
    public IPage<FdProjectFile> findByPage(Page<FdProjectFile> page, QueryWrapper<FdProjectFile> queryWrapper) {
        return fdProjectFileMapper.findByPage(page, queryWrapper);
    }

    @Override
    public IPage<FdProjectFile> validationCodeFindByPage(Page<FdProjectFile> page, QueryWrapper<FdProjectFile> queryWrapper) {
        return fdProjectFileMapper.validationCodeFindByPage(page, queryWrapper);
    }

    /**
     * @return void
     * @Author gurui
     * @Description //TODO 根据id删除化合物
     * @Date 17:33 2019/6/13
     * @Param [id]
     **/
    @Override
    public void deleteCompound(String id) {
        //获得要删除id
        List list = Arrays.asList(id.split(","));
        FdProjectFile projectFile = new FdProjectFile();
        for (int i = 0; i < list.size(); i++) {
            //获取化合物信息
            projectFile.setId(list.get(i).toString());
            FdProjectFileCompound compound = new FdProjectFileCompound();
            compound.setDelFlag("1");
            UpdateWrapper<FdProjectFileCompound> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("project_id", projectFile.getId());
            fdProjectFileCompoundService.update(compound, updateWrapper);
        }

    }

    /**
     * @return void
     * @Author gurui
     * @Description //TODO 根据id删除联系人
     * @Date 17:33 2019/6/13
     * @Param [id]
     **/
    @Override
    public void deleteContracts(String id) {
        //获得要删除的id
        List list = Arrays.asList(id.split(","));
        FdProjectFile projectFile = new FdProjectFile();
        for (int i = 0; i < list.size(); i++) {
            projectFile.setId(list.get(i).toString());
            //获取联系人
            FdProjectFileContacts contacts = new FdProjectFileContacts();
            contacts.setDelFlag("1");
            UpdateWrapper<FdProjectFileContacts> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("project_id", projectFile.getId());
            fdProjectFileContactsService.update(contacts, updateWrapper);
        }
    }

    /**
     * @return void
     * @Author gurui
     * @Description //TODO 根据id批量删除项目档案同时删除其他
     * @Date 10:58 2019/6/18
     * @Param [id]
     **/
    @Override
    public void deleteAll(String id) {
        //获得要删除的id
        List<String> list = Arrays.asList(id.split(","));
        FdProjectFile fdProjectFile = new FdProjectFile();
        for (int i = 0; i < list.size(); i++) {
            fdProjectFile.setId(list.get(i));
            //删除化合物
            LambdaQueryWrapper<FdProjectFileCompound> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(FdProjectFileCompound::getProjectId, fdProjectFile.getId());
            fdProjectFileCompoundService.remove(queryWrapper1);
            //删除联系人
            LambdaQueryWrapper<FdProjectFileContacts> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(FdProjectFileContacts::getProjectId, fdProjectFile.getId());
            fdProjectFileContactsService.remove(queryWrapper2);
            //删除项目成员
            LambdaQueryWrapper<FdProjectFileMembers> queryWrapper3 = new LambdaQueryWrapper<>();
            queryWrapper3.eq(FdProjectFileMembers::getProjectId, fdProjectFile.getId());
            fdProjectFileMembersService.remove(queryWrapper3);
            //删除生物分析方法号
            LambdaQueryWrapper<FdProjectFileMethod> queryWrapper4 = new LambdaQueryWrapper<>();
            queryWrapper4.eq(FdProjectFileMethod::getProjectId, fdProjectFile.getId());
            fdProjectFileMethodService.remove(queryWrapper4);
            //删除生物分析方法确认项目号
            LambdaQueryWrapper<FdProjectFileConfirm> queryWrapper5 = new LambdaQueryWrapper<>();
            queryWrapper5.eq(FdProjectFileConfirm::getProjectId, fdProjectFile.getId());
            fdProjectFileConfirmService.remove(queryWrapper5);
            //删除生物分析方法验证号
            LambdaQueryWrapper<FdProjectFileValidation> queryWrapper6 = new LambdaQueryWrapper<>();
            queryWrapper6.eq(FdProjectFileValidation::getProjectId, fdProjectFile.getId());
            fdProjectFileValidationService.remove(queryWrapper6);
            //删除生物分析样本分析号
            LambdaQueryWrapper<FdProjectFileSample> queryWrapper7 = new LambdaQueryWrapper<>();
            queryWrapper7.eq(FdProjectFileSample::getProjectId, fdProjectFile.getId());
            fdProjectFileSampleService.remove(queryWrapper7);
            //删除项目文件上传
            LambdaQueryWrapper<FdProjectFileUpload> queryWrapper8 = new LambdaQueryWrapper<>();
            queryWrapper8.eq(FdProjectFileUpload::getProjectId, fdProjectFile.getId());
            fdProjectFileUploadService.remove(queryWrapper8);
            //删除申办方
            LambdaQueryWrapper<FdProjectFileBidparty> queryWrapper9 = new LambdaQueryWrapper<>();
            queryWrapper9.eq(FdProjectFileBidparty::getProjectId, fdProjectFile.getId());
            fdProjectFileBidpartyService.remove(queryWrapper9);
            //删除本体
            fdProjectFileService.removeById(fdProjectFile);
        }
    }

    /***
     * @Title:项目成员数据
     * @Description:TODO
     * @param
     * @return java.util.List<org.jeecg.modules.basedata.entity.FdPersonnel>
     * @exception
     * @author 陈创
     * @date 2019/7/2 14:06
     * @updatehistory:
     *   2019/7/2 14:06 新增
     */
    @Override
    public List<FdPersonnel> getPersonnelList() {
        return fdProjectFileMapper.getPersonnelList();
    }

    //新增
    public void projectSave(FdProjectFile fdProjectFile) {
        //新增档案
        fdProjectFile.setDelFlag("0");
        String status=fdProjectFile.getStatus().trim();//去除空格的状态
        //状态为空则默认为0
        if(StringUtils.isEmpty(status)){
            fdProjectFile.setStatus("0");
        }
        fdProjectFileService.save(fdProjectFile);
    }

    //修改
    public void projectUpdate(FdProjectFile fdProjectFile) {
        //项目档案id
        String projectId = fdProjectFile.getId();
        //删除化合物
        LambdaQueryWrapper<FdProjectFileCompound> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(FdProjectFileCompound::getProjectId, projectId);
        queryWrapper1.eq(FdProjectFileCompound::getDelFlag, "0");
        // fdProjectFileCompoundService.remove(queryWrapper1);
        //需修改化合物的值
        FdProjectFileCompound fdProjectFileCompound=new FdProjectFileCompound();
        fdProjectFileCompound.setDelFlag("1");
        fdProjectFileCompoundService.update(fdProjectFileCompound,queryWrapper1);

        //删除联系人
        LambdaQueryWrapper<FdProjectFileContacts> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(FdProjectFileContacts::getProjectId, projectId);
        queryWrapper2.eq(FdProjectFileContacts::getDelFlag, "0");
        // fdProjectFileContactsService.remove(queryWrapper2);
        //需修改联系人的值
        FdProjectFileContacts fdProjectFileContacts=new FdProjectFileContacts();
        fdProjectFileContacts.setDelFlag("1");
        fdProjectFileContactsService.update(fdProjectFileContacts,queryWrapper2);

        //删除项目成员
        LambdaQueryWrapper<FdProjectFileMembers> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(FdProjectFileMembers::getProjectId, projectId);
        queryWrapper3.eq(FdProjectFileMembers::getDelFlag, "0");
        // fdProjectFileMembersService.remove(queryWrapper3);
        FdProjectFileMembers fdProjectFileMembers=new FdProjectFileMembers();
        fdProjectFileMembers.setDelFlag("1");
        fdProjectFileMembersService.update(fdProjectFileMembers,queryWrapper3);

        //删除生物分析方法号
        LambdaQueryWrapper<FdProjectFileMethod> queryWrapper4 = new LambdaQueryWrapper<>();
        queryWrapper4.eq(FdProjectFileMethod::getProjectId, projectId);
        fdProjectFileMethodService.remove(queryWrapper4);
        //删除生物分析方法确认项目号
        LambdaQueryWrapper<FdProjectFileConfirm> queryWrapper5 = new LambdaQueryWrapper<>();
        queryWrapper5.eq(FdProjectFileConfirm::getProjectId, projectId);
        fdProjectFileConfirmService.remove(queryWrapper5);
        //删除生物分析方法验证号
        LambdaQueryWrapper<FdProjectFileValidation> queryWrapper6 = new LambdaQueryWrapper<>();
        queryWrapper6.eq(FdProjectFileValidation::getProjectId, projectId);
        fdProjectFileValidationService.remove(queryWrapper6);
        //删除生物分析样本分析号
        LambdaQueryWrapper<FdProjectFileSample> queryWrapper7 = new LambdaQueryWrapper<>();
        queryWrapper7.eq(FdProjectFileSample::getProjectId, projectId);
        fdProjectFileSampleService.remove(queryWrapper7);
        //删除项目文件上传
        LambdaQueryWrapper<FdProjectFileUpload> queryWrapper8 = new LambdaQueryWrapper<>();
        queryWrapper8.eq(FdProjectFileUpload::getProjectId, projectId);
        fdProjectFileUploadService.remove(queryWrapper8);
        //修改项目档案
        fdProjectFileService.updateById(fdProjectFile);
    }

    //根据申办方生成客户档案
    public void customer(FdProjectFile fdProjectFile) {
        //创建客户档案联系人集合
        List<FdCustomerContacts> customerContactsList = new ArrayList<>();
        //循环项目档案客户联系人
        for (FdProjectFileContacts contacts : fdProjectFile.getFdProjectFileContactsList()) {
            //创建客户档案联系人
            FdCustomerContacts customerContacts = new FdCustomerContacts();
            customerContacts.setContactPsn(contacts.getContactPsn());
            customerContacts.setContectTel(contacts.getContactTel());
            customerContacts.setEmail(contacts.getEmail());
            customerContacts.setIsDefault(contacts.getIsDefault());
            //添加到客户档案联系人
            customerContactsList.add(customerContacts);
        }
        //创建客户档案
        FdCustomer customer = new FdCustomer();
        //默认编码为当前时间
        String code = "KH" + new SimpleDateFormat("yyyyMMddhhmmss").format(new Date().getTime());
        customer.setCode(code);
        customer.setName(fdProjectFile.getCustomerId());
        customer.setEnable("0");
        customer.setAbbreviation(fdProjectFile.getAbbreviation());
        customer.setFdCustomerContactsList(customerContactsList);
        //调用客户档案新增
        fdCustomerService.saveCustomer(customer);
        //添加到项目档案外键
        fdProjectFile.setCustomerName(customer.getId());
    }

    //获取有项目权限的人员ids
    public void addPersonnelWithAuthorityIds(FdProjectFile fdProjectFile){
        //判断分配人员是否有值
        if(fdProjectFile.getFdProjectFileMembersList()!=null&&fdProjectFile.getFdProjectFileMembersList().size()>0){
            //获取人员
            String personnels="";
            for(FdProjectFileMembers fdProjectFileMembers:fdProjectFile.getFdProjectFileMembersList()){
                if(personnels==""){
                    personnels=fdProjectFileMembers.getPersonnelId();
                }else{
                    personnels=personnels+","+fdProjectFileMembers.getPersonnelId();
                }
            }
            personnels=personnels+","+fdProjectFile.getBd()+","+fdProjectFile.getClinicalPi()+","+fdProjectFile.getClinicalPm()+","+fdProjectFile.getInvestigator()+","+fdProjectFile.getBioanalyticalPm()+","+fdProjectFile.getAssistant()+","+fdProjectFile.getProjectManagerId();
            fdProjectFile.setPersonnelWithAuthorityIds(personnels);
        }
    }

    //添加所有
    @Override
    public void addProjectFile(FdProjectFile fdProjectFile) {
        addPersonnelWithAuthorityIds(fdProjectFile);
        //保存报价单
        if (fdProjectFile.getId() == null || fdProjectFile.getId().equals(" ")) {
            //新增操作
            projectSave(fdProjectFile);
        } else {
            //修改操作
            projectUpdate(fdProjectFile);
        }
        //化合物
        if (fdProjectFile.getFdProjectFileCompoundList() != null) {
            int i = 1;
            for (FdProjectFileCompound fdProjectFileCompound : fdProjectFile.getFdProjectFileCompoundList()) {
                fdProjectFileCompound.setProjectId(fdProjectFile.getId());
                fdProjectFileCompound.setId("");
                fdProjectFileCompound.setSort(i + "");
                i++;
            }
            fdProjectFileCompoundService.saveBatch(fdProjectFile.getFdProjectFileCompoundList());
        }
        if (fdProjectFile.getFdProjectFileContactsList() != null) {
            //联系人添加项目id
            int i= 1;
            int o = 0;
            for (FdProjectFileContacts fdProjectFileContacts : fdProjectFile.getFdProjectFileContactsList()) {
                fdProjectFileContacts.setProjectId(fdProjectFile.getId());
                fdProjectFileContacts.setContactsIndex(i+"");
                if (fdProjectFileContacts.getIsDefault().equals("1")){
                    o++;
                }
                fdProjectFileContacts.setId("");
                i++;
            }
            if (o>1){
                throw new RuntimeException("默认联系人只能有一个");
            }
            if (o==0){
                throw new RuntimeException("默认联系人至少有一个");
            }

            fdProjectFileContactsService.saveBatch(fdProjectFile.getFdProjectFileContactsList());
        }
        if (fdProjectFile.getFdProjectFileMembersList() != null) {
            //项目成员
            int i =1;
            for (FdProjectFileMembers fdProjectFileMembers : fdProjectFile.getFdProjectFileMembersList()) {
                fdProjectFileMembers.setProjectId(fdProjectFile.getId());
                fdProjectFileMembers.setMembersIndex(i+"");
                fdProjectFileMembers.setId("");
                i++;
                //人员角色
                LambdaQueryWrapper<SysRole> queryRole = new LambdaQueryWrapper<>();
                queryRole.eq(SysRole::getId, fdProjectFileMembers.getPersonnelRole());
                List<SysRole> roles = sysRoleService.list(queryRole);
                for (SysRole sysRole : roles) {
                    fdProjectFileMembers.setRoleName(sysRole.getRoleName());
                }
                //人员编码
                LambdaQueryWrapper<FdCustomer> queryCustomer = new LambdaQueryWrapper<>();
                queryCustomer.eq(FdCustomer::getId, fdProjectFileMembers.getPersonnelCode());
                List<FdCustomer> customers = fdCustomerService.list(queryCustomer);
                for (FdCustomer fdCustomer : customers) {
                    fdProjectFileMembers.setCodeName(fdCustomer.getCode());
                }
            }
            fdProjectFileMembersService.saveBatch(fdProjectFile.getFdProjectFileMembersList());
        }
        if (fdProjectFile.getFdProjectFileMethodList() != null) {
            //生物分析方法号添加项目id
            int i = 1;
            for (FdProjectFileMethod fdProjectFileMethod : fdProjectFile.getFdProjectFileMethodList()) {
                fdProjectFileMethod.setProjectId(fdProjectFile.getId());
                fdProjectFileMethod.setId("");
                fdProjectFileMethod.setSort(i + "");
                i++;
            }
            fdProjectFileMethodService.saveBatch(fdProjectFile.getFdProjectFileMethodList());
        }
        if (fdProjectFile.getFdProjectFileConfirmList() != null) {
            //生物分析方法确认项目号添加项目id
            int i = 1;
            for (FdProjectFileConfirm fdProjectFileConfirm : fdProjectFile.getFdProjectFileConfirmList()) {
                fdProjectFileConfirm.setProjectId(fdProjectFile.getId());
                fdProjectFileConfirm.setId("");
                fdProjectFileConfirm.setSort(i + "");
                i++;
            }
            fdProjectFileConfirmService.saveBatch(fdProjectFile.getFdProjectFileConfirmList());
        }
        if (fdProjectFile.getFdProjectFileValidationList() != null) {
            //生物分析方法验证号添加项目id
            int i = 1;
            for (FdProjectFileValidation fdProjectFileValidation : fdProjectFile.getFdProjectFileValidationList()) {
                fdProjectFileValidation.setProjectId(fdProjectFile.getId());
                fdProjectFileValidation.setId("");
                fdProjectFileValidation.setSort(i + "");
                i++;
            }
            fdProjectFileValidationService.saveBatch(fdProjectFile.getFdProjectFileValidationList());
        }
        if (fdProjectFile.getFdProjectFileSampleList() != null) {
            //生物分析样本分析号添加项目id
            int i = 1;
            for (FdProjectFileSample fdProjectFileSample : fdProjectFile.getFdProjectFileSampleList()) {
                fdProjectFileSample.setProjectId(fdProjectFile.getId());
                fdProjectFileSample.setId("");
                fdProjectFileSample.setSort(i + "");
                i++;
            }
            fdProjectFileSampleService.saveBatch(fdProjectFile.getFdProjectFileSampleList());
        }
        if (fdProjectFile.getFdProjectFileUploadList() != null) {
            //项目文件上传添加项目id
            for (FdProjectFileUpload fdProjectFileUpload : fdProjectFile.getFdProjectFileUploadList()) {
                fdProjectFileUpload.setProjectId(fdProjectFile.getId());
                fdProjectFileUpload.setId(null);
            }
            fdProjectFileUploadService.saveBatch(fdProjectFile.getFdProjectFileUploadList());
        }

        //写回申办方，跟新报价中最终申办方
        FdProjectQuotation fdProjectQuotation=new FdProjectQuotation();
        fdProjectQuotation.setSponsorName(fdProjectFile.getCustomerName());
        fdProjectQuotation.setContractId(fdProjectFile.getContractId());
        fdProjectQuotationService.updateSponsorName(fdProjectQuotation);
    }


    /**
     * @return void
     * @Author gurui
     * @Description //TODO  根据项目档案id查询一条一样的数据
     * @Date 9:15 2019/7/5
     * @Param [id]
     **/
    @Override
    public void copy(String id) {
        //根据id获得项目档案
        FdProjectFile fdProjectFile = fdProjectFileMapper.selectById(id);
        //根据项目档案保存
        fdProjectFile.setId("");
        fdProjectFile.setCreateTime(null);
        fdProjectFile.setContractCode(fdProjectFile.getContractCode() + "(副本)");
        fdProjectFileService.save(fdProjectFile);
        //化合物
        LambdaQueryWrapper<FdProjectFileCompound> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(FdProjectFileCompound::getProjectId, id);
        List<FdProjectFileCompound> fdProjectFileCompoundList = fdProjectFileCompoundService.list(queryWrapper1);
        fdProjectFile.setFdProjectFileCompoundList(fdProjectFileCompoundList);
        //联系人
        LambdaQueryWrapper<FdProjectFileContacts> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(FdProjectFileContacts::getProjectId, id);
        List<FdProjectFileContacts> fdProjectFileContactsList = fdProjectFileContactsService.list(queryWrapper2);
        fdProjectFile.setFdProjectFileContactsList(fdProjectFileContactsList);
        //项目成员
        LambdaQueryWrapper<FdProjectFileMembers> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(FdProjectFileMembers::getProjectId, id);
        List<FdProjectFileMembers> fdProjectFileMembersList = fdProjectFileMembersService.list(queryWrapper3);
        fdProjectFile.setFdProjectFileMembersList(fdProjectFileMembersList);
        //生物分析方法号
        LambdaQueryWrapper<FdProjectFileMethod> queryWrapper4 = new LambdaQueryWrapper<>();
        queryWrapper4.eq(FdProjectFileMethod::getProjectId, id);
        List<FdProjectFileMethod> fdProjectFileMethodList = fdProjectFileMethodService.list(queryWrapper4);
        fdProjectFile.setFdProjectFileMethodList(fdProjectFileMethodList);
        //生物分析方法确认项目号
        LambdaQueryWrapper<FdProjectFileConfirm> queryWrapper5 = new LambdaQueryWrapper<>();
        queryWrapper5.eq(FdProjectFileConfirm::getProjectId, id);
        List<FdProjectFileConfirm> fdProjectFileConfirmList = fdProjectFileConfirmService.list(queryWrapper5);
        fdProjectFile.setFdProjectFileConfirmList(fdProjectFileConfirmList);
        //生物分析方法验证号
        LambdaQueryWrapper<FdProjectFileValidation> queryWrapper6 = new LambdaQueryWrapper<>();
        queryWrapper6.eq(FdProjectFileValidation::getProjectId, id);
        List<FdProjectFileValidation> fdProjectFileValidationList = fdProjectFileValidationService.list(queryWrapper6);
        fdProjectFile.setFdProjectFileValidationList(fdProjectFileValidationList);
        //生物分析样本分析号
        LambdaQueryWrapper<FdProjectFileSample> queryWrapper7 = new LambdaQueryWrapper<>();
        queryWrapper7.eq(FdProjectFileSample::getProjectId, id);
        List<FdProjectFileSample> fdProjectFileSampleList = fdProjectFileSampleService.list(queryWrapper7);
        fdProjectFile.setFdProjectFileSampleList(fdProjectFileSampleList);
        //项目文件上传
        LambdaQueryWrapper<FdProjectFileUpload> queryWrapper8 = new LambdaQueryWrapper<>();
        queryWrapper8.eq(FdProjectFileUpload::getProjectId, id);
        List<FdProjectFileUpload> fdProjectFileUploadList = fdProjectFileUploadService.list(queryWrapper8);
        fdProjectFile.setFdProjectFileUploadList(fdProjectFileUploadList);
        //申办方
        LambdaQueryWrapper<FdProjectFileBidparty> queryWrapper9 = new LambdaQueryWrapper<>();
        queryWrapper9.eq(FdProjectFileBidparty::getProjectId, id);
        List<FdProjectFileBidparty> fdProjectFileBidpartyList = fdProjectFileBidpartyService.list(queryWrapper9);
        fdProjectFile.setFdProjectFileBidpartyList(fdProjectFileBidpartyList);
        //化合物
        int i = 1;
        for (FdProjectFileCompound fdProjectFileCompound : fdProjectFile.getFdProjectFileCompoundList()) {
            fdProjectFileCompound.setProjectId(fdProjectFile.getId());
            fdProjectFileCompound.setId("");
            fdProjectFileCompound.setSort(i + "");
            i++;
        }
        //联系人添加项目id
        int ii=1;
        for (FdProjectFileContacts fdProjectFileContacts : fdProjectFile.getFdProjectFileContactsList()) {
            fdProjectFileContacts.setProjectId(fdProjectFile.getId());
            fdProjectFileContacts.setIsDefault(fdProjectFile.getIsDefault());
            fdProjectFileContacts.setContactsIndex(ii+"");
            fdProjectFileContacts.setId("");
            ii++;
        }
        //项目成员
        int iii=1;
        for (FdProjectFileMembers fdProjectFileMembers : fdProjectFile.getFdProjectFileMembersList()) {
            fdProjectFileMembers.setProjectId(fdProjectFile.getId());
            fdProjectFileMembers.setMembersIndex(iii+"");
            fdProjectFileMembers.setId("");
            iii++;
            //人员角色
            LambdaQueryWrapper<SysRole> queryRole = new LambdaQueryWrapper<>();
            queryRole.eq(SysRole::getId, fdProjectFileMembers.getPersonnelRole());
            List<SysRole> roles = sysRoleService.list(queryRole);
            for (SysRole sysRole : roles) {
                fdProjectFileMembers.setRoleName(sysRole.getRoleName());
            }
            //人员编码
            LambdaQueryWrapper<FdCustomer> queryCustomer = new LambdaQueryWrapper<>();
            queryCustomer.eq(FdCustomer::getId, fdProjectFileMembers.getPersonnelCode());
            List<FdCustomer> customers = fdCustomerService.list(queryCustomer);
            for (FdCustomer fdCustomer : customers) {
                fdProjectFileMembers.setCodeName(fdCustomer.getCode());
            }
        }
        //生物分析方法号添加项目id
        int iiii = 1;
        for (FdProjectFileMethod fdProjectFileMethod : fdProjectFile.getFdProjectFileMethodList()) {
            fdProjectFileMethod.setProjectId(fdProjectFile.getId());
            fdProjectFileMethod.setId("");
            fdProjectFileMethod.setSort(ii + "");
            iiii++;
        }

        //生物分析方法确认项目号添加项目id
        int iiiii = 1;
        for (FdProjectFileConfirm fdProjectFileConfirm : fdProjectFile.getFdProjectFileConfirmList()) {
            fdProjectFileConfirm.setProjectId(fdProjectFile.getId());
            fdProjectFileConfirm.setId("");
            fdProjectFileConfirm.setSort(iii + "");
            iiiii++;
        }
        //生物分析方法验证号添加项目id
        int iiiiii = 1;
        for (FdProjectFileValidation fdProjectFileValidation : fdProjectFile.getFdProjectFileValidationList()) {
            fdProjectFileValidation.setProjectId(fdProjectFile.getId());
            fdProjectFileValidation.setId("");
            fdProjectFileValidation.setSort(iiiiii + "");
            i++;
        }
        //生物分析样本分析号添加项目id
        int iiiiiii = 1;
        for (FdProjectFileSample fdProjectFileSample : fdProjectFile.getFdProjectFileSampleList()) {
            fdProjectFileSample.setProjectId(fdProjectFile.getId());
            fdProjectFileSample.setId("");
            fdProjectFileSample.setSort(iiiiiii + "");
            i++;
        }
        //项目文件上传添加项目id
        for (FdProjectFileUpload fdProjectFileUpload : fdProjectFile.getFdProjectFileUploadList()) {
            fdProjectFileUpload.setProjectId(fdProjectFile.getId());
            fdProjectFileUpload.setId(null);
        }
        //批量新增
        fdProjectFileUploadService.saveBatch(fdProjectFile.getFdProjectFileUploadList());
        fdProjectFileSampleService.saveBatch(fdProjectFile.getFdProjectFileSampleList());
        fdProjectFileValidationService.saveBatch(fdProjectFile.getFdProjectFileValidationList());
        fdProjectFileConfirmService.saveBatch(fdProjectFile.getFdProjectFileConfirmList());
        fdProjectFileMethodService.saveBatch(fdProjectFile.getFdProjectFileMethodList());
        fdProjectFileMembersService.saveBatch(fdProjectFile.getFdProjectFileMembersList());
        fdProjectFileContactsService.saveBatch(fdProjectFile.getFdProjectFileContactsList());
        fdProjectFileCompoundService.saveBatch(fdProjectFile.getFdProjectFileCompoundList());
    }

    /***
     * @Title:查找项目档案中是否已存在新增选取的合同
            * @Description:TODO
            * @param contractId
            * @return int
            * @exception
            * @author 陈创
            * @date 2019/7/16 11:12
            * @updatehistory:
     *   2019/7/16 11:12 新增
            */
    @Override
    public int fileContractIdentical(String contractId) {
        LambdaQueryWrapper<FdProjectFile> fdProjectFileLambdaQueryWrapper=new LambdaQueryWrapper<>();
        fdProjectFileLambdaQueryWrapper.eq(FdProjectFile::getContractId,contractId);
        fdProjectFileLambdaQueryWrapper.eq(FdProjectFile::getDelFlag,"0");
        List<FdProjectFile> list=fdProjectFileService.list(fdProjectFileLambdaQueryWrapper);
        return list.size();
    }


    /**
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @Author gurui
     * @Description //TODO 弃审核数据 ,使其 状态为0
     * @Date 10:33 2019/6/28
     * @Param [ids]
     **/
    @Override
    public Map<String, Object> auditBatch(List<String> ids) {
        Map<String, Object> map = new HashMap<>();
        String unAudit = " ";//可审计
        String audit = " ";//已审计
        //根据多个id查询报价对象集合
        List<FdProjectFile> fdProjectFileList = fdProjectFileMapper.selectBatchIds(ids);
        //修改
        if (fdProjectFileList == null || fdProjectFileList.isEmpty()) {
            audit += "未找到修改数据</br>";
        } else {
            //判断哪些可关闭哪些不可关闭
            for (FdProjectFile fdProjectFile : fdProjectFileList) {
                if (fdProjectFile.getStatus().equals("0")) {
                    audit += fdProjectFile.getProjectName() + "，";
                } else {
                    unAudit += fdProjectFile.getProjectName() + "，";
                }
            }
            //可关闭存在则操作数据库修改
            if (!unAudit.equals("")) {
                fdProjectFileMapper.auditBatch(ids);
            }
        }
        //去掉最后一个"，"号
        audit = audit.substring(0, audit.length() - 1);
        unAudit = unAudit.substring(0, unAudit.length() - 1);
        //加关键词
        if (!unAudit.equals("")) {
            unAudit = unAudit + "弃审核成功";
        }
        if (!audit.equals("")) {
            audit = "其中:" + audit + "已处于未审核状态。<br> ";
        }
        map.put("msg", unAudit + audit);
        return map;
    }

    /**
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @Author gurui
     * @Description //TODO 审核数据 ,使其 状态为1
     * @Date 10:42 2019/6/28
     * @Param [ids]
     **/
    @Override
    public Map<String, Object> reviewBatch(List<String> ids) {
        Map<String, Object> map = new HashMap<>();
        String unReview = " "; //未审核
        String review = " "; //已审核
        //根据多个id查询报价对象集合
        List<FdProjectFile> fdProjectFileList = fdProjectFileMapper.selectBatchIds(ids);
        //修改
        if (fdProjectFileList == null || fdProjectFileList.isEmpty()) {
            review += "未找到修改数据</br>";
        } else {
            //判断哪些已审核哪些未审核
            for (FdProjectFile fdProjectFile : fdProjectFileList) {
                if (fdProjectFile.getStatus().equals("0")) {
                    unReview += fdProjectFile.getProjectName() + ",";
                }
                if (fdProjectFile.getStatus().equals("1")) {
                    review += fdProjectFile.getProjectName() + ",";
                }
                if (fdProjectFile.getStatus().equals("2")) {
                    throw new RuntimeException("已关闭！无法审核");
                }
                if (fdProjectFile.getStatus().equals("3")) {
                    throw new RuntimeException("已转合同！无法审核");
                }
            }
            //去掉最后一个","号
            unReview = unReview.substring(0, unReview.length() - 1);
            review = review.substring(0, review.length() - 1);
            //操作数据库将未审核的修改成已审核
            if (!unReview.equals("")) {
                fdProjectFileMapper.reviewBatch(ids);
            }
        }
        //加关键词
        if (!unReview.equals("")) {
            unReview = unReview + "审核成功";
        }
        if (!review.equals("")) {
            review = "其中：" + review + "已审核。<br>";
        }
        map.put("msg", unReview + review);
        return map;
    }

    /**
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @Author gurui
     * @Description //TODO 根据合同id修改状态为2
     * @Date 19:29 2019/7/4
     * @Param [ids]
     **/
    @Override
    public Map<String, Object> closeBatch(List<String> ids) {
        Map<String, Object> map = new HashMap<>();
        String succ = " ";//可关闭
        String closed = " ";//已关闭
        //根据多个id查询报价对象集合
        List<FdProjectFile> projectFileList = fdProjectFileMapper.selectBatchIds(ids);

        //修改
        if (projectFileList == null || projectFileList.isEmpty()) {
            closed += "未找到修改数据</br>";
        } else {
            //判断哪些可关闭哪些不可关闭
            for (FdProjectFile fdProjectFile : projectFileList) {
                if (fdProjectFile.getStatus().equals("2")) {
                    closed += fdProjectFile.getProjectName() + "，";
                } else {
                    succ += fdProjectFile.getProjectName() + "，";
                }
            }
            //可关闭存在则操作数据库修改
            if (!succ.equals("")) {
                fdProjectFileMapper.closeBatch(ids);
            }
        }
        //去掉最后一个"，"号
        closed = closed.substring(0, closed.length() - 1);
        succ = succ.substring(0, succ.length() - 1);
        //加关键词
        if (!succ.equals("")) {
            succ = succ + "关闭成功";
        }
        if (!closed.equals("")) {
            closed = "其中：" + closed + "已处关闭状态。<br>";
        }
        map.put("msg", succ + closed);
        return map;
    }

    //分别获取不同状态下的项目档案数
    @Override
    public int queryStatus0() {
        return fdProjectFileMapper.queryStatus0();
    }

    @Override
    public int queryStatus1() {
        return fdProjectFileMapper.queryStatus1();
    }

    @Override
    public int queryStatus2() {
        return fdProjectFileMapper.queryStatus2();
    }

    @Override
    public int queryStatus3() {
        return fdProjectFileMapper.queryStatus3();
    }

    @Override
    public int queryStatus4() {
        return fdProjectFileMapper.queryStatus4();
    }

    @Override
    public FdProjectFile myGetById(String id) {
        return fdProjectFileMapper.myGetById(id);
    }

    /***
     * @Title:选择合同的id查询档案对象
            * @Description:TODO
            * @param contractId
            * @return org.jeecg.modules.projectManagement.entity.FdProjectFile
            * @exception 
            * @author 陈创
            * @date 2019/12/6 17:23
            * @updatehistory:
     *   2019/12/6 17:23 新增
            */
    @Override
    public FdProjectFile selectContractById(String contractId) {
        FdProjectFile fdProjectFile=fdProjectFileMapper.selectContractById(contractId);
        //实验地点数据字典
        List<Map<String,Object>> dictItemsByCode= sysDictService.queryDictItemsByCode("biological_analysis_laboratory_location");
        if(dictItemsByCode!=null){//判断是否为空
            //循环获取字典名称
            for(Map<String,Object> dict:dictItemsByCode){
                //找到对应的字典数据
                if(dict.get("value").equals(fdProjectFile.getLabAddress())){
                    fdProjectFile.setLabAddressName(String.valueOf(dict.get("text")));
                }
            }
        }
        return fdProjectFile;
    }

    @Override
    public IPage<FdProjectFile> findInformationByPage(Page<FdProjectFile> page, QueryWrapper<FdProjectFile> queryWrapper) {
        return fdProjectFileMapper.findInformationByPage(page,queryWrapper);
    }


}
