package com.ruoyi.web.bst;

import com.ruoyi.bst.lightingNum.domain.LightingNum;
import com.ruoyi.bst.lightingNum.domain.LightingNumQuery;
import com.ruoyi.bst.lightingNum.domain.LightingNumVO;
import com.ruoyi.bst.lightingNum.domain.dto.GiftLightingNumDTO;
import com.ruoyi.bst.lightingNum.service.LightingNumService;
import com.ruoyi.bst.store.service.StoreValidator;
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.enums.BusinessType;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.user.service.UserValidator;
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.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 爆灯次数列表Controller
 *
 * @author ruoyi
 * @date 2025-04-26
 */
@RestController
@RequestMapping("/bst/lightingNum")
public class LightingNumController extends BaseController
{
    @Autowired
    private LightingNumService lightingNumService;
    @Autowired
    private StoreValidator storeValidator;

    /**
     * 查询爆灯次数列表列表
     */
    @PreAuthorize("@ss.hasPermi('bst:lightingNum:list')")
    @GetMapping("/list")
    public TableDataInfo list(LightingNumQuery query)
    {
        startPage();
        startOrderBy();
        query.setScope(true);
        List<LightingNumVO> list = lightingNumService.selectLightingNumList(query);
        return getDataTable(list);
    }

    /**
     * 导出爆灯次数列表列表
     */
    @PreAuthorize("@ss.hasPermi('bst:lightingNum:export')")
    @Log(title = "爆灯次数列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LightingNumQuery query)
    {
        query.setScope(true);
        List<LightingNumVO> list = lightingNumService.selectLightingNumList(query);
        ExcelUtil<LightingNumVO> util = new ExcelUtil<LightingNumVO>(LightingNumVO.class);
        util.exportExcel(response, list, "爆灯次数列表数据");
    }

    /**
     * 获取爆灯次数列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:lightingNum:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(lightingNumService.selectLightingNumById(id,true));
    }

    /**
     * 修改爆灯次数
     */
    @PreAuthorize("@ss.hasPermi('bst:lightingNum:edit')")
    @Log(title = "修改爆灯次数", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LightingNum lightingNum)
    {
        return toAjax(lightingNumService.updateLightingNum(lightingNum));
    }

    /**
     * 赠送用户爆灯次数
     */
    @PreAuthorize("@ss.hasPermi('bst:lightingNum:gift')")
    @Log(title = "赠送用户爆灯次数", businessType = BusinessType.UPDATE)
    @PutMapping("/giftLightingNum")
    public AjaxResult giftLightingNum(@Valid @RequestBody GiftLightingNumDTO dto)
    {
        // 权限校验
        if (!storeValidator.canEdit(dto.getStoreId())){
            return error("您无权限操作当前店铺信息");
        }
        return toAjax(lightingNumService.giftLightingNum(dto));
    }


    /**
     * 查询用户在店铺下的爆灯次数
     */
    @PreAuthorize("@ss.hasPermi('bst:lightingNum:userLightingNum')")
    @GetMapping("/userLightingNum")
    public AjaxResult userLightingNum(LightingNumQuery query)
    {
        query.setScope(true);
        List<LightingNumVO> list = lightingNumService.selectLightingNumList(query);
        return success(CollectionUtils.firstElement(list));
    }
}
