package com.ruoyi.web.bst;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.bst.area.service.AreaValidator;
import com.ruoyi.bst.areaJoin.domain.enums.AreaJoinPermission;
import com.ruoyi.bst.vip.domain.Vip;
import com.ruoyi.bst.vip.domain.VipQuery;
import com.ruoyi.bst.vip.domain.VipVO;
import com.ruoyi.bst.vip.service.VipService;
import com.ruoyi.bst.vip.service.VipValidator;
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.page.TableDataInfo;
import com.ruoyi.common.core.validate.ValidGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.LogBizType;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 卡券Controller
 *
 * @author ruoyi
 * @date 2025-05-23
 */
@RestController
@RequestMapping("/bst/vip")
public class VipController extends BaseController
{
    @Autowired
    private VipService vipService;

    @Autowired
    private VipValidator vipValidator;

    @Autowired
    private AreaValidator areaValidator;

    /**
     * 查询卡券列表
     */
    @PreAuthorize("@ss.hasPermi('bst:vip:list')")
    @GetMapping("/list")
    public TableDataInfo list(VipQuery query) {
        startPage();
        startOrderBy();
        query.setScope(true);
        query.addAreaPermission(AreaJoinPermission.VIP_VIEW.getCode());
        List<VipVO> list = vipService.selectVipList(query);
        return getDataTable(list);
    }

    /**
     * 导出卡券列表
     */
    @PreAuthorize("@ss.hasPermi('bst:vip:export')")
    @Log(title = "卡券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VipQuery query)
    {
        query.setScope(true);
        query.addAreaPermission(AreaJoinPermission.VIP_VIEW.getCode());
        List<VipVO> list = vipService.selectVipList(query);
        ExcelUtil<VipVO> util = new ExcelUtil<VipVO>(VipVO.class);
        util.exportExcel(response, list, "卡券数据");
    }

    /**
     * 获取卡券详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:vip:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return success(vipService.selectVipById(id, true));
    }

    /**
     * 新增卡券
     */
    @PreAuthorize("@ss.hasPermi('bst:vip:add')")
    @Log(title = "卡券", businessType = BusinessType.INSERT, bizIdName = "arg0", bizType = LogBizType.VIP)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) Vip vip){
        // 权限校验
        if (vip.getAreaId() != null) {
            ServiceUtil.assertion(!areaValidator.canEdit(vip.getAreaId()), "您无权操作ID为%s的运营区", vip.getAreaId());
        }
        return toAjax(vipService.insertVip(vip));
    }

    /**
     * 修改卡券
     */
    @PreAuthorize("@ss.hasPermi('bst:vip:edit')")
    @Log(title = "卡券", businessType = BusinessType.UPDATE, bizIdName = "arg0", bizType = LogBizType.VIP)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Update.class) Vip vip) {
        // 权限校验
        if (vip.getAreaId() != null) {
            ServiceUtil.assertion(!areaValidator.canEdit(vip.getAreaId()), "您无权操作ID为%s的运营区", vip.getAreaId());
        }
        if (vip.getId() != null) {
            ServiceUtil.assertion(!vipValidator.canEdit(vip.getId()), "您无权操作ID为%s的卡券", vip.getId());
        }
        return toAjax(vipService.updateVip(vip));
    }

    /**
     * 删除卡券
     */
    @PreAuthorize("@ss.hasPermi('bst:vip:remove')")
    @Log(title = "卡券", businessType = BusinessType.DELETE, bizIdName = "arg0", bizType = LogBizType.VIP)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        ServiceUtil.assertion(!vipValidator.canEdit(ids), "您无权操作ID为%s的卡券", ids);
        return toAjax(vipService.logicDel(ids));
    }
}
