package com.czg.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.market.dto.MkPointsConfigDTO;
import com.czg.market.dto.MkPointsUserDTO;
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.czg.utils.CzgStrUtils;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.Pattern;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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


/**
 * 积分配置
 *
 * @author ww
 */
@RestController
@RequestMapping("/admin/points/")
public class PointsConfigController {

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

    /**
     * 积分:配置:详情
     */
    @GetMapping("/config")
    @SaAdminCheckPermission(parentName = "积分相关", value = "points:config:info", name = "积分-配置-详情")
    public CzgResult<MkPointsConfig> getPointsBasicSetting() {
        MkPointsConfig entity = pointsConfigService.getById(StpKit.USER.getShopId());
        return CzgResult.success(entity);
    }

    /**
     * 积分:配置:新增/更新
     */
    @PostMapping("/config")
    @SaAdminCheckPermission(parentName = "积分相关", value = "points:config:up", name = "积分-配置-新增/更新")
    public CzgResult<Void> savePointsBasicSetting(@RequestBody @Validated MkPointsConfigDTO dto) {
        Long shopId = StpKit.USER.getShopId();
        dto.setShopId(shopId);
        MkPointsConfig entity = BeanUtil.copyProperties(dto, MkPointsConfig.class);
        MkPointsConfig record = pointsConfigService.getById(shopId);
        if (record == null) {
            pointsConfigService.save(entity);
        } else {
            pointsConfigService.updateById(entity);
        }
        return CzgResult.success();
    }

    /**
     * 积分:用户:列表
     */
    @GetMapping("/userPage")
    @SaAdminCheckPermission(parentName = "积分相关", value = "points:user:list", name = "积分-用户-积分列表")
    public CzgResult<Page<MkPointsUserDTO>> getPointsUserPage(
            @RequestParam(required = false) @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确") String phone,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size) {
        return CzgResult.success(pointsUserService.getPointsUserPage(CzgStrUtils.getStrOrNull(phone), page, size));
    }

    /**
     * 积分:用户:积分详情
     */
    @GetMapping("/userRecord")
    @SaAdminCheckPermission(parentName = "积分相关", value = "points:user:record", name = "积分-用户-积分记录")
    public CzgResult<Page<MkPointsUserRecord>> getPointsUserRecord(@RequestParam(required = false, defaultValue = "1") Integer page,
                                                                   @RequestParam(required = false, defaultValue = "10") Integer size,
                                                                   @RequestParam(required = false) Long id) {
        return CzgResult.success(userRecordService.pageByPointsUserId(page, size, id));
    }

    /**
     * 获取用户积分 包括配置信息
     * {
     * "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);
    }

}