package com.czg.controller.admin;

import com.czg.exception.CzgException;
import com.czg.market.dto.MemberConfigDTO;
import com.czg.market.dto.MemberLevelDTO;
import com.czg.market.service.MemberLevelConfigService;
import com.czg.market.service.MemberOrderService;
import com.czg.market.service.TbMemberConfigService;
import com.czg.market.vo.MemberConfigVO;
import com.czg.market.vo.MemberLevelVO;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.AssertUtil;
import com.czg.validator.group.UpdateGroup;
import jakarta.annotation.Resource;
import jakarta.validation.groups.Default;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Map;

/**
 * 会员配置管理
 *
 * @author ww
 */

@RestController
@RequestMapping("/admin/member")
public class MemberController {
    @Resource
    private TbMemberConfigService memberConfigService;
    @Resource
    private MemberLevelConfigService memberLevelConfigService;
    @Resource
    private MemberOrderService memberOrderService;


    /**
     * 会员购买订单记录 分页
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param key       用户id
     * @return 订单信息
     */
    @GetMapping("/order")
    public CzgResult<Map<String, Object>> memberOrderPage(@RequestParam(required = false) String startTime,
                                                          @RequestParam(required = false) String endTime, @RequestParam(required = false) String key) {
        return CzgResult.success(memberOrderService.pageInfo(startTime, endTime, key));
    }

    /**
     * 配置信息获取
     * 权限标识: activate:list
     */
    @GetMapping
    public CzgResult<MemberConfigVO> detail() {
        return CzgResult.success(memberConfigService.detail(StpKit.USER.getShopId()));
    }

    /**
     * 配置信息修改
     *
     * @return 是否成功
     */
    @PostMapping
    public CzgResult<Boolean> edit(@Validated @RequestBody MemberConfigDTO memberDTO) {
        AssertUtil.isTrue(!StpKit.USER.isMainShop(), "无权限操作");
        return CzgResult.success(memberConfigService.edit(StpKit.USER.getShopId(), memberDTO));
    }


    /**
     * 会员等级添加
     *
     * @return 是否成功
     */
    @PostMapping("/level")
    public CzgResult<Boolean> addLevel(@Validated @RequestBody MemberLevelDTO levelDTO) throws CzgException {
        AssertUtil.isTrue(!StpKit.USER.isMainShop(), "无权限操作");
        return CzgResult.success(memberConfigService.addLevel(StpKit.USER.getShopId(), levelDTO));
    }

    /**
     * 会员等级修改
     *
     * @return 是否成功
     */
    @PutMapping("/level")
    public CzgResult<Boolean> editLevel(@Validated({UpdateGroup.class, Default.class}) @RequestBody MemberLevelDTO levelDTO) throws CzgException {
        AssertUtil.isTrue(!StpKit.USER.isMainShop(), "无权限操作");
        return CzgResult.success(memberConfigService.editLevel(StpKit.USER.getShopId(), levelDTO));
    }

    /**
     * 会员等级删除
     *
     * @return 是否成功
     */
    @DeleteMapping("/level/{id}")
    public CzgResult<Boolean> deleteLevel(@PathVariable Long id) {
        AssertUtil.isTrue(!StpKit.USER.isMainShop(), "无权限操作");
        return CzgResult.success(memberLevelConfigService.removeLevel(id, StpKit.USER.getShopId()));
    }

    /**
     * 会员等级列表
     *
     * @return 是否成功
     */
    @GetMapping("/level/list")
    public CzgResult<ArrayList<MemberLevelVO>> levelList() {
        return CzgResult.success(memberConfigService.listLevel(StpKit.USER.getShopId()));
    }


    /**
     * 会员等级详情
     *
     * @return 是否成功
     */
    @PutMapping("/level/detail")
    public CzgResult<ArrayList<MemberLevelVO>> levelList(@RequestParam Integer id) {
        AssertUtil.isTrue(!StpKit.USER.isMainShop(), "无权限操作");
        return CzgResult.success(memberConfigService.listLevel(StpKit.USER.getShopId()));
    }


}
