package com.liang.service.impl;

import com.liang.dao.PrjAnnualbudgetDao;
import com.liang.dao.PrjProjectDao;
import com.liang.entity.PrjAnnualbudget;
import com.liang.entity.PrjApproverecord;
import com.liang.dao.PrjApproverecordDao;
import com.liang.entity.PrjProject;
import com.liang.entity.SysUserInfo;
import com.liang.service.PrjApproverecordService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

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

/**
 * 科技项目审批(PrjApproverecord)表服务实现类
 *
 * @author makejava
 * @since 2023-05-03 13:23:12
 */
@Service("prjApproverecordService")
public class PrjApproverecordServiceImpl implements PrjApproverecordService {
    @Resource
    private PrjApproverecordDao prjApproverecordDao;
    @Resource
    private PrjAnnualbudgetDao prjAnnualbudgetDao;
    @Resource
    private PrjProjectDao prjProjectDao;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public PrjApproverecord queryById(Integer id) {
        return this.prjApproverecordDao.queryById(id);
    }

    @Override
    public int updateRevokeRecord(int xmid) {
        return this.prjApproverecordDao.updateRevokeRecord(xmid);
    }

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

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

    @Override
    public int insertBatch(List<PrjApproverecord> recordList) {
        return this.prjApproverecordDao.insertBatch(recordList);
    }

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

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

    @Override
    public List<PrjApproverecord> getApproveList(Map map) {
        return this.prjApproverecordDao.getApproveList(map);
    }

    @Override
    public List<PrjApproverecord> getApproveByXmId(Map map) {
        return this.prjApproverecordDao.getApproveByXmId(map);
    }

    @Override
    public List<PrjApproverecord> getApproveByXmId2(Map map) {
        return this.prjApproverecordDao.getApproveByXmId2(map);
    }

    @Override
    public PrjApproverecord getOldApproveRecord(Integer id) {
        return prjApproverecordDao.getOldApproveRecord(id);
    }

    @Override
    public List<PrjApproverecord> getPrjAnnualBudgetRecordList(String userId) {
        Map<String, String> paraMap = new HashMap<>();
        paraMap.put("userId", userId);
        paraMap.put("tablename","prj_AnnualBudgetRecord");
// 获取待审批的年度预算修改申请
        List<PrjApproverecord> xglist = prjApproverecordDao.getApproveList(paraMap);
        for (int i = 0; i < xglist.size(); i++) {
            PrjAnnualbudget prjAnnualbudget = prjAnnualbudgetDao.queryById(xglist.get(i).getXmid());
            if (prjAnnualbudget!=null) {
                xglist.get(i).setErpbh(prjAnnualbudget.getErpbh());
                xglist.get(i).setSpzt(xglist.get(i).getZt());
            }
        }
        return xglist;
    }

    @Override
    public List<PrjApproverecord> getPrjProjectRecordRecordList(String userId) {
        Map<String, String> paraMap = new HashMap<>();
        paraMap.put("userId", userId);
        paraMap.put("tablename","prj_ProjectRecord");
// 获取待审批的任务书修改申请
        List<PrjApproverecord> xglist = prjApproverecordDao.getApproveList(paraMap);
        for (int i = 0; i < xglist.size(); i++) {
            PrjProject prjProject = prjProjectDao.queryById(xglist.get(i).getXmid());
            if (prjProject!=null) {
                xglist.get(i).setErpbh(prjProject.getErpbh());
                xglist.get(i).setSpzt(xglist.get(i).getZt());
            }
        }
        return xglist;
    }

    @Override
    public List<PrjApproverecord> getprjContractRecordList(String userId) {
        Map<String, String> paraMap = new HashMap<>();
        paraMap.put("userId", userId);
        paraMap.put("tablename","prj_Contract");
// 获取待审批的任务书修改申请
        List<PrjApproverecord> xglist = prjApproverecordDao.getApproveList(paraMap);
        for (int i = 0; i < xglist.size(); i++) {
            xglist.get(i).setSpzt(xglist.get(i).getZt());
        }
        return xglist;
    }

    @Override
    public void updatSpList(String id,Integer userId) {
        this.prjApproverecordDao.updatSpList(id,userId);
    }


    @Override
    public int insertApprove(List<PrjApproverecord> recordList) {
        int num = 0;
        num += this.prjApproverecordDao.updateApprove(recordList.get(0));
        if (recordList.size() > 1)
            num += this.prjApproverecordDao.insert(recordList.get(1));

        return num;
    }
}
