/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.gpsboot.service.maintain.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.tofly.api.UserClent;
import com.tofly.gpsboot.common.system.IsAuditEnum;
import com.tofly.gpsboot.common.system.IsReadEnum;
import com.tofly.gpsboot.common.system.IsSubmitEnum;
import com.tofly.pojo.maintain.vo.RepairrelayVo;
import com.tofly.gpsboot.mapper.maintain.RepairrelayMapper;
import com.tofly.gpsboot.service.maintain.RepairrelayService;
import com.tofly.pojo.User;
import com.tofly.pojo.maintain.Repairrelay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * 抢维修工单转发记录信息表
 *
 * @author admin
 * @date Tue Apr 06 00:00:00 CST 2021
 */
@Service("repairrelayService")
public class RepairrelayServiceImpl extends ServiceImpl<RepairrelayMapper, Repairrelay> implements RepairrelayService {
    @Autowired
    private UserClent userClent;

    @Override
    public List<RepairrelayVo> viewConversion(List<Repairrelay> infoList) {
        List<RepairrelayVo> infoVoList = new ArrayList<>();
        infoList.forEach(dto -> {
            RepairrelayVo baseInfo = JSON.parseObject(JSON.toJSONString(dto), RepairrelayVo.class);
            // 添加转发人/审核人名称/上报人名称 转换上报状态/是否查看/审核结果
            if(null!=dto.getRelayUser()){
                User result = userClent.getById(dto.getRelayUser()).getResult();
                if(null!=result){
                    baseInfo.setRelayUserName(result.getRealName());
                }
            }
            Long auditUser = dto.getAuditUser();
            Long submitUser = dto.getSubmitUser();
            if (null!= auditUser && 0!=auditUser) {
                User userAudit = userClent.getById(auditUser).getResult();
                if(null!=userAudit){
                    baseInfo.setAuditUserName(userAudit.getRealName());
                }

            }
            if (null!= submitUser && 0!=submitUser) {
                User userSubmit = userClent.getById(submitUser).getResult();
                if(null!=userSubmit){
                    baseInfo.setSubmitUserName(userSubmit.getRealName());
                }

            }

            baseInfo.setIssubmit(IsSubmitEnum.getCodeForValue(dto.getIssubmit()));
            baseInfo.setIsread(IsReadEnum.getCodeForValue(dto.getIsread()));
            baseInfo.setAuditResult(IsAuditEnum.getCodeForValue(dto.getAuditResult()));
            User userCu = userClent.getById(dto.getCreateUser()).getResult();
            if(null!=userCu){
                baseInfo.setCreateUserName(userCu.getRealName());
            }

            infoVoList.add(baseInfo);
        });
        infoVoList.sort(Comparator.comparing(RepairrelayVo::getCreateTime).reversed());
        return infoVoList;
    }
}
