package com.yuchen.saas.park.service.pull.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yuchen.saas.message.api.feign.ws.WebSocketFeignServer;
import com.yuchen.saas.message.api.vo.NoticeVo;
import com.yuchen.saas.park.api.entity.device.PmgBatSmartEquipment;
import com.yuchen.saas.park.api.entity.park.PmgBatParkPeople;
import com.yuchen.saas.park.api.entity.park.PmgBatParkVisitor;
import com.yuchen.saas.park.api.entity.park.PmgLogPeopleCount;
import com.yuchen.saas.park.api.vo.fault.DeviceProperty;
import com.yuchen.saas.park.service.service.IPeopleCountService;
import com.yuchen.saas.park.service.service.PmgBatParkPeopleService;
import com.yuchen.saas.park.service.service.PmgBatParkVisitorService;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.tool.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 吕海泉
 * @create 2020/12/21 19:45
 */
@Slf4j
@Service
public class PropertyHandlerImpl implements PropertyHandler {

    @Autowired
    private WebSocketFeignServer pushService;

    @Autowired
    private PmgBatParkPeopleService pmgBatParkPeopleService;

    @Autowired
    private IPeopleCountService iPeopleCountService;

    @Autowired
    private PmgBatParkVisitorService pmgBatParkVisitorService;

    @Override
    public void DeviceDataHandler() {
    }

    @Override
    public void FaceDatahandler(PmgBatSmartEquipment pmgBatSmartEquipment, DeviceProperty deviceProperty) {
        if(deviceProperty.getIdentify().equals("face_comparison_information")){
            PmgLogPeopleCount pmgLogPeopleCount = new PmgLogPeopleCount();
            JSONObject jsonObject = JSONObject.parseObject((String) deviceProperty.getValue());
            Float  temperature = jsonObject.getFloat("temperature");
            log.info(String.valueOf(temperature));
            String facePhoto_src = jsonObject.getString("facePhoto_src");
            String result = jsonObject.getString("result");
            String event_time = jsonObject.getString("event_time");
            String user_id = jsonObject.getString("user_id");
            int score = jsonObject.getIntValue("score");
            String l = null;
            if(StringUtil.isNotBlank(user_id)) {
               l  = user_id.substring(0,user_id.length()-1);
            }

            if(StringUtil.isBlank(user_id)) {
                JSONObject jsonObject1  = new JSONObject();
                jsonObject1.put("temperature",temperature);
                jsonObject1.put("facePhoto_src",facePhoto_src);
                jsonObject1.put("timeStamp",event_time);
                jsonObject1.put("score",score);
                jsonObject1.put("peopleId",user_id);
                jsonObject1.put("sex","");
                jsonObject1.put("building","陌生人");
                jsonObject1.put("buildingName","");
                jsonObject1.put("imgSrc","");
                jsonObject1.put("company","");
                jsonObject1.put("type","陌生人");
                jsonObject1.put("name","");
                jsonObject1.put("equipmentName", pmgBatSmartEquipment.getName());
                jsonObject1.put("setPosition", pmgBatSmartEquipment.getSetPosition());
                // 20210729修改
                jsonObject1.put("buildingId", pmgBatSmartEquipment.getBuildingId());
                jsonObject1.put("floorId", pmgBatSmartEquipment.getFloorId());
                jsonObject1.put("roomId", pmgBatSmartEquipment.getRoomId());
                pushService.postFace(NoticeVo.getByParkIdAndData(String.valueOf(pmgBatSmartEquipment.getParkId()), JSON.toJSONString(jsonObject1)));

                pmgLogPeopleCount.setTemperature(temperature);
//                pmgLogPeopleCount.setImgsrc(facePhoto_src);
                pmgLogPeopleCount.setScore(score);
                pmgLogPeopleCount.setName("陌生人");
                pmgLogPeopleCount.setType("陌生人");
                pmgLogPeopleCount.setPersonId("");
                pmgLogPeopleCount.setTimeStamp(Long.valueOf(event_time));
                pmgLogPeopleCount.setEquipmentName(pmgBatSmartEquipment.getName());
                pmgLogPeopleCount.setProductKey(pmgBatSmartEquipment.getProductKey());
                pmgLogPeopleCount.setDeviceName(pmgBatSmartEquipment.getDeviceName());
                pmgLogPeopleCount.setParkId(pmgBatSmartEquipment.getParkId());
                pmgLogPeopleCount.setSetPosition(pmgBatSmartEquipment.getSetPosition());
                // 20210729修改
                pmgLogPeopleCount.setBuildingId(pmgBatSmartEquipment.getBuildingId());
                pmgLogPeopleCount.setFloorId(pmgBatSmartEquipment.getFloorId());
                pmgLogPeopleCount.setRoomId(pmgBatSmartEquipment.getRoomId());
                pmgLogPeopleCount.setCreateDept(pmgBatSmartEquipment.getCreateDept());

                iPeopleCountService.save(pmgLogPeopleCount);
            }

            if(l.equals("1")) {
               PmgBatParkPeople pmgBatParkPeople =  pmgBatParkPeopleService.getById(user_id);
               if(pmgBatParkPeople != null) {

                     // 发送到stomp
                   JSONObject jsonObject1  = new JSONObject();
                   jsonObject1.put("name",pmgBatParkPeople.getName());
                   jsonObject1.put("temperature",temperature);
                   jsonObject1.put("facePhoto_src",facePhoto_src);
                   jsonObject1.put("timeStamp",event_time);
                   jsonObject1.put("score",score);
                   jsonObject1.put("peopleId",user_id);
                   jsonObject1.put("sex",pmgBatParkPeople.getSex());
                   // 20210729修改
                   jsonObject1.put("buildingId",pmgBatParkPeople.getBuildingId());
                   jsonObject1.put("floorId",pmgBatParkPeople.getFloorId());
                   jsonObject1.put("roomId",pmgBatParkPeople.getRoomId());

                   jsonObject1.put("imgSrc",pmgBatParkPeople.getImgsrc());
                   jsonObject1.put("company",pmgBatParkPeople.getCompany());
                   jsonObject1.put("type",pmgBatParkPeople.getType());
                   jsonObject1.put("equipmentName", pmgBatSmartEquipment.getName());
                   jsonObject1.put("parkId", pmgBatSmartEquipment.getParkId());
                   jsonObject1.put("setPosition", pmgBatSmartEquipment.getSetPosition());

                   pushService.postFace(NoticeVo.getByParkIdAndData(String.valueOf(pmgBatSmartEquipment.getParkId()), JSON.toJSONString(jsonObject1)));

                     pmgLogPeopleCount.setTemperature(temperature);
        //                     pmgLogPeopleCount.setImgsrc(facePhoto_src);
                     pmgLogPeopleCount.setScore(score);
                     pmgLogPeopleCount.setName(pmgBatParkPeople.getName());
                     pmgLogPeopleCount.setSex(pmgBatParkPeople.getSex());
                     pmgLogPeopleCount.setType(pmgBatParkPeople.getType());
                     // 20210729修改
                     pmgLogPeopleCount.setBuildingId(pmgBatParkPeople.getBuildingId());
                     pmgLogPeopleCount.setFloorId(pmgBatParkPeople.getFloorId());
                     pmgLogPeopleCount.setRoomId(pmgBatParkPeople.getRoomId());

                     pmgLogPeopleCount.setPersonId(user_id);
                     pmgLogPeopleCount.setTimeStamp(Long.valueOf(event_time));
                   pmgLogPeopleCount.setEquipmentName(pmgBatSmartEquipment.getName());
                   pmgLogPeopleCount.setProductKey(pmgBatSmartEquipment.getProductKey());
                   pmgLogPeopleCount.setDeviceName(pmgBatSmartEquipment.getDeviceName());
                   pmgLogPeopleCount.setParkId(pmgBatSmartEquipment.getParkId());
                   pmgLogPeopleCount.setSetPosition(pmgBatSmartEquipment.getSetPosition());
                   pmgLogPeopleCount.setCreateDept(pmgBatSmartEquipment.getCreateDept());
                   iPeopleCountService.save(pmgLogPeopleCount);
               }
            }
            if(l.equals("2")) {

                PmgBatParkVisitor pmgBatParkVisitor = pmgBatParkVisitorService.getById(Long.valueOf(user_id));
                if(pmgBatParkVisitor != null) {

                    // 发送到stomp
                    JSONObject jsonObject1  = new JSONObject();
                    jsonObject1.put("name",pmgBatParkVisitor.getName());
                    jsonObject1.put("temperature",temperature);
                    jsonObject1.put("facePhoto_src",facePhoto_src);
                    jsonObject1.put("timeStamp",event_time);
                    jsonObject1.put("score",score);
                    jsonObject1.put("peopleId",user_id);
                    jsonObject1.put("sex","1");
                    // 20210729修改
                    jsonObject1.put("buildingId",pmgBatParkVisitor.getBuildingId());
                    jsonObject1.put("floorId",pmgBatParkVisitor.getFloorId());
//                    jsonObject1.put("roomId",pmgBatParkVisitor.getRoomId());

                    jsonObject1.put("imgSrc",pmgBatParkVisitor.getFaceImg());
//                    jsonObject1.put("company",pmgBatParkVisitor.getCompany());
//                    jsonObject1.put("type",pmgBatParkVisitor.getType());
                    jsonObject1.put("equipmentName", pmgBatSmartEquipment.getName());
                    jsonObject1.put("parkId", pmgBatSmartEquipment.getParkId());
                    jsonObject1.put("setPosition", pmgBatSmartEquipment.getSetPosition());
                    pushService.postFace(NoticeVo.getByParkIdAndData(String.valueOf(pmgBatSmartEquipment.getParkId()), JSON.toJSONString(jsonObject1)));

                    pmgLogPeopleCount.setTemperature(temperature);
//                    pmgLogPeopleCount.setImgsrc(facePhoto_src);
                    pmgLogPeopleCount.setScore(score);
                    pmgLogPeopleCount.setName(pmgBatParkVisitor.getName());
                    pmgLogPeopleCount.setSex("1");
//                    pmgLogPeopleCount.setCode(pmgBatParkVisitor.getCode());
//                    pmgLogPeopleCount.setType(pmgBatParkVisitor.getType());
                    // 20210729修改
                    pmgLogPeopleCount.setBuildingId(pmgBatParkVisitor.getBuildingId());
                    pmgLogPeopleCount.setFloorId(pmgBatParkVisitor.getFloorId());
//                    pmgLogPeopleCount.setRoomId(pmgBatParkVisitor.getRoomId());

                    pmgLogPeopleCount.setPersonId(user_id);
                    pmgLogPeopleCount.setTimeStamp(Long.valueOf(event_time));
                    pmgLogPeopleCount.setEquipmentName(pmgBatSmartEquipment.getName());
                    pmgLogPeopleCount.setProductKey(pmgBatSmartEquipment.getProductKey());
                    pmgLogPeopleCount.setDeviceName(pmgBatSmartEquipment.getDeviceName());
                    pmgLogPeopleCount.setParkId(pmgBatSmartEquipment.getParkId());
                    pmgLogPeopleCount.setSetPosition(pmgBatSmartEquipment.getSetPosition());
                    pmgLogPeopleCount.setCreateDept(pmgBatSmartEquipment.getCreateDept());
                    iPeopleCountService.save(pmgLogPeopleCount);
                }
//                if(pmgBatParkVisitor.getStartTime()<event_time && pmgBatParkVisitor.getEndTime()>event_time) {
//
//                }
            }


        }
    }



}
