package org.springblade.openinterface.temperature;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.community.agency.entity.Agency;
import org.springblade.community.agency.service.IAgencyService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.smartapplication.temperature.entity.TemperatureDevice;
import org.springblade.smartapplication.temperature.service.ITemperatureDeviceService;
import org.springblade.smartapplication.temperaturerecord.service.ITemperatureRecordService;
import org.springblade.smartapplication.temperaturerecord.vo.TemperatureRecordVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zwx
 * @create 2020/12/16 0:34
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("openinterface/temperature")
public class CameraController  extends BladeController {

	private ITemperatureRecordService temperatureRecordService;

	private ITemperatureDeviceService temperatureDeviceService;

	private IAgencyService agencyService;

    /**
     * 功能描述: 红外热成像人脸、测温推送
     * @param recordVO 请求参数
     * @return ResultBean<Boolean>
     *
     */
	@PostMapping(value = "/infrared/data/notice")
    public R infraredDataNotice(@RequestBody TemperatureRecordVO recordVO){

		//目前还没能拿到抓拍的设备sn号，无法获取关联的园区或分区，所以先写死园区的orgPosition
		recordVO.setOrgPosition("/640000/640100/640121/1333195565676253185/1338406098847293442");
		recordVO.setTenantId("804981");
		recordVO.setMatchValue(recordVO.getMatch());
		//体温异常数据判断及处理
		recordVO.setIsException(Double.parseDouble(recordVO.getTemperature()) > 37.3 ? 1 : 0);

		String productId = recordVO.getProductId();
		String deviceId = recordVO.getDeviceId();

		// 若根据产品编号和设备编号，能找到对应的社区或园区，就用其替换掉默认的orgPosition和tenantId
		if (productId != null && deviceId != null){
			TemperatureDevice device = new TemperatureDevice();
			device.setProductId(productId);
			device.setDeviceId(deviceId);
			TemperatureDevice one = temperatureDeviceService.getOne(Condition.getQueryWrapper(device));
			if (one != null){
				Agency agency = agencyService.getById(one.getAgencyId());
				recordVO.setOrgPosition(agency.getOrgPosition());
				recordVO.setTenantId(agency.getTenantId());
			}

		}

		return R.status(temperatureRecordService.saveOrUpdate(recordVO));
    }

}
