package com.yfd.platform.system.controller;


import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yfd.platform.annotation.Log;
import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.modules.basedata.domain.AlgorithmModel;
import com.yfd.platform.system.domain.SysConfig;
import com.yfd.platform.system.service.ISysConfigService;
import com.yfd.platform.system.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Timestamp;


/**
 * <p>
 * 系统全局配置 前端控制器
 * </p>
 *
 *
 * @since 2022-01-19
 */
@RestController
@RequestMapping("/system/config")
@Api(tags = "系统全局配置")
public class SysConfigController {
    @Resource
    private ISysConfigService configService;

    @Resource
    private IUserService userService;

    @PostMapping("/getOneById")
    @ApiOperation("根据id查询全局配置详情记录")
    @ResponseBody
    public SysConfig getOneById(String id) {
        return configService.getOne(null);
    }

    @PostMapping("/addConfig")
    @ApiOperation("根据id查询全局配置详情记录")
    @ResponseBody
    public ResponseResult addConfig(@RequestBody SysConfig config) throws IOException, UnsupportedAudioFileException {
        if (StrUtil.isEmpty(config.getId())) {
            config.setId(IdUtil.fastSimpleUUID());
        }

        config.setLastmodifydate(new Timestamp(System.currentTimeMillis()));
        boolean ok = configService.save(config);
        return ResponseResult.success();
    }

    @PostMapping("/updateById")
    @ApiOperation("根据id修改全局配置记录")
    @ResponseBody
    public ResponseResult updateById(@RequestBody SysConfig config) {
        config.setLastmodifydate(new Timestamp(System.currentTimeMillis()));
        boolean ok = configService.saveOrUpdate(config);
        if (ok) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error("操作失败!");
        }
    }

    @Log(module = "系统全局配置", value = "上传系统操作手册", type = "0")
    @PostMapping("/uploadConfigFile")
    @ApiOperation("上传系统操作手册")
    @ResponseBody
    public ResponseResult uploadConfigFile(MultipartFile file) throws FileNotFoundException {
        if (file.isEmpty()) {
            return ResponseResult.error("文件是空的");
        }
        String filename = file.getOriginalFilename();
        String suffix = StrUtil.subAfter(filename, ".", true).toLowerCase();
        if (!"pdf".equals(suffix)) {
            return ResponseResult.error("非法文件类型");
        }
        String contentType = file.getContentType();
        if (!"application/pdf".equals(contentType)) {
            return ResponseResult.error("非法文件内容");
        }
        configService.uploadModelFile(file);
        return ResponseResult.success();
    }

    @Log(module = "系统全局配置", value = "下载操作手册", type = "0")
    @PostMapping("/downloadConfigFile")
    @ApiOperation("下载操作手册")
    public ResponseResult downloadConfigFile(String fileName, HttpServletResponse response) throws FileNotFoundException {
        if (StrUtil.isBlank(fileName)) {
            return ResponseResult.error("文件名是空的");
        }
        if (fileName.contains("\\") || fileName.contains("/")) {
            return ResponseResult.error("非法文件路径");
        }
        String suffix = StrUtil.subAfter(fileName, ".", true).toLowerCase();
        if (!"pdf".equals(suffix)) {
            return ResponseResult.error("非法文件类型");
        }

        configService.downloadConfigFile(fileName, response);
        return ResponseResult.success("下载成功");
    }

    @PostMapping("/deleteConfigFile")
    @ApiOperation("删除文件")
    public ResponseResult deleteConfigFile(String id, String fileName) {
        if (StrUtil.isBlank(fileName)) {
            throw new RuntimeException("文件名是空的");
        }
        if (fileName.contains("\\") || fileName.contains("/")) {
            throw new RuntimeException("非法文件路径");
        }
        String suffix = StrUtil.subAfter(fileName, ".", true).toLowerCase();
        if (!"pdf".equals(suffix)) {
            throw new RuntimeException("非法文件类型");
        }
        int count = configService.count(new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getId, id).eq(SysConfig::getId, fileName));
        if (count < 0) {
            return ResponseResult.error("删除失败");
        }
        boolean ok = configService.deleteConfigFile(id, fileName);
        if (ok) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error("删除失败");
        }
    }

}
