package com.fs.company.controller.qw;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.course.domain.LuckyBag;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.qw.service.ILuckyBagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 福袋管理
 *
 * @author fs
 * @date 2024-12-02
 */
@RestController
@RequestMapping("/qw/luckyBag")
public class LuckyBagController extends BaseController
{
    @Autowired
    private ILuckyBagService luckyBagService;
    @Autowired
    private TokenService tokenService;

//    @PreAuthorize("@ss.hasPermi('course:reward:list')")
    @GetMapping("/list")
    public TableDataInfo list(LuckyBag reward)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        reward.setCompanyId(String.valueOf(loginUser.getCompany().getCompanyId()));
        List<LuckyBag> list = luckyBagService.selectLuckyBagList(reward);
        return getDataTable(list);
    }


//    @PreAuthorize("@ss.hasPermi('luckybag:reward:add')")
    @Log(title = "奖励配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LuckyBag reward) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        reward.setCreateName(loginUser.getUser().getNickName());
        reward.setCompanyId(String.valueOf(loginUser.getCompany().getCompanyId()));
        reward.setUpdateBy(String.valueOf(loginUser.getUser().getUserId()));
        reward.setCreateId(loginUser.getUser().getUserId());
        reward.setCreateTime(new Date());
        reward.setUpdateId(loginUser.getUser().getUserId());
        reward.setUpdateTime(new Date());
       return toAjax(luckyBagService.add(reward));
    }


//    @PreAuthorize("@ss.hasPermi('course:reward:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        LuckyBag reward = new LuckyBag();
        reward.setId(id);
        return AjaxResult.success(luckyBagService.selectLuckyBagList(reward));
    }

//    @PreAuthorize("@ss.hasPermi('course:reward:edit')")
    @Log(title = "修改福袋配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LuckyBag reward) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        reward.setUpdateId(loginUser.getUser().getUserId());
        reward.setUpdateTime(new Date());
        return toAjax(luckyBagService.updateLuckyBag(reward));
    }

//    @PreAuthorize("@ss.hasPermi('course:reward:remove')")
    @Log(title = "奖励配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(luckyBagService.deleteLuckyBagByIds(ids));
    }
}
