/**
 * Copyright (C), 2022-04-13
 * FileName: Consumer
 * Author:   wanglongda
 * Date:     2022/4/13 16:08
 * Description: 消费者
 */
package me.zhengjie.config.rocket;/**
 * Created Name: wanglongda
 * Created Time: 2022/4/13 16:08
 * Description: iot-ykt
 */

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.dm.dmAttendanceRecord.domain.DmAttendanceRecord;
import me.zhengjie.modules.dm.dmAttendanceRecord.repository.DmAttendanceRecordRepository;
import me.zhengjie.modules.dmAccessTraffic.domain.DmAccessTraffic;
import me.zhengjie.modules.dmAccessTraffic.repository.DmAccessTrafficRepository;
import me.zhengjie.modules.dmAccessTraffic.service.mapstruct.DmAccessTrafficMapper;
import me.zhengjie.modules.dmuser.domain.DmUser;
import me.zhengjie.modules.dmuser.service.DmUserService;
import me.zhengjie.modules.dmuser.service.mapstruct.DmUserMapper;
import me.zhengjie.utils.StringUtils;
import me.zhengjie.ykt.domain.DmRoomDevice;
import me.zhengjie.ykt.service.DmRoomDeviceService;
import nl.basjes.shaded.org.springframework.util.ObjectUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <功能简要> <br>
 * <消费者>
 *
 * @Author wanglongda
 * @createTime 2022/4/13 16:08
 * @Version 1.0.0
 */
@Component
@Service
@Slf4j
//@RocketMQMessageListener(consumerGroup = "accessTrafficGroup", topic = "accessTrafficTopic")
@RocketMQMessageListener(consumerGroup = "my-producer-group", topic = "entrance-topic")
public class Consumer implements RocketMQListener<String> {
    @Resource
    private DmAccessTrafficRepository dmAccessTrafficRepository;
    @Resource
    private DmAccessTrafficMapper dmAccessTrafficMapper;
    @Resource
    private DmUserService dmUserService;
    @Resource
    private DmUserMapper dmUserMapper;
    @Resource
    private DmRoomDeviceService dmRoomDeviceService;
    @Resource
    private  DmAttendanceRecordRepository dmAttendanceRecordRepository;

    @Override
    public void onMessage(String message) {
        log.info("RocketMq message : {}",message);
        try {
            JSONObject jsonObject = JSONObject.parseObject(message);
            String userID = jsonObject.getString("userID");
            if (!ObjectUtils.isEmpty(userID)) {
                log.info("jsonObject message : {}", jsonObject.toJSONString());
                DmUser dmUser = dmUserService.findByUserNumber(jsonObject.getString("userID"));//根据人员卡号查询用户
                List<DmRoomDevice> roomDevice = dmRoomDeviceService.findByCode(jsonObject.getString("deviceID"),null);
                // 如果没有查到用户 那就是虚假id
                if (!ObjectUtils.isEmpty(dmUser)){
                    DmAccessTraffic dmAccessTraffic = new DmAccessTraffic();
                    dmAccessTraffic.setRoomDevice(ObjectUtils.isEmpty(roomDevice.get(0))?null:roomDevice.get(0).getCode());
                    dmAccessTraffic.setCardNumber(jsonObject.getString("cardID"));
                    dmAccessTraffic.setUser(dmUser);//用户信息
                    dmAccessTraffic.setAccessTime(jsonObject.getTimestamp("date"));//通行时间
                    dmAccessTraffic.setComeGoOut(jsonObject.getString("eventType")); //事件类型
                    dmAccessTraffic.setIsSuccess(jsonObject.getBoolean("open")); //是否通行成功
                    dmAccessTraffic.setPassageWay(jsonObject.getString("type"));//通行方式 1 刷卡  2二维码  3人脸
                    DmAccessTraffic newDmAccessTraffic = dmAccessTrafficRepository.save(dmAccessTraffic);
                    if (StringUtils.equals(dmUser.getUsertype(), "00")){
                        String erpAttendanceApi = "http://10.21.104.230:8700/attendance-api/device/identify/callback";
                        //组装返回数据
                        JSONObject ertAttendanceJSON = new JSONObject();
                        ertAttendanceJSON.put("deviceKey", newDmAccessTraffic.getRoomDevice());
                        ertAttendanceJSON.put("personId", newDmAccessTraffic.getUser().getUserNumber());
                        ertAttendanceJSON.put("time", newDmAccessTraffic.getAccessTime());
                        ertAttendanceJSON.put("type", "face_0");
                        System.out.println("ertAttendanceJSON:" + ertAttendanceJSON);
                        String res = HttpRequest.get(erpAttendanceApi).body(ertAttendanceJSON.toJSONString()).execute().body();
                        log.info("res:" + res);
                    } else if (StringUtils.equals(dmUser.getUsertype(), "01")) {
                    List<DmAttendanceRecord> dmAttendanceRecords = dmAttendanceRecordRepository.findByUserNumber(dmUser.getId());
                    if (org.springframework.util.ObjectUtils.isEmpty(dmAttendanceRecords) || dmAttendanceRecords.size() < 2) {
                        DmAttendanceRecord dmAttendanceRecord = new DmAttendanceRecord();
                        dmAttendanceRecord.setDmUser(dmUser);
                        dmAttendanceRecord.setCardNumber(dmUser.getCardId());
                        dmAttendanceRecord.setRoomDevice(roomDevice.get(0).getCode());
                        dmAttendanceRecord.setAttendanceTime(jsonObject.getTimestamp("date"));
                        dmAttendanceRecord.setOpen(jsonObject.getBoolean("open"));
                        if (jsonObject.getBoolean("open")) {
                            dmAttendanceRecordRepository.save(dmAttendanceRecord);
                        }
                    } else if (dmAttendanceRecords.size() >= 2) {
                        DmAttendanceRecord dmAttendanceRecord = new DmAttendanceRecord();
                        dmAttendanceRecord.setId(dmAttendanceRecords.get(0).getId());
                        dmAttendanceRecord.setDmUser(dmUser);
                        dmAttendanceRecord.setCardNumber(dmUser.getCardId());
                        dmAttendanceRecord.setRoomDevice(roomDevice.get(0).getCode());
                        dmAttendanceRecord.setAttendanceTime(jsonObject.getTimestamp("date"));
                        dmAttendanceRecord.setOpen(jsonObject.getBoolean("open"));
                        if (jsonObject.getBoolean("open")) {
                            dmAttendanceRecordRepository.save(dmAttendanceRecord);
                        }
                    }
                }
              }
            }
        }catch (Exception e){
            log.info("socketMq监听失败，报错信息：{}",e.getMessage());
        }

    }
}
