package com.liang.service.impl;

import com.liang.common.base.BaseResult;
import com.liang.common.enums.ProjectTypeEnnum;
import com.liang.common.exception.CustomException;
import com.liang.dao.PrjAnnualbudgetDao;
import com.liang.dao.PrjAnnualbudgetdetailDao;
import com.liang.entity.PrjAnnualbudget;
import com.liang.entity.PrjAnnualbudgetdetail;
import com.liang.entity.PrjProject;
import com.liang.service.PrjAnnualbudgetService;
import com.liang.service.PrjProjectService;
import io.jsonwebtoken.lang.Collections;
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.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 年度预算(PrjAnnualbudget)表服务实现类
 *
 * @author makejava
 * @since 2023-05-17 08:56:50
 */
@Service("prjAnnualbudgetService")
@RestController
public class PrjAnnualbudgetServiceImpl implements PrjAnnualbudgetService {
    @Resource
    private PrjAnnualbudgetDao prjAnnualbudgetDao;
    @Resource
    private PrjAnnualbudgetdetailDao prjAnnualbudgetdetailDao;

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

    @Override
    public PrjAnnualbudget queryByRws(Integer rws) {
        return this.prjAnnualbudgetDao.queryByRws(rws);
    }

    @Override
    public PrjAnnualbudget getAnnualBudget(Map map) {
        return this.prjAnnualbudgetDao.getAnnualBudget(map);
    }

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

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

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

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

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.prjAnnualbudgetDao.deleteById(id) > 0;
    }

    @Override
    public List<PrjAnnualbudget> getNdysList(Map map) {
        return this.prjAnnualbudgetDao.getNdysList(map);
    }

    @Override
    public List<String> getYearList() {
        return this.prjAnnualbudgetDao.getYearList();
    }

    @Override
    public int batchDelete(List<Integer> idList) {
        prjAnnualbudgetdetailDao.batchDelete(idList);
        return this.prjAnnualbudgetDao.batchDelete(idList);
    }

    @Override
    public int updateSpzt(PrjAnnualbudget annualbudget) {
        int num = prjAnnualbudgetDao.updateSpzt(annualbudget);
//        审批通过后修改记录中添加审批通过状态
//        1.获取年度预算列表
        List<PrjAnnualbudgetdetail> xgjl = prjAnnualbudgetdetailDao.getXGJL(annualbudget.getId());
//       2.拼接审批通过状态字段数据
        if (annualbudget.getSpzt().equals("审批结束")&&num>0){
            for (int i = 0; i < xgjl.size(); i++) {
                String xgjlxz = xgjl.get(i).getYsje() + ","+xgjl.get(i).getXmz()+","+xgjl.get(i).getByj();
//                xgjl.get(i).setXgjlxz(xgjlxz);
            prjAnnualbudgetdetailDao.updateSPTGZT(annualbudget.getId(),xgjl.get(i).getFymc(),xgjlxz);
            }
        }
        return num;
    }

    @Resource
    PrjProjectService prjProjectService;

    /**
     * 更新 系统计算执行 和 实际已执行
     *
     * @param rws
     */
    @Override
    public void updateSJYZXAndXTJSYZX(Integer rws) {
        List<PrjAnnualbudget> prjAnnualbudgets = prjAnnualbudgetDao.listByRws(rws);
        //第一年的年度预算不需要更新
        if (!Collections.isEmpty(prjAnnualbudgets)) {
            //更新 系统计算已执行
            prjAnnualbudgetDao.updateXTJSYZX(rws);
            //更新 历史年度预算的实际已执行
            prjAnnualbudgetDao.updateSJYZX(rws);
            //更新已执行总计
            prjAnnualbudgetDao.updateSJYZXSum(rws);
        }
    }

    @GetMapping("/updateSJYZXAndXTJSYZX/{rwsId}/{nysId}")
    @Transactional(rollbackFor = Exception.class)
    public BaseResult updateSJYZXAndXTJSYZX(@PathVariable("rwsId") Integer rwsId, @PathVariable("nysId") Integer nysId) {
        PrjAnnualbudget prjAnnualbudget = prjAnnualbudgetDao.queryById(nysId);
        try {
            if (ProjectTypeEnnum.ZFLXM.getCode().equals(prjAnnualbudget.getXmlx())) {
                //政府项目
                //更新费用名称
                prjAnnualbudgetDao.updateZFXMFYMC(nysId, rwsId);
            } else if (ProjectTypeEnnum.SXGSKJXM.getCode().equals(prjAnnualbudget.getXmlx())) {
                //陕西公司科技项目
                prjAnnualbudgetDao.updateSXGSKJXMFYMC(nysId, rwsId);
            } else if (ProjectTypeEnnum.GWGSKJXM.getCode().equals(prjAnnualbudget.getXmlx())) {
                //国网公司科技项目
                prjAnnualbudgetDao.updateGWGSKJXMFYMC(nysId, rwsId);
            }
        } catch (Exception e) {
            throw new CustomException("已执行数据同步失败");
        }

        return BaseResult.success("已执行数据同步成功");
    }

    @Override
    public PrjAnnualbudget queryByRws1(Integer rws, Integer nf) {
        return prjAnnualbudgetDao.queryByRws1(rws, nf);
    }

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