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

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.entity.*;
import com.bohuikeji.delivery.backstage.pay.BytePay;
import com.bohuikeji.delivery.backstage.pay.KuaiShouPay;
import com.bohuikeji.delivery.backstage.service.*;
import com.bohuikeji.frame.module.admin.annotation.InitLog;
import com.bohuikeji.frame.module.admin.base.ControllerBase;
import com.bohuikeji.frame.module.admin.controller.FrameTableController;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 支付、退款业务
 */
@Slf4j
@Controller
@ResponseBody
@RestController
@RequestMapping("/settle")
public class TSettleController extends ControllerBase {

    @Autowired
    ITSettleService settleService;
    @Autowired
    FrameTableController frameTableController;



    /**
     *抖音订单交易分账
     * @param
     * @return
     * @throws AlipayApiException
     * @throws IOException
     */
    @InitLog(value = "抖音订单交易分账",action = "分账")
    @ApiOperation("抖音订单交易分账")
    @RequestMapping("/get/settle")
    public APIEntity orderRefund(int[] settleIds){
        log.info("分账记录id:{}",settleIds);
        JSONObject jsonObject=null;
        for (Integer id : settleIds){
            TSettle settle = settleService.getOne(new QueryWrapper<TSettle>().eq("settle_id", id));
            jsonObject = BytePay.getOrderSettle(settle);
            if(jsonObject.get("err_no").equals(0)){
                settle.setSettleNo(jsonObject.getString("settle_no"));
                settleService.updateById(settle);
            }else {
                return APIEntity.fail(jsonObject.getString("err_tips"));
            }
        }
        log.info("抖音分账记录返回:{}",jsonObject);
        return APIEntity.success("分账成功");
    }

    /**
     *快手订单交易结算
     * @param
     * @return
     * @throws AlipayApiException
     * @throws IOException
     */
    @InitLog(value = "快手订单交易结算",action = "结算")
    @ApiOperation("快手订单交易结算")
    @RequestMapping("/get/settlement")
    public APIEntity ordersettlement(int[] settleIds){
        log.info("结算记录id:{}",settleIds);
        JSONObject jsonObject=null;
        for (Integer id : settleIds){
            TSettle settle = settleService.getOne(new QueryWrapper<TSettle>().eq("settle_id", id));
            jsonObject = KuaiShouPay.getOrderSettle(settle);
            if(jsonObject.getInteger("result").equals(1)){
                settle.setSettleNo(jsonObject.getJSONObject("settle_info").getString("settle_no"));
                settleService.updateById(settle);
            }else {
                return APIEntity.fail(jsonObject.getString("error_msg"));
            }
        }
        log.info("抖音分账记录返回:{}",jsonObject);
        return APIEntity.success("分账成功");
    }


    /**
     * 默认表格组合查询方法
     * @param startDate
     * @param endDate
     * @param type
     * @return
     * @throws IOException
     */
    @GetMapping("/record/query")
    public APIEntity toQueryPage(String startDate, String endDate, String type) {
        //log.info("@@@@@@@@时开始时间：{}，结束时间：{}，查询类型：{}",startDate.replaceAll("-",""),endDate.replaceAll("-",""),type);
        String file = BytePay.getOrderPayRecord(startDate,endDate,type);
        //log.info("抖音交易数据返回：{}",file);
        return APIEntity.success(file);
    }


}
