package com.bonus.canteen.core.account.v3.mq.send;

import com.bonus.canteen.core.account.v3.constants.AccStatusEnum;
import com.bonus.canteen.core.account.v3.custom.AccMqCustomBusiness;
import com.bonus.canteen.core.account.v3.mapper.AccInfoMapper;
import com.bonus.canteen.core.account.v3.mq.send.dto.RepAccMqDTO;
import com.bonus.canteen.core.account.v3.mq.send.model.AccStatusChangeModel;
import com.bonus.canteen.core.account.v3.mq.send.model.RepAccMqDetailModel;
import com.bonus.canteen.core.account.v3.mq.send.model.RepAccMqModel;
import com.bonus.canteen.core.account.v3.service.AccSubBusinessService;
import com.bonus.canteen.core.account.v3.service.AccTradeService;
import com.bonus.canteen.core.account.v3.service.AccTradeWalletDetailService;
import com.bonus.canteen.core.common.constant.LeMqConstant;
import com.bonus.canteen.core.common.utils.JacksonUtil;
//import com.bonus.canteen.core.common.utils.TenantContextHolder;
//import com.bonus.canteen.core.order.mq.MqUtil;
import com.bonus.canteen.core.common.utils.TenantContextHolder;
import com.bonus.canteen.core.order.mq.MqUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component
public class AccSendMq {
   private static final Logger log = LoggerFactory.getLogger(AccSendMq.class);
   @Resource
   @Lazy
   private AccTradeService accTradeService;
   @Resource
   @Lazy
   private AccTradeWalletDetailService accTradeWalletDetailService;
   @Resource
   @Lazy
   private AccMqCustomBusiness accMqCustomBusiness;
   @Resource
   @Lazy
   private AccSubBusinessService accSubBusinessService;

   @Autowired
   @Lazy
   private AccInfoMapper accInfoMapper;

   public void accTradeSendHandle(Long tenantId, List<RepAccMqDTO> sendList) {
      log.info("账户交易变动发送mq:商家={},参数数量：{}", tenantId, sendList.size());
      List<Long> tradeList = (List)sendList.stream().map(RepAccMqDTO::getTradeId).collect(Collectors.toList());
      List<RepAccMqModel> resultList = this.accTradeService.listAccTradeForSend(tradeList);
      List<RepAccMqDetailModel> walletDetail = this.accTradeWalletDetailService.listAccTradeWalletDetailForSend(tradeList);
      Map<Long, List<RepAccMqDetailModel>> walletDetailMap = (Map)walletDetail.stream().collect(Collectors.groupingBy(RepAccMqDetailModel::getFlowId));
      Map<Long, RepAccMqDTO> mqDtoMap = (Map)sendList.stream().collect(Collectors.toMap(RepAccMqDTO::getTradeId, Function.identity()));
      Locale locale = LocaleContextHolder.getLocale();
      String country = locale.getCountry();
      String language = locale.getLanguage();
      resultList.forEach((item) -> {
         item.setWalletDetail((List)walletDetailMap.get(item.getFlowId()));
         item.setMerchantId(tenantId);
         item.setCountry(country);
         item.setLanguage(language);
         item.assembleParam((RepAccMqDTO)mqDtoMap.get(item.getFlowId()));
      });
      resultList = this.accMqCustomBusiness.preAssembleAccTradeSendMq(resultList);
      this.accSubBusinessService.subsidyChangeBusiness(resultList);
      resultList.forEach((result) -> {
         log.info("账户交易变动发送mq内容：{}", JacksonUtil.writeValueAsString(result));
         MqUtil.send(JacksonUtil.writeValueAsString(result), LeMqConstant.Topic.ACC_TRADE_REPORT);
      });
   }
   public void accChangeSendHandle(List<Long> custIdList, Integer accStatus) {
      if (AccStatusEnum.sendMqStatus().contains(accStatus)) {
         log.info("账户状态变动发送mq_入参人员数量={},accStatus={}", custIdList.size(), accStatus);
         List<AccStatusChangeModel> resultList = this.accInfoMapper.queryAccStatusByCustIdList(custIdList);
         Long tenantId = TenantContextHolder.getTenantId();
         LocalDateTime now = LocalDateTime.now();
         resultList.forEach((item) -> {
            item.setMerchantId(tenantId);
            item.setAccStatus(accStatus);
            item.setUpDateTime(now);
            log.info("账户状态变动发送mq内容：{}", JacksonUtil.writeValueAsString(item));
            MqUtil.send(JacksonUtil.writeValueAsString(item), LeMqConstant.Topic.ACC_STATUS_CHANGE);
         });
      }
   }


}
