package com.ktg.iscs.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.pojo.CommonResult;
import com.ktg.common.utils.poi.ExcelUtil;
import com.ktg.iscs.domain.IsMailTemplate;
import com.ktg.iscs.service.IIsMailTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 系统邮件模版Controller
 *
 * @author cgj
 * @date 2025-01-15
 */
@Api(tags = "系统邮件模版")
@RestController
@RequestMapping("/iscs/template")
public class IsMailTemplateController extends BaseController {
    @Autowired
    private IIsMailTemplateService isMailTemplateService;

    @ApiOperation("查询系统邮件模版-分页")
    @Parameters({@Parameter(name = "page", description = "Page"), @Parameter(name = "isMailTemplate", description = "实体参数")})
    @PreAuthorize("@ss.hasPermi('iscs:template:list')")
    @GetMapping("/getIsMailTemplatePage")
    public CommonResult<Page<IsMailTemplate>> getIsMailTemplatePage(Page<IsMailTemplate> page, IsMailTemplate isMailTemplate) {
        Page<IsMailTemplate> result = isMailTemplateService.getIsMailTemplatePage(page, isMailTemplate);
        return CommonResult.success(result);
    }

    @ApiOperation("导出系统邮件模版列表")
    @Parameter(name = "isMailTemplate", description = "实体参数")
    @PreAuthorize("@ss.hasPermi('iscs:template:export')")
    @Log(title = "系统邮件模版", businessType = BusinessType.EXPORT)
    @PostMapping("/exportIsMailTemplate")
    public void exportIsMailTemplate(HttpServletResponse response, IsMailTemplate isMailTemplate) {
        Page<IsMailTemplate> page = new Page<>();
        page.setSize(-1);
        page.setCurrent(1);
        List<IsMailTemplate> list = isMailTemplateService.page(page, Wrappers.<IsMailTemplate>lambdaQuery().orderByDesc(IsMailTemplate::getTemplateCode)).getRecords();
        ExcelUtil<IsMailTemplate> util = new ExcelUtil<IsMailTemplate>(IsMailTemplate.class);
        util.exportExcel(response, list, "系统邮件模版数据");
    }

    @ApiOperation("获取系统邮件模版详细信息")
    @Parameter(name = "templateCode", description = "templateCode")
    @PreAuthorize("@ss.hasPermi('iscs:template:query')")
    @GetMapping(value = "/selectIsMailTemplateById")
    public CommonResult<IsMailTemplate> selectIsMailTemplateById(Long templateId) {
        return CommonResult.success(isMailTemplateService.getById(templateId));
    }

    @ApiOperation("新增系统邮件模版")
    @PreAuthorize("@ss.hasPermi('iscs:template:add')")
    @Log(title = "系统邮件模版", businessType = BusinessType.INSERT)
    @PostMapping("/insertIsMailTemplate")
    public CommonResult<Boolean> insertIsMailTemplate(@RequestBody @Parameter(name = "isMailTemplate", description = "新增数据类,放到body") IsMailTemplate isMailTemplate) {
        return CommonResult.success(isMailTemplateService.insertIsMailTemplate(isMailTemplate));
    }

    @ApiOperation("修改系统邮件模版")
    @PreAuthorize("@ss.hasPermi('iscs:template:edit')")
    @Log(title = "系统邮件模版", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsMailTemplate")
    public CommonResult<Boolean> updateIsMailTemplate(@RequestBody @Parameter(name = "isMailTemplate", description = "修改数据类,放到body") IsMailTemplate isMailTemplate) {
        return CommonResult.success(isMailTemplateService.updateIsMailTemplate(isMailTemplate));
    }

    @ApiOperation("删除系统邮件模版")
    @PreAuthorize("@ss.hasPermi('iscs:template:remove')")
    @Log(title = "系统邮件模版", businessType = BusinessType.DELETE)
    @PostMapping("/deleteIsMailTemplateByTemplateCodes")
    public CommonResult<Boolean> deleteIsMailTemplateByTemplateCodes(String templateIds) {
        return CommonResult.success(isMailTemplateService.deleteIsMailTemplateByTemplateCodes(templateIds));
    }
}
