package com.bohuikeji.delivery.backstage.controller.test;

import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.bohuikeji.delivery.backstage.controller.callback.ThirdCallBack;
import com.bohuikeji.delivery.backstage.entity.TCourier;
import com.bohuikeji.delivery.backstage.entity.TCourierCard;
import com.bohuikeji.delivery.backstage.listener.order.OrderCheckPriceListener;
import com.bohuikeji.delivery.backstage.pay.AliPay;
import com.bohuikeji.delivery.backstage.service.*;
import com.bohuikeji.delivery.backstage.service.third.JdServer;
import com.bohuikeji.delivery.backstage.service.third.ShansongServer;
import com.bohuikeji.delivery.backstage.service.third.UUServer;
import com.bohuikeji.frame.module.admin.base.ApiController;
import com.bohuikeji.frame.module.admin.component.RedisUtil;
import com.bohuikeji.frame.module.admin.config.MqConf;
import com.bohuikeji.frame.module.admin.entity.back.PageEntity;
import com.bohuikeji.frame.module.admin.utils.JsonUtil;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * 通知推送测试
 * @author 10552
 */
@Slf4j
@RestController
@Api(tags = {"数据统计测试"})
@RequestMapping("/data/test")
@RequiredArgsConstructor
public class DataTest extends ApiController {

    @Autowired
    ITCourierService courierService;
    @Autowired
    ITCourierCardService courierCardService;

    final RabbitTemplate rabbitTemplate;

    final ITOrderService orderService;
    /*final IShanSongService shanSongService;*/

    final ThirdCallBack songCallBack;
    final ITCustomerService itCustomerService;
    final UUServer uuServer;
    final ShansongServer shansongServer;
    final RedisUtil redisUtil;
    final MqConf mqConf;
    final OrderCheckPriceListener orderCheckPriceListener;
    final JdServer jdServer;
    final ISocketService socketService;
    final ITOrderArrearsService arrearsService;



    @GetMapping("/get/list")
    public PageEntity aliOAuth(String auth_code,String app_id,String scope,String state){
        log.error("支付宝授权回调成功，auth_code：[{}]，app_id=[{}]，scope=[{}],state=[{}]",auth_code,app_id,scope,state);
        TCourier courier=courierService.getById(state);
        if(courier==null){
            log.error("无法识别派送员：[{}]",state);
            return PageEntity.page("courier/ali_OAuth_fail");
        }
        AlipayUserInfoShareResponse response= AliPay.getUserInfo(auth_code);
        if(response==null){
            log.error("支付宝获取用户信息失败，用户：[{}]未授",courier.getCourierName());
            return PageEntity.page("courier/ali_OAuth_fail");
        }
        if(!response.isSuccess()){
            log.error("支付宝获取用户信息失败,支付宝信息：[{}]",response.getSubMsg());
            return PageEntity.page("courier/ali_OAuth_fail");
        }
        log.error("获取到支付宝用户信息：[{}]", JsonUtil.objToJson(response).toJSONString());
        TCourierCard courierCard=new TCourierCard();
        courierCard.setCourierId(courier.getCourierId());
        courierCard.setCardBank("支付宝");
        courierCard.setBankType("网商银行");
        courierCard.setCreateDate(LocalDateTime.now());
        courierCard.setCardNum(response.getUserId());
        courierCard.setCardName(response.getNickName());
        courierCard.setIdNumber(courier.getCourierCard());
        courierCardService.save(courierCard);
        return PageEntity.page("courier/ali_OAuth_success").add("courier",courier);
    }

}
