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.SecurityUtils;
import com.ruoyi.member.domain.MemberPoints;
import com.ruoyi.member.service.IMemberPointsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 博主积分变动明细Controller
 *
 * @author pqz
 * @date 2025-03-17
 */
@RestController
@RequestMapping("/member/points")
public class MemberPointsController extends BaseController {
    @Autowired
    private IMemberPointsService memberPointsService;

    /**
     * 分页查询积分变动明细
     *
     * @param userId   用户id
     * @param pageNum  分页参数
     * @param pageSize 分页参数
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 15:24 2025/3/29
     **/
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(name = "userId") Long userId,
                           @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<MemberPoints> page = new Page<>(pageNum, pageSize);
        IPage<MemberPoints> list = memberPointsService.queryListPage(userId, page);
        return success(list);
    }

    /**
     * 查询在途订单
     * @author PQZ
     * @date 14:36 2025/4/29 
     * @return com.ruoyi.common.core.domain.AjaxResult
    **/
    @GetMapping("/getOnWayOrder")
    public AjaxResult getOnWayOrder() {
        Long userId = SecurityUtils.getUserId();
        List<MemberPoints> list = memberPointsService.getOnWayOrder(userId);
        return success(list.isEmpty()?null:list.get(0));
    }


    /**
     * 新增博主积分变动记录
     *
     * @param memberPoints {@link MemberPoints}
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 15:29 2025/3/29
     **/
    @Log(title = "博主积分变动明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MemberPoints memberPoints) {
        memberPointsService.savePoints(memberPoints);
        return success();
    }

    /**
     * 积分提现
     *
     * @param memberPoints {@link MemberPoints}
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 17:14 2025/4/28
     **/
    @Log(title = "积分提现", businessType = BusinessType.INSERT)
    @PostMapping("/payout")
    public AjaxResult payout(@RequestBody MemberPoints memberPoints) {
        try {
            memberPointsService.payout(memberPoints);
            JSONObject res =new JSONObject();
            res.put("orderNo",memberPoints.getOrderNo());
            return success(res);
        }catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改博主积分变动明细
     */
    @Log(title = "博主积分变动明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberPoints memberPoints) {
        return toAjax(memberPointsService.updateById(memberPoints));
    }

    /**
     * 删除博主积分变动明细
     */
    @Log(title = "博主积分变动明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        List<String> list = new ArrayList<>(Arrays.asList(ids));
        return toAjax(memberPointsService.removeByIds(list));
    }
}
