package com.mvc.recognition.noconfig;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.mvc.model.TAireadModel;
import com.mvc.model.TExtendIdentModel;
import com.mvc.model.aihcxd.ResultMap;
import com.mvc.model.aihcxd.WmInModel;
import com.mvc.netty.dealconstant.ResourceTag;
import com.mvc.service.aihcxd.WmIdenService;
import com.mvc.service.aiiden.UnitType;
import com.mvc.service.copy.TExtendIdentService;
import com.mvc.utils.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.http.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;

/**
 * 免配置服务侧识别
 */
@Component
public class NoConfiguredRecognition {

    private static Logger log = LoggerFactory.getLogger(WmIdenService.class);

    @Resource
    private Environment env;

    @Resource
    private TExtendIdentService extendIdentService;

    /**
     * 处理免配置识别业务
     *
     * @param wmInModel
     * @param aireadModel
     */
    public void handleNoConfigRecognition(WmInModel wmInModel, TAireadModel aireadModel) {
        String url = env.getProperty(ResourceTag.NoConfigServerUrlTag.getTag());
        File file = new File(wmInModel.getImagePath());
        NoConfigInModel inModel = new NoConfigInModel();
        processLastData(inModel, aireadModel.getDeviceSn());
        ResponseEntity<String> response = HttpUtil.postInParam(url, inModel, file);
        if (response == null) {
            return;
        }
        log.info("服务侧免配置识别结果:" + response.getBody());
        if (response.getStatusCode() == HttpStatus.OK) {
            String json = response.getBody();
            ResultMap resultMap = JSON.parseObject(json, new TypeReference<>() {
            });
            if (resultMap != null && resultMap.getFlag().equals("1")) {
                NoConfigOutModel outModel = JSON.toJavaObject((JSONObject) resultMap.getRes(), NoConfigOutModel.class);
                TExtendIdentModel model = new TExtendIdentModel(outModel, aireadModel);
                model.setIdentType(1);
                extendIdentService.saveTExtendIdent(model);
            }
        }
    }

    /**
     * 处理上次数据
     *
     * @param inModel
     * @param deviceSn
     */
    private void processLastData(NoConfigInModel inModel, String deviceSn) {
        TExtendIdentModel model = extendIdentService.getBelievableModelByDeviceSn(deviceSn);
        if (model == null) {
            return;
        }

        inModel.setNumNumbers(model.getDigitNum());

        UnitType unitType = UnitType.getElement(model.getUnitType());
        if (unitType != null) {
            inModel.setNumberUnit(unitType.getValue());
        }
        inModel.setMax_flow_per_hour(model.getMaxFlow().doubleValue());
        inModel.setLastOkValue(model.getIdentValue().doubleValue());//?要不要考虑使用审核值
        inModel.setLastOkValueTime(model.getIdentTime());
        inModel.setServerLastOkRotateAngle(model.getRotateAngle());
    }
}
