package com.czg.controller.user;

import cn.hutool.core.bean.BeanUtil;
import com.czg.account.dto.shopuser.ShopUserAddDTO;
import com.czg.account.dto.shopuser.ShopUserDetailDTO;
import com.czg.account.dto.shopuser.ShopUserEditDTO;
import com.czg.account.dto.shopuser.ShopUserVipCardDTO;
import com.czg.account.entity.ShopInfo;
import com.czg.account.entity.ShopUser;
import com.czg.account.entity.ShopUserFlow;
import com.czg.account.service.ShopUserFlowService;
import com.czg.account.service.UShopUserService;
import com.czg.account.vo.ShopUserFlowInfoVO;
import com.czg.annotation.Debounce;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.MyQueryWrapper;
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 UShopUserService shopUserService;
    @Resource
    private ShopUserFlowService shopUserFlowService;

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

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

    /**
     * 确认信息
     * @return 是否成功
     */
    @PostMapping("/confirm")
    public CzgResult<Boolean> confirm(@RequestBody @Validated ShopUserEditDTO shopUserEditDTO) {
        ShopUser shopUser = new ShopUser();
        BeanUtil.copyProperties(shopUserEditDTO, shopUser);
        shopUserService.getService().updateInfo(shopUser);
        return CzgResult.success();
    }



    /**
     * 获取余额余额明细
     * @return 分页数据
     */
    @GetMapping("/moneyRecord")
    public CzgResult<Page<ShopUserFlowInfoVO>> getMoneyRecord(@RequestParam Long shopId) {
        ShopUser shopUserInfo = shopUserService.getShopUserInfo(shopId, StpKit.USER.getLoginIdAsLong());
        return CzgResult.success(shopUserFlowService.pageAs(PageUtil.buildPage(), new MyQueryWrapper().eq(ShopUserFlow::getShopUserId, shopUserInfo.getId())
                        .selectAll(ShopUserFlow.class)
                .eq(ShopUserFlow::getUserId, StpKit.USER.getLoginIdAsLong()).leftJoin(ShopInfo.class).on(ShopUserFlow::getShopId, ShopInfo::getId)
                        .select(ShopInfo::getShopName)
                .orderBy(ShopUserFlow::getId, false), ShopUserFlowInfoVO.class));
    }

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