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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.jeecg.modules.stm.project.create.entity.StProjectAct;
import org.jeecg.modules.stm.project.create.mapper.StProjectActMapper;
import org.jeecg.modules.stm.project.create.service.IStProjectActService;
import org.springframework.stereotype.Service;

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

import java.util.List;

/**
 * @Description: st_project_act
 * @Author: jeecg-boot
 * @Date:   2023-08-21
 * @Version: V1.0
 */
@Service
public class StProjectActServiceImpl extends ServiceImpl<StProjectActMapper, StProjectAct> implements IStProjectActService {

    @Autowired
    private IStProjectCheckService iStProjectCheckService;
    @Override
    public void saveInfo(Integer projectCreId, String collectionNo) {
        QueryWrapper<StProjectAct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PROJECT_CRE_ID",projectCreId);
        StProjectAct stProjectAct = this.getOne(queryWrapper);
        String userCode = UserUtil.getUserCode();
        if(stProjectAct!=null) {
            if (!StringUtils.equals(stProjectAct.getCollectionNo(), collectionNo)) {
                stProjectAct.setCollectionNo(collectionNo);
                iStProjectCheckService.saveInfo(projectCreId,collectionNo);
            }
            stProjectAct.setUpdatedUser(userCode);
            this.updateById(stProjectAct);
        }else{
            stProjectAct = new StProjectAct();
            stProjectAct.setProjectCreId(projectCreId);
            stProjectAct.setCollectionNo(collectionNo);
            stProjectAct.setCreatedUser(userCode);
            stProjectAct.setUpdatedUser(userCode);
            this.save(stProjectAct);
            iStProjectCheckService.saveInfo(projectCreId,collectionNo);
        }
    }

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