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

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.service.CrudService;
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 org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true)
public class ItemService extends CrudService<ItemMapper, WbsItem> {
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ProjectService projectService;


    public WbsItem get(String id) {
        return super.get(id);
    }

    /**
     *获取诚信扣款状态
     */
    public WbsItem getState(String id){
        return itemMapper.getState(id);
    }


    public List<WbsItem> findList(WbsProject wbsProject) {
        return itemMapper.getList(wbsProject);
    }

    @Transactional(readOnly = false)
    public void update(List<WbsItem> wbsItems){
        itemMapper.updateList(wbsItems);
    }

    @Transactional(readOnly = false)
    public void save(WbsItem wbsItem) {
        String projectId = wbsItem.getProjectId();
        WbsProject wbsProject = projectService.get(projectId);
        //项目数递增
        int num = wbsProject.getNumber()+1;
        projectService.updateNum(num,projectId);
        wbsItem.setCreateDate(new Date());
        itemMapper.insert(wbsItem);
    }

    @Transactional(readOnly = false)
    public void delete(WbsItem wbsItem) {
        super.delete(wbsItem);
    }

    public List<WbsItem> findItemByName(String itemName,String projectId){
        return itemMapper.findItemByName(itemName,projectId);
    }

    //删除
    @Transactional(readOnly = false)
    public Integer updateItem(WbsItem wbsItem){ return itemMapper.updateItem(wbsItem); }

    //根据工程删除 项目
    @Transactional(readOnly = false)
    public Integer deleteItem(String parentId){
        return itemMapper.deleteItem(parentId);
    }
    //删除项目
    @Transactional(readOnly = false)
    public AjaxJson deleteItemOne(WbsItem wbsItem){
        WbsItem wbsItem1 = itemMapper.findItemById(wbsItem.getId());
        WbsProject wbsProject = projectService.findProjectById(wbsItem1.getProjectId());
        Integer number = wbsProject.getNumber()-1;
        wbsProject.setNumber(number);
        wbsItem.setDelFlag("1");
        AjaxJson j = new AjaxJson();
        try {
            itemMapper.updateItem(wbsItem);
            projectService.updateProjectNumber(wbsProject);
            j.setSuccess(true);
            j.setMsg(wbsItem1.getProjectId());
        }catch (Exception e){
            j.setSuccess(false);
            j.setMsg("删除结算书失败!");
        }
        return j;
    }

    /**
     * 诚信扣款
     * @param list
     */
    @Transactional(readOnly = false)
    public void updateFee(List<WbsItem> list){
//        List<WbsItem> newlist = list.stream().collect(Collectors.collectingAndThen(Collectors.
//                toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getUnitType()))), ArrayList::new));
//      List<WbsItem> list1 = getNew(list);
       itemMapper.updateFee(list);
    }

    /**
     * 更新扣款状态
     * @param list
     * @return
     */
    private List<WbsItem> getNew(List<WbsItem> list){
        for(int i=0;i<list.size();i++){
            String unitType = list.get(i).getUnitType();
            if(unitType == null){
                continue;
            }
            String state = list.get(i).getState();
            if(state.equals("0")){
                continue;
            }
            for(int j=i+1;j<list.size();j++){
                if(unitType.equals(list.get(j).getUnitType())){
                    list.get(j).setState("0");
                }
            }
        }
        return list;
    }



}
