package com.yida.data.school.transaction.controller;

import cc.mrbird.febs.common.redis.service.RedisService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.annotation.ControllerLog;
import com.yida.data.common.core.common.ModuleName;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.apply.EduApplyProductOrder;
import com.yida.data.common.core.entity.apply.EduApplyProductRefund;
import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.entity.system.enums.OperationLogTypeEnum;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.log.annotation.OperationLog;
import com.yida.data.school.dto.transaction.EduApplyProductOrderDTO;
import com.yida.data.school.transaction.service.EduProductOrderService;
import com.yida.data.school.vo.transcation.PayInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "交易-订单")
@RestController
@RequestMapping("/transaction/order")
public class EduProductOrderController {

    @Resource
    private EduProductOrderService eduProductOrderService;
    @Resource
    private RedisService redisService;

    @ApiOperation("修改支付状态")
    @GetMapping("/upDateEduProductOrder")
    @OperationLog(module = ModuleName.PRODUCT, methods = "修改支付状态", type = OperationLogTypeEnum.UPDATE)
    public ResultBean upDateEduProductOrder(@ApiParam("生成订单") @RequestParam EduApplyProductOrder eduApplyProductOrder) {
        return eduProductOrderService.updateProductOder(eduApplyProductOrder);
    }

    @ApiOperation("待付款/交易成功订单批量分页查询")
    @PostMapping("/selectPageListProductOrder")
    public ResultBean<IPage<EduApplyProductOrder>> selectPageListProductOrder(@ApiParam("订单查询条件") @RequestBody EduApplyProductOrderDTO dto) {
        Page page = new Page();
        page.setSize(dto.getSize());
        page.setCurrent(dto.getCurrent());

        IPage<EduApplyProductOrder> list = eduProductOrderService.selectPageListProductOrderService(page, dto);
        return ResultBean.buildSuccess(list);
    }

    @ApiOperation("手机端待付款/交易成功订单批量查询")
    @GetMapping("/selectListProductOrder")
    public ResultBean<List<EduApplyProductOrder>> selectListProductOrder(@ApiParam("用户手机号") @RequestParam String phone,
                                                                         @ApiParam("studentId") @RequestParam Long studentId) {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        return ResultBean.buildSuccess(eduProductOrderService.selectListProductOrder(currentUser.getDeptId(), phone,
                studentId));
    }


    @ApiOperation("生成订单")
    @GetMapping("/createOrder")
    @OperationLog(module = ModuleName.PRODUCT, methods = "生成订单", type = OperationLogTypeEnum.INSERT)
    public ResultBean<PayInfoVO> insertEduProductOrder(@ApiParam("商品id") Long productId,
                                                       @ApiParam("学生id") Long[] studentId,
                                                       @ApiParam("支付方式，0-微信jsapi，1-微信h5") Integer payType) {
        return ResultBean.buildSuccess(eduProductOrderService.createOrder(productId, studentId, payType, FebsUtil.getCurrentUser(),
                FebsUtil.getHttpServletRequestIpAddress()));
    }

    @ApiOperation("发起支付")
    @GetMapping("/payOrder")
    @OperationLog(module = ModuleName.PRODUCT, methods = "发起支付", type = OperationLogTypeEnum.INSERT)
    public ResultBean<PayInfoVO> payOrder(String orderCode) {
        Object prepay = redisService.get(CachePrefixConstant.ORDER_FLAG + orderCode);
        PayInfoVO infoVO = eduProductOrderService.getWxJsSign(orderCode, prepay.toString());
        infoVO.setOrderCode(orderCode);
        return ResultBean.buildSuccess(infoVO);
    }

    @ApiOperation("发起退款")
    @GetMapping("/refund")
    @OperationLog(module = ModuleName.PRODUCT, methods = "发起退款", type = OperationLogTypeEnum.UPDATE)
    public ResultBean refund(EduApplyProductRefund refund) {
        eduProductOrderService.refund(refund, FebsUtil.getCurrentUser());
        return ResultBean.buildSuccess();
    }

    //@ApiOperation("下载账单")
    //@GetMapping("/downloadBill")
    //public ResultBean downloadBill(@ApiParam("日期，yyyy-MM-dd") String date,
    //                               @ApiParam("账单类型,ALL:所有账单，SUCCESS：当日成功支付的账单，REFUND：当日退款账单") String type) {
    //    return ResultBean.buildSuccess(WxPayUtil.applyBill(date, type));
    //}

    //@GetMapping("/getInfo")
    //public ResultBean getInfo(String code) throws URISyntaxException, IOException {
    //    return ResultBean.buildSuccess(WxPayUtil.queryOrder(code));
    //}

    //@ApiOperation("微信jsapi调起支付签名")
    //@GetMapping("/getWxJsSign")
    //public ResultBean getWxJsSign(String appId, String timestamp, String nonceStr, String packge) {
    //    String code = appId + "\n" + timestamp + "\n" + nonceStr + "\n" + packge + "\n";
    //    Sign sign = SecureUtil.sign(SignAlgorithm.SHA256withRSA, WxPayConstant.API_CREDIT_PRIVATE_KEY_NO_HEADER,
    //            null);
    //    byte[] res = sign.sign(code.getBytes());
    //    String signRes = Base64.encode(res);
    //    return ResultBean.buildSuccess(signRes);
    //}

    @ApiOperation("订单详情查询")
    @GetMapping("/selectEduProductOrder")
    public ResultBean selectEduProductOrder(@ApiParam("订单id") Long orderId) {
        return ResultBean.buildSuccess(eduProductOrderService.selectEduProductOrder(orderId));
    }

    @ApiOperation("订单完成（前台支付完成后调用）")
    @GetMapping("/orderSuccess")
    @OperationLog(module = ModuleName.PRODUCT, methods = "订单完成（前台支付完成后调用）", type = OperationLogTypeEnum.UPDATE)
    public ResultBean orderSuccess(@ApiParam("订单号") String orderCode) {
        eduProductOrderService.orderSuccess(orderCode);
        return ResultBean.buildSuccess();
    }
}
