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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.stm.common.service.IStCommonService;
import org.stm.common.vo.DutyVo;
import org.stm.project.create.entity.StProjectOrg;
import org.stm.project.create.entity.StProjectUser;
import org.stm.project.create.mapper.StProjectUserMapper;
import org.stm.project.create.service.IStProjectUserService;
import org.springframework.stereotype.Service;

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

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

/**
 * @Description: st_project_user
 * @Author: jeecg-boot
 * @Date:   2023-08-21
 * @Version: V1.0
 */
@Service
public class StProjectUserServiceImpl extends ServiceImpl<StProjectUserMapper, StProjectUser> implements IStProjectUserService {

    @Autowired
    private ISysUserService iSysUserService;

    @Override
    public void saveInfo(Integer projectCreId, List<String> userCheck) {
        String userCode = UserUtil.getUserCode();
        for(String uCode : userCheck){
            StProjectUser stProjectUser = new StProjectUser();
            stProjectUser.setCreatedUser(userCode);
            stProjectUser.setUpdatedUser(userCode);
            stProjectUser.setProjectCreId(projectCreId);
            stProjectUser.setUserNo(uCode);
            SysUser sysUser = iSysUserService.getUserByName(uCode);
            if(sysUser!=null){
                stProjectUser.setUserName(sysUser.getRealname());
            }else{
                stProjectUser.setUserName("-");
            }
            this.save(stProjectUser);
        }
    }

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