package org.jeecg.modules.stm.plan.trance.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.stm.common.service.IStCommonService;

import org.jeecg.modules.stm.common.vo.DutyVo;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.jeecg.modules.stm.plan.trance.entity.StPlanTrance;
import org.jeecg.modules.stm.plan.trance.mapper.StPlanTranceMapper;
import org.jeecg.modules.stm.plan.trance.service.IStPlanTranceService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.stm.plan.trance.vo.StPlanTranceVo;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: st_plan_trance
 * @Author: jeecg-boot
 * @Date:   2023-07-16
 * @Version: V1.0
 */
@Service
public class StPlanTranceServiceImpl extends ServiceImpl<StPlanTranceMapper, StPlanTrance> implements IStPlanTranceService {

    @Autowired
    private ISysDictService iSysDictService;

    @Autowired
    private ISysUserService iSysUserService;
    @Override
    public <E extends IPage<StPlanTrance>> E page(E page, Wrapper<StPlanTrance> queryWrapper) {
        IPage<StPlanTrance> pageList = super.page(page, queryWrapper);
        List<StPlanTrance> voList = new ArrayList<>();
        List<StPlanTrance> list = pageList.getRecords();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        for (StPlanTrance stPlanTrance : list) {
            StPlanTranceVo stPlanTranceVo = new StPlanTranceVo();
            BeanUtils.copyProperties(stPlanTrance, stPlanTranceVo);
            // 翻译和增强处理
            DutyVo dutyVo = SpringContextUtils.getBean(IStCommonService.class).queryDutyByCode(stPlanTrance.getHeadCode());
            if (dutyVo != null) {
                stPlanTranceVo.setHeadName(dutyVo.initDutyNames());
                stPlanTranceVo.setExecDepName(dutyVo.getDepartName());
            } else {
                stPlanTranceVo.setHeadName("-");
            }
            DutyVo dutyVo1 = SpringContextUtils.getBean(IStCommonService.class).queryDutyByCode(stPlanTrance.getApproveUser());
            if(dutyVo!=null){
                stPlanTranceVo.setApproveUserName(dutyVo.initDutyNames());
                stPlanTranceVo.setApproveDepName(dutyVo.getDepartName());
            }else{
                stPlanTranceVo.setApproveUserName("-");
            }
            String name = iSysDictService.queryDictTextByKey("projecttype", stPlanTrance.getProjectType());
            if (StringUtils.isNotEmpty(name)) {
                stPlanTranceVo.setProjectTypeName(name);
            } else {
                stPlanTranceVo.setProjectTypeName("-");
            }
            SysUser sysUser = iSysUserService.getUserByName(stPlanTranceVo.getUpdatedUser());
            if(sysUser!=null) {
                stPlanTranceVo.setUpdateUserName(sysUser.getRealname());
            }else {
                stPlanTranceVo.setUpdateUserName("-");
            }
            voList.add(stPlanTranceVo);
        }
        pageList.setRecords(voList);
        return (E) pageList;
    }
}
