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.IsMaterialsLoanException;
import com.ktg.iscs.domain.vo.exception.IsMaterialsLoanExceptionPageVO;
import com.ktg.iscs.service.IIsMaterialsLoanExceptionService;
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-02-10
 */
@Api(tags = "物资归还异常")
@RestController
@RequestMapping("/iscs/exception")
public class IsMaterialsLoanExceptionController extends BaseController
{
    @Autowired
    private IIsMaterialsLoanExceptionService isMaterialsLoanExceptionService;

    @ApiOperation("查询物资归还异常-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "isMaterialsLoanException", description = "实体参数")
    })
    @PreAuthorize("@ss.hasPermi('iscs:exception:list')")
    @GetMapping("/getIsMaterialsLoanExceptionPage")
    public CommonResult<Page<IsMaterialsLoanExceptionPageVO>> getIsMaterialsLoanExceptionPage(Page<IsMaterialsLoanException> page, IsMaterialsLoanExceptionPageVO isMaterialsLoanException)
    {
        Page<IsMaterialsLoanExceptionPageVO> result = isMaterialsLoanExceptionService.getIsMaterialsLoanExceptionPage(page, isMaterialsLoanException);
        return CommonResult.success(result);
    }

    @ApiOperation("导出物资归还异常列表")
    @Parameter(name = "isMaterialsLoanException", description = "实体参数")
    @PreAuthorize("@ss.hasPermi('iscs:exception:export')")
    @Log(title = "物资归还异常", businessType = BusinessType.EXPORT)
    @PostMapping("/exportIsMaterialsLoanException")
    public void exportIsMaterialsLoanException(HttpServletResponse response, IsMaterialsLoanException isMaterialsLoanException)
    {
        Page<IsMaterialsLoanException> page = new Page<>();
        page.setSize(-1);
        page.setCurrent(1);
        List<IsMaterialsLoanException> list = isMaterialsLoanExceptionService.page(page, Wrappers.<IsMaterialsLoanException>lambdaQuery()
                .orderByDesc(IsMaterialsLoanException::getMisplaceId)).getRecords();
        ExcelUtil<IsMaterialsLoanException> util = new ExcelUtil<IsMaterialsLoanException>(IsMaterialsLoanException.class);
        util.exportExcel(response, list, "物资归还异常数据");
    }

    @ApiOperation("获取物资归还异常详细信息")
    @Parameter(name = "exceptionId", description = "exceptionId")
    @PreAuthorize("@ss.hasPermi('iscs:exception:query')")
    @GetMapping(value = "/selectIsMaterialsLoanExceptionById")
    public CommonResult<IsMaterialsLoanException> selectIsMaterialsLoanExceptionById(Long exceptionId)
    {
        return CommonResult.success(isMaterialsLoanExceptionService.getById(exceptionId));
    }

    @ApiOperation("新增物资归还异常")
    @PreAuthorize("@ss.hasPermi('iscs:exception:add')")
    @Log(title = "物资归还异常", businessType = BusinessType.INSERT)
    @PostMapping("/insertIsMaterialsLoanException")
    public CommonResult<Boolean> insertIsMaterialsLoanException(@RequestBody @Parameter(name = "isMaterialsLoanException", description = "新增数据类,放到body") IsMaterialsLoanException isMaterialsLoanException)
    {
        return CommonResult.success(isMaterialsLoanExceptionService.save(isMaterialsLoanException));
    }

    @ApiOperation("修改物资归还异常")
    @PreAuthorize("@ss.hasPermi('iscs:exception:edit')")
    @Log(title = "物资归还异常", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsMaterialsLoanException")
    public CommonResult<Boolean> updateIsMaterialsLoanException(@RequestBody @Parameter(name = "isMaterialsLoanException", description = "修改数据类,放到body") IsMaterialsLoanException isMaterialsLoanException)
    {
        return CommonResult.success(isMaterialsLoanExceptionService.updateById(isMaterialsLoanException));
    }

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