package com.bonus.core.device.door.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.core.common.constant.LeMqConstant;
import com.bonus.core.common.utils.SysUtil;
import com.bonus.core.device.door.enums.EditUserToDeviceEnum;
import com.bonus.core.device.door.enums.TriggerTypeEnum;
import com.bonus.core.device.door.mapper.DcCustDoorPwdMapper;
import com.bonus.core.device.door.model.DcCustDoorPwd;
import com.bonus.core.device.door.service.DcAutoRuleDetailService;
import com.bonus.core.device.door.service.DcCustDoorPwdService;
import com.bonus.core.order.mq.MqUtil;
import com.bonus.utils.id.Id;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

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

@Service
public class DcCustDoorPwdServiceImpl extends ServiceImpl<DcCustDoorPwdMapper, DcCustDoorPwd> implements DcCustDoorPwdService {
   @Resource
   @Lazy
   private DcAutoRuleDetailService dcAutoRuleDetailService;


   @Override
   public void checkUserToDevice(Long custId, TriggerTypeEnum triggerTypeEnum, EditUserToDeviceEnum editUserToDeviceEnum) {
      Boolean push = this.dcAutoRuleDetailService.checkUser(custId, triggerTypeEnum);
      if (Boolean.TRUE.equals(push)) {
         this.senEditUserMq(Collections.singletonList(custId), editUserToDeviceEnum.getKey());
      }
   }

   public void senEditUserMq(List<Long> custIds, String operate) {
      this.senEditUserMq(custIds, new ArrayList(), operate);
   }

   public void senEditUserMq(List<Long> custIds, List<Long> deviceIds, String operate) {
      JSONObject params = new JSONObject();
      params.put("custId", custIds);
      params.put("deviceIds", Optional.ofNullable(deviceIds).orElse(new ArrayList()));
      params.put("operate", operate);
      params.put("messageId", Id.next());
      params.put("crBy", SecurityUtils.getUsername());
      params.put("operator", SecurityUtils.getUserId());
      params.put("filePrefix", SysUtil.getPrefix());
      params.put("prefixBucket", SysUtil.getBucket());
      MqUtil.send(params, LeMqConstant.Topic.DEVICE_PUSH_USER_DATA);
   }

}
