package com.fs.company.controller.qw;

import java.util.Date;
import java.util.List;

import com.fs.common.utils.ServletUtils;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.qw.domain.RedPacket;
import com.fs.qw.service.IRedPacketService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 授权手机号红包配置Controller
 * 
 * @author fs
 * @date 2026-03-17
 */
@RestController
@RequestMapping("/qw/packet")
public class RedPacketController extends BaseController
{
    @Autowired
    private IRedPacketService redPacketService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询授权手机号红包配置列表
     */
    @PreAuthorize("@ss.hasPermi('qw:packet:list')")
    @GetMapping("/list")
    public TableDataInfo list(RedPacket redPacket)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        redPacket.setCompanyId(loginUser.getCompany().getCompanyId());
        List<RedPacket> list = redPacketService.selectRedPacketList(redPacket);
        return getDataTable(list);
    }

    /**
     * 导出授权手机号红包配置列表
     */
    @PreAuthorize("@ss.hasPermi('qw:packet:export')")
    @Log(title = "授权手机号红包配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RedPacket redPacket)
    {
        List<RedPacket> list = redPacketService.selectRedPacketList(redPacket);
        ExcelUtil<RedPacket> util = new ExcelUtil<RedPacket>(RedPacket.class);
        return util.exportExcel(list, "授权手机号红包配置数据");
    }

    /**
     * 获取授权手机号红包配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('qw:packet:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(redPacketService.selectRedPacketById(id));
    }

    /**
     * 新增授权手机号红包配置
     */
    @PreAuthorize("@ss.hasPermi('qw:packet:add')")
    @Log(title = "授权手机号红包配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RedPacket redPacket)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        redPacket.setCompanyId(loginUser.getCompany().getCompanyId());
        redPacket.setCreateBy(loginUser.getUser().getNickName());
        redPacket.setCreateById(loginUser.getUser().getUserId());
        redPacket.setCreateTime(new Date());
        redPacket.setUpdateBy(loginUser.getUser().getNickName());
        redPacket.setUpdateById(loginUser.getUser().getUserId());
        return toAjax(redPacketService.insertRedPacket(redPacket));
    }

    /**
     * 修改授权手机号红包配置
     */
    @PreAuthorize("@ss.hasPermi('qw:packet:edit')")
    @Log(title = "授权手机号红包配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RedPacket redPacket)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        redPacket.setUpdateBy(loginUser.getUser().getNickName());
        redPacket.setUpdateById(loginUser.getUser().getUserId());
        redPacket.setUpdateTime(new Date());
        return toAjax(redPacketService.updateRedPacket(redPacket));
    }

    /**
     * 删除授权手机号红包配置
     */
    @PreAuthorize("@ss.hasPermi('qw:packet:remove')")
    @Log(title = "授权手机号红包配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(redPacketService.deleteRedPacketByIds(ids));
    }
}
