package com.fs.company.controller.course;

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.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.course.domain.FsMessTemp;
import com.fs.course.service.IFsMessTempService;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 群发模板Controller
 *
 * @author fs
 * @date 2025-01-09
 */
@RestController
@RequestMapping("/temp")
public class FsMessTempController extends BaseController
{
    @Autowired
    private IFsMessTempService fsMessTempService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询群发模板列表
     */
    @PreAuthorize("@ss.hasPermi('shop:temp:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsMessTemp fsMessTemp)
    {
        startPage();
        List<FsMessTemp> list = fsMessTempService.selectFsMessTempList(fsMessTemp);
        return getDataTable(list);
    }

    /**
     * 导出群发模板列表
     */
    @PreAuthorize("@ss.hasPermi('shop:temp:export')")
    @Log(title = "群发模板", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsMessTemp fsMessTemp)
    {
        List<FsMessTemp> list = fsMessTempService.selectFsMessTempList(fsMessTemp);
        ExcelUtil<FsMessTemp> util = new ExcelUtil<FsMessTemp>(FsMessTemp.class);
        return util.exportExcel(list, "群发模板数据");
    }

    /**
     * 获取群发模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:temp:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fsMessTempService.selectFsMessTempById(id));
    }

    /**
     * 新增群发模板
     */
    @PreAuthorize("@ss.hasPermi('shop:temp:add')")
    @Log(title = "群发模板", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody FsMessTemp fsMessTemp)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsMessTemp.setCompanyId(loginUser==null?null:loginUser.getCompany().getCompanyId());
        fsMessTempService.insertFsMessTemp(fsMessTemp);
        return R.ok();
    }

    /**
     * 修改群发模板
     */
    @PreAuthorize("@ss.hasPermi('shop:temp:edit')")
    @Log(title = "群发模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsMessTemp fsMessTemp)
    {
        return toAjax(fsMessTempService.updateFsMessTemp(fsMessTemp));
    }

    /**
     * 删除群发模板
     */
    @PreAuthorize("@ss.hasPermi('shop:temp:remove')")
    @Log(title = "群发模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fsMessTempService.deleteFsMessTempByIds(ids));
    }
}
