package com.yc.ship.module.trade.service.order.handler;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.yc.ship.module.system.api.sms.SmsSendApi;
import com.yc.ship.module.system.api.sms.dto.send.SmsSendSingleToUserReqDTO;
import com.yc.ship.module.system.enums.sms.SmsSendTypeEnum;
import com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO;
import com.yc.ship.module.trade.dal.dataobject.order.TradeOrderUserDO;
import com.yc.ship.module.trade.service.order.TradeOrderRepositoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

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


/**
 * &#064;Description 消息发送
 * &#064;Author qsl
 * &#064;Date 2025/8/28 13:54
 */
@Component
@Order(TradeOrderHandler.MESSAGE)
@Slf4j
public class TradeOrderMessageHandler implements TradeOrderHandler {

    @Resource
    private SmsSendApi smsSendApi;
    @Resource
    private TradeOrderRepositoryService tradeOrderRepositoryService;

    @Override
    public void afterProcessSuccess(TradeOrderDO tradeOrderDO) {
        try {
            List<Map<String,Object>> orderProductList = tradeOrderRepositoryService.getOrderProduct(tradeOrderDO.getId());
            TradeOrderUserDO tradeOrderUserDO = tradeOrderRepositoryService.getOrderUser(tradeOrderDO.getId());
            if(tradeOrderUserDO == null || StrUtil.isBlank(tradeOrderUserDO.getMobile())){
                return;
            }
            for (Map<String,Object> productMap : orderProductList) {
                Long templateId = smsSendApi.getTemplateIdByProduct(MapUtil.getLong(productMap,"product_id"), SmsSendTypeEnum.PAY.getType()).getData();
                if(templateId!=null){
                    SmsSendSingleToUserReqDTO smsSendSingleToUserReqDTO = new SmsSendSingleToUserReqDTO();
                    smsSendSingleToUserReqDTO.setTemplateId(templateId);
                    smsSendSingleToUserReqDTO.setMobile(tradeOrderUserDO.getMobile());
                    smsSendSingleToUserReqDTO.setTemplateParams(getTemplateParams(tradeOrderDO,productMap,tradeOrderUserDO));
                    smsSendApi.sendSms(smsSendSingleToUserReqDTO);
                }
            }
        }catch (Exception e){
            log.error("短信发送异常",e);
        }
    }

    private Map<String, Object> getTemplateParams(TradeOrderDO tradeOrderDO,Map<String,Object> productMap,TradeOrderUserDO tradeOrderUserDO){
        Map<String, Object> map = new HashMap<>();
        map.put("product_name",MapUtil.getStr(productMap,"product_name"));
        map.put("order_no",tradeOrderDO.getOrderNo());
        map.put("travel_date", DateUtil.formatDate(MapUtil.getDate(productMap,"use_date")));
        map.put("price",MapUtil.getStr(productMap,"actual_price"));//单价
        map.put("contact_name",tradeOrderUserDO.getContactName());
        map.put("total_ticket",MapUtil.getLong(productMap,"buyCount"));
        map.put("amount",tradeOrderDO.getPayAmount());
        map.put("order_status",tradeOrderDO.getOrderStatus());
        map.put("contact_tel",tradeOrderUserDO.getMobile());
        return map;
    }

    @Override
    public void afterRefundOrder(Long refundId) {
        try {

        }catch (Exception e){
            log.error("短信发送异常",e);
        }
    }

}
