package com.liang.service.impl;

import com.liang.common.base.BaseResult;
import com.liang.common.utils.StringUtils;
import com.liang.dao.*;
import com.liang.entity.*;
import com.liang.service.PrjCompletecompanyService;
import com.liang.service.PrjProjectService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 项目(PrjProject)表服务实现类
 *
 * @author makejava
 * @since 2023-03-08 14:54:54
 */
@Service("prjProjectService")
@RestController
public class PrjProjectServiceImpl implements PrjProjectService {
    @Resource
    private PrjProjectDao prjProjectDao;
    @Resource
    private PrjMemberDao memberDao;
    @Resource
    private PrjCompletecompanyDao completecompanyDao;
    @Resource
    private PrjSubtopicDao subtopicDao;
    @Resource
    private PrjDivisioncompanyDao divisioncompanyDao;
    @Resource
    private PrjSubtopicplanDao subtopicplanDao;
    @Resource
    private PrjBudgetDao prjBudgetDao;
    @Resource
    private PrjFeeschemeDao feeschemeDao;
    @Resource
    private PrjUndertakecompanyDao undertakecompanyDao;
    @Resource
    private PrjContactsDao contactsDao;
    @Resource
    private PrjCompletecompanyService prjCompletecompanyService;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public PrjProject queryById(Integer id) {
        return this.prjProjectDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param prjProject  筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<PrjProject> queryByPage(PrjProject prjProject, PageRequest pageRequest) {
        long total = this.prjProjectDao.count(prjProject);
        return new PageImpl<>(this.prjProjectDao.queryAllByLimit(prjProject, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param prjProject 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(PrjProject prjProject) {
        return this.prjProjectDao.insert(prjProject);
    }

    @Override
    public int insertNormal(PrjProject prjProject) {
        return this.prjProjectDao.insertNormal(prjProject);
    }

    /**
     * 撤销审批状态
     *
     * @param prjProject 实例对象
     * @return 实例对象
     */
    @Override
    public int tjrevokeApprove(PrjProject prjProject) {
        return this.prjProjectDao.tjrevokeApprove(prjProject);
    }

    /**
     * 修改数据
     *
     * @param prjProject 实例对象
     * @return 实例对象
     */
    @Override
    public int update(PrjProject prjProject) {
        return this.prjProjectDao.update(prjProject);
    }

    @Override
    public int updateNormal(PrjProject prjProject) {
        return this.prjProjectDao.updateNormal(prjProject);
    }

    @Override
    public int updateTXSM(String txsm, Integer id) {
        return this.prjProjectDao.updateTXSM(txsm, id);
    }

    @Override
    public int updateContent(String yjnrjkhzb, Integer id) {
        return this.prjProjectDao.updateContent(yjnrjkhzb, id);
    }

    @Override
    public int updateYGWTSM(String ygwtsm, Integer id) {
        return this.prjProjectDao.updateYGWTSM(ygwtsm, id);
    }

    @Override
    public int updateFJ(String fj, Integer id) {
        return this.prjProjectDao.updateFJ(fj, id);
    }

    @Override
    public int updateSpzt(PrjProject project) {
        return this.prjProjectDao.updateSpzt(project);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        //删除项目完成人
        memberDao.deleteByXm(id);
        //删除项目完成单位
        completecompanyDao.deleteByXm(id);

        //删除子课题分工单位
        divisioncompanyDao.deleteByXm(id);
        //删除子课题计划
        subtopicplanDao.deleteByXm(id);
        //删除项目子课题
        subtopicDao.deleteByXm(id);

        //删除项目预算
        prjBudgetDao.deleteByXm(id);
        //删除出资方案
        feeschemeDao.deleteByXm(id);

        //删除承担单位
        undertakecompanyDao.deleteById(id);
        //删除联系方式
        contactsDao.deleteByXm(id);

        //删除项目任务书
        return this.prjProjectDao.deleteById(id) > 0;
    }

    @Override
    public List<PrjProject> getProjectList(Map map) {
        return this.prjProjectDao.getProjectList(map);
    }

    @Override
    public List<PrjProject> getNdysProjectList(Map map) {
        return this.prjProjectDao.getNdysProjectList(map);
    }

    @Override
    public List<PrjProject> getByjProjectList(Map map) {
        return this.prjProjectDao.getByjProjectList(map);
    }

    @Override
    public int batchDelete(List<Integer> idList) {
        //删除项目完成人
        memberDao.batchDeleteByXm(idList);
        //删除项目完成单位
        completecompanyDao.batchDeleteByXm(idList);

        //删除子课题分工单位
        divisioncompanyDao.batchDeleteByXm(idList);
        //删除子课题计划
        subtopicplanDao.batchDeleteByXm(idList);
        //删除项目子课题
        subtopicDao.batchDeleteByXm(idList);

        //删除项目预算
        prjBudgetDao.batchDeleteByXm(idList);
        //删除出资方案
        feeschemeDao.batchDeleteByXm(idList);
        //删除承担单位
        undertakecompanyDao.batchDeleteByCzfa(idList);
        //删除联系方式
        contactsDao.batchDeleteByXm(idList);

        //删除项目任务书
        return this.prjProjectDao.batchDelete(idList);
    }

    @Resource
    private PrjCompletecompanyDao prjCompletecompanyDao;

    @Override
    public List<PrjProject> getRwsList(Map map) {
        return this.prjProjectDao.getRwsList(map);
    }

    @Override
    public List<FeeManagemant> getFyglList(Map map) {
        return this.prjProjectDao.getFyglList(map);
    }

    @Resource
    private PrjAnnualbudgetdetailDao prjAnnualbudgetdetailDao;

    @Override
    @GetMapping("/updatePrj_AnnualBudgetDetail")
    @Transactional()
    public BaseResult updatePrj_AnnualBudgetDetail(@RequestParam(value = "rws") Integer rws) {

        PrjProject prjProject = this.queryById(rws);
        if ("s2".equals(prjProject.getXmlx())) {
            //国网
            List<PrjCompletecompany> prjCompletecompanies = prjCompletecompanyDao.queryByXM(rws);
            String DW = "";
            if (CollectionUtils.isNotEmpty(prjCompletecompanies)) {
                for (int i = 0; i < prjCompletecompanies.size(); i++) {
                    if (prjCompletecompanies.get(i).getDwmc().contains("国网陕西")) {
                        DW = "DW" + (++i);
                        break;
                    }
                }
                //更新项目总预算
                BigDecimal XMZYS = prjAnnualbudgetdetailDao.selectXMZYS(rws);
                int numXMZYS = prjAnnualbudgetdetailDao.updateXMZYS(rws, XMZYS);
                //更新外委研究支出费
                BigDecimal WWYJZCF = null;
                if (StringUtils.isNotBlank(DW)) {
                    // 更新除 总计与 外委研究支出费之外的 数据同步
                    int updateNum = prjAnnualbudgetdetailDao.updateRWSYS(rws, DW);
                   /* // 更新外委
                    int updateWw = prjAnnualbudgetdetailDao.updateWW(rws,DW);*/
                    if ("s2".equals(prjProject.getXmlx())) {
                        //国网项目
                        WWYJZCF = prjAnnualbudgetdetailDao.selectWWYJZCF_GW(rws, DW);
                    }
                } else {
                    //没有陕西国网支出 清空年度预算
                    prjAnnualbudgetdetailDao.updateNDYS(rws);
                    WWYJZCF = prjBudgetDao.queryYSJEByXMAndZJ(rws);
                }
                // 判断外委研究支出费中 外委研究支出费是否为空值
                // 获取任务书外委预算额
                BigDecimal ww = prjAnnualbudgetdetailDao.getWw(rws,DW);
                if (WWYJZCF != null || ww != null){
                    if (WWYJZCF != null && ww != null){
                        BigDecimal sum  = WWYJZCF.add(ww);
                        int numWWYJZCF = prjAnnualbudgetdetailDao.updateWWYJZCF(rws, sum);
                    }else if (WWYJZCF != null){
                        int numWWYJZCF = prjAnnualbudgetdetailDao.updateWWYJZCF(rws, WWYJZCF);
                    }else if (ww != null){
                        int numWWYJZCF = prjAnnualbudgetdetailDao.updateWWYJZCF(rws, ww);
                    }
                    //更新外委支出总费
                    prjAnnualbudgetdetailDao.updateWWYJZCFSUM(rws);
                }
            }
        }
        if ("s1".equals(prjProject.getXmlx())) {
            //陕西项目
            List<PrjCompletecompany> prjCompletecompanies = prjCompletecompanyDao.queryByXM(rws);
            String DW = "";
            if (CollectionUtils.isNotEmpty(prjCompletecompanies)) {
                for (int i = 0; i < prjCompletecompanies.size(); i++) {
                    if (prjCompletecompanies.get(i).getDwmc().contains("国网陕西")) {
                        DW = "DW" + (++i);
                        break;
                    }
                }
                //更新项目总预算
                BigDecimal XMZYS = prjAnnualbudgetdetailDao.selectXMZYS_SX(rws);
                int numXMZYS = prjAnnualbudgetdetailDao.updateXMZYS(rws, XMZYS);
                //更新外委研究支出费
                BigDecimal WWYJZCF = null;
                if (StringUtils.isNotBlank(DW)) {
                    int updateNum = prjAnnualbudgetdetailDao.updateRWSYS(rws, DW);
                    WWYJZCF = prjAnnualbudgetdetailDao.selectWWYJZCF_SX(rws, DW);
                } else {
                    //没有陕西国网支出 清空年度预算
                    prjAnnualbudgetdetailDao.updateNDYS(rws);
                    WWYJZCF = prjBudgetDao.queryYSJEByXMAndZJ(rws);
                }
                int numWWYJZCF = prjAnnualbudgetdetailDao.updateWWYJZCF(rws, WWYJZCF);
                //更新外委支出总费
                prjAnnualbudgetdetailDao.updateWWYJZCFSUM(rws);
            }
        }
        return BaseResult.success("同步成功");
    }
    @Override
    public List<PrjProject> getErpBh(String erpBh) {
        return this.prjProjectDao.getErpBh(erpBh);
    }

    @Override
    public void updateSPTGZT(PrjProject prjProject) {
        Integer xmid = prjProject.getId();
        List<PrjBudget> xgjl = prjBudgetDao.getBudgetList(xmid);
//        获取完成单位列表
        String xm = String.valueOf(xmid);
        Map<String, String> paraMap = new HashMap<>();
        paraMap.put("xm", xm);
        List<PrjCompletecompany> list = prjCompletecompanyService.getCompanyList(paraMap);
//        拼接修改记录
        for (int i = 0; i < xgjl.size(); i++) {
            String xg = "";
            if (xgjl.get(i).getYsje()!=null) {
                xg = xgjl.get(i).getYsje().toString()+",";
            }else {
                xg = "0,";
            }
            for (int j = 0; j < list.size(); j++) {
                int k = j + 1;
                switch (k) {
                    case 1:
                        if (xgjl.get(i).getDw1()!=null){
                            xg=xg.concat(xgjl.get(i).getDw1().toString()).concat(",");
                        }else {
                            xg=xg.concat("0").concat(",");
                        }
                        break;
                    case 2:
                        if (xgjl.get(i).getDw2()!=null){
                            xg=xg.concat(xgjl.get(i).getDw2().toString()).concat(",");
                        }else {
                            xg=xg.concat("0").concat(",");
                        }                        break;
                    case 3:
                        if (xgjl.get(i).getDw3()!=null){
                            xg=xg.concat(xgjl.get(i).getDw3().toString()).concat(",");
                        }else {
                            xg=xg.concat("0").concat(",");
                        }                        break;
                    case 4:
                        if (xgjl.get(i).getDw4()!=null){
                            xg=xg.concat(xgjl.get(i).getDw4().toString()).concat(",");
                        }else {
                            xg=xg.concat("0").concat(",");
                        }                        break;
                    case 5:
                        if (xgjl.get(i).getDw5()!=null){
                            xg=xg.concat(xgjl.get(i).getDw5().toString()).concat(",");
                        }else {
                            xg=xg.concat("0").concat(",");
                        }                        break;
                    case 6:
                        if (xgjl.get(i).getDw6()!=null){
                            xg=xg.concat(xgjl.get(i).getDw6().toString()).concat(",");
                        }else {
                            xg=xg.concat("0").concat(",");
                        }                        break;
                    case 7:
                        if (xgjl.get(i).getDw7()!=null){
                            xg=xg.concat(xgjl.get(i).getDw7().toString()).concat(",");
                        }else {
                            xg=xg.concat("0").concat(",");
                        }                        break;
                    case 8:
                        if (xgjl.get(i).getDw8()!=null){
                            xg=xg.concat(xgjl.get(i).getDw8().toString()).concat(",");
                        }else {
                            xg=xg.concat("0").concat(",");
                        }                        break;
                    case 9:
                        if (xgjl.get(i).getDw9()!=null){
                            xg=xg.concat(xgjl.get(i).getDw9().toString()).concat(",");
                        }else {
                            xg=xg.concat("0").concat(",");
                        }                        break;
                    case 10:
                        if (xgjl.get(i).getDw10()!=null){
                            xg=xg.concat(xgjl.get(i).getDw10().toString()).concat("");
                        }else {
                            xg=xg.concat("0").concat("");
                        }                        break;
                }
            }
            prjBudgetDao.updateSPTGZT(xg,xgjl.get(i).getMc(),xgjl.get(i).getXm());
        }
    }
    @Override
    @GetMapping("/updateSPTGZT1")
    @Transactional()
    public void updateSPTGZT1(@RequestParam(value = "rws") Integer rws) {
        PrjProject prjProject = prjProjectDao.queryById(rws);
        Integer xmid = prjProject.getId();
        List<PrjBudget> xgjl = prjBudgetDao.getBudgetList(xmid);
//        获取完成单位列表
        String xm = String.valueOf(xmid);
        Map<String, String> paraMap = new HashMap<>();
        paraMap.put("xm", xm);
        List<PrjCompletecompany> list = prjCompletecompanyService.getCompanyList(paraMap);
//        拼接修改记录修
        for (int i = 0; i < xgjl.size(); i++) {
            String xg = "";
            if (xgjl.get(i).getYsje()!=null) {
                xg = xgjl.get(i).getYsje().toString()+",";
            }else {
                xg = "0,";
            }
            for (int j = 0; j < list.size(); j++) {
                int k = j + 1;
                switch (k) {
                    case 1:
                        if (xgjl.get(i).getDw1()!=null){
                            xg=xg.concat(xgjl.get(i).getDw1().toString()).concat(",");
                        }else {
                            xg=xg.concat("0").concat(",");
                        }
                        break;
                    case 2:
                        if (xgjl.get(i).getDw2()!=null){
                            xg=xg.concat(xgjl.get(i).getDw2().toString()).concat(",");
                        }else {
                            xg=xg.concat("0").concat(",");
                        }                        break;
                    case 3:
                        if (xgjl.get(i).getDw3()!=null){
                            xg=xg.concat(xgjl.get(i).getDw3().toString()).concat(",");
                        }else {
                            xg=xg.concat("0").concat(",");
                        }                        break;
                    case 4:
                        if (xgjl.get(i).getDw4()!=null){
                            xg=xg.concat(xgjl.get(i).getDw4().toString()).concat(",");
                        }else {
                            xg=xg.concat("0").concat(",");
                        }                        break;
                    case 5:
                        if (xgjl.get(i).getDw5()!=null){
                            xg=xg.concat(xgjl.get(i).getDw5().toString()).concat(",");
                        }else {
                            xg=xg.concat("0").concat(",");
                        }                        break;
                    case 6:
                        if (xgjl.get(i).getDw6()!=null){
                            xg=xg.concat(xgjl.get(i).getDw6().toString()).concat(",");
                        }else {
                            xg=xg.concat("0").concat(",");
                        }                        break;
                    case 7:
                        if (xgjl.get(i).getDw7()!=null){
                            xg=xg.concat(xgjl.get(i).getDw7().toString()).concat(",");
                        }else {
                            xg=xg.concat("0").concat(",");
                        }                        break;
                    case 8:
                        if (xgjl.get(i).getDw8()!=null){
                            xg=xg.concat(xgjl.get(i).getDw8().toString()).concat(",");
                        }else {
                            xg=xg.concat("0").concat(",");
                        }                        break;
                    case 9:
                        if (xgjl.get(i).getDw9()!=null){
                            xg=xg.concat(xgjl.get(i).getDw9().toString()).concat(",");
                        }else {
                            xg=xg.concat("0").concat(",");
                        }                        break;
                    case 10:
                        if (xgjl.get(i).getDw10()!=null){
                            xg=xg.concat(xgjl.get(i).getDw10().toString()).concat("");
                        }else {
                            xg=xg.concat("0").concat("");
                        }                        break;
                }
            }
            prjBudgetDao.updateSPTGZT(xg,xgjl.get(i).getMc(),xgjl.get(i).getXm());
        }
    }

    @Override
    public void updateSFXG(Integer xmid) {
        prjProjectDao.updateSFXG(xmid);
    }

    @Override
    public int updateZy(PrjProjects prjProjects) {
        return this.prjProjectDao.updateZy(prjProjects);
    }


}
