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.StProjectOutsourcer;
import org.jeecg.modules.stm.project.create.mapper.StProjectOutsourcerMapper;
import org.jeecg.modules.stm.project.create.service.IStProjectOutsourcerService;
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.outsourcer.entity.StOutsourcerManage;
import org.jeecg.modules.stm.projectbase.outsourcer.service.IStOutsourcerManageService;
import org.jeecg.modules.stm.utils.UserUtil;

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

/**
 * @Description: st_project_outsourcer
 * @Author: jeecg-boot
 * @Date:   2023-08-21
 * @Version: V1.0
 */
@Service
public class StProjectOutsourcerServiceImpl extends ServiceImpl<StProjectOutsourcerMapper, StProjectOutsourcer> implements IStProjectOutsourcerService {

    @Autowired
    private IStOutsourcerManageService iStOutsourcerManageService;

    @Override
    public void saveInfo(Integer projectCreId, List<Serializable> outerCheck) {
        String userCode = UserUtil.getUserCode();
        for(Serializable id : outerCheck){
            StProjectOutsourcer entity = new StProjectOutsourcer();
            entity.setCreatedUser(userCode);
            entity.setUpdatedUser(userCode);
            entity.setProjectCreId(projectCreId);
            StOutsourcerManage manage = iStOutsourcerManageService.getById(id);
            entity.setOutsourcerNo(manage.getOutsourcerNo());
            entity.setOutsourcerName(manage.getOutsourcerName());
            this.save(entity);
        }
    }

    @Override
    public void copy(ProjectImportVo projectImportVo) {
        QueryWrapper<StProjectOutsourcer> queryClearWrapper = new QueryWrapper<>();
        queryClearWrapper.eq("PROJECT_CRE_ID",projectImportVo.getId());
        this.remove(queryClearWrapper);
        QueryWrapper<StProjectOutsourcer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PROJECT_CRE_ID",projectImportVo.getCopyId());
        List<StProjectOutsourcer> list = this.list(queryWrapper);
        String userCode = UserUtil.getUserCode();
        if(!CollectionUtils.isEmpty(list)){
            for(StProjectOutsourcer 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<StProjectOutsourcer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PROJECT_CRE_ID",projectCreId);
        List<StProjectOutsourcer> list = this.list(queryWrapper);
        List<String> data = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)) {
            for (StProjectOutsourcer entity : list) {
                data.add(entity.getOutsourcerName());
            }
        }
        return data;
    }
}
