package com.czg.controller.user;

import com.czg.market.entity.MkPointsGoods;
import com.czg.market.entity.MkPointsGoodsRecord;
import com.czg.market.entity.ShopCoupon;
import com.czg.market.service.MkPointsGoodsRecordService;
import com.czg.market.service.MkPointsGoodsService;
import com.czg.market.service.ShopCouponService;
import com.czg.order.dto.CommonRefundDTO;
import com.czg.order.dto.LtPayOtherDTO;
import com.czg.order.service.PointsGoodPayService;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.ServletUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
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/pointGoods/")
public class UPointGoodsController {

    @Resource
    private MkPointsGoodsService pointsGoodsService;
    @Resource
    private MkPointsGoodsRecordService goodsRecordService;
    @Resource
    private PointsGoodPayService goodPayService;
    @Resource
    private ShopCouponService shopCouponService;

    /**
     * 商品列表
     * {
     * "pointsGoods": 商品信息,
     * "pointsUser": 用户积分信息
     * }
     */
    @GetMapping("/page")
    public CzgResult<Map<String, Object>> getPointsGoodsSettingPage(@RequestParam(defaultValue = "1", required = false) Integer page,
                                                                    @RequestParam(defaultValue = "10", required = false) Integer size,
                                                                    @RequestParam(required = false) String goodsCategory,
                                                                    Long shopId) {
        Map<String, Object> data = pointsGoodsService.getPointsGoodsPageByUser(page, size, shopId, goodsCategory, StpKit.USER.getLoginIdAsLong());
        return CzgResult.success(data);
    }

    /**
     * 商品详情
     */
    @GetMapping("/{id}")
    public CzgResult<MkPointsGoods> getPointsGoodsSettingById(@PathVariable("id") Long id) {
        MkPointsGoods goods = pointsGoodsService.getById(id);
        if ("优惠券".equals(goods.getGoodsCategory())) {
            ShopCoupon one = shopCouponService.getOne(QueryWrapper.create().eq(ShopCoupon::getId, goods.getCouponId())
                    .eq(ShopCoupon::getShopId, goods.getShopId())
                    .eq(ShopCoupon::getStatus, 1)
                    .eq(ShopCoupon::getIsDel, 0));
            goods.setCouponInfo(one);
        }
        return CzgResult.success(goods);
    }

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

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

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

    /**
     * 我的兑换记录
     */
    @GetMapping("/record/page")
    public CzgResult<Page<MkPointsGoodsRecord>> getGoodsRecordPage(@RequestParam(defaultValue = "1", required = false) int page,
                                                                   @RequestParam(defaultValue = "10", required = false) int size,
                                                                   @RequestParam(required = false) String status,
                                                                   @RequestParam Long shopId) {
        Page<MkPointsGoodsRecord> pages = goodsRecordService.userGoodsRecordPage(page, size, shopId, status, StpKit.USER.getLoginIdAsLong());
        return CzgResult.success(pages);
    }

    /**
     * 兑换记录详情
     */
    @GetMapping("/record/one")
    public CzgResult<MkPointsGoodsRecord> getGoodsRecordOne(@RequestParam Long id,
                                                            @RequestParam Long shopId) {
        QueryWrapper queryWrapper = QueryWrapper.create().eq(MkPointsGoodsRecord::getId, id).eq(MkPointsGoodsRecord::getShopId, shopId).eq(MkPointsGoodsRecord::getUserId, StpKit.USER.getLoginIdAsLong());
        MkPointsGoodsRecord one = goodsRecordService.getOne(queryWrapper);
        if (one != null) {
            one.fillCouponInfo();
        }
        return CzgResult.success(one);
    }
}