package com.zd.laboratory.onemachine.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.zd.common.core.utils.FileConfigUtils;
import com.zd.common.core.utils.UrlFormatUtils;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.common.swagger.config.Knife4jConfiguration;
import com.zd.laboratory.onemachine.domain.ApkLog;
import com.zd.laboratory.onemachine.domain.ApkLogVo;
import com.zd.laboratory.onemachine.domain.ApkUpload;
import com.zd.laboratory.onemachine.domain.ApkUploadVo;
import com.zd.laboratory.service.ApkFileUpLoadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.List;

/**
 * Controller
 *
 * @author cyl
 * @date 2021/12/7
 */
@RestController
@RequestMapping("/apkfile")
@ApiSupport(author = Knife4jConfiguration.Author.CYL)
@Api(tags = {"【apk上传文件】"})
public class ApkFileUpLoadController extends BaseController {

    @Autowired
    private ApkFileUpLoadService apkFileUpLoadService;

    @Autowired
    private FileConfigUtils fileConfigUtils;

    /**
     * 新增apk上传文件(文件和日志一起上传)
     */
    @Log(title = "apk上传文件", businessType = BusinessType.INSERT)
    @PreAuthorize(hasPermi = "laboratory:apkfile:add")
    @PostMapping
    public AjaxResult add(@RequestBody ApkUploadVo apkUploadVo) {
        return toAjax(apkFileUpLoadService.insertApkFileUpLoad(apkUploadVo));
    }


    /**
     * apk上传文件
     */
    @Log(title = "apk上传文件", businessType = BusinessType.INSERT)
    @PreAuthorize(hasPermi = "laboratory:apkfile:add")
    @PostMapping("/addApkUpload")
    public AjaxResult addApkUpload(ApkUpload apkUpload) {
        return toAjax(apkFileUpLoadService.insertApkUpLoad(apkUpload));
    }


    /**
     * 查询apk上传的日志版本信息
     */
    @PreAuthorize(hasPermi = "laboratory:apkfile:list")
    @GetMapping("/list")
    @ApiOperation("查询apk版本信息分页")
    public TableDataInfo list(ApkLogVo apkLogVo) {
        startPage();
        List<ApkLogVo> list = apkFileUpLoadService.selectApkLogList(apkLogVo);
        return getDataTable(list);
    }

    /**
     * 查询apk上传的版本信息
     */
    @GetMapping("/getApkUploadList")
    @ApiOperation("查询apk上传的版本信息")
    public List<ApkUploadVo> getApkUploadList(ApkUploadVo apkLogVo) {
        List<ApkUploadVo> list = apkFileUpLoadService.selectApkFileUpLoadList(apkLogVo);
        return list;
    }

    /**
     * 查询apk上传的版本信息
     */
    @GetMapping("/getApkUploadListByLimit")
    @ApiOperation("查询apk上传的版本信息")
    public TableDataInfo getApkUploadListByLimit(ApkUploadVo apkLogVo) {
        startPage();
        List<ApkUploadVo> list = apkFileUpLoadService.selectApkFileUpLoadList(apkLogVo);
        return getDataTable(list);
    }


    /**
     * log新增升级日志
     */
    @Log(title = "log新增升级日志", businessType = BusinessType.INSERT)
    @PreAuthorize(hasPermi = "laboratory:apkfile:add")
    @PostMapping("/addApkLog")
    public AjaxResult addApkLog(@RequestBody ApkLogVo apkLogVo) {
        return toAjax(apkFileUpLoadService.addApkLog(apkLogVo));
    }

    /**
     * log修改升级日志
     */
    @Log(title = "log修改升级日志", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPermi = "laboratory:apkfile:update")
    @PutMapping("/updateApkLog")
    public AjaxResult updateApkLog(@RequestBody ApkLogVo apkLogVo) {
        return toAjax(apkFileUpLoadService.updateApkLog(apkLogVo));
    }


    /**
     * apk升级日志信息查询（一体机调用）
     */
    @GetMapping("/onepcApkDetail/{hardwareNum}")
    public AjaxResult onepcApkDetail(@PathVariable("hardwareNum") String hardwareNum) {
        ApkLogVo apkLogVo = new ApkLogVo();
        apkLogVo.setOnepcHardwareNum(hardwareNum);
        ApkLogVo detail = apkFileUpLoadService.selectApkLogDetail(apkLogVo);
        if (detail != null) {
            String url=fileConfigUtils.getLocalUrl()+ File.separator+detail.getApkFileUpload();
            url= UrlFormatUtils.getHttpsORHttpUrl(url);
            detail.setApkFileUpload(url);
            return AjaxResult.success(detail);
        }
        return AjaxResult.error("没有需要更新的apk包");
    }


    /**
     * apk升级情况上报（一体机调用）
     */
    @Log(title = "apk升级情况上报（一体机调用）", businessType = BusinessType.UPDATE)
    @PutMapping("/onepcApkUpdate/{hardwareNum}/{logStatus}")
    public AjaxResult onepcApkUpdate(@PathVariable("hardwareNum") String hardwareNum, @PathVariable("logStatus") Integer logStatus) {
        ApkLog apkLog = new ApkLog();
        apkLog.setOnepcHardwareNum(hardwareNum);
        apkLog.setLogStatus(logStatus);
        return toAjax(apkFileUpLoadService.updateApkLogByStatus(apkLog));
    }


    /**
     * 删除apk上传的包信息
     */
    @ApiOperation(value = "删除apk上传的包信息")
    @PreAuthorize(hasPermi = "laboratory:log:remove")
    @Log(title = "删除apk上传的包信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{apkId}")
    public ResultData remove(@PathVariable Long apkId) {
        return ResultData.result(apkFileUpLoadService.deleteApkUploadByIds(apkId));
    }
}
