package com.liang.service.impl;

import com.liang.dao.PrjAnnualbudgetXgjlDao;
import com.liang.dao.PrjBudgetDao;
import com.liang.dao.PrjCompletecompanyDao;
import com.liang.entity.*;
import com.liang.dao.PrjAnnualbudgetdetailDao;
import com.liang.service.PrjAnnualbudgetXgjlService;
import com.liang.service.PrjAnnualbudgetdetailService;
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.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 年度预算明细(PrjAnnualbudgetdetail)表服务实现类
 *
 * @author makejava
 * @since 2023-05-19 21:59:15
 */
@Service("prjAnnualbudgetdetailService")
public class PrjAnnualbudgetdetailServiceImpl implements PrjAnnualbudgetdetailService {
    @Resource
    private PrjAnnualbudgetdetailDao prjAnnualbudgetdetailDao;
    @Resource
    private PrjBudgetDao prjBudgetDao;

    @Resource
    private PrjCompletecompanyDao prjCompletecompanyDao;

    @Resource
    private PrjAnnualbudgetXgjlService prjAnnualbudgetXgjlService;


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

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

    /**
     * 新增数据
     *
     * @param prjAnnualbudgetdetail 实例对象
     * @return 实例对象
     */
    @Override
    public PrjAnnualbudgetdetail insert(PrjAnnualbudgetdetail prjAnnualbudgetdetail) {
        this.prjAnnualbudgetdetailDao.insert(prjAnnualbudgetdetail);
        return prjAnnualbudgetdetail;
    }

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

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

    @Override
    public int insertBatchByXm(Integer xm, Integer nys) {
        List<PrjBudget> budgetList = prjBudgetDao.getBudgetList(xm);
        List<PrjCompletecompany> compList = prjCompletecompanyDao.queryByXM(xm);
        List flag = new ArrayList<>();
        double temp = -1;
        if (compList.size() > 0) {
            for (int i = 0; i < compList.size(); i++) {
                if ("国网陕西省电力公司".equals(compList.get(i).getDwqc()) || "国网陕西省电力有限公司".equals(compList.get(i).getDwqc()) || "国网陕西省电力公司电力科学研究院".equals(compList.get(i).getDwqc()) || "国网陕西省电力有限公司电力科学研究院".equals(compList.get(i).getDwqc())) {
                    flag.add(i);
                }
            }
        }

        if (budgetList.size() > 0) {
            for (int i = 0; i < budgetList.size(); i++) {
                budgetList.get(i).setXm(nys);
                if (flag.size() > 0) {
                    for (int j = 0; j < flag.size(); j++) {
                        if (temp == -1) {
                            temp = 0;
                        }
                        switch (flag.get(j).toString()) {
                            case "0":
                                if (budgetList.get(i).getDw1() != null) {
                                    temp = temp + budgetList.get(i).getDw1();
                                } else {
                                    temp = temp + 0.0;
                                }
                                break;
                            case "1":
                                if (budgetList.get(i).getDw2() != null) {
                                    temp = temp + budgetList.get(i).getDw2();
                                } else {
                                    temp = temp + 0.0;
                                }
                                break;
                            case "2":
                                if (budgetList.get(i).getDw3() != null) {
                                    temp = temp + budgetList.get(i).getDw3();
                                } else {
                                    temp = temp + 0.0;
                                }
                                break;
                            case "3":
                                if (budgetList.get(i).getDw4() != null) {
                                    temp = temp + budgetList.get(i).getDw4();
                                } else {
                                    temp = temp + 0.0;
                                }
                                break;
                            case "4":
                                if (budgetList.get(i).getDw5() != null) {
                                    temp = temp + budgetList.get(i).getDw5();
                                } else {
                                    temp = temp + 0.0;
                                }
                                break;
                            case "5":
                                if (budgetList.get(i).getDw6() != null) {
                                    temp = temp + budgetList.get(i).getDw6();
                                } else {
                                    temp = temp + 0.0;
                                }
                                break;
                            case "6":
                                if (budgetList.get(i).getDw7() != null) {
                                    temp = temp + budgetList.get(i).getDw7();
                                } else {
                                    temp = temp + 0.0;
                                }
                                break;
                            case "7":
                                if (budgetList.get(i).getDw8() != null) {
                                    temp = temp + budgetList.get(i).getDw8();
                                } else {
                                    temp = temp + 0.0;
                                }
                                break;
                            case "8":
                                if (budgetList.get(i).getDw9() != null) {
                                    temp = temp + budgetList.get(i).getDw9();
                                } else {
                                    temp = temp + 0.0;
                                }
                                break;
                            case "9":
                                if (budgetList.get(i).getDw10() != null) {
                                    temp = temp + budgetList.get(i).getDw10();
                                } else {
                                    temp = temp + 0.0;
                                }
                                break;
                            default:
                                System.out.println("flagflag:" + flag);
                                break;
                        }
                    }
                }
                if (temp > -1) {
                    budgetList.get(i).setXmzys(temp);
                    temp = -1;
                }
            }
            return this.prjAnnualbudgetdetailDao.insertBatch(budgetList);
        } else
            return 0;
    }

    @Override
    public List<PrjAnnualbudgetdetail> getBudgetList(Integer nys) {
        return this.prjAnnualbudgetdetailDao.getBudgetList(nys);
    }

    @Override
    public List<PrjAnnualbudgetdetail> getFysqBudgetList(Integer nys) {
        return this.prjAnnualbudgetdetailDao.getFysqBudgetList(nys);
    }

    @Override
    public List<PrjAnnualbudgetdetail> getSubNode(Integer nys, Integer page, Integer limit, String mc) {
        return this.prjAnnualbudgetdetailDao.getSubNode(nys,page,limit,mc);
    }

    @Override
    public List<PrjAnnualbudgetdetail> getBz(Map map) {
        return this.prjAnnualbudgetdetailDao.getBz(map);
    }

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

    @Override
    public Integer getSubNodeCount(Integer nys, Integer page, Integer limit) {
        return this.prjAnnualbudgetdetailDao.getSubNodeCount(nys,page,limit);
    }

    @Override
    public List<PrjAnnualbudgetdetail> listAllBudgetDetail(Integer nys) {
        return this.prjAnnualbudgetdetailDao.listAllBudgetDetail(nys);
    }

    @Override
    public List<PrjAnnualbudgetdetail> getXGJL(int xmid) {
        List<PrjAnnualbudgetdetail> xgjl = prjAnnualbudgetdetailDao.getXGJL(xmid);
        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);
        }
        List<PrjAnnualbudgetXgjl> prjAnnualbudgetXgjlList=prjAnnualbudgetXgjlService.getXGJL(xmid);
//        获取修改记录
        for (int i = 0; i < prjAnnualbudgetXgjlList.size(); i++) {
            int k=i+1;
            List<PrjAnnualbudgetXgjl> xg   =prjAnnualbudgetXgjlService.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 Double getXMZYS(String id) {
        Double xmzys = prjAnnualbudgetdetailDao.getXMZYS(id);
        return xmzys;
    }

    @Override
    public int updateXg(List<PrjAnnualBudgetRecord> record) {
        int count = 0;
        for (PrjAnnualBudgetRecord budget : record) {
            count += this.prjAnnualbudgetdetailDao.updateXG(budget);
        }
        return count;    }

    @Override
    public List<PrjAnnualbudgetdetail> getSptgZt(Integer id) {
        List<PrjAnnualbudgetdetail> list=prjAnnualbudgetdetailDao.getSptgZt(id);
        for (int i = 0; i < list.size(); i++) {
            String[] strings = list.get(i).getSptgzt().split(",");
            list.get(i).setYsje(Double.valueOf(strings[0]));
            list.get(i).setXmz(Double.valueOf(strings[1]));
            list.get(i).setByj(Double.valueOf(strings[2]));
        }
//        编辑旧数据
        return list;
    }

    @Override
    public PrjAnnualbudgetdetail getByj(Map map) {
        return this.prjAnnualbudgetdetailDao.getByj(map);
    }

    @Override
    public Double getKmXmzys(Integer nys, String fymc) {


        Double kmXmzys = prjAnnualbudgetdetailDao.getKmXmzys(nys, fymc);
        if (kmXmzys !=null){
            return kmXmzys;
        }else {
            return  0.0;
        }
    }

}
