package com.bonus.canteen.core.supply.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.canteen.core.common.utils.MqUtil;
import com.bonus.canteen.core.supply.constant.DrpNoticeStyleEnum;
import com.bonus.canteen.core.supply.constant.DrpPurNoticeStatusEnum;
import com.bonus.canteen.core.supply.constant.NoticeTemplateTypeEnum;
import com.bonus.canteen.core.supply.domain.DrpPurNotice;
import com.bonus.canteen.core.supply.domain.DrpSupplier;
import com.bonus.canteen.core.supply.dto.DrpNoticeGeneralMqDTO;
import com.bonus.canteen.core.supply.mapper.DrpPurNoticeMapper;
import com.bonus.canteen.core.supply.mapper.DrpSupplierMapper;
import com.bonus.canteen.core.supply.service.DrpPurNoticeService;
import com.bonus.common.houqin.constant.DelFlagEnum;
import com.bonus.common.houqin.mq.constant.LeMqConstant;
import com.bonus.common.houqin.utils.SM4EncryptUtils;
import com.bonus.common.houqin.utils.id.Id;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class DrpPurNoticeServiceImpl extends ServiceImpl<DrpPurNoticeMapper, DrpPurNotice> implements DrpPurNoticeService {
   private static final Logger log = LoggerFactory.getLogger(DrpPurNoticeServiceImpl.class);
   @Autowired
   private DrpPurNoticeMapper drpPurNoticeMapper;
   @Autowired
   private DrpSupplierMapper drpSupplierMapper;



   public void sendSmsNoticeGeneral(Long supplierId, String phone, String mainId, String key) {
      DrpNoticeGeneralMqDTO dto = new DrpNoticeGeneralMqDTO();
      Long noticeId = Id.next();
      if (supplierId != null) {
         DrpSupplier drpSupplier = (DrpSupplier)this.drpSupplierMapper.selectOne(Wrappers.lambdaQuery(DrpSupplier.class).eq(DrpSupplier::getSupplierId, supplierId).eq(DrpSupplier::getDelFlag, DelFlagEnum.DEL_FALSE.key()).select(DrpSupplier::getNoticeStyle, DrpSupplier::getIfReceiveNotice, DrpSupplier::getEmail, DrpSupplier::getLinkmanNumber, DrpSupplier::getSupplierId, DrpSupplier::getSupplierUserId, DrpSupplier::getSupplierName));
         dto.setSupplierName(drpSupplier.getSupplierName());
         if (phone == null) {
            if (drpSupplier.getNoticeStyle() != 2) {
               log.info("当前供应商不存在或未配置短信通知！");
               return;
            }

            phone = drpSupplier.getLinkmanNumber();
         }
      }

      phone = SM4EncryptUtils.sm4Decrypt(phone);
      dto.setNoticeType(NoticeTemplateTypeEnum.SMS.getKey());
      dto.setNoticeId(noticeId);
      dto.setRecipientList(Collections.singletonList(phone));
      dto.setCode(mainId);
      dto.setDrpInquirySendNoticeType(key);
      MqUtil.send(JSONUtil.toJsonStr(dto), LeMqConstant.Topic.NOTICE_DRP_GENERAL);
      DrpPurNotice drpPurNotice = new DrpPurNotice();
      drpPurNotice.setNoticeId(noticeId);
      drpPurNotice.setPlanId(mainId);
      drpPurNotice.setNoticeTime(LocalDateTime.now());
      drpPurNotice.setNoticeType(1);
      drpPurNotice.setNoticeProcess(1);
      drpPurNotice.setNoticeStyle(DrpNoticeStyleEnum.SMS_NOTICE.key());
      drpPurNotice.setSupplierId(supplierId);
      drpPurNotice.setSupplierSingleId(key);
      if (CharSequenceUtil.isBlank(phone)) {
         log.info("手机号不存在");
         drpPurNotice.setNoticeStatus(DrpPurNoticeStatusEnum.ERROR.key());
         drpPurNotice.setErrorReason("手机号不存在!");
         this.drpPurNoticeMapper.insert(drpPurNotice);
      } else {
         drpPurNotice.setNoticeStatus(DrpPurNoticeStatusEnum.WAITING_TO_SEND.key());
         this.drpPurNoticeMapper.insert(drpPurNotice);
      }
   }


}
