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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.jeecg.modules.stm.project.create.entity.StProjectSys;
import org.jeecg.modules.stm.project.create.mapper.StProjectSysMapper;
import org.jeecg.modules.stm.project.create.service.IStProjectSysService;
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.projectbase.sys.entity.StSysManage;
import org.jeecg.modules.stm.projectbase.sys.service.IStSysManageService;
import org.jeecg.modules.stm.utils.UserUtil;

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

/**
 * @Description: st_project_sys
 * @Author: jeecg-boot
 * @Date:   2023-08-21
 * @Version: V1.0
 */
@Service
public class StProjectSysServiceImpl extends ServiceImpl<StProjectSysMapper, StProjectSys> implements IStProjectSysService {

    @Autowired
    private IStSysManageService iStSysManageService;

    @Override
    public void saveInfo(Integer projectCreId, List<Serializable> sysCheck) {
        String userCode = UserUtil.getUserCode();
        for(Serializable id : sysCheck){
            StProjectSys entity = new StProjectSys();
            entity.setCreatedUser(userCode);
            entity.setUpdatedUser(userCode);
            entity.setProjectCreId(projectCreId);
            StSysManage  stSysManage = iStSysManageService.getById(id);
            entity.setSysNo(stSysManage.getSysNo());
            entity.setSysName(stSysManage.getSysName());
            this.save(entity);
        }
    }
    @Override
    public void copy(ProjectImportVo projectImportVo) {
        QueryWrapper<StProjectSys> queryClearWrapper = new QueryWrapper<>();
        queryClearWrapper.eq("PROJECT_CRE_ID",projectImportVo.getId());
        this.remove(queryClearWrapper);
        QueryWrapper<StProjectSys> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PROJECT_CRE_ID",projectImportVo.getCopyId());
        List<StProjectSys> list = this.list(queryWrapper);
        String userCode = UserUtil.getUserCode();
        if(!CollectionUtils.isEmpty(list)){
            for(StProjectSys entity:list){
                entity.setId(null);
                entity.setProjectCreId(projectImportVo.getId());
                entity.setCreatedUser(userCode);
                entity.setUpdatedUser(userCode);
                this.save(entity);
            }
        }
    }

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