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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.finance.entity.FdSupplementaryContractReceivables;
import org.jeecg.modules.finance.entity.FdSupplementaryContractReceivablesB;
import org.jeecg.modules.finance.mapper.FdSupplementaryContractReceivablesBMapper;
import org.jeecg.modules.finance.mapper.FdSupplementaryContractReceivablesMapper;
import org.jeecg.modules.finance.service.IFdSupplementaryContractReceivablesBService;
import org.jeecg.modules.finance.service.IFdSupplementaryContractReceivablesService;
import org.jeecg.modules.projectManagement.entity.FdContractClause;
import org.jeecg.modules.projectManagement.entity.FdContractCompound;
import org.jeecg.modules.projectManagement.entity.FdProjectFile;
import org.jeecg.modules.projectManagement.entity.FdProjectFileCompound;
import org.jeecg.modules.projectManagement.mapper.FdProjectFileMapper;
import org.jeecg.modules.projectManagement.service.IFdContractClauseService;
import org.jeecg.modules.projectManagement.service.IFdContractCompoundService;
import org.jeecg.modules.projectManagement.service.IFdProjectFileCompoundService;
import org.jeecg.modules.projectManagement.service.IFdProjectFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.jeecg.common.util.oConvertUtils;

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

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

/**
 * @Description: 补充合同应收款表
 * @author： gur
 * @date： 2019-07-19
 * @version： V1.0
 */
@Service
@Transactional
public class FdSupplementaryContractReceivablesServiceImpl extends ServiceImpl<FdSupplementaryContractReceivablesMapper, FdSupplementaryContractReceivables> implements IFdSupplementaryContractReceivablesService {



    @Resource
    IFdSupplementaryContractReceivablesBService fdSupplementaryContractReceivablesBService;

    @Resource
    FdSupplementaryContractReceivablesMapper fdSupplementaryContractReceivablesMapper;

    @Resource
    FdSupplementaryContractReceivablesBMapper fdSupplementaryContractReceivablesBMapper;

    @Resource
    IFdSupplementaryContractReceivablesService fdSupplementaryContractReceivablesService;

    @Resource
    IFdProjectFileCompoundService fdProjectFileCompoundService;

    @Autowired
    FdProjectFileMapper fdProjectFileMapper;

    @Resource
    IFdContractCompoundService iFdContractCompoundService;

    /**
     * @Author gurui
     * @Description //TODO 添加补充合同应收款表同时添加其子表
     * @Date 18:06 2019/7/20
     * @Param [fdSupplementaryContractReceivables]
     * @return void
     **/
    @Override
    public void addAll(FdSupplementaryContractReceivables fdSupplementaryContractReceivables) {
        if (oConvertUtils.isEmpty(fdSupplementaryContractReceivables.getId())) {
            //新增
            fdSupplementaryContractReceivables.setDelFlag("0");
            fdSupplementaryContractReceivables.setStatus("0");
            fdSupplementaryContractReceivablesService.save(fdSupplementaryContractReceivables);
        } else {
            //修改
            fdSupplementaryContractReceivablesService.updateById(fdSupplementaryContractReceivables);
            LambdaQueryWrapper<FdSupplementaryContractReceivablesB> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FdSupplementaryContractReceivablesB::getPkSupplementaryContractReceivables, fdSupplementaryContractReceivables.getId());
            fdSupplementaryContractReceivablesBService.remove(queryWrapper);
        }
        //保存子表
        for (FdSupplementaryContractReceivablesB fdSupplementaryContractReceivablesB : fdSupplementaryContractReceivables.getFdSupplementaryContractReceivablesBList()) {
            fdSupplementaryContractReceivablesB.setId("");
            fdSupplementaryContractReceivablesB.setPkSupplementaryContractReceivables(fdSupplementaryContractReceivables.getId());
        }
        fdSupplementaryContractReceivablesBService.saveBatch(fdSupplementaryContractReceivables.getFdSupplementaryContractReceivablesBList());
    }
    // 删除补充合同应收款表同时删除其子表
    @Override
    public boolean deleteAll(String id) {
        boolean result = true;
        try {
            FdSupplementaryContractReceivables fdSupplementaryContractReceivables = getById(id);
            if (oConvertUtils.isNotEmpty(fdSupplementaryContractReceivables.getFdSupplementaryContractReceivablesBList())){
                LambdaQueryWrapper<FdSupplementaryContractReceivablesB> queryWrapper=new LambdaQueryWrapper<>();
                queryWrapper.eq(FdSupplementaryContractReceivablesB::getPkSupplementaryContractReceivables,id);
                fdSupplementaryContractReceivablesBService.remove(queryWrapper);
            }
            removeById(id);
        }catch (Exception e){
            e.printStackTrace();
            result=false;
        }
        return result;
    }
    /**
     * @Author gurui
     * @Description //TODO  弃审核数据使其状态为0
     * @Date 18:07 2019/7/20
     * @Param [ids]
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    @Override
    public Map<String, Object> auditBatch(List<String> ids) {
        Map<String, Object> map = new HashMap<>();
        String unAudit = " ";//可审计
        String audit = " ";//已审计
        //根据多个id查询报价对象集合
        List<FdSupplementaryContractReceivables> fdSupplementaryContractReceivables = fdSupplementaryContractReceivablesMapper.selectBatchIds(ids);
        //修改
        if (fdSupplementaryContractReceivables == null || fdSupplementaryContractReceivables.isEmpty()) {
            audit += "未找到修改数据</br>";
        } else {
            //判断哪些可关闭哪些不可关闭
            for (FdSupplementaryContractReceivables fdSupplementaryContractReceivables1 : fdSupplementaryContractReceivables) {
                if (fdSupplementaryContractReceivables1.getStatus().equals("0")) {
                    audit += fdSupplementaryContractReceivables1.getProjectName() + "，";
                } else {
                    unAudit += fdSupplementaryContractReceivables1.getProjectName() + "，";
                }
            }
            //可关闭存在则操作数据库修改
            if (!unAudit.equals("")) {
                fdSupplementaryContractReceivablesMapper.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;
    }
    /**
     * @Author gurui
     * @Description //TODO 审核数据使其状态为1
     * @Date 18:07 2019/7/20
     * @Param [ids]
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    @Override
    public Map<String, Object> reviewBatch(List<String> ids) {
        Map<String, Object> map = new HashMap<>();
        String unReview = " "; //未审核
        String review = " "; //已审核
        //根据多个id查询报价对象集合
        List<FdSupplementaryContractReceivables> fdSupplementaryContractReceivablesList = fdSupplementaryContractReceivablesMapper.selectBatchIds(ids);
        //修改
        if (fdSupplementaryContractReceivablesList == null || fdSupplementaryContractReceivablesList.isEmpty()) {
            review += "未找到修改数据</br>";
        } else {
            //判断哪些已审核哪些未审核
            for (FdSupplementaryContractReceivables fdBioanalysisSampleMilestones : fdSupplementaryContractReceivablesList) {
                if (fdBioanalysisSampleMilestones.getStatus().equals("0")) {
                    unReview += fdBioanalysisSampleMilestones.getProjectName()+",";
                }
                if (fdBioanalysisSampleMilestones.getStatus().equals("1")) {
                    review += fdBioanalysisSampleMilestones.getProjectName()+",";
                }
                if (fdBioanalysisSampleMilestones.getStatus().equals("2")) {
                    throw new RuntimeException("已关闭！无法审核");
                }
                if (fdBioanalysisSampleMilestones.getStatus().equals("3")) {
                    throw new RuntimeException("已转合同！无法审核");
                }
            }
            //去掉最后一个","号
            unReview = unReview.substring(0, unReview.length() - 1);
            review = review.substring(0, review.length() - 1);
            //操作数据库将未审核的修改成已审核
            if (!unReview.equals("")) {
                fdSupplementaryContractReceivablesMapper.reviewBatch(ids);
            }
        }
        //加关键词
        if (!unReview.equals("")) {
            unReview = unReview + "审核成功";
        }
        if (!review.equals("")) {
            review = "其中：" + review + "已审核。<br>";
        }
        map.put("msg", unReview + review);
        return map;
    }

    @Resource
    private IFdContractClauseService fdContractClauseService;

    /**
     * @Author gurui
     * @Description //TODO 根据合同id查询相关信息
     * @Date 18:07 2019/7/20
     * @Param [contractId]
     * @return org.jeecg.modules.finance.entity.FdSupplementaryContractReceivables
     **/
    @Override
    public FdSupplementaryContractReceivables querySupplementartByConId(String pkProjectFile) {
        FdSupplementaryContractReceivables fdSupplementaryContractReceivables = fdSupplementaryContractReceivablesMapper.querySupplementartByConId(pkProjectFile);

        //  查询收款节点
        QueryWrapper<FdContractClause> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("contract_id", fdSupplementaryContractReceivables.getPkContract());
        queryWrapper.eq("del_flag","0");
        queryWrapper.lambda().orderByAsc(FdContractClause::getSort);
        List<FdContractClause> list = fdContractClauseService.list(queryWrapper);
        fdSupplementaryContractReceivables.setFdContractClauseList(list);

        //化合物
//        LambdaQueryWrapper<FdProjectFileCompound> fdProjectFileCompoundLambdaQueryWrapper=new LambdaQueryWrapper<>();
//        fdProjectFileCompoundLambdaQueryWrapper.eq(FdProjectFileCompound::getProjectId,fdSupplementaryContractReceivables.getPkProjectFile());
//        fdProjectFileCompoundLambdaQueryWrapper.eq(FdProjectFileCompound::getDelFlag,"0");
//        List<FdProjectFileCompound> fdProjectFileCompoundList=fdProjectFileCompoundService.list(fdProjectFileCompoundLambdaQueryWrapper);
//        //循环找到所对应的化合物
//        //储存多个化合物的名称
//        String compound="";
//        int i = 0;
//        for(FdProjectFileCompound fdProjectFileCompound:fdProjectFileCompoundList){
//            if(i==0){
//                compound=fdProjectFileCompound.getCompound();
//            }else{
//                compound=compound+"、"+fdProjectFileCompound.getCompound();
//            }
//            i++;
//        }
        LambdaQueryWrapper<FdContractCompound> fdContractCompoundLambdaQueryWrapper=new LambdaQueryWrapper<>();
        fdContractCompoundLambdaQueryWrapper.eq(FdContractCompound::getContractId,fdSupplementaryContractReceivables.getPkContract());
        fdContractCompoundLambdaQueryWrapper.eq(FdContractCompound::getDelFlag,"0");
        fdContractCompoundLambdaQueryWrapper.orderByAsc(FdContractCompound::getSort);
        List<FdContractCompound> fdContractCompoundList=iFdContractCompoundService.list(fdContractCompoundLambdaQueryWrapper);
        //循环找到所对应的化合物
        //储存多个化合物的名称
        String compound="";
        int i = 0;
        for(FdContractCompound fdContractCompound:fdContractCompoundList){
            if(i==0){
                compound=fdContractCompound.getCompound();
            }else{
                compound=compound+"、"+fdContractCompound.getCompound();
            }
            i++;
        }
        fdSupplementaryContractReceivables.setCompound(compound);

        return fdSupplementaryContractReceivables;
    }

    @Override
    public List<FdProjectFile> supplementContractList() {
        return fdProjectFileMapper.supplementContractList();
    }

    @Override
    public List<FdSupplementaryContractReceivables> getListAndCreditPeriod() {
        return fdSupplementaryContractReceivablesMapper.getListAndCreditPeriod();
    }


}
