package com.ktg.iscs.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
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.IsMaterialsCheckRecord;
import com.ktg.iscs.domain.dto.checkRecord.CheckRecordPageDTO;
import com.ktg.iscs.domain.dto.checkRecord.CheckRecordParamDTO;
import com.ktg.iscs.domain.vo.checkRecord.IsMaterialsCheckRecordVO;
import com.ktg.iscs.service.IIsMaterialsCheckRecordService;
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-14
 */
@Api(tags = "物资检查记录")
@RestController
@RequestMapping("/iscs/check")
public class IsMaterialsCheckRecordController extends BaseController
{
    @Autowired
    private IIsMaterialsCheckRecordService isMaterialsCheckRecordService;

    @ApiOperation("查询物资检查记录-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "dto", description = "实体参数")
    })
    @PreAuthorize("@ss.hasPermi('iscs:check:list')")
    @GetMapping("/getIsMaterialsCheckRecordPage")
    public CommonResult<Page<IsMaterialsCheckRecordVO>> getIsMaterialsCheckRecordPage(Page<IsMaterialsCheckRecord> page, CheckRecordPageDTO dto)
    {
        Page<IsMaterialsCheckRecordVO> result = isMaterialsCheckRecordService.getIsMaterialsCheckRecordPage(page, dto);
        return CommonResult.success(result);
    }

    @ApiOperation("导出物资检查记录列表")
    @Parameter(name = "isMaterialsCheckRecord", description = "实体参数")
    @PreAuthorize("@ss.hasPermi('iscs:check:export')")
    @Log(title = "物资检查记录", businessType = BusinessType.EXPORT)
    @PostMapping("/exportIsMaterialsCheckRecord")
    public void exportIsMaterialsCheckRecord(HttpServletResponse response, CheckRecordPageDTO dto)
    {
        Page<IsMaterialsCheckRecord> page = new Page<>();
        page.setSize(-1);
        page.setCurrent(1);
        List<IsMaterialsCheckRecordVO> list = isMaterialsCheckRecordService.getIsMaterialsCheckRecordPage(page, dto).getRecords();
        ExcelUtil<IsMaterialsCheckRecordVO> util = new ExcelUtil<>(IsMaterialsCheckRecordVO.class);
        util.exportExcel(response, list, "物资检查记录数据");
    }

    @ApiOperation("获取物资检查记录详细信息")
    @Parameter(name = "recordId", description = "recordId")
    @PreAuthorize("@ss.hasPermi('iscs:check:query')")
    @GetMapping(value = "/selectIsMaterialsCheckRecordById")
    public CommonResult<IsMaterialsCheckRecord> selectIsMaterialsCheckRecordById(Long recordId)
    {
        return CommonResult.success(isMaterialsCheckRecordService.getById(recordId));
    }

    @ApiOperation("新增物资检查记录")
    @PreAuthorize("@ss.hasPermi('iscs:check:add')")
    @Log(title = "物资检查记录", businessType = BusinessType.INSERT)
    @PostMapping("/insertIsMaterialsCheckRecord")
    public CommonResult<Boolean> insertIsMaterialsCheckRecord(@RequestBody @Parameter(name = "dto", description = "新增数据类,放到body") CheckRecordParamDTO dto)
    {
        return CommonResult.success(isMaterialsCheckRecordService.insertIsMaterialsCheckRecord(dto));
    }

    @ApiOperation("修改物资检查记录")
    @PreAuthorize("@ss.hasPermi('iscs:check:edit')")
    @Log(title = "物资检查记录", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsMaterialsCheckRecord")
    public CommonResult<Boolean> updateIsMaterialsCheckRecord(@RequestBody @Parameter(name = "isMaterialsCheckRecord", description = "修改数据类,放到body") IsMaterialsCheckRecord isMaterialsCheckRecord)
    {
        return CommonResult.success(isMaterialsCheckRecordService.updateById(isMaterialsCheckRecord));
    }

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