package com.jeeplus.modules.sg.project.service;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.common.utils.sg.ExcelUtil;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sg.overheadline.entity.JkxlDetailFee;
import com.jeeplus.modules.sg.overheadline.entity.Settlement;
import com.jeeplus.modules.sg.overheadline.mapper.OverheadLineMapper;
import com.jeeplus.modules.sg.overheadline.service.FeeAdjustService;
import com.jeeplus.modules.sg.overheadline.service.OverheadLineService;
import com.jeeplus.modules.sg.overheadline.service.SettlementService;
import com.jeeplus.modules.sg.overheadline.util.BashInfo;
import com.jeeplus.modules.sg.project.entity.WbsItem;
import com.jeeplus.modules.sg.project.entity.WbsProject;
import com.jeeplus.modules.sg.project.entity.WbsSelection;
import com.jeeplus.modules.sg.project.mapper.ItemMapper;
import com.jeeplus.modules.sg.project.mapper.ProjectMapper;
import com.jeeplus.modules.sg.substation.service.SubstationService;
import com.jeeplus.modules.sg.substation.util.SubstationInfo;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class ProjectService extends CrudService<ProjectMapper, WbsProject> {
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ItemService itemService;
    @Autowired
    private OverheadLineService overheadLineService;
    @Autowired
    private SubstationService substationService;
    @Autowired
    private SettlementService settlementService;
    @Autowired
    private FeeAdjustService feeAdjustService;
    @Autowired
    private OverheadLineMapper overheadLineMapper;


    public WbsProject get(String id) {
        WbsProject wbsProject =  super.get(id);
        WbsProject wbsProject1 = new WbsProject();
        wbsProject1.setProjectId(id);
        List<WbsItem> items = itemService.findList(wbsProject1);
        wbsProject.setWbsItems(items);
        return wbsProject;
    }

    @Transactional(readOnly = false)
    public String save(MultipartFile file ,WbsItem wbsItem) throws IOException, InvalidFormatException {
        //架空类型判断
        ImportExcel jk = new ImportExcel(file,1,new String[]{BashInfo.SHEET_GCHZ,BashInfo.SHEET_GCHZ1},false); //架空汇总
        if(ImportExcel.getSheet(jk) != null){
            wbsItem.setType(BashInfo.PROJECT_JKXL);
            overheadLineService.saveJk(file,wbsItem);
            return BashInfo.PROJECT_JKXL;
        }
        //电缆类型判断
        ImportExcel dljz = new ImportExcel(file, 1, BashInfo.SHEET_JZHZB,false);   //电缆线路建筑工程费用汇总表
        ImportExcel dlaz = new ImportExcel(file, 1, BashInfo.SHEET_HZHZB,false);   //电缆线路安装工程费用汇总表
        if(ImportExcel.getSheet(dlaz) != null || ImportExcel.getSheet(dljz) != null){
            wbsItem.setType(BashInfo.PROJECT_DLXL);
            overheadLineService.saveDl(file,wbsItem);
            return BashInfo.PROJECT_DLXL;
        }
        //变电站类型判断
        ImportExcel bdjz = new ImportExcel(file, 1, SubstationInfo.SHEET_JZHZB,false);   //变电站建筑工程费用汇总表
        ImportExcel bdaz = new ImportExcel(file, 1, SubstationInfo.SHEET_HZHZB,false);   //变电站安装工程费用汇总表
        if(ImportExcel.getSheet(bdjz) != null || ImportExcel.getSheet(bdaz) != null){
            wbsItem.setType(BashInfo.PROJECT_BDZ);
            substationService.save(file,wbsItem);
            return BashInfo.PROJECT_BDZ;
        }
        //WBS类型判断
        ImportExcel importExcel = new ImportExcel(file,1,0);
        double bdzJzTotal = ExcelUtil.getDouble(importExcel,BashInfo.BZDWBS,1,4);
        double bdzAzTotal = ExcelUtil.getDouble(importExcel,BashInfo.BZDWBS,1,7);
        double dlJzTotal = ExcelUtil.getDouble(importExcel,BashInfo.DLWBS,1,4);
        double dlAzTotal = ExcelUtil.getDouble(importExcel,BashInfo.DLWBS,1,7);
        double jkxlTotal = ExcelUtil.getDouble(importExcel,BashInfo.JKXLWBS,1,7);
        if(bdzAzTotal != 0.00 || bdzJzTotal != 0.00){
            String id = wbsItem.getId();
            WbsItem wbsItem1 = itemService.get(id);
            if(wbsItem1 == null){
                wbsItem.setType(BashInfo.PROJECT_BDZ);
                wbsItem.setDelFlag("0");
                //保存项目
                itemService.save(wbsItem);
            }
            double[] totalJzFees = ExcelUtil.getDoubleArray(importExcel,SubstationInfo.BDZJZWBS,1,4);
            double[] totalAzFees = ExcelUtil.getDoubleArray(importExcel,SubstationInfo.BDZAZWBS,1,7);
            double[] otherFees = ExcelUtil.getDoubleArray(importExcel,SubstationInfo.BDZOTHWBS,1,9);
            List<JkxlDetailFee> list = new ArrayList<>();
            JkxlDetailFee jkxlDetailFee = null;
            for(int i=0;i<totalJzFees.length;i++){
                jkxlDetailFee = new JkxlDetailFee();
                jkxlDetailFee.setId(id);
                jkxlDetailFee.setWbsCode(SubstationInfo.BDZJZWBS[i]);
                jkxlDetailFee.setTotalFee(totalJzFees[i]);
                list.add(jkxlDetailFee);
            }
            for(int i=0;i<totalAzFees.length;i++){
                jkxlDetailFee = new JkxlDetailFee();
                jkxlDetailFee.setId(id);
                jkxlDetailFee.setWbsCode(SubstationInfo.BDZAZWBS[i]);
                jkxlDetailFee.setTotalFee(totalAzFees[i]);
                list.add(jkxlDetailFee);
            }
            for(int j=0;j<otherFees.length;j++){
                jkxlDetailFee = new JkxlDetailFee();
                jkxlDetailFee.setId(id);
                jkxlDetailFee.setWbsCode(SubstationInfo.BDZOTHWBS[j]);
                jkxlDetailFee.setTotalFee(otherFees[j]);
                list.add(jkxlDetailFee);
            }
            overheadLineMapper.saveList(list);
            return BashInfo.PROJECT_BDZ;
        }
        if(dlJzTotal != 0.00 || dlAzTotal != 0.00){
            String id = wbsItem.getId();
            WbsItem wbsItem1 = itemService.get(id);
            if(wbsItem1 == null){
                wbsItem.setType(BashInfo.PROJECT_DLXL);
                wbsItem.setDelFlag("0");
                //保存项目
                itemService.save(wbsItem);
            }
            double[] totalJzFees = ExcelUtil.getDoubleArray(importExcel,BashInfo.JZWBSIDS,1,4);
            double[] totalAzFees = ExcelUtil.getDoubleArray(importExcel,BashInfo.AZWBSIDS,1,7);
            double[] otherFees = ExcelUtil.getDoubleArray(importExcel,BashInfo.DLOTHIDS,1,9);
            List<JkxlDetailFee> list = new ArrayList<>();
            JkxlDetailFee jkxlDetailFee = null;
            for(int i=0;i<totalJzFees.length;i++){
                jkxlDetailFee = new JkxlDetailFee();
                jkxlDetailFee.setId(id);
                jkxlDetailFee.setWbsCode(BashInfo.JZWBSIDS[i]);
                jkxlDetailFee.setTotalFee(totalJzFees[i]);
                list.add(jkxlDetailFee);
            }
            for(int i=0;i<totalAzFees.length;i++){
                jkxlDetailFee = new JkxlDetailFee();
                jkxlDetailFee.setId(id);
                jkxlDetailFee.setWbsCode(BashInfo.AZWBSIDS[i]);
                jkxlDetailFee.setTotalFee(totalAzFees[i]);
                list.add(jkxlDetailFee);
            }
            for(int j=0;j<otherFees.length;j++){
                jkxlDetailFee = new JkxlDetailFee();
                jkxlDetailFee.setId(id);
                jkxlDetailFee.setWbsCode(BashInfo.DLOTHIDS[j]);
                jkxlDetailFee.setTotalFee(otherFees[j]);
                list.add(jkxlDetailFee);
            }
            overheadLineMapper.saveList(list);
            return BashInfo.PROJECT_DLXL;
        }
        if(jkxlTotal != 0.00){
            String id = wbsItem.getId();
            WbsItem wbsItem1 = itemService.get(id);
            if(wbsItem1 == null){
                wbsItem.setType(BashInfo.PROJECT_JKXL);
                wbsItem.setDelFlag("0");
                //保存项目
                itemService.save(wbsItem);
            }
            double[] totalFees = ExcelUtil.getDoubleArray(importExcel,BashInfo.WBSIDS,1,7);
            double[] otherFees = ExcelUtil.getDoubleArray(importExcel,BashInfo.WBSOTHIDS,1,9);
            List<JkxlDetailFee> list = new ArrayList<>();
            JkxlDetailFee jkxlDetailFee = null;
            for(int i=0;i<totalFees.length;i++){
                jkxlDetailFee = new JkxlDetailFee();
                jkxlDetailFee.setId(id);
                jkxlDetailFee.setWbsCode(BashInfo.WBSIDS[i]);
                jkxlDetailFee.setTotalFee(totalFees[i]);
                list.add(jkxlDetailFee);
            }
            for(int j=0;j<otherFees.length;j++){
                jkxlDetailFee = new JkxlDetailFee();
                jkxlDetailFee.setId(id);
                jkxlDetailFee.setWbsCode(BashInfo.WBSOTHIDS[j]);
                jkxlDetailFee.setTotalFee(otherFees[j]);
                list.add(jkxlDetailFee);
            }
            overheadLineMapper.saveList(list);
            return BashInfo.PROJECT_JKXL;
        }

        return BashInfo.INVALIDFORMAT;
    }



    public List<WbsProject> findList(WbsProject wbsProject) {
        List<WbsProject> list =  projectMapper.findList(wbsProject);
            for(WbsProject wbsProject1 : list){
                String id = wbsProject1.getId();
                wbsProject.setProjectId(id);
                List<WbsItem> items = itemService.findList(wbsProject);
                wbsProject1.setWbsItems(items);
            }
        return list;
    }

    //分页查询
    public Page<WbsProject> findPage(Page<WbsProject> page, WbsProject wbsProject) {
        //数据权限过滤
        dataRuleFilter(wbsProject);
        wbsProject.setPage(page);
        List<WbsProject> list =  projectMapper.findList(wbsProject);
        long count = page.getCount();
        Integer pageNo = page.getPageNo();
        //根据工程ID查询项目信息
        for(WbsProject wbsProject1 : list){
            String id = wbsProject1.getId();
            wbsProject.setProjectId(id);
            List<WbsItem> items = itemService.findList(wbsProject);
            wbsProject1.setWbsItems(items);
        }
        if(StringUtils.isNoneBlank(wbsProject.getItemName()) || wbsProject.getItemStartDate() != null || wbsProject.getItemEndDate() != null || StringUtils.isNoneBlank(wbsProject.getUnit()) || StringUtils.isNoneBlank(wbsProject.getType())){
            List<WbsProject> wbsProjects = new ArrayList<>();
            for(WbsProject wbsProject1 : list){
                if(wbsProject1.getWbsItems().size()>0){
                    wbsProjects.add(wbsProject1);
                }
            }
            page.setCount(wbsProjects.size());
            page.setList(wbsProjects);
            return page;
        }
        page.setPageNo(pageNo);
        page.setCount(count);
        page.setList(list);
        return page;
    }


    public List<WbsProject> findByName(String projectName) {
        return projectMapper.findByName(projectName);
    }

    @Transactional(readOnly = false)
    public void save(WbsProject wbsProject) {
        super.save(wbsProject);
    }

    @Transactional(readOnly = false)
    public void update(WbsProject wbsProject,List<WbsItem> wbsItems){
        save(wbsProject);
        itemService.update(wbsItems);
    }

    @Transactional(readOnly = false)
    public void updateNum(int number,String id){
        projectMapper.updateNum(number,id);
    }

    @Transactional(readOnly = false)
    public void delete(WbsProject wbsProject) {
        String id = wbsProject.getId();
        WbsProject wbsProject1 = get(id);
        List<WbsItem> wbsItems = wbsProject1.getWbsItems();
        for(WbsItem wbsItem : wbsItems){
            String itemId = wbsItem.getId();
            WbsItem wbsItem1 = new WbsItem();
            wbsItem1.setId(itemId);
            itemService.delete(wbsItem1);
            overheadLineService.delete(itemId);
            feeAdjustService.delete(itemId);
            settlementService.delete(itemId);
        }
        super.delete(wbsProject);
    }



    //修改工程下项目数量
    @Transactional(readOnly = false)
    public Integer updateProjectNumber(WbsProject wbsProject){
        return projectMapper.updateProNumber(wbsProject);
    }

    //根据id查找工程
    public WbsProject findProjectById(String id){
        return projectMapper.findProjectById(id);
    }

    //删除工程
    @Transactional(readOnly = false)
    public AjaxJson deleteProject(WbsProject wbsProject){
        AjaxJson j = new AjaxJson();
        j.setSuccess(true);
        j.setMsg("删除工程成功!");
        try {
            itemService.deleteItem(wbsProject.getId());
            wbsProject.setDelFlag("1");
            projectMapper.deleteProject(wbsProject);
        }catch (Exception e){
            j.setSuccess(false);
            j.setMsg("删除工程失败!");
        }
        return j;
    }


}
