package com.sw.common.accessory.api.controller;

import com.sw.common.accessory.server.dto.ApplyAccessoryData;
import com.sw.common.accessory.server.interfaces.in.AccyInService;
import com.sw.common.core.exception.BusinessException;
import com.sw.common.core.response.Result;
import com.sw.common.core.response.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * @Author wupeng
 * @Description 外部系统调用的RPC接口
 * @Date 16:43 2024/1/2
 * @Param
 * @return
 **/
@RestController
@RequestMapping("/accy")
@Api(tags = "外部系统调用的RPC接口")
public class RpcController {

    @Autowired
    private AccyInService accyInService;

    @ApiOperation(value = "退库申请审核状态反馈")
    @GetMapping("/stockReturnApplyFeedback")
    public Result<Void> stockReturnApplyFeedback(@RequestParam("code") String code,
                                                 @RequestParam("status") int status,
                                                 @RequestParam("userConfirm") String userConfirm) throws Exception {
        try {
            accyInService.stockReturnApplyFeedback(code, status, userConfirm);
            return ResultGenerator.success();
        } catch (Exception e) {
            return ResultGenerator.fail("退库申请审核状态反馈异常，" + e.getMessage());
        }
    }

    @ApiOperation(value = "辅材申请审核状态反馈")
    @PostMapping("/applyAccessoryFeedback")
    public Result<Void> applyAccessoryFeedback(@RequestBody @Valid ApplyAccessoryData applyAccessoryDatas) throws Exception {
        try {
            accyInService.applyAccessoryFeedback(applyAccessoryDatas);
            return ResultGenerator.success();
        } catch (Exception e) {
            return ResultGenerator.fail("辅材申请审核状态反馈异常，" + e.getMessage());
        }
    }

    /**
     * 辅材唯一码，指定设备上机
     *
     * @param uniqueCode      辅材唯一码
     * @param equipmentCode   设备编号
     * @param accessoryStatus 辅材状态
     */
    @ApiOperation(value = "辅材唯一码，指定设备上机")
    @GetMapping("/upper")
    public Result<Void> upper(@RequestParam("uniqueCode") String uniqueCode,
                              @RequestParam("equipmentCode") String equipmentCode,
                              @RequestParam("accessoryStatus") Integer accessoryStatus) throws Exception {
        try {
            accyInService.upper(uniqueCode, equipmentCode, accessoryStatus);
            return ResultGenerator.success();
        } catch (Exception e) {
            return ResultGenerator.fail("辅材唯一码，指定设备上机异常，" + e.getMessage());
        }
    }

    /**
     * 辅材唯一码，指定设备下机
     *
     * @param uniqueCode    辅材唯一码
     * @param equipmentCode 设备编号
     */
    @ApiOperation(value = "辅材唯一码，指定设备下机")
    @GetMapping("/down")
    public Result<Void> down(@RequestParam("uniqueCode") String uniqueCode,
                             @RequestParam("equipmentCode") String equipmentCode,
                             @RequestParam("accessoryStatus") Integer accessoryStatus) throws Exception {
        try {
            accyInService.down(uniqueCode, equipmentCode, accessoryStatus);
            return ResultGenerator.success();
        } catch (Exception e) {
            return ResultGenerator.fail("辅材唯一码，指定设备下机异常，" + e.getMessage());
        }
    }

    /**
     * 下机
     *
     * @param uniqueCode    辅材唯一码
     * @param equipmentCode 设备编号
     * @param life          本次消耗寿命
     */
    @ApiOperation(value = "辅材唯一码，指定设备下机")
    @GetMapping("/downWithLife")
    public Result<Void> downWithLife(@RequestParam("uniqueCode") String uniqueCode,
                                     @RequestParam("equipmentCode") String equipmentCode,
                                     @RequestParam("life") Integer life,
                                     @RequestParam("accessoryStatus") Integer accessoryStatus) throws Exception {
        try {
            accyInService.down(uniqueCode, equipmentCode, life, accessoryStatus);
            return ResultGenerator.success();
        } catch (Exception e) {
            return ResultGenerator.fail("辅材唯一码，指定设备下机异常，" + e.getMessage());
        }
    }

    /**
     * 通过辅材唯一码 判断可不可以上机
     *
     * @param uniqueCode 辅材唯一码
     * @throws BusinessException 不能上机，返回业务异常
     */
    @ApiOperation(value = "通过辅材唯一码 判断可不可以上机")
    @GetMapping("/canUpper")
    public Result<Void> canUpper(@RequestParam("uniqueCode") String uniqueCode) throws BusinessException {
        try {
            accyInService.canUpper(uniqueCode);
            return ResultGenerator.success();
        } catch (BusinessException e) {
            return ResultGenerator.fail("通过辅材唯一码 判断是否以上机异常，" + e.getMessage());
        }
    }

    /**
     * 通过设备编号 以及 寿命类型增加寿命消耗
     *
     * @param equipmentCode 设备编号
     * @param lifeType      寿命类型
     * @param life          本次消耗寿命
     * @throws BusinessException 不能上机，返回业务异常
     */
    @ApiOperation(value = "增加寿命消耗")
    @GetMapping("/consumptionLife")
    public Result<Void> consumptionLife(@RequestParam("equipmentCode") String equipmentCode,
                                        @RequestParam("lifeType") Integer lifeType,
                                        @RequestParam("life") Integer life) throws BusinessException {
        try {
            accyInService.consumptionLife(equipmentCode, lifeType, life);
            return ResultGenerator.success();
        } catch (BusinessException e) {
            return ResultGenerator.fail("通增加寿命消耗异常，" + e.getMessage());
        }
    }


}
