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.IsException;
import com.ktg.iscs.service.IIsExceptionService;
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.Date;
import java.util.List;

/**
 * 异常记录Controller
 *
 * @author cgj
 * @date 2025-03-06
 */
@Api(tags = "异常记录")
@RestController
@RequestMapping("/iscs/exception")
public class IsExceptionController extends BaseController
{
    @Autowired
    private IIsExceptionService isExceptionService;

    @ApiOperation("查询异常记录-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "isException", description = "实体参数")
    })
    @PreAuthorize("@ss.hasPermi('iscs:exception:list')")
    @GetMapping("/getIsExceptionPage")
    public CommonResult<Page<IsException>> getIsExceptionPage(Page<IsException> page, IsException isException)
    {
        Page<IsException> result = isExceptionService.getIsExceptionPage(page, isException);
        return CommonResult.success(result);
    }

    @ApiOperation("导出异常记录列表")
    @Parameter(name = "isException", description = "实体参数")
    @PreAuthorize("@ss.hasPermi('iscs:exception:export')")
    @Log(title = "异常记录", businessType = BusinessType.EXPORT)
    @PostMapping("/exportIsException")
    public void exportIsException(HttpServletResponse response, IsException isException)
    {
        Page<IsException> page = new Page<>();
        page.setSize(-1);
        page.setCurrent(1);
        List<IsException> list = isExceptionService.page(page, Wrappers.<IsException>lambdaQuery()
                .orderByDesc(IsException::getExceptionId)).getRecords();
        ExcelUtil<IsException> util = new ExcelUtil<IsException>(IsException.class);
        util.exportExcel(response, list, "异常记录数据");
    }

    @ApiOperation("获取异常记录详细信息")
    @Parameter(name = "exceptionId", description = "exceptionId")
    @PreAuthorize("@ss.hasPermi('iscs:exception:query')")
    @GetMapping(value = "/selectIsExceptionById")
    public CommonResult<IsException> selectIsExceptionById(Long exceptionId)
    {
        return CommonResult.success(isExceptionService.selectIsExceptionById(exceptionId));
    }

    @ApiOperation("新增异常记录")
    @PreAuthorize("@ss.hasPermi('iscs:exception:add')")
    @Log(title = "异常记录", businessType = BusinessType.INSERT)
    @PostMapping("/insertIsException")
    public CommonResult<Boolean> insertIsException(@RequestBody @Parameter(name = "isException", description = "新增数据类,放到body") IsException isException)
    {
        isException.setRaiseTime(new Date());
        return CommonResult.success(isExceptionService.save(isException));
    }

    @ApiOperation("结束异常")
    @PreAuthorize("@ss.hasPermi('iscs:exception:edit')")
    @Log(title = "结束异常", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsException")
    public CommonResult<Boolean> updateIsException(@RequestBody @Parameter(name = "isException", description = "修改数据类,放到body") IsException isException)
    {
        isException.setHandleTime(new Date());
        return CommonResult.success(isExceptionService.updateById(isException));
    }

    @ApiOperation("删除异常记录")
    @PreAuthorize("@ss.hasPermi('iscs:exception:remove')")
    @Log(title = "异常记录", businessType = BusinessType.DELETE)
	@PostMapping("/deleteIsExceptionByExceptionIds")
    public CommonResult<Boolean> deleteIsExceptionByExceptionIds(String exceptionIds)
    {
        Assert.notBlank(exceptionIds, "请选择需要删除的数据!");
        Long[] longIds = Convert.toLongArray(exceptionIds);
        return CommonResult.success(isExceptionService.removeBatchByIds(Arrays.asList(longIds)));
    }
}
