package com.liang.service.impl;

import com.liang.dao.BaseProjectbudgetDao;
import com.liang.entity.*;
import com.liang.dao.PrjBudgetDao;
import com.liang.service.PrjBudgetService;
import com.liang.service.PrjCompletecompanyService;
import com.liang.service.PrjProjectXgjlService;
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;

/**
 * 项目预算(PrjBudget)表服务实现类
 *
 * @author makejava
 * @since 2023-03-10 07:49:56
 */
@Service("prjBudgetService")
public class PrjBudgetServiceImpl implements PrjBudgetService {
    @Resource
    private PrjBudgetDao prjBudgetDao;
    @Resource
    private BaseProjectbudgetDao baseProjectbudgetDao;
    @Resource
    private PrjProjectXgjlService prjProjectXgjlService;
    @Resource
    private PrjCompletecompanyService prjCompletecompanyService;

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

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

    /**
     * 新增数据
     *
     * @param prjBudget 实例对象
     * @return 实例对象
     */
    @Override
    public PrjBudget insert(PrjBudget prjBudget) {
        this.prjBudgetDao.insert(prjBudget);
        return prjBudget;
    }

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

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

    @Override
    public boolean deleteByXm(Integer xm) {
        return this.prjBudgetDao.deleteByXm(xm) > 0;
    }

    @Override
    public List<PrjBudget> getBudgetList(Integer xm) {
        return this.prjBudgetDao.getBudgetList(xm);
    }

    @Override
    public int insertBatchByXmlx(String xmlx, Integer xm) {
        List<BaseProjectbudget> budgetList = baseProjectbudgetDao.getBudgetListByXmlx(xmlx);
        for (int i = 0; i < budgetList.size(); i++) {
            budgetList.get(i).setXm(xm);
        }
        return this.prjBudgetDao.insertBatch(budgetList);
    }

    @Override
    public int updateBatch(List<PrjBudget> prjBudgets) {
        int count = 0;
        for (PrjBudget budget : prjBudgets) {
            count += this.prjBudgetDao.update(budget);
        }
        return count;
    }

    @Override
    public int updateYsByXm(Integer xm, Integer count) {
        int num = this.prjBudgetDao.updateYsByXm(xm, count);
        this.prjBudgetDao.updateYSYE(xm);
        this.prjBudgetDao.emptyYSYE(xm);
        return num;
    }

    @Override
    public List<PrjBudget> getXGJL(int xmid) {
        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;
                }
            }
            xgjl.get(i).setXg(xg);
        }
        List<PrjProjectXgjl> prjProjectXgjlList=prjProjectXgjlService.getXGJL(xmid);
//        获取修改记录
        for (int i = 0; i < prjProjectXgjlList.size(); i++) {
            int k=i+1;
            List<PrjProjectXgjl> xg   =prjProjectXgjlService.getXG(k,xmid);
            switch (k){
                case 1:
                    for (int j = 0; j < xg.size(); j++) {
                        xgjl.get(j).setXg1(xg.get(j).getXg());
                    };break;
                case 2:
                    for (int j = 0; j < xg.size(); j++) {
                        xgjl.get(j).setXg2(xg.get(j).getXg());
                    };break;
                case 3:
                    for (int j = 0; j < xg.size(); j++) {
                        xgjl.get(j).setXg3(xg.get(j).getXg());
                    };break;
                case 4:
                    for (int j = 0; j < xg.size(); j++) {
                        xgjl.get(j).setXg4(xg.get(j).getXg());
                    };break;
                case 5:
                    for (int j = 0; j < xg.size(); j++) {
                        xgjl.get(j).setXg5(xg.get(j).getXg());
                    };break;
                case 6:
                    for (int j = 0; j < xg.size(); j++) {
                        xgjl.get(j).setXg6(xg.get(j).getXg());
                    };break;
                case 7:
                    for (int j = 0; j < xg.size(); j++) {
                        xgjl.get(j).setXg7(xg.get(j).getXg());
                    };break;
                case 8:
                    for (int j = 0; j < xg.size(); j++) {
                        xgjl.get(j).setXg8(xg.get(j).getXg());
                    };break;
                case 9:
                    for (int j = 0; j < xg.size(); j++) {
                        xgjl.get(j).setXg9(xg.get(j).getXg());
                    };break;
                case 10:
                    for (int j = 0; j < xg.size(); j++) {
                        xgjl.get(j).setXg10(xg.get(j).getXg());
                    };break;
            }

        }

        return xgjl;
    }

    @Override
    public int updateXg(List<PrjBudgetRecord> prjBudgetRecordList) {
        int count = 0;
        for (PrjBudgetRecord budget : prjBudgetRecordList) {

            count += this.prjBudgetDao.updateXG(budget);
        }
        return count;
    }

    @Override
    public List<PrjBudget> getOldBudgetList(Integer id) {
        return this.prjBudgetDao.getOldBudgetList(id);
    }
}
