package com.yida.data.device.telephone.controller;

import cc.mrbird.febs.common.redis.service.RedisService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.constant.CachePrefixConstant;
import com.yida.data.common.core.entity.system.enums.OperationLogTypeEnum;
import com.yida.data.common.core.entity.telephone.EduTelephoneOrder;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.device.dto.telephone.OrderPageDTO;
import com.yida.data.device.telephone.service.EduTelephoneOrderService;
import com.yida.data.device.vo.telephone.OrderInfoVO;
import com.yida.data.device.vo.telephone.OrderPageVO;
import com.yida.data.log.annotation.OperationLog;
import com.yida.data.school.vo.transcation.PayInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 亲情电话-充值订单 Controller
 *
 * @author zhanghaijun
 * @date 2021-08-10 17:00:48
 */
@Api(tags = "亲情电话-订单")
@Slf4j
@Validated
@RestController
@RequestMapping("/telephone/order")
@RequiredArgsConstructor
public class EduTelephoneOrderController {

    private final EduTelephoneOrderService eduTelephoneOrderService;
    @Resource
    private RedisService redisService;

    @ApiOperation("分页查询订单信息")
    @GetMapping("/listOrderPage")
    public ResultBean<IPage<OrderPageVO>> listOrderPage(OrderPageDTO dto) {
        return ResultBean.buildSuccess(eduTelephoneOrderService.listOrderPage(dto));
    }

    @ApiOperation("导出订单")
    @PostMapping("/exportOrder")
    public void exportOrder(OrderPageDTO dto, HttpServletResponse response) {
        eduTelephoneOrderService.exportOrder(dto, response);
    }

    @ApiOperation("查询订单详情")
    @GetMapping("/getOrder")
    public ResultBean<OrderInfoVO> getOrder(Long id) {
        return ResultBean.buildSuccess(eduTelephoneOrderService.getOrder(id));
    }

    @ApiOperation("前台查询订单信息")
    @GetMapping("/listOrder")
    public ResultBean<List<EduTelephoneOrder>> listOrder(@ApiParam("卡id") @RequestParam Long cardId) {
        return ResultBean.buildSuccess(eduTelephoneOrderService.list(Wrappers.lambdaQuery(new EduTelephoneOrder())
            .eq(EduTelephoneOrder::getCardId, cardId).orderByDesc(EduTelephoneOrder::getCreateDate)));
    }

    @ApiOperation("生成订单")
    @GetMapping("/createOrder")
    @OperationLog(module = ModuleName.TELEPHONE, methods = "生成订单", type = OperationLogTypeEnum.INSERT)
    public ResultBean<PayInfoVO> insertEduProductOrder(@ApiParam("充值金额") Double money,
        @ApiParam("ic卡id") Long icCardId,
        @ApiParam("支付方式，0-微信jsapi，1-微信h5") Integer payType) {
        return ResultBean.buildSuccess(eduTelephoneOrderService.createOrder(money, icCardId, payType, FebsUtil.getCurrentUser(),
            FebsUtil.getHttpServletRequestIpAddress()));
    }

    @ApiOperation("订单完成（前台支付完成后调用）")
    @GetMapping("/orderSuccess")
    public ResultBean orderSuccess(@ApiParam("订单号") String orderCode) {
        eduTelephoneOrderService.orderSuccess(orderCode);
        return ResultBean.buildSuccess();
    }

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

    //@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);
    //}
}
