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

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
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.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.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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;


    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;
    }


    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;
    }
}
