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 com.skr.mdm.service.impl.equipment.DeviceDebugLogServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author nfq
 * @date 2020/8/11 17:28
 */
@RestController
@RequestMapping("log")
@Api(tags = "用户日志")
public class UserLogController {

    @Autowired
    private DeviceDebugLogServiceImpl deviceDebugLogService;

    @PostMapping("upload")
    @ApiOperation("上传用户日志")
    public ResponseResult uploadLog(@RequestParam @ApiParam(value = "数据", required = true) String data,
                                    @ApiIgnore Authentication authentication) {
        LoginUserDTO loginUserDTO = (LoginUserDTO) authentication.getPrincipal();
        CmdHandlerService strategy = StrategyFactory.getStrategy(CmdTypeConstants.UPLOAD_LOG);
        return strategy.cmdHandler(loginUserDTO.getId(), data);
    }


    @PostMapping("confirm")
    @ApiOperation("日志确认")
    public ResponseResult logConfirm(@RequestParam @ApiParam(value = "数据", required = true) String data,
                                     @ApiIgnore Authentication authentication) {
        LoginUserDTO loginUserDTO = (LoginUserDTO) authentication.getPrincipal();
        CmdHandlerService strategy = StrategyFactory.getStrategy(CmdTypeConstants.LOG_CONFIRM);
        return strategy.cmdHandler(loginUserDTO.getId(), data);
    }

    @PostMapping("file/upload")
    @ApiOperation("调试日志上传 含有info 上传失败原因")
    public ResponseResult debugUploadLog(@RequestParam @ApiParam(value = "文件", required = true) MultipartFile file,
                                          @RequestParam(required = false) String info,
                                    @ApiIgnore Authentication authentication) {
        LoginUserDTO loginUserDTO = (LoginUserDTO) authentication.getPrincipal();
        return deviceDebugLogService.upload(loginUserDTO, info, file);
    }
}