/*
 * Author: Zev
 * 生成ServiceImpl
 */
package com.tofly.gpsboot.service.paln.task.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.tofly.api.UserClent;
import com.tofly.common.oauth.util.SecurityUtils;
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.plan.dto.PlanstopDto;
import com.tofly.pojo.plan.dto.ReportAuditDto;
import com.tofly.pojo.plan.vo.PlanstopVo;
import com.tofly.gpsboot.mapper.plan.PlanstopMapper;
import com.tofly.gpsboot.service.paln.task.PlanService;
import com.tofly.gpsboot.service.paln.task.PlanstopService;
import com.tofly.gpsboot.service.report.IReportPlanStopServer;
import com.tofly.pojo.User;
import com.tofly.pojo.plan.Plan;
import com.tofly.pojo.plan.Planstop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 巡检计划暂停信息表：TF_YWPN_PLANSTOP_W
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@Service("planstopService")
public class PlanstopServiceImpl extends ServiceImpl<PlanstopMapper, Planstop> implements PlanstopService {

    @Autowired
    private IReportPlanStopServer iReportPlanStopServer;

    @Autowired
    private PlanService planService;
    @Autowired
    private UserClent userClent;

    @Override
    public List<PlanstopVo> viewConversion(List<Planstop> infoList) {

        List<PlanstopVo> infoVoList = new ArrayList<>();
        infoList.forEach(info ->{
            PlanstopVo baseInfo = JSONObject.parseObject(JSON.toJSONString(info),PlanstopVo.class);
            // 添加审核人名称/上报人名称 转换上报状态/是否查看/审核结果
            Long auditUser = info.getAuditUser();
            Long submitUser = info.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());
                }

            }
            User reslut = userClent.getById(baseInfo.getCreateUser()).getResult();
            if(null!=reslut){
                baseInfo.setCreateUserName(reslut.getRealName());

            }

            baseInfo.setIsSubmit(IsSubmitEnum.getCodeForValue(info.getIsSubmit()));
            baseInfo.setIsread(IsReadEnum.getCodeForValue(info.getIsread()));
            baseInfo.setAuditResult(IsAuditEnum.getCodeForValue(info.getAuditResult()));

            infoVoList.add(baseInfo);
        });
        return infoVoList;
    }


    @Override
    public Boolean saveInfo(PlanstopDto dto) {
        Planstop planStop = JSON.parseObject(JSON.toJSONString(dto), Planstop.class);
        this.save(planStop);
        // 自动上报
        ReportAuditDto reportAuditDto = new ReportAuditDto();
        reportAuditDto.setId(planStop.getId());
        reportAuditDto.setReportId(SecurityUtils.getUserId());
        // 获取当前巡检计划派工人(创建人)
        List<Plan> list = planService.list(new QueryWrapper<Plan>().lambda().eq(Plan::getId, planStop.getPlanId()));
        if (null != list && list.size() > 0) {
            Long createUser = list.get(0).getCreateUser();
            reportAuditDto.setAuditId(createUser);
        }
        iReportPlanStopServer.report(reportAuditDto);
        return true;
    }
}
