package com.cpop.jambox.business.controller.backstage;

import com.alibaba.excel.EasyExcel;
import com.cpop.core.base.R;
import com.cpop.jambox.business.bo.*;
import com.cpop.jambox.business.dto.EasyLearnPageDto;
import com.cpop.jambox.business.service.EasyLearnOrderService;
import com.cpop.jambox.business.vo.EasyLearnPageVo;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * 果酱-放心学订单表 控制层。
 *
 * @author DB
 * @since 2023-12-14
 */
@RestController
@Api(tags = "果酱-放心学订单表接口")
@RequestMapping("/backstage/easyLearn")
public class BackstageEasyLearnController {

    @Autowired
    private EasyLearnOrderService easyLearnOrderService;


    /**
     * 获取放心学分页
     * @author DB
     * @since 2023/12/15
     * @param bo 查询参数
     * @return R<Page<EasyLearnPageVo>>
     */
    @ApiOperation("查询放心学分页")
    @GetMapping("/getEasyLearnPage")
    public R<Page<EasyLearnPageVo>> getEasyLearnPage(@Validated EasyLearnPageBo bo) {
        Page<EasyLearnPageVo> page = easyLearnOrderService.getEasyLearnPage(bo);
        return R.ok(page);
    }

    /**
     * 导出放心学数据
     * @author DB
     * @since 2023/12/15
     * @param response 响应
     * @param bo 请求参数
     */
    @GetMapping("/getEasyLearnXml")
    @ApiOperation("导出放心学数据")
    public void getEasyLearnXml(HttpServletResponse response, @ApiParam("分页查询条件") @Validated EasyLearnPageBo bo) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyExcel没有关系
        String fileFullName = "放心学";
        switch (bo.getOrderType()) {
            case 0:
                fileFullName = fileFullName + " 先学后付-次付 ";
                break;
            case 1:
                fileFullName = fileFullName + " 先学后付-月付 ";
                break;
            case 2:
                fileFullName = fileFullName + " 微信支付 ";
                break;
            case 3:
                fileFullName = fileFullName + " 数字人民币 ";
                break;
            case 4:
                fileFullName = fileFullName + " 机构会员 ";
                break;
            default:
        }
        String fileName = URLEncoder.encode(fileFullName + "数据", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), EasyLearnPageDto.class).sheet("记录").doWrite(easyLearnOrderService.getEasyLearnXml(bo));
    }

    /**
     * 一次性支付下单
     * @author DB
     * @since  2023/10/23 12:15
     * @param bo 下单请求对象
     * @return com.cpop.core.base.R<java.lang.Void>
     */
    @PostMapping("/oncePlaceOrder")
    @ApiOperation("一次性支付下单")
    public R<Object> oncePlaceOrder(@RequestBody @Validated @ApiParam("一次性支付") OncePlaceOrderBo bo) {
        return R.ok(easyLearnOrderService.oncePlaceOrder(bo));
    }

    /**
     * 放心学统一支付
     * @author DB
     * @since 2023/12/28
     * @param bo 请求参数
     * @return R<Object>
     */
    @Deprecated
    @PostMapping("/unionPay")
    @ApiOperation("放心学统一支付")
    public R<Object> unionPay(@RequestBody @Validated EasyLearnUnionPayBo bo) {
        return R.ok(easyLearnOrderService.unionPay(bo));
    }

    /**
     * 放心学统一支付
     * @author DB
     * @since 2024/1/9
     * @param bo 请求参数
     * @return R<Object>
     */
    @PostMapping("/unifiedPay")
    @ApiOperation("放心学统一支付(测试)")
    public R<Object> unifiedPay(@RequestBody @Validated @ApiParam("放心学统一支付") EasyLearnUnifiedPayBo bo) {
        return R.ok(easyLearnOrderService.unifiedPay(bo));
    }

}
