package com.czg.controller.user;

import com.czg.account.dto.shopuser.ShopUserAddDTO;
import com.czg.account.dto.shopuser.ShopUserDetailDTO;
import com.czg.account.dto.shopuser.ShopUserVipCardDTO;
import com.czg.account.entity.MemberPointsLog;
import com.czg.account.entity.ShopUser;
import com.czg.account.entity.ShopUserFlow;
import com.czg.account.service.MemberPointsLogService;
import com.czg.account.service.PointsExchangeRecordService;
import com.czg.account.service.ShopUserFlowService;
import com.czg.account.service.ShopUserService;
import com.czg.annotation.Debounce;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 店铺会员相关
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/user/shopUser")
public class UShopUserController {
    @Resource
    private ShopUserService shopUserService;
    @Resource
    private ShopUserFlowService shopUserFlowService;
    @Resource
    private PointsExchangeRecordService pointsExchangeRecordService;
    @Resource
    private MemberPointsLogService memberPointsLogService;

    /**
     * 获取当前店铺会员信息
     * 请求头必须携带shopId, 不携带则会失败
     *
     * @return 店铺会员信息
     */
    @GetMapping
    public CzgResult<ShopUserDetailDTO> get(Long shopId) {
        return CzgResult.success(shopUserService.getInfo(shopId == null ? StpKit.USER.getHeadShopId() : shopId, StpKit.USER.getLoginIdAsLong()));
    }

    /**
     * 获取当前用户所有店铺会员信息
     *
     * @return 店铺会员信息列表
     */
    @GetMapping("/vipCard")
    public CzgResult<Page<ShopUserVipCardDTO>> list() {
        return CzgResult.success(shopUserService.vipCard(StpKit.USER.getLoginIdAsLong()));
    }

    /**
     * 获取动态会员码
     *
     * @return 店铺会员信息列表
     */
    @GetMapping("/code")
    public CzgResult<String> code(Long shopId) {
        return shopUserService.getCode(StpKit.USER.getLoginIdAsLong(), shopId == null ? StpKit.USER.getHeadShopId() : shopId);
    }

    /**
     * 加入会员
     * @param shopUserAddDTO 会员信息
     * @return 是否成功
     */
    @PostMapping
    @Debounce("#shopUserAddDTO.phone")
    public CzgResult<Boolean> join(@RequestBody @Validated ShopUserAddDTO shopUserAddDTO) {
        return CzgResult.success(shopUserService.join(StpKit.USER.getHeadShopId(), StpKit.USER.getLoginIdAsLong(), shopUserAddDTO));
    }


    /**
     * 获取余额余额明细
     * @return 分页数据
     */
    @GetMapping("/moneyRecord")
    public CzgResult<Page<ShopUserFlow>> getMoneyRecord() {
        return CzgResult.success(shopUserFlowService.page(PageUtil.buildPage(), new QueryWrapper().eq(ShopUserFlow::getShopId, StpKit.USER.getShopId())
                .eq(ShopUserFlow::getUserId, StpKit.USER.getLoginIdAsLong()).orderBy(ShopUserFlow::getId, false)));
    }

    /**
     * 获取余额余额明细详情
     * @param id 明细id
     * @return 明细数据
     */
    @GetMapping("/moneyRecord/detail")
    public CzgResult<ShopUserFlow> getMoneyRecordDetail(@RequestParam Long id) {
        return CzgResult.success(shopUserFlowService.getOne(new QueryWrapper().eq(ShopUserFlow::getShopId, StpKit.USER.getShopId())
                .eq(ShopUserFlow::getUserId, StpKit.USER.getLoginIdAsLong()).eq(ShopUserFlow::getId, id)));
    }

    /**
     * 获取积分明细
     * @return 分页数据
     */
    @GetMapping("/pointsRecord")
    public CzgResult<Page<MemberPointsLog>> getPointsRecord() {
        ShopUser shopUser = shopUserService.getOne(new QueryWrapper().eq(ShopUser::getShopId, StpKit.USER.getShopId()).eq(ShopUser::getUserId, StpKit.USER.getLoginIdAsLong()));
        return CzgResult.success(memberPointsLogService.page(PageUtil.buildPage(), new QueryWrapper().eq(MemberPointsLog::getShopId, StpKit.USER.getShopId())
                .eq(MemberPointsLog::getShopUserId, shopUser.getId()).orderBy(MemberPointsLog::getId, false)));
    }

    /**
     * 获取积分明细详情
     * @param id 明细id
     * @return 明细数据
     */
    @GetMapping("/pointsRecord/detail")
    public CzgResult<MemberPointsLog> getPointsRecordDetail(@RequestParam Long id) {
        ShopUser shopUser = shopUserService.getOne(new QueryWrapper().eq(ShopUser::getShopId, StpKit.USER.getShopId()).eq(ShopUser::getUserId, StpKit.USER.getLoginIdAsLong()));
        return CzgResult.success(memberPointsLogService.getOne(new QueryWrapper().eq(MemberPointsLog::getShopId, StpKit.USER.getShopId())
                .eq(MemberPointsLog::getShopUserId, shopUser.getId()).eq(MemberPointsLog::getId, id)));
    }


    public static void main(String[] args) {

    }
}
