package com.ruoyi.web.controller.mch;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ValidGroup;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ss.vipLevel.domain.VipLevel;
import com.ruoyi.ss.vipLevel.domain.VipLevelQuery;
import com.ruoyi.ss.vipLevel.domain.VipLevelVO;
import com.ruoyi.ss.vipLevel.service.VipLevelService;
import com.ruoyi.ss.vipLevel.service.VipLevelValidator;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wjh
 * 2024/12/9
 */
@RestController
@RequestMapping("/mch/vipLevel")
public class MchVipLevelController extends BaseController {

    @Autowired
    private VipLevelService vipLevelService;

    @Autowired
    private VipLevelValidator vipLevelValidator;

    @ApiOperation("商户获取VIP等级列表")
    @GetMapping("/list")
    public TableDataInfo list(VipLevelQuery query) {
        startPage();
        query.setMchId(getUserId());
        List<VipLevelVO> list = vipLevelService.selectVipLevelList(query);
        return getDataTable(list);
    }

    @ApiModelProperty("获取VIP等级明细")
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        VipLevelVO vo = vipLevelService.selectVipLevelById(id);
        if (!vipLevelValidator.canView(vo, getUserId())) {
            return error("您无权查看该信息");
        }
        return success(vo);
    }

    @ApiModelProperty("新增VIP等级")
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.FrontCreate.class) VipLevel data) {
        data.setMchId(getUserId());
        return toAjax(vipLevelService.insertVipLevel(data));
    }

    @ApiModelProperty("修改VIP等级")
    @PutMapping
    public AjaxResult update(@RequestBody @Validated(ValidGroup.FrontUpdate.class) VipLevel data) {
        if (!vipLevelValidator.canOpera(data.getId(), getUserId())) {
            return error("您无权操作该数据");
        }
        return toAjax(vipLevelService.updateVipLevel(data));
    }

    @ApiModelProperty("删除VIP等级")
    @DeleteMapping("/{ids}")
    public AjaxResult del(@PathVariable List<Long> ids) {
        if (!vipLevelValidator.canOperaAll(ids, getUserId())) {
            return error("您无权操作该数据");
        }
        return toAjax(vipLevelService.deleteVipLevelByIds(ids));
    }

}
