package com.ktg.iscs.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
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.IsMailNotifyConfig;
import com.ktg.iscs.service.IIsMailNotifyConfigService;
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.Arrays;
import java.util.List;

/**
 * 系统邮件提醒周期配置Controller
 *
 * @author cgj
 * @date 2025-01-15
 */
@Api(tags = "系统邮件提醒周期配置")
@RestController
@RequestMapping("/iscs/notify")
public class IsMailNotifyConfigController extends BaseController
{
    @Autowired
    private IIsMailNotifyConfigService isMailNotifyConfigService;

    @ApiOperation("查询系统邮件提醒周期配置-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "isMailNotifyConfig", description = "实体参数")
    })
    @PreAuthorize("@ss.hasPermi('iscs:notify:list')")
    @GetMapping("/getIsMailNotifyConfigPage")
    public CommonResult<Page<IsMailNotifyConfig>> getIsMailNotifyConfigPage(Page<IsMailNotifyConfig> page, IsMailNotifyConfig isMailNotifyConfig)
    {
        Page<IsMailNotifyConfig> result = isMailNotifyConfigService.getIsMailNotifyConfigPage(page, isMailNotifyConfig);
        return CommonResult.success(result);
    }

    @ApiOperation("导出系统邮件提醒周期配置列表")
    @Parameter(name = "isMailNotifyConfig", description = "实体参数")
    @PreAuthorize("@ss.hasPermi('iscs:notify:export')")
    @Log(title = "系统邮件提醒周期配置", businessType = BusinessType.EXPORT)
    @PostMapping("/exportIsMailNotifyConfig")
    public void exportIsMailNotifyConfig(HttpServletResponse response, IsMailNotifyConfig isMailNotifyConfig)
    {
        Page<IsMailNotifyConfig> page = new Page<>();
        page.setSize(-1);
        page.setCurrent(1);
        List<IsMailNotifyConfig> list = isMailNotifyConfigService.page(page, Wrappers.<IsMailNotifyConfig>lambdaQuery()
                .orderByDesc(IsMailNotifyConfig::getConfigId)).getRecords();
        ExcelUtil<IsMailNotifyConfig> util = new ExcelUtil<IsMailNotifyConfig>(IsMailNotifyConfig.class);
        util.exportExcel(response, list, "系统邮件提醒周期配置数据");
    }

    @ApiOperation("获取系统邮件提醒周期配置详细信息")
    @Parameter(name = "configId", description = "configId")
    @PreAuthorize("@ss.hasPermi('iscs:notify:query')")
    @GetMapping(value = "/selectIsMailNotifyConfigById")
    public CommonResult<IsMailNotifyConfig> selectIsMailNotifyConfigById(Long configId)
    {
        return CommonResult.success(isMailNotifyConfigService.getById(configId));
    }

    @ApiOperation("新增系统邮件提醒周期配置")
    @PreAuthorize("@ss.hasPermi('iscs:notify:add')")
    @Log(title = "系统邮件提醒周期配置", businessType = BusinessType.INSERT)
    @PostMapping("/insertIsMailNotifyConfig")
    public CommonResult<Boolean> insertIsMailNotifyConfig(@RequestBody @Parameter(name = "isMailNotifyConfig", description = "新增数据类,放到body") IsMailNotifyConfig isMailNotifyConfig)
    {
        return CommonResult.success(isMailNotifyConfigService.save(isMailNotifyConfig));
    }

    @ApiOperation("修改系统邮件提醒周期配置")
    @PreAuthorize("@ss.hasPermi('iscs:notify:edit')")
    @Log(title = "系统邮件提醒周期配置", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsMailNotifyConfig")
    public CommonResult<Boolean> updateIsMailNotifyConfig(@RequestBody @Parameter(name = "isMailNotifyConfig", description = "修改数据类,放到body") IsMailNotifyConfig isMailNotifyConfig)
    {
        return CommonResult.success(isMailNotifyConfigService.updateById(isMailNotifyConfig));
    }

    @ApiOperation("删除系统邮件提醒周期配置")
    @PreAuthorize("@ss.hasPermi('iscs:notify:remove')")
    @Log(title = "系统邮件提醒周期配置", businessType = BusinessType.DELETE)
	@PostMapping("/deleteIsMailNotifyConfigByConfigIds")
    public CommonResult<Boolean> deleteIsMailNotifyConfigByConfigIds(String configIds)
    {
        Assert.notBlank(configIds, "请选择需要删除的数据!");
        Long[] longIds = Convert.toLongArray(configIds);
        return CommonResult.success(isMailNotifyConfigService.removeBatchByIds(Arrays.asList(longIds)));
    }

    @ApiOperation("修改自动创建周期检查计划")
    @PreAuthorize("@ss.hasPermi('iscs:notify:edit')")
    @Log(title = "修改自动创建周期检查计划", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAutomaticConfig")
    public CommonResult<Boolean> updateAutomaticConfig(@RequestBody @Parameter(name = "isMailNotifyConfig", description = "修改数据类,放到body") IsMailNotifyConfig isMailNotifyConfig)
    {
        return CommonResult.success(isMailNotifyConfigService.updateAutomaticConfig(isMailNotifyConfig));
    }

    @ApiOperation("获取系统邮件提醒周期配置详细信息code")
    @Parameter(name = "templateCode", description = "templateCode")
    @PreAuthorize("@ss.hasPermi('iscs:notify:query')")
    @GetMapping(value = "/selectIsMailNotifyConfigByCode")
    public CommonResult<IsMailNotifyConfig> selectIsMailNotifyConfigByCode(String templateCode)
    {
        Assert.notBlank(templateCode, "templateCode不可为空！");
        return CommonResult.success(isMailNotifyConfigService.getOne(Wrappers.<IsMailNotifyConfig>lambdaQuery()
                .eq(IsMailNotifyConfig::getTemplateCode, templateCode)));
    }

}
