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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.jeecg.modules.stm.project.create.entity.StProjectXm;
import org.jeecg.modules.stm.project.create.mapper.StProjectXmMapper;
import org.jeecg.modules.stm.project.create.service.IStProjectXmService;
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.project.entity.StProjectManage;
import org.jeecg.modules.stm.projectbase.project.service.IStProjectManageService;
import org.jeecg.modules.stm.utils.UserUtil;

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

/**
 * @Description: st_project_xm
 * @Author: jeecg-boot
 * @Date:   2023-08-21
 * @Version: V1.0
 */
@Service
public class StProjectXmServiceImpl extends ServiceImpl<StProjectXmMapper, StProjectXm> implements IStProjectXmService {

    @Autowired
    private IStProjectManageService iStProjectManageService;

    @Override
    public void saveInfo(Integer projectCreId, List<Serializable> projectCheck) {
        String userCode = UserUtil.getUserCode();
        for(Serializable id : projectCheck){
            StProjectXm entity = new StProjectXm();
            entity.setCreatedUser(userCode);
            entity.setUpdatedUser(userCode);
            entity.setProjectCreId(projectCreId);
            StProjectManage stProjectManage = iStProjectManageService.getById(id);
            entity.setProjectNo(stProjectManage.getProjectNo());
            entity.setProjectName(stProjectManage.getProjectName());
            this.save(entity);
        }
    }
    @Override
    public void copy(ProjectImportVo projectImportVo) {
        QueryWrapper<StProjectXm> queryClearWrapper = new QueryWrapper<>();
        queryClearWrapper.eq("PROJECT_CRE_ID",projectImportVo.getId());
        this.remove(queryClearWrapper);
        QueryWrapper<StProjectXm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PROJECT_CRE_ID",projectImportVo.getCopyId());
        List<StProjectXm> list = this.list(queryWrapper);
        String userCode = UserUtil.getUserCode();
        if(!CollectionUtils.isEmpty(list)){
            for(StProjectXm 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<StProjectXm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PROJECT_CRE_ID",projectCreId);
        List<StProjectXm> list = this.list(queryWrapper);
        List<String> data = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)) {
            for (StProjectXm entity : list) {
                data.add(entity.getProjectName());
            }
        }
        return data;
    }
}
