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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@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 List<WbsItem> findList(WbsProject wbsProject) {
        return itemMapper.getList(wbsProject);
    }

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


    //    //根据项目删除结算
//    @Transactional(readOnly = false)
//    public Integer deleteCount(String itemId){
//        return itemMapper.deleteCount(itemId);
//    }
//    //根据工程删除 项目
//    @Transactional(readOnly = false)
//    public Integer deleteAdjust(String itemId){
//        return itemMapper.deleteAdjust(itemId);
//    }
//    //根据工程删除 项目
//    @Transactional(readOnly = false)
//    public Integer deleteDetail(String itemId){
//        return itemMapper.deleteDetail(itemId);
//    }
//
//    public List<WbsItem> findItem(String parentId){
//        return itemMapper.findItem(parentId);
//    }
//    public WbsItem findItemById(String id){
//        return itemMapper.findItemById(id);
//    }
}
