package com.heyu.api.controller.vv;

import com.google.gson.Gson;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.enums.RoleEnums;
import com.heyu.api.data.service.vv.VvPackageService;
import com.kuaidi100.sdk.response.SubscribePushParamResp;
import com.kuaidi100.sdk.response.SubscribePushResult;
import com.kuaidi100.sdk.response.SubscribeResp;
import com.kuaidi100.sdk.utils.SignUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/app/kuaidi")
public class AppKuaiDiCallBackController {

    @Autowired
    private VvPackageService vvPackageService;

    /***
     * https://api.1024api.com/api-interface/app/kuaidi/callback
     *
     * 列表
     */
    @Describe("快递100回调接口")
    @RequestMapping("/callback")
    public SubscribeResp callback(String param, String sign) {

        log.info("kuaidi callback param:{},sign", param, sign);

        //建议记录一下这个回调的内容，方便出问题后双方排查问题
        //log.debug("快递100订阅推送回调结果|{}|{}",param,sign);
        //订阅时传的salt,没有可以忽略
        String salt = "";
        String ourSign = SignUtils.sign(param + salt);
        SubscribeResp subscribeResp = new SubscribeResp();
        subscribeResp.setResult(Boolean.TRUE);
        subscribeResp.setReturnCode("200");
        subscribeResp.setMessage("成功");
        //加密如果相等，属于快递100推送；否则可以忽略掉当前请求
        if (ourSign.equals(sign)) {
            //TODO 业务处理
            SubscribePushParamResp subscribePushParamResp = new Gson().fromJson(param, SubscribePushParamResp.class);

            SubscribePushResult subscribePushResult = subscribePushParamResp.getLastResult();
            if (subscribePushResult != null) {
                vvPackageService.selectVvKuaidiAndUpdateData(subscribePushResult.getNu(), RoleEnums.kuaidi100_callback.getRole());
            }
            return subscribeResp;
        }
        return null;

    }

}
