package com.platform.yijia.controller;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.platform.yijia.param.request.PayOrderRequest;
import com.platform.yijia.pojo.AppUserInfo;
import com.platform.yijia.pojo.PayOrder;
import com.platform.yijia.service.AppUserInfoService;
import com.platform.yijia.service.PayOrderService;
import com.platform.yijia.utils.CodeMsg;
import com.platform.yijia.utils.ResultData;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/api")
public class PayOrderController {
    private static Logger logger =(Logger) LoggerFactory.getLogger(PayOrderController.class);
    @Resource
    private PayOrderService payOrderService;
    @Resource
    private AppUserInfoService appUserInfoService;

    //一键支付，生成订单
    @RequestMapping(value = "/AddPayOrderInfo", consumes = "application/json", method = RequestMethod.POST)
    @ResponseBody
    public String  AddPayOrderInfo(@RequestBody PayOrderRequest payOrderRequest){
        Gson gson =new Gson();
        ResultData resultData =null;
        //根据token 和登录类型 身份验证
        AppUserInfo appUserInfo =new AppUserInfo();
        //订单类型 是小程序还是公众号
        appUserInfo.setUserType(payOrderRequest.getUserType());
        if(payOrderRequest.getUserType().equals("1")){
            //1 是公众号
            appUserInfo.setBlogToken(payOrderRequest.getToken());
        }else if(payOrderRequest.getUserType().equals("2")){
            //2是小程序
            appUserInfo.setMinaToken(payOrderRequest.getToken());
        }
        List<AppUserInfo> appUserInfoList =appUserInfoService.Authentication(appUserInfo);
        JSONObject resp = new JSONObject();
        //判断登录人员是否验证成功
        if(appUserInfoList!=null&&appUserInfoList.size()>0){
            PayOrder payOrder=new PayOrder();
            payOrder.setConsumer(appUserInfoList.get(0).getBlogNickName()); //昵称
            payOrder.setConsumerId(appUserInfoList.get(0).getUserId()); //用户id
            //添加订单信息，返回订单号
            String ordNo =payOrderService.AddPayOrderInfo(payOrderRequest, payOrder);
            resultData=ResultData.success(ordNo);
        }else{
            //登录人验证失败
            resultData=ResultData.error(CodeMsg.USER_NOT_EXSIST);
        }
        return gson.toJson(resultData);
    }
    /**
     * 根据订单号获取订单信息
     * produces="application/json;charset=UTF-8",consumes = "application/json",
     * @RequestBody String orderno
     * @param
     * @return
     */
    @RequestMapping(value = "/getPayOrderList", method = RequestMethod.GET)
    @ResponseBody
    public String getPayOrderList(@RequestParam String orderno){
        Gson gson =new Gson();
        ResultData resultData =null;
        if(orderno!=null){
            if(StringUtils.isNotBlank(orderno)){
                PayOrderRequest payOrderRequest =new PayOrderRequest();
                payOrderRequest.setOrderNo(orderno);
                List<PayOrder> payOrderList = payOrderService.getPayOrderList(payOrderRequest.getOrderNo());
                resultData=ResultData.success(payOrderList);
            }else {
                resultData=ResultData.error(CodeMsg.SEARCH_FAIL);
            }
        }else{
            resultData=ResultData.error(CodeMsg.USER_NOT_EXSIST);
        }
        return gson.toJson(resultData);
    }
}
