package com.yeejoin.amos.api.openapi.face.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.yeejoin.amos.api.openapi.face.model.ElevatorAlarmModel;
import com.yeejoin.amos.api.openapi.face.model.ElevatorAlarmTypeModel;
import com.yeejoin.amos.api.openapi.face.model.ElevatorModel;
import com.yeejoin.amos.api.openapi.face.model.EquipmentCategoryModel;
import com.yeejoin.amos.api.openapi.face.orm.dao.ElevatorAlarmMapper;
import com.yeejoin.amos.api.openapi.face.orm.entity.ElevatorAlarm;

import org.typroject.tyboot.core.foundation.utils.Bean;
import org.typroject.tyboot.core.foundation.utils.ValidationUtil;
import org.typroject.tyboot.core.rdbms.service.BaseService;
import org.typroject.tyboot.core.restful.exception.instance.BadRequest;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 电梯故障表 服务类
 * </p>
 *
 * @author 子杨
 * @since 2021-09-27
 */
@Component
public class ElevatorAlarmService extends
		BaseService<ElevatorAlarmModel, ElevatorAlarm, ElevatorAlarmMapper>
{

	@Autowired
	private ElevatorService elevatorService;
	
	@Autowired
	private ElevatorAlarmTypeService  elevatorAlarmTypeService;
	
	@Autowired
	private EquipmentCategoryService equipmentCategoryService;
	
	public Map<String, String> equipmentCategoryMap = null;
	
	public Map<String, String> alarmTypeMap = null;
	
	/**
	 * 
	 * <pre>
	 * 批量保存电梯故障信息
	 * </pre>
	 *
	 * @param models
	 * @return
	 * @throws Exception
	 */
	public String multCreateElevatorAlarm(List<ElevatorAlarmModel> models) throws Exception
	{

		if (ValidationUtil.isEmpty(models))
			throw new BadRequest("参数校验失败.");

		for (ElevatorAlarmModel elevatorAlarmModel : models)
		{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			elevatorAlarmModel.setStartDate(
					elevatorAlarmModel.getStartTime()!=null? sdf.parse(elevatorAlarmModel.getStartTime()): null);
			elevatorAlarmModel.setDealDate(
					elevatorAlarmModel.getDealTime()!=null? sdf.parse(elevatorAlarmModel.getDealTime()): null);
			
			ElevatorAlarmModel oldModel = queryModelByCodeAndFaultId(elevatorAlarmModel.getRegisterCode(), elevatorAlarmModel.getFaultId());
			//如果存在该故障编码则进行更新
			if (ValidationUtil.isEmpty(oldModel))
			{
				ElevatorModel elevatorModel = elevatorService.queryByRegisterCode(elevatorAlarmModel.getRegisterCode());
				//只保存现有维护电梯的告警信息
				if (!ValidationUtil.isEmpty(elevatorModel))
				{
					elevatorAlarmModel.setRescueCode(elevatorModel.getRescueCode());
					elevatorAlarmModel.setProvince(elevatorModel.getProvince());
					elevatorAlarmModel.setCity(elevatorModel.getCity());
					elevatorAlarmModel.setDistrict(elevatorModel.getDistrict());
					elevatorAlarmModel.setAddress(elevatorModel.getAddress());
					//填充电梯分类
					if (ValidationUtil.isEmpty(equipmentCategoryMap))
					{
						equipmentCategoryMap = new HashMap<String, String>();
						List<EquipmentCategoryModel> allEquipmentCategory = equipmentCategoryService.queryAllForlist();
						for (EquipmentCategoryModel equipmentCategoryModel : allEquipmentCategory)
						{
							equipmentCategoryMap.put(equipmentCategoryModel.getCode(), equipmentCategoryModel.getName());
						}
					}
					String category = equipmentCategoryMap.get(elevatorModel.getCategory());
					elevatorAlarmModel.setDeviceCategory(category!=null? category:elevatorModel.getCategory());
					//填充告警类型
					if (ValidationUtil.isEmpty(alarmTypeMap))
					{
						alarmTypeMap = new HashMap<String, String>();
						List<ElevatorAlarmTypeModel> allAlarmType = elevatorAlarmTypeService.queryAllForlist();
						for (ElevatorAlarmTypeModel elevatorAlarmTypeModel : allAlarmType)
						{
							alarmTypeMap.put(String.valueOf(elevatorAlarmTypeModel.getAlarmCode()), elevatorAlarmTypeModel.getAlarmName());
						}
					}
					String alarmType = alarmTypeMap.get(elevatorAlarmModel.getType());
					elevatorAlarmModel.setTypeName(alarmType!= null? alarmType: elevatorAlarmModel.getType());
					
					this.createWithModel(elevatorAlarmModel);
				}
			}else 
			{
				Bean.copyExistPropertis(elevatorAlarmModel, oldModel);
	            this.updateWithModel(oldModel);
			}
		}

		return "OK";
	}

	/**
	 * 
	 * <pre>
	 * 根据电梯注册编码和故障编码查询电梯故障信息
	 * </pre>
	 *
	 * @param registerCode
	 * @param faultId
	 */
	private ElevatorAlarmModel queryModelByCodeAndFaultId(String registerCode, String faultId)
	{
		return this.queryModelByParams(registerCode, faultId);
	}
	
}
