package com.czg.controller.user;

import com.czg.account.vo.PointsShopListVO;
import com.czg.market.entity.MkPointsConfig;
import com.czg.market.entity.MkPointsUser;
import com.czg.market.entity.MkPointsUserRecord;
import com.czg.market.service.MkPointsConfigService;
import com.czg.market.service.MkPointsUserRecordService;
import com.czg.market.service.MkPointsUserService;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 积分配置
 *
 * @author ww
 */
@RestController
@RequestMapping("/user/point/")
public class UPointsController {

    @Resource
    private MkPointsConfigService pointsConfigService;
    @Resource
    private MkPointsUserService pointsUserService;
    @Resource
    private MkPointsUserRecordService pointsUserRecordService;

    /**
     * 获取配置
     */
    @GetMapping("pointsConfig")
    public CzgResult<MkPointsConfig> getPointsConfig(@RequestParam(required = false) Long shopId) {
        if (shopId == null) {
            shopId = StpKit.USER.getShopId();
        }
        return CzgResult.success(pointsConfigService.getById(shopId));
    }

    /**
     * 获取用户积分 包括配置信息
     * {
     * "pointsConfig": 配置信息,
     * "pointsUser": 用户积分信息
     * }
     */
    @GetMapping("userPoints")
    public CzgResult<Map<String, Object>> userPoints(@RequestParam(required = false) Long shopUserId) {
        Long shopId = StpKit.USER.getShopId();
        Map<String, Object> result = new HashMap<>(2);
        MkPointsConfig pointsConfig = pointsConfigService.getById(shopId);
        MkPointsUser pointsUser = pointsUserService.getOne(QueryWrapper.create().eq(MkPointsUser::getShopId, shopId).eq(MkPointsUser::getShopUserId, shopUserId));
        result.put("pointsConfig", pointsConfig == null ? "" : pointsConfig);
        result.put("pointsUser", pointsUser == null ? "" : pointsUser);
        return CzgResult.success(result);
    }

    /**
     * 获取用户所有门店下积分列表
     */
    @GetMapping("/shopList")
    public CzgResult<List<PointsShopListVO>> pointsShopList(@RequestParam(required = false) String shopName) {
        return CzgResult.success(pointsUserService.pointsShopList(StpKit.USER.getLoginIdAsLong(), shopName));
    }

    /**
     * 获取积分明细
     *
     * @return 分页数据
     */
    @GetMapping("/userRecord")
    public CzgResult<Page<MkPointsUserRecord>> getPointsRecord(@RequestParam(required = false, defaultValue = "1") Integer page,
                                                               @RequestParam(required = false, defaultValue = "10") Integer size,
                                                               @RequestParam(required = false) Long id) {

        return CzgResult.success(pointsUserRecordService.getPointsUserRecord(page, size, id));
    }
}
