package com.ktg.iscs.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.core.domain.AjaxResult;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.pojo.CommonResult;
import com.ktg.common.utils.poi.ExcelUtil;
import com.ktg.iscs.domain.IsMaterials;
import com.ktg.iscs.domain.dto.hardwareApi.LoanParamDTO;
import com.ktg.iscs.domain.dto.materials.MaterialBindingDTO;
import com.ktg.iscs.domain.dto.materials.MaterialsPageDTO;
import com.ktg.iscs.domain.vo.materials.ImportMaterialsVO;
import com.ktg.iscs.domain.vo.materials.MaterialsPageVO;
import com.ktg.iscs.service.IIsMaterialsService;
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.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 物资Controller
 *
 * @author cgj
 * @date 2024-11-08
 */
@Api(tags = "物资")
@RestController
@RequestMapping("/iscs/materials")
public class IsMaterialsController extends BaseController {
    @Autowired
    private IIsMaterialsService isMaterialsService;

    /**
     * 查询物资分页
     */
    @ApiOperation("查询物资-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "dto", description = "实体参数")
    })
    @PreAuthorize("@ss.hasPermi('iscs:materials:list')")
    @GetMapping("/getIsMaterialsPage")
    public CommonResult<Page<MaterialsPageVO>> getIsMaterialsPage(Page<IsMaterials> page, MaterialsPageDTO dto) throws JsonProcessingException {
        Page<MaterialsPageVO> result = isMaterialsService.getIsMaterialsPage(page, dto);
        return CommonResult.success(result);
    }

    @ApiOperation("查询异常还错柜子的物资")
    @Parameters({
            @Parameter(name = "dto", description = "实体参数")
    })
    @PreAuthorize("@ss.hasPermi('iscs:materials:list')")
    @GetMapping("/getExMaterials")
    public CommonResult<List<MaterialsPageVO>> getExMaterials(MaterialsPageDTO dto) {
        List<MaterialsPageVO> result = isMaterialsService.getExMaterials(dto);
        return CommonResult.success(result);
    }

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

    /**
     * 获取物资详细信息
     */
    @ApiOperation("获取物资详细信息")
    @Parameter(name = "materialsId", description = "materialsId")
    @PreAuthorize("@ss.hasPermi('iscs:materials:query')")
    @GetMapping(value = "/selectIsMaterialsById")
    public CommonResult<IsMaterials> selectIsMaterialsById(Long materialsId) {
        return CommonResult.success(isMaterialsService.selectIsMaterialsByMaterialsId(materialsId));
    }

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

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

    @ApiOperation("借出/归还物资")
    @PreAuthorize("@ss.hasPermi('iscs:materials:loan')")
    @Log(title = "借出/归还物资", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsMaterialById")
    public CommonResult<String> updateIsMaterialById(@RequestBody @Parameter(name = "dto", description = "修改数据类,放到body") LoanParamDTO dto) {
        return CommonResult.success(isMaterialsService.updateIsMaterialById(dto));
    }

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

    @ApiOperation("导入模板-物资")
    @Log(title = "导入模板-物资")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ImportMaterialsVO> util = new ExcelUtil<>(ImportMaterialsVO.class);
        ImportMaterialsVO importMaterialsVO = new ImportMaterialsVO();
        importMaterialsVO.setCabinetName("测试物资柜(测试示例行可删除)");
        importMaterialsVO.setMaterialsName("测试白大褂K380(测试示例行可删除)");
        importMaterialsVO.setMaterialsTypeName("测试白大褂(测试示例行可删除)");
        importMaterialsVO.setValueName("s");
        importMaterialsVO.setMaterialsRfid("7418965826");
        importMaterialsVO.setSupplier("测试实验室(测试示例行可删除)");
        importMaterialsVO.setExpirationDate(new Date());
        ArrayList<ImportMaterialsVO> importMaterialsVOS = new ArrayList<>();
        importMaterialsVOS.add(importMaterialsVO);
        util.importTempExcelWithDate(response, "物资数据", importMaterialsVOS);
    }

    @ApiOperation("导入-物资")
    @Log(title = "导入-物资", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('iscs:materials:import')")
    @PostMapping("/importMaterials")
    public AjaxResult importMaterials(MultipartFile file, boolean updateSupport) throws Exception {
        String filename = file.getOriginalFilename();
        String extension = FilenameUtils.getExtension(filename);
        // 文件不是 .xlsx 或 .xls
        Assert.isFalse(!"xls".equalsIgnoreCase(extension) && !"xlsx".equalsIgnoreCase(extension), "文件不是xls或xlsx，请重新上传！");
        ExcelUtil<ImportMaterialsVO> util = new ExcelUtil<>(ImportMaterialsVO.class);
        List<ImportMaterialsVO> itemList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = isMaterialsService.importMaterials(itemList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @ApiOperation("绑定物资-物资柜绑定物资")
    @PreAuthorize("@ss.hasPermi('iscs:materials:binding')")
    @Log(title = "物资", businessType = BusinessType.UPDATE)
    @PostMapping("/updateMaterialsBinding")
    public CommonResult<Boolean> updateMaterialsBinding(@RequestBody @Parameter(name = "dto", description = "修改数据类,放到body") MaterialBindingDTO dto) {
        return CommonResult.success(isMaterialsService.updateMaterialsBinding(dto));
    }
}
