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.IsMaterialsChangeRecord;
import com.ktg.iscs.domain.dto.change.ChangeRecordDTO;
import com.ktg.iscs.domain.vo.change.ChangeRecordVO;
import com.ktg.iscs.service.IIsMaterialsChangeRecordService;
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/change")
public class IsMaterialsChangeRecordController extends BaseController
{
    @Autowired
    private IIsMaterialsChangeRecordService isMaterialsChangeRecordService;

    @ApiOperation("查询物资更换记录-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "dto", description = "实体参数")
    })
    @PreAuthorize("@ss.hasPermi('iscs:change:list')")
    @GetMapping("/getIsMaterialsChangeRecordPage")
    public CommonResult<Page<ChangeRecordVO>> getIsMaterialsChangeRecordPage(Page<IsMaterialsChangeRecord> page, ChangeRecordDTO dto)
    {
        Page<ChangeRecordVO> result = isMaterialsChangeRecordService.getIsMaterialsChangeRecordPage(page, dto);
        return CommonResult.success(result);
    }

    @ApiOperation("导出物资更换记录列表")
    @Parameter(name = "isMaterialsChangeRecord", description = "实体参数")
    @PreAuthorize("@ss.hasPermi('iscs:change:export')")
    @Log(title = "物资更换记录", businessType = BusinessType.EXPORT)
    @PostMapping("/exportIsMaterialsChangeRecord")
    public void exportIsMaterialsChangeRecord(HttpServletResponse response, IsMaterialsChangeRecord isMaterialsChangeRecord)
    {
        Page<IsMaterialsChangeRecord> page = new Page<>();
        page.setSize(-1);
        page.setCurrent(1);
        List<IsMaterialsChangeRecord> list = isMaterialsChangeRecordService.page(page, Wrappers.<IsMaterialsChangeRecord>lambdaQuery()
                .orderByDesc(IsMaterialsChangeRecord::getChangeId)).getRecords();
        ExcelUtil<IsMaterialsChangeRecord> util = new ExcelUtil<IsMaterialsChangeRecord>(IsMaterialsChangeRecord.class);
        util.exportExcel(response, list, "物资更换记录数据");
    }

    @ApiOperation("获取物资更换记录详细信息")
    @Parameter(name = "changeId", description = "changeId")
    @PreAuthorize("@ss.hasPermi('iscs:change:query')")
    @GetMapping(value = "/selectIsMaterialsChangeRecordById")
    public CommonResult<IsMaterialsChangeRecord> selectIsMaterialsChangeRecordById(Long changeId)
    {
        return CommonResult.success(isMaterialsChangeRecordService.getById(changeId));
    }

    @ApiOperation("新增物资更换记录(物资清单中的数据一换一)")
    @PreAuthorize("@ss.hasPermi('iscs:change:add')")
    @Log(title = "物资更换记录", businessType = BusinessType.INSERT)
    @PostMapping("/insertIsMaterialsChangeRecord")
    public CommonResult<Boolean> insertIsMaterialsChangeRecord(@RequestBody @Parameter(name = "isMaterialsChangeRecord", description = "新增数据类,放到body") IsMaterialsChangeRecord isMaterialsChangeRecord)
    {
        return CommonResult.success(isMaterialsChangeRecordService.insertIsMaterialsChangeRecord(isMaterialsChangeRecord));
    }

    @ApiOperation("修改物资更换记录")
    @PreAuthorize("@ss.hasPermi('iscs:change:edit')")
    @Log(title = "物资更换记录", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsMaterialsChangeRecord")
    public CommonResult<Boolean> updateIsMaterialsChangeRecord(@RequestBody @Parameter(name = "isMaterialsChangeRecord", description = "修改数据类,放到body") IsMaterialsChangeRecord isMaterialsChangeRecord)
    {
        return CommonResult.success(isMaterialsChangeRecordService.updateById(isMaterialsChangeRecord));
    }

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