package com.ruoyi.member.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.member.domain.MemberOrder;
import com.ruoyi.member.service.IMemberOrderService;
import com.ruoyi.member.vo.MemberOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 订单Controller
 *
 * @author pqz
 * @date 2025-03-17
 */
@RestController
@RequestMapping("/member/order")
public class MemberOrderController extends BaseController {
    @Autowired
    private IMemberOrderService memberOrderService;


    /**
     * 订单列表查询
     *
     * @param memberOrder {@link MemberOrderVO}
     * @param pageNum     分页参数
     * @param pageSize    分页参数
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 15:24 2025/3/25
     **/
    @GetMapping("/list")
    public AjaxResult list(MemberOrderVO memberOrder,
                           @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<MemberOrder> page = new Page<>(pageNum, pageSize);
        IPage<MemberOrderVO> list = memberOrderService.queryListPage(memberOrder, page);
        return success(list);
    }

    /**
     * 订单支付回调
     *
     * @param memberOrder {@link MemberOrder}
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 14:01 2025/3/26
     **/
    @Log(title = "订单支付回调", businessType = BusinessType.INSERT)
    @PostMapping("/payCallback")
    public AjaxResult payCallback(@RequestBody MemberOrder memberOrder) {
        memberOrderService.payCallback(memberOrder.getOrderNo());
        return toAjax(true);
    }


    /**
     * 导出订单列表
     */
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MemberOrder memberOrder) {
        List<MemberOrder> list = memberOrderService.list();
        ExcelUtil<MemberOrder> util = new ExcelUtil<MemberOrder>(MemberOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(memberOrderService.getById(id));
    }

    /**
     * 保存订单并吊起支付接口
     *
     * @param memberOrder {@link MemberOrder}
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 14:00 2025/4/8
     **/
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/uniSaveOrder")
    public AjaxResult add(@RequestBody MemberOrder memberOrder) {
        String orderNo = memberOrderService.saveOrder(memberOrder);
        JSONObject res =new JSONObject();
        res.put("orderNo",orderNo);
        return success(res);
    }

    /**
     * 修改订单
     */
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberOrder memberOrder) {
        return toAjax(memberOrderService.updateById(memberOrder));
    }

    /**
     * 删除订单
     */
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        List<String> list = new ArrayList<>(Arrays.asList(ids));
        return toAjax(memberOrderService.removeByIds(list));
    }
}
