package cn.newfeifan.mall.module.trade.framework.delivery.core.client.impl.kd100;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.newfeifan.mall.framework.common.util.json.JsonUtils;
import cn.newfeifan.mall.module.trade.dal.dataobject.delivery.DeliveryExpressDO;
import cn.newfeifan.mall.module.trade.dal.dataobject.order.TradeOrderDO;
import cn.newfeifan.mall.module.trade.dal.mysql.delivery.DeliveryExpressMapper;
import cn.newfeifan.mall.module.trade.dal.mysql.order.TradeOrderMapper;
import cn.newfeifan.mall.module.trade.enums.order.TradeOrderStateEnum;
import cn.newfeifan.mall.module.trade.framework.delivery.config.TradeExpressProperties;
import cn.newfeifan.mall.module.trade.framework.delivery.core.client.ExpressClient;
import cn.newfeifan.mall.module.trade.framework.delivery.core.client.dto.ExpressTrackQueryReqDTO;
import cn.newfeifan.mall.module.trade.framework.delivery.core.client.dto.ExpressTrackRespDTO;
import cn.newfeifan.mall.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryReqDTO;
import cn.newfeifan.mall.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryRespDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.module.trade.enums.ErrorCodeConstants.EXPRESS_API_QUERY_ERROR;
import static cn.newfeifan.mall.module.trade.enums.ErrorCodeConstants.EXPRESS_API_QUERY_FAILED;
import static cn.newfeifan.mall.module.trade.framework.delivery.core.client.convert.ExpressQueryConvert.INSTANCE;

/**
 * 快递 100 客户端
 *
 * @author jason
 */
@Slf4j
@AllArgsConstructor
public class Kd100ExpressClient implements ExpressClient {

    public Kd100ExpressClient(RestTemplate restTemplate,TradeExpressProperties.Kd100Config config){
        this.restTemplate = restTemplate;
        this.config = config;
    }

    private static final String REAL_TIME_QUERY_URL = "https://poll.kuaidi100.com/poll/query.do";

    private final RestTemplate restTemplate;
    private final TradeExpressProperties.Kd100Config config;

    @Resource
    private TradeOrderMapper tradeOrderMapper;

    @Resource
    private DeliveryExpressMapper deliveryExpressMapper;

    /**
     * 查询快递轨迹
     *
     * @see <a href="https://api.kuaidi100.com/debug-tool/query/">接口文档</a>
     *
     * @param reqDTO 查询请求参数
     * @return 快递轨迹
     */
    @Override
    public List<ExpressTrackRespDTO> getExpressTrackList(ExpressTrackQueryReqDTO reqDTO) {
        // 发起请求
        Kd100ExpressQueryReqDTO requestDTO = INSTANCE.convert2(reqDTO)
                .setExpressCode(reqDTO.getExpressCode().toLowerCase());
        Kd100ExpressQueryRespDTO respDTO = httpRequest(REAL_TIME_QUERY_URL, requestDTO,
                Kd100ExpressQueryRespDTO.class);

        // 处理结果
        if (Objects.equals("false", respDTO.getResult())) {
            throw exception(EXPRESS_API_QUERY_FAILED, respDTO.getMessage());
        }
        if (CollUtil.isEmpty(respDTO.getTracks())) {
            return Collections.emptyList();
        }
        List<ExpressTrackRespDTO> expressTrackRespDTOS = INSTANCE.convertList2(respDTO.getTracks());

        //如果状态为已签收("3"),则保存到订单信息中
        if(respDTO.getState().equals(TradeOrderStateEnum.RECEIVED.getState())){
            updateOrder(respDTO.getExpressCompanyCode(),respDTO.getLogisticsNo(),expressTrackRespDTOS.get(0).getTime());
        }
        return expressTrackRespDTOS;
    }

    /**
     * 保存订单的签收时间
     * @param com 快递公司编码
     * @param nu 单号
     */
    private void updateOrder(String com, String nu, LocalDateTime time){
        DeliveryExpressDO deliveryExpressDO = deliveryExpressMapper.selectByCode(com);
        TradeOrderDO tradeOrderDO = tradeOrderMapper.selectOne(new LambdaQueryWrapper<TradeOrderDO>()
                .eq(TradeOrderDO::getLogisticsNo, nu)
                .eq(TradeOrderDO::getLogisticsId, deliveryExpressDO.getId())
                .isNull(TradeOrderDO::getReceivingTime)
        );
        if(tradeOrderDO == null){
            return;
        }
        tradeOrderDO.setReceivingTime(time);

        tradeOrderMapper.updateById(tradeOrderDO);
    }

    /**
     * 快递 100 API 请求
     *
     * @param url 请求 url
     * @param req 对应请求的请求参数
     * @param respClass 对应请求的响应 class
     * @param <Req> 每个请求的请求结构 Req DTO
     * @param <Resp> 每个请求的响应结构 Resp DTO
     */
    private <Req, Resp> Resp httpRequest(String url, Req req, Class<Resp> respClass) {
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        // 请求体
        String param = JsonUtils.toJsonString(req);
        String sign = generateReqSign(param, config.getKey(), config.getCustomer()); // 签名
        MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
        requestBody.add("customer", config.getCustomer());
        requestBody.add("sign", sign);
        requestBody.add("param", param);
        log.debug("[httpRequest][请求参数({})]", requestBody);

        // 发送请求
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
        log.debug("[httpRequest][的响应结果({})]", responseEntity);
        // 处理响应
        if (!responseEntity.getStatusCode().is2xxSuccessful()) {
            throw exception(EXPRESS_API_QUERY_ERROR);
        }
        return JsonUtils.parseObject(responseEntity.getBody(), respClass);
    }

    private String generateReqSign(String param, String key, String customer) {
        String plainText = String.format("%s%s%s", param, key, customer);
        return HexUtil.encodeHexStr(DigestUtil.md5(plainText), false);
    }

}
