package com.ktg.iscs.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
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.iscs.domain.IsCabinetOpenRecord;
import com.ktg.iscs.domain.IsMaterials;
import com.ktg.iscs.domain.dto.hardwareMaterialApi.*;
import com.ktg.iscs.domain.vo.hardwareMaterialApi.*;
import com.ktg.iscs.service.HardwareMaterialApiService;
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.web.bind.annotation.*;

import java.util.List;

/**
 * 硬件调用接口
 *
 * @author cgj
 * @date 2024-10-16
 */
@Api(tags = "硬件物资调用接口")
@RestController
@RequestMapping("/iscs/hardware/material-api")
public class HardwareMaterialApiController extends BaseController {
    @Autowired
    private HardwareMaterialApiService hardwareMaterialApiService;

    // ----------------------------------手动更换物资-------------------------------------------------------
    @ApiOperation("获取物资柜手动更换物资类型和异常数量和物资信息")
    @Parameter(name = "cabinetId / cabinetCode", description = "物资柜ID / 物资柜code")
    @GetMapping(value = "/selectExMaterialTypeById")
    public CommonResult<List<ExMaterialTypeVO>> selectExMaterialTypeById(Long cabinetId, String cabinetCode) throws JsonProcessingException {
        return CommonResult.success(hardwareMaterialApiService.selectExMaterialTypeById(cabinetId, cabinetCode));
    }

    /*@ApiOperation("手动更换数据校验")
    @Log(title = "手动更换数据校验", businessType = BusinessType.INSERT)
    @PostMapping("/checkReplaceData")
    public CommonResult<Boolean> checkReplaceData(@RequestBody @Parameter(name = "dto", description = "数据类,放到body") ReplaceMaterialDTO dto) {
        return CommonResult.success(hardwareMaterialApiService.checkReplaceData(dto));
    }*/

    @ApiOperation("手动更换物资-校验后的提交")
    @Log(title = "手动更换物资-校验后的提交", businessType = BusinessType.INSERT)
    @PostMapping("/insertCheckedRecord")
    public CommonResult<Boolean> insertCheckedRecord(@RequestBody @Parameter(name = "dto", description = "数据类,放到body") ReplaceMaterialParamDTO dto) throws JsonProcessingException {
        return CommonResult.success(hardwareMaterialApiService.insertCheckedRecord(dto));
    }

    @ApiOperation("手动更换物资")
    @Log(title = "手动更换物资", businessType = BusinessType.INSERT)
    @PostMapping("/insertReplaceRecord")
    public CommonResult<Boolean> insertReplaceRecord(@RequestBody @Parameter(name = "dto", description = "数据类,放到body") ReplaceMaterialParamDTO dto) throws JsonProcessingException {
        return CommonResult.success(hardwareMaterialApiService.insertReplaceRecord(dto));
    }

    @ApiOperation("手动更换物资-实体操作")
    @Log(title = "手动更换物资-实体操作", businessType = BusinessType.UPDATE)
    @PostMapping("/updateMaterialsReplace")
    public CommonResult<Boolean> updateMaterialsReplace(@RequestBody @Parameter(name = "dto", description = "修改数据类,放到body") ReplaceMaterialsDTO dto) {
        return CommonResult.success(hardwareMaterialApiService.updateMaterialsReplace(dto));
    }


    // ----------------------------------物资柜开门异常-------------------------------------------------------

    @ApiOperation("新增物资柜开门超时异常")
    @Log(title = "新增物资柜开门超时异常", businessType = BusinessType.INSERT)
    @PostMapping("/insertCabinetOpenTimeout")
    public CommonResult<Boolean> insertCabinetOpenTimeout(@RequestBody @Parameter(name = "dto", description = "数据类,放到body") OpenTimeoutDTO dto) {
        return CommonResult.success(hardwareMaterialApiService.insertCabinetOpenTimeout(dto));
    }

    @ApiOperation("解除柜开门超时异常")
    @Log(title = "解除柜开门超时异常", businessType = BusinessType.INSERT)
    @PostMapping("/updateCabinetOpenTimeout")
    public CommonResult<Boolean> updateCabinetOpenTimeout(@RequestBody @Parameter(name = "dto", description = "数据类,放到body") OpenTimeoutDTO dto) {
        return CommonResult.success(hardwareMaterialApiService.updateCabinetOpenTimeout(dto));
    }

    // ----------------------------------物资柜开门记录-------------------------------------------------------

    @ApiOperation("新增物资柜开门记录")
    @Log(title = "新增物资柜开门记录", businessType = BusinessType.INSERT)
    @PostMapping("/insertCabinetOpenRecord")
    public CommonResult<Boolean> insertCabinetOpenRecord(@RequestBody @Parameter(name = "dto", description = "数据类,放到body") IsCabinetOpenRecord dto) {
        return CommonResult.success(hardwareMaterialApiService.insertCabinetOpenRecord(dto));
    }

    @ApiOperation("物资柜关门")
    @Log(title = "物资柜关门", businessType = BusinessType.INSERT)
    @PostMapping("/updateCabinetClose")
    public CommonResult<Boolean> updateCabinetClose(@RequestBody @Parameter(name = "dto", description = "数据类,放到body") IsCabinetOpenRecord dto) {
        return CommonResult.success(hardwareMaterialApiService.updateCabinetClose(dto));
    }

    // ----------------------------------物资取还-------------------------------------------------------
    @ApiOperation("获取物资柜物品分类及分类下物资数量和列表")
    @Parameter(name = "cabinetId / cabinetCode", description = "物资柜ID / 物资柜CODE")
    @GetMapping(value = "/selectMaterialsByCabinetId")
    public CommonResult<List<CabinetMaterialVO>> selectMaterialsByCabinetId(Long cabinetId, String cabinetCode) throws JsonProcessingException {
        return CommonResult.success(hardwareMaterialApiService.selectMaterialsByCabinetId(cabinetId, cabinetCode));
    }

    @ApiOperation("通过RFID获取物资信息")
    @Parameter(name = "rfid", description = "物资rfid")
    @GetMapping(value = "/selectMaterialByRfid")
    public CommonResult<IsMaterials> selectMaterialByRfid(String rfid) {
        return CommonResult.success(hardwareMaterialApiService.selectMaterialByRfid(rfid));
    }

    @ApiOperation("通过RFID列表获取批量物资信息")
    @Parameter(name = "list", description = "物资rfid列表")
    @PostMapping(value = "/selectMaterialsByRfidList")
    public CommonResult<List<IsMaterials>> selectMaterialsByRfidList(@RequestBody @Parameter(name = "dto", description = "数据类,放到body") RfidListDTO dto) {
        return CommonResult.success(hardwareMaterialApiService.selectMaterialsByRfidList(dto));
    }

    @ApiOperation("RFID所属柜子区分")
    @Parameter(name = "list", description = "物资rfid列表")
    @PostMapping(value = "/selectCabinetMaterials")
    public CommonResult<CabinetMaterialsVO> selectCabinetMaterials(@RequestBody @Parameter(name = "dto", description = "数据类,放到body") CabinetRfidListDTO dto) {
        return CommonResult.success(hardwareMaterialApiService.selectCabinetMaterials(dto));
    }


    // 在物资controller   ----- updateIsMaterialById
    /*@ApiOperation("物资领取/归还")
    @Log(title = "物资领取/归还", businessType = BusinessType.UPDATE)
    @PostMapping("/updateMaterialReceiveReturn")
    public CommonResult<String> updateMaterialReceiveReturn(@RequestBody @Parameter(name = "dto", description = "修改数据类,放到body") RecRetParamDTO dto) throws Exception {
        return CommonResult.success(hardwareMaterialApiService.updateMaterialReceiveReturn(dto));
    }*/

    // ----------------------------------主界面-------------------------------------------------------
    @ApiOperation("主界面")
    @Parameter(name = "cabinetCode", description = "物资柜CODE")
    @GetMapping("/getCabinetHomePage")
    public CommonResult<CabinetHomePageVO> getCabinetHomePage(String cabinetCode) {
        CabinetHomePageVO result = hardwareMaterialApiService.getCabinetHomePage(cabinetCode);
        return CommonResult.success(result);
    }

    // ----------------------------------物资使用说明-------------------------------------------------------
    @ApiOperation("查询物资使用说明")
    @GetMapping("/getInstructionsList")
    public CommonResult<List<InstructionVO>> getInstructionsList() {
        List<InstructionVO> result = hardwareMaterialApiService.getInstructionsList();
        return CommonResult.success(result);
    }

    // ----------------------------------物资检查计划-------------------------------------------------------
    @ApiOperation("在某个柜子上我的最近一次物资检查计划")
    @Parameter(name = "cabinetCode", description = "物资柜CODE")
    @GetMapping("/getLastCheckPlanByCabinetCode")
    public CommonResult<CabinetCheckVO> getLastCheckPlanByCabinetCode(String cabinetCode) {
        CabinetCheckVO result = hardwareMaterialApiService.getLastCheckPlanByCabinetCode(cabinetCode);
        return CommonResult.success(result);
    }

    @ApiOperation("物资检查表")
    @Parameters({
            @Parameter(name = "cabinetCode", description = "物资柜CODE"),
            @Parameter(name = "planId", description = "物资计划id")}
    )
    @GetMapping("/getCheckMaterialsByCabinetCode")
    public CommonResult<MaterialsCheckInitVO> getCheckMaterialsByCabinetCode(String cabinetCode, Long planId) {
        MaterialsCheckInitVO result = hardwareMaterialApiService.getCheckMaterialsByCabinetCode(cabinetCode, planId);
        return CommonResult.success(result);
    }

    @ApiOperation("开始检查")
    @Log(title = "开始检查", businessType = BusinessType.INSERT)
    @PostMapping("/startCheckPlan")
    public CommonResult<Boolean> startCheckPlan(@RequestBody @Parameter(name = "dto", description = "放到body") StartCheckPlanDTO dto) {
        return CommonResult.success(hardwareMaterialApiService.startCheckPlan(dto));
    }

    @ApiOperation("提交物资检查记录")
    @Log(title = "提交物资检查记录", businessType = BusinessType.INSERT)
    @PostMapping("/insertCheckRecord")
    public CommonResult<Boolean> insertCheckRecord(@RequestBody @Parameter(name = "dto", description = "放到body") CheckParamDTO dto) {
        return CommonResult.success(hardwareMaterialApiService.insertCheckRecord(dto));
    }

    @ApiOperation("最终提交物资检查记录")
    @Log(title = "最终提交物资检查记录", businessType = BusinessType.INSERT)
    @PostMapping("/insertSubmitPlan")
    public CommonResult<Boolean> insertSubmitPlan(@RequestBody @Parameter(name = "dto", description = "放到body") SubmitPlanDTO dto) {
        return CommonResult.success(hardwareMaterialApiService.insertSubmitPlan(dto));
    }

    @ApiOperation("检查签名")
    @Log(title = "检查签名", businessType = BusinessType.INSERT)
    @PostMapping("/insertSign")
    public CommonResult<Boolean> insertSign(@RequestBody @Parameter(name = "dto", description = "放到body") SignDTO dto) {
        return CommonResult.success(hardwareMaterialApiService.insertSign(dto));
    }


    // ----------------------------------自动更换-------------------------------------------------------
    @ApiOperation("自动更换")
    @Log(title = "自动更换", businessType = BusinessType.UPDATE)
    @PostMapping("/insertAutoChange")
    public CommonResult<AutoReplaceReportVO> insertAutoChange(@RequestBody @Parameter(name = "dto", description = "数据类,放到body") AutoChangeDTO dto) {
        return CommonResult.success(hardwareMaterialApiService.insertAutoChange(dto));
    }

}
