package com.skr.mdm.controller;

import com.skr.mdm.constant.CmdTypeConstants;
import com.skr.mdm.dto.LoginUserDTO;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.service.CmdHandlerService;
import com.skr.mdm.service.StrategyFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author nfq
 * @date 2020/8/11 16:58
 */
@RestController
@RequestMapping("equipment")
@Api(tags = "设备信息上传")
public class EquipmentInfoController {

    @PostMapping("upload")
    @ApiOperation("设备信息上传")
    public ResponseResult uploadEquipmentInfo(@RequestParam @ApiParam(value = "设备信息", required = true) String data,
                                              @ApiIgnore Authentication authentication) {
        LoginUserDTO loginUserDTO = (LoginUserDTO) authentication.getPrincipal();
        CmdHandlerService strategy = StrategyFactory.getStrategy(CmdTypeConstants.DEV_INFO);
        return strategy.cmdHandler(loginUserDTO.getId(), data);
    }

    @PostMapping("card/bind")
    @ApiOperation("机卡绑定解绑")
    public ResponseResult cardBindUnbind(@RequestParam @ApiParam(value = "绑定解绑参数", required = true) String data,
                                         @ApiIgnore Authentication authentication) {
        LoginUserDTO loginUserDTO = (LoginUserDTO) authentication.getPrincipal();
        CmdHandlerService strategy = StrategyFactory.getStrategy(CmdTypeConstants.CARD_BIND_UNBIND);
        return strategy.cmdHandler(loginUserDTO.getId(), data);
    }

    @PostMapping("card/state")
    @ApiOperation("机卡绑定状态（0x386）")
    public ResponseResult cardBindUnbind(@ApiIgnore Authentication authentication) {
        LoginUserDTO loginUserDTO = (LoginUserDTO) authentication.getPrincipal();
        CmdHandlerService strategy = StrategyFactory.getStrategy(CmdTypeConstants.CARD_BIND_STATE);
        return strategy.cmdHandler(loginUserDTO.getId(), null);
    }

    @PostMapping("delete")
    @ApiOperation("设备删除")
    public ResponseResult delete(@RequestParam @ApiParam(value = "", required = true) String data,
                                 @ApiIgnore Authentication authentication) {
        LoginUserDTO loginUserDTO = (LoginUserDTO) authentication.getPrincipal();
        CmdHandlerService strategy = StrategyFactory.getStrategy(CmdTypeConstants.EQUIPMENT_DEL);
        return strategy.cmdHandler(loginUserDTO.getId(), data);
    }
}