/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2022/8/23
 */
package org.springblade.modules.business.equipment.inspection.base.wrapper;


import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.business.equipment.inspection.base.entity.EquipmentInspectionInfoEntity;
import org.springblade.modules.business.equipment.inspection.base.vo.EquipmentInspectionInfoVO;
import org.springblade.modules.business.equipment.inspection.plan.entity.EquipmentInspectionPlanEntity;
import org.springblade.modules.business.equipment.inspection.plan.service.IEquipmentInspectionPlanService;
import org.springblade.modules.business.equipment.inspection.report.dto.EquipmentInspectionReportDTO;
import org.springblade.modules.business.equipment.inspection.report.entity.EquipmentInspectionReportEntity;
import org.springblade.modules.business.equipment.inspection.report.service.IEquipmentInspectionReportService;
import org.springblade.modules.business.equipment.inspection.report.vo.EquipmentInspectionReportVO;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IRoleService;
import org.springblade.modules.system.service.IUserService;

import java.util.List;
import java.util.Objects;

/***
 * Date:2022/8/23
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */

public class EquipmentInspectionWrapper extends BaseEntityWrapper<EquipmentInspectionInfoEntity, EquipmentInspectionInfoVO> {
    public static EquipmentInspectionWrapper build() {
        return new EquipmentInspectionWrapper();
    }

    private static IUserService userService;

    private static IUserService getUserService() {
        if (userService == null) {
            userService = SpringUtil.getBean(IUserService.class);
        }
        return userService;
    }

    private static IEquipmentInspectionReportService reportService;

    private static IEquipmentInspectionReportService getReportService() {
        if (reportService == null) {
            reportService = SpringUtil.getBean(IEquipmentInspectionReportService.class);
        }
        return reportService;
    }

    private static IEquipmentInspectionPlanService planService;

    private static IEquipmentInspectionPlanService getPlanService() {
        if (planService == null) {
            planService = SpringUtil.getBean(IEquipmentInspectionPlanService.class);
        }
        return planService;
    }

    @Override
    public EquipmentInspectionInfoVO entityVO(EquipmentInspectionInfoEntity entity) {
        EquipmentInspectionInfoVO equipmentInspectionInfoVO = Objects.requireNonNull(BeanUtil.copy(entity, EquipmentInspectionInfoVO.class));
        return entityVO(equipmentInspectionInfoVO);
    }

    public EquipmentInspectionInfoVO entityVO(EquipmentInspectionInfoVO entity) {

        if (Func.notNull(entity.getInspectionReportId())) {
            EquipmentInspectionReportEntity reportEntity = getReportService().getById(entity.getInspectionReportId());
            if (Func.notNull(reportEntity)) {
                User user = getUserService().getById(reportEntity.getCreateUser());
                entity.setServicePersonName(user.getRealName());
                entity.setInspectionReportTime(reportEntity.getCreateTime());
            }
        }

        EquipmentInspectionPlanEntity planEntity = getPlanService().getById(entity.getInspectionPlanId());
        entity.setInspectionCompleteTime(planEntity.getInspectionCompleteTime());
        entity.setPlanScheduledType(planEntity.getPlanScheduledType());
        entity.setTimeType(planEntity.getTimeType());

        if (entity.getInspectionStatus() == 2 || entity.getInspectionStatus() == 3) {
            EquipmentInspectionReportDTO dto = new EquipmentInspectionReportDTO();
            dto.setInspectionId(entity.getId());
            List<EquipmentInspectionReportVO> reportEntitys = getReportService().selectList(dto);
            if (Func.notNull(reportEntitys)) {
                entity.setReportList(reportEntitys);
            }
        }

//		String rtuKindDesc = "";
//		if (entity.getIsRain() != null && entity.getIsRain() == 1) {
//			rtuKindDesc = "雨量站";
//		}
//		if (entity.getIsRes() != null && entity.getIsRes() == 1) {
//			if (rtuKindDesc.length() > 0) {
//				rtuKindDesc += "/";
//			}
//			rtuKindDesc += "水位站";
//		} else if (entity.getIsRiver() != null && entity.getIsRiver() == 1) {
//			if (rtuKindDesc.length() > 0) {
//				rtuKindDesc += "/";
//			}
//			rtuKindDesc += "水位站";
//		}
//		if (entity.getIsGround() != null && entity.getIsGround() == 1) {
//			if (rtuKindDesc.length() > 0) {
//				rtuKindDesc += "/";
//			}
//			rtuKindDesc += "墒情站";
//		}
//		if (entity.getIsVideo() != null && entity.getIsVideo() == 1) {
//			if (rtuKindDesc.length() > 0) {
//				rtuKindDesc += "/";
//			}
//			rtuKindDesc += "视频站";
//		}
//		entity.setRtuKindDesc(rtuKindDesc);
        return entity;
    }
}
