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

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(WbsSelection wbsSelection) {
        return itemMapper.findList(wbsSelection);
    }

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