package org.jeecg.modules.stm.project.create.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.jeecg.modules.stm.project.create.entity.StProjectOrg;
import org.jeecg.modules.stm.project.create.mapper.StProjectOrgMapper;
import org.jeecg.modules.stm.project.create.service.IStProjectOrgService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.stm.project.create.vo.ProjectImportVo;
import org.jeecg.modules.stm.utils.UserUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: st_project_org
 * @Author: jeecg-boot
 * @Date:   2023-08-21
 * @Version: V1.0
 */
@Service
public class StProjectOrgServiceImpl extends ServiceImpl<StProjectOrgMapper, StProjectOrg> implements IStProjectOrgService {

    @Autowired
    private ISysDepartService iSysDepartService;

    @Override
    public void saveInfo(Integer projectCreId, List<Serializable> departCheck) {
        // todo 先不考虑修改的
        String userCode = UserUtil.getUserCode();
        for(Serializable id:departCheck) {
            StProjectOrg stProjectOrg = new StProjectOrg();
            stProjectOrg.setProjectCreId(projectCreId);
            stProjectOrg.setDepartId(String.valueOf(id));
            SysDepart sysDepart = iSysDepartService.getById(id);
            stProjectOrg.setDepartName(sysDepart.getDepartName());
            stProjectOrg.setCreatedUser(userCode);
            stProjectOrg.setUpdatedUser(userCode);
            this.save(stProjectOrg);
        }
    }

    @Override
    public void copy(ProjectImportVo projectImportVo) {
        QueryWrapper<StProjectOrg> queryClearWrapper = new QueryWrapper<>();
        queryClearWrapper.eq("PROJECT_CRE_ID",projectImportVo.getId());
        this.remove(queryClearWrapper);
        QueryWrapper<StProjectOrg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PROJECT_CRE_ID",projectImportVo.getCopyId());
        List<StProjectOrg> stProjectOrgList = this.list(queryWrapper);
        String userCode = UserUtil.getUserCode();
        if(!CollectionUtils.isEmpty(stProjectOrgList)){
            for(StProjectOrg projectOrg:stProjectOrgList){
                projectOrg.setId(null);
                projectOrg.setProjectCreId(projectImportVo.getId());
                projectOrg.setCreatedUser(userCode);
                projectOrg.setUpdatedUser(userCode);
                this.save(projectOrg);
            }
        }
    }

    @Override
    public List<String> listNamesByProjectCreId(Integer projectCreId) {
        QueryWrapper<StProjectOrg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PROJECT_CRE_ID",projectCreId);
        List<StProjectOrg> stProjectOrgList = this.list(queryWrapper);
        List<String> data = new ArrayList<>();
        if(!CollectionUtils.isEmpty(stProjectOrgList)) {
            for (StProjectOrg entity : stProjectOrgList) {
                data.add(entity.getDepartName());
            }
        }
        return data;
    }
}
