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.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sg.overheadline.entity.Settlement;
import com.jeeplus.modules.sg.overheadline.service.OverheadLineService;
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;


    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("1");
            overheadLineService.saveJk(file,wbsItem);
            return "1";
        }
        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("2");
            overheadLineService.saveDl(file,wbsItem);
            return "2";
        }
        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("3");
            substationService.save(file,wbsItem);
            return "3";
        }
        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) {
        wbsProject.setPage(page);
        User user = UserUtils.getUser();
        if(!user.getId().equals("1")){
            wbsProject.setCreateBy(user);
        }
        List<WbsProject> list =  projectMapper.findList(wbsProject);
        long count = page.getCount();
        Integer pageNo = page.getPageNo();
        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 updateNum(int number,String id){
        projectMapper.updateNum(number,id);
    }

    @Transactional(readOnly = false)
    public void delete(WbsProject wbsProject) {
        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 {
//          List<WbsItem> wbsItems = itemService.findItem(wbsProject.getId());
//            for (int i = 0; i < wbsItems.size(); i++) {
//                itemService.deleteCount(wbsItems.get(i).getId());
//                itemService.deleteAdjust(wbsItems.get(i).getId());
//                itemService.deleteDetail(wbsItems.get(i).getId());
//            }
            itemService.deleteItem(wbsProject.getId());
            wbsProject.setDelFlag("1");
            projectMapper.deleteProject(wbsProject);
        }catch (Exception e){
            j.setSuccess(false);
            j.setMsg("删除工程失败!");
        }
        return j;
    }
}
