/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.gpsboot.service.paln.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.AuditSateEnum;
import com.tofly.gpsboot.common.system.AuditTypeEnum;
import com.tofly.pojo.plan.vo.CheckhistoryVo;
import com.tofly.gpsboot.mapper.plan.CheckhistoryMapper;
import com.tofly.gpsboot.service.paln.CheckhistoryService;
import com.tofly.pojo.User;
import com.tofly.pojo.plan.Checkhistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 审核记录历史表
 *
 * @author admin
 * @date Tue Mar 30 00:00:00 CST 2021
 */
@Service("checkhistoryService")
public class CheckhistoryServiceImpl extends ServiceImpl<CheckhistoryMapper, Checkhistory> implements CheckhistoryService {

    @Autowired
    private UserClent userClent;

    @Override
    public List<CheckhistoryVo> viewConversion(List<Checkhistory> infoList) {
        List<CheckhistoryVo> infoVoList = new ArrayList<>();
        infoList.forEach(info -> {
            CheckhistoryVo baseInfo = JSON.parseObject(JSON.toJSONString(info), CheckhistoryVo.class);
            // 转换审核类型/审核意见
            baseInfo.setType(AuditTypeEnum.getCodeForValue(info.getType()));
            baseInfo.setAuditResult(AuditSateEnum.getCodeForValue(info.getAuditResult()));
            Long auditUser = info.getAuditUser();
            if (null != auditUser && 0 != auditUser) {
                User userAudit = userClent.getById(auditUser).getResult();
                if(null!=userAudit){
                    baseInfo.setAuditUserName(userAudit.getRealName());
                }

            }
            infoVoList.add(baseInfo);
        });
        return infoVoList.stream().sorted(Comparator.comparing(CheckhistoryVo::getAuditTime).reversed()).collect(Collectors.toList());
    }

    @Override
    public boolean recordHis(String type, Long checkId, String auditResult, String auditNotes, Long auditUser, Date auditTime) {
        Checkhistory info = new Checkhistory();
        info.setType(type);
        info.setCheckId(checkId);
        info.setAuditResult(auditResult);
        info.setAuditNotes(auditNotes);
        info.setAuditUser(auditUser);
        info.setAuditTime(auditTime);
        return this.save(info);
    }
}
