package com.czg.controller.user;

import com.czg.market.dto.GbWareQueryParamDTO;
import com.czg.market.entity.GbWare;
import com.czg.market.service.GbWareService;
import com.czg.order.dto.CommonRefundDTO;
import com.czg.order.dto.GbOrderQueryParam;
import com.czg.order.dto.GroupJoinDTO;
import com.czg.order.service.GbOrderService;
import com.czg.order.vo.GbOrderDetailVO;
import com.czg.order.vo.GbWareVO;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.ServletUtil;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 用户拼团
 *
 * @author ww
 */
@RestController
@RequestMapping("/user/gbOrder")
public class UGbOrderController {


    @Resource
    private GbOrderService orderService;

    @Resource
    private GbWareService wareService;

    /**
     * 商品列表
     */
    @GetMapping("/ware/page")
    public CzgResult<Page<GbWare>> getPointsGoodsSettingPage(@RequestParam(defaultValue = "1", required = false) Integer page,
                                                             @RequestParam(defaultValue = "10", required = false) Integer size,
                                                             @RequestParam(required = false) String wareName,
                                                             @RequestParam(required = false) Integer groupPeopleNum,
                                                             Long shopId) {
        GbWareQueryParamDTO param = new GbWareQueryParamDTO();
        param.setPage(page);
        param.setSize(size);
        param.setOnlineStatus(1);
        param.setWareName(wareName);
        param.setGroupPeopleNum(groupPeopleNum);
        Page<GbWare> data = wareService.getGbWarePage(param, shopId, false);
        return CzgResult.success(data);
    }

    /**
     * 拼团商品详情
     */
    @GetMapping("/ware/detail")
    public CzgResult<GbWareVO> getWareDetail(@RequestParam Long shopId, @RequestParam Long wareId) {
        return CzgResult.success(orderService.getWareDetail(shopId, wareId, StpKit.USER.getLoginIdAsLong()));
    }


    /**
     * 我的拼团记录
     */
    @GetMapping("/record/page")
    public CzgResult<Page<GbOrderDetailVO>> getGoodsRecordPage(GbOrderQueryParam param) {
        param.setUserId(StpKit.USER.getLoginIdAsLong());
        Page<GbOrderDetailVO> pages = orderService.getGbOrderPage(param);
        return CzgResult.success(pages);
    }

    /**
     * 拼团订单详情
     */
    @GetMapping("/record/detail")
    public CzgResult<GbOrderDetailVO> getGoodsRecordDetail(
            @RequestParam Long shopId,
            @RequestParam(required = false) Long detailId,
            @RequestParam(required = false) String groupOrderNo) {
        return CzgResult.success(orderService.getGoodsRecordDetail(shopId, detailId, groupOrderNo));
    }


    /**
     * 生成订单
     * 小程序支付
     * payType 必填  支付方式，ALIPAY 支付宝，WECHAT 微信
     * openId  必填
     */
    @PostMapping("/exchange")
    public CzgResult<Map<String, Object>> exchange(HttpServletRequest request, @Validated @RequestBody GroupJoinDTO param) {
        param.setUserId(StpKit.USER.getLoginIdAsLong());
        param.setIp(ServletUtil.getClientIP(request));
        return orderService.groupJoin(param);
    }

    /**
     * 申请退单
     */
    @PostMapping("/applyRefund")
    public CzgResult<Boolean> applyRefund(@RequestBody @Validated CommonRefundDTO param) {
        return CzgResult.success(orderService.applyRefund(param, StpKit.USER.getLoginIdAsLong()));
    }

    /**
     * 取消退单
     */
    @PostMapping("/cancelRefund")
    public CzgResult<Boolean> cancelRefund(@RequestBody @Validated CommonRefundDTO param) {
        return CzgResult.success(orderService.cancelRefund(param, StpKit.USER.getLoginIdAsLong(), null));
    }
}