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.IsMailSendTaskItem;
import com.ktg.iscs.service.IIsMailSendTaskItemService;
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-16
 */
@Api(tags = "系统邮件任务")
@RestController
@RequestMapping("/iscs/sendTask")
public class IsMailSendTaskItemController extends BaseController
{
    @Autowired
    private IIsMailSendTaskItemService isMailSendTaskItemService;

    @ApiOperation("查询系统邮件任务-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "isMailSendTaskItem", description = "实体参数")
    })
    @PreAuthorize("@ss.hasPermi('iscs:sendTask:list')")
    @GetMapping("/getIsMailSendTaskItemPage")
    public CommonResult<Page<IsMailSendTaskItem>> getIsMailSendTaskItemPage(Page<IsMailSendTaskItem> page, IsMailSendTaskItem isMailSendTaskItem)
    {
        Page<IsMailSendTaskItem> result = isMailSendTaskItemService.getIsMailSendTaskItemPage(page, isMailSendTaskItem);
        return CommonResult.success(result);
    }

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

    @ApiOperation("获取系统邮件任务详细信息")
    @Parameter(name = "mailId", description = "mailId")
    @PreAuthorize("@ss.hasPermi('iscs:sendTask:query')")
    @GetMapping(value = "/selectIsMailSendTaskItemById")
    public CommonResult<IsMailSendTaskItem> selectIsMailSendTaskItemById(Long mailId)
    {
        return CommonResult.success(isMailSendTaskItemService.getById(mailId));
    }

    @ApiOperation("新增系统邮件任务")
    @PreAuthorize("@ss.hasPermi('iscs:sendTask:add')")
    @Log(title = "系统邮件任务", businessType = BusinessType.INSERT)
    @PostMapping("/insertIsMailSendTaskItem")
    public CommonResult<Boolean> insertIsMailSendTaskItem(@RequestBody @Parameter(name = "isMailSendTaskItem", description = "新增数据类,放到body") IsMailSendTaskItem isMailSendTaskItem)
    {
        return CommonResult.success(isMailSendTaskItemService.save(isMailSendTaskItem));
    }

    @ApiOperation("修改系统邮件任务")
    @PreAuthorize("@ss.hasPermi('iscs:sendTask:edit')")
    @Log(title = "系统邮件任务", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsMailSendTaskItem")
    public CommonResult<Boolean> updateIsMailSendTaskItem(@RequestBody @Parameter(name = "isMailSendTaskItem", description = "修改数据类,放到body") IsMailSendTaskItem isMailSendTaskItem)
    {
        return CommonResult.success(isMailSendTaskItemService.updateById(isMailSendTaskItem));
    }

    @ApiOperation("删除系统邮件任务")
    @PreAuthorize("@ss.hasPermi('iscs:sendTask:remove')")
    @Log(title = "系统邮件任务", businessType = BusinessType.DELETE)
	@PostMapping("/deleteIsMailSendTaskItemByMailIds")
    public CommonResult<Boolean> deleteIsMailSendTaskItemByMailIds(String mailIds)
    {
        Assert.notBlank(mailIds, "请选择需要删除的数据!");
        Long[] longIds = Convert.toLongArray(mailIds);
        return CommonResult.success(isMailSendTaskItemService.removeBatchByIds(Arrays.asList(longIds)));
    }
}
