package com.ktg.iscs.controller;

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.IsMaterialsCabinet;
import com.ktg.iscs.domain.dto.materials.BindingMaterialsDTO;
import com.ktg.iscs.domain.vo.cabinet.IsMaterialsCabinetVO;
import com.ktg.iscs.service.IIsMaterialsCabinetService;
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.List;

/**
 * 物资柜Controller
 *
 * @author cgj
 * @date 2024-11-08
 */
@Api(tags = "物资柜")
@RestController
@RequestMapping("/iscs/cabinet")
public class IsMaterialsCabinetController extends BaseController
{
    @Autowired
    private IIsMaterialsCabinetService isMaterialsCabinetService;

    /**
     * 查询物资柜分页
     */
    @ApiOperation("查询物资柜-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "isMaterialsCabinet", description = "实体参数")
    })
    @PreAuthorize("@ss.hasPermi('iscs:cabinet:list')")
    @GetMapping("/getIsMaterialsCabinetPage")
    public CommonResult<Page<IsMaterialsCabinetVO>> getIsMaterialsCabinetPage(Page<IsMaterialsCabinet> page, IsMaterialsCabinet isMaterialsCabinet)
    {
        return CommonResult.success(isMaterialsCabinetService.getIsMaterialsCabinetPage(page, isMaterialsCabinet));
    }

    /**
     * 导出物资柜列表
     */
    @ApiOperation("导出物资柜列表")
    @Parameter(name = "isMaterialsCabinet", description = "实体参数")
    @PreAuthorize("@ss.hasPermi('iscs:cabinet:export')")
    @Log(title = "物资柜", businessType = BusinessType.EXPORT)
    @PostMapping("/exportIsMaterialsCabinet")
    public void exportIsMaterialsCabinet(HttpServletResponse response, IsMaterialsCabinet isMaterialsCabinet)
    {
        List<IsMaterialsCabinet> list = isMaterialsCabinetService.selectIsMaterialsCabinetList(isMaterialsCabinet);
        ExcelUtil<IsMaterialsCabinet> util = new ExcelUtil<IsMaterialsCabinet>(IsMaterialsCabinet.class);
        util.exportExcel(response, list, "物资柜数据");
    }

    /**
     * 获取物资柜详细信息
     */
    @ApiOperation("获取物资柜详细信息")
    @Parameter(name = "cabinetId", description = "cabinetId")
    @PreAuthorize("@ss.hasPermi('iscs:cabinet:query')")
    @GetMapping(value = "/selectIsMaterialsCabinetById")
    public CommonResult<IsMaterialsCabinet> selectIsMaterialsCabinetById(Long cabinetId)
    {
        return CommonResult.success(isMaterialsCabinetService.selectIsMaterialsCabinetByCabinetId(cabinetId));
    }

    @ApiOperation("获取物资柜详细信息")
    @Parameter(name = "cabinetCode", description = "cabinetCode")
    // @PreAuthorize("@ss.hasPermi('iscs:cabinet:query')")
    @GetMapping(value = "/selectIsMaterialsCabinetByCode")
    public CommonResult<IsMaterialsCabinet> selectIsMaterialsCabinetByCode(String cabinetCode)
    {
        return CommonResult.success(isMaterialsCabinetService.getOne(Wrappers.<IsMaterialsCabinet>lambdaQuery()
                .eq(IsMaterialsCabinet::getCabinetCode, cabinetCode)));
    }

    /**
     * 新增物资柜
     */
    @ApiOperation("新增物资柜")
    @PreAuthorize("@ss.hasPermi('iscs:cabinet:add')")
    @Log(title = "物资柜", businessType = BusinessType.INSERT)
    @PostMapping("/insertIsMaterialsCabinet")
    public CommonResult<Boolean> insertIsMaterialsCabinet(@RequestBody @Parameter(name = "isMaterialsCabinet", description = "新增数据类,放到body") IsMaterialsCabinet isMaterialsCabinet)
    {
        return CommonResult.success(isMaterialsCabinetService.insertIsMaterialsCabinet(isMaterialsCabinet) == 1);
    }

    /**
     * 修改物资柜
     */
    @ApiOperation("修改物资柜")
    @PreAuthorize("@ss.hasPermi('iscs:cabinet:edit')")
    @Log(title = "物资柜", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsMaterialsCabinet")
    public CommonResult<Boolean> updateIsMaterialsCabinet(@RequestBody @Parameter(name = "isMaterialsCabinet", description = "修改数据类,放到body") IsMaterialsCabinet isMaterialsCabinet)
    {
        return CommonResult.success(isMaterialsCabinetService.updateIsMaterialsCabinet(isMaterialsCabinet) == 1);
    }

    /**
     * 删除物资柜
     */
    @ApiOperation("删除物资柜")
    @PreAuthorize("@ss.hasPermi('iscs:cabinet:remove')")
    @Log(title = "物资柜", businessType = BusinessType.DELETE)
	@PostMapping("/deleteIsMaterialsCabinetByCabinetIds")
    public CommonResult<Boolean> deleteIsMaterialsCabinetByCabinetIds(String cabinetIds)
    {
        return CommonResult.success(isMaterialsCabinetService.deleteIsMaterialsCabinetByCabinetIds(cabinetIds) != 0);
    }

    @ApiOperation("绑定物资-物资柜绑定/解绑物资（物资更换，多对多）")
    @PreAuthorize("@ss.hasPermi('iscs:materials:binding')")
    @Log(title = "物资", businessType = BusinessType.UPDATE)
    @PostMapping("/updateMaterialsBindingRemove")
    public CommonResult<Boolean> updateMaterialsBindingRemove(@RequestBody @Parameter(name = "dto", description = "修改数据类,放到body") BindingMaterialsDTO dto)
    {
        return CommonResult.success(isMaterialsCabinetService.updateMaterialsBindingRemove(dto));
    }

}
