package com.fdkankan.platform.agent.controller.manager;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.controller.BaseController;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.response.ResultData;
import com.fdkankan.common.util.FileMd5Util;
import com.fdkankan.platform.agent.entity.AppFile;
import com.fdkankan.platform.agent.request.RequestAppFile;
import com.fdkankan.platform.agent.service.IAppFileService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Log4j2
@RestController
@RequestMapping("/api/manager/app")
public class ManagerAPPController extends BaseController {

    @Autowired
    private IAppFileService managerAppService;


    @PostMapping(value = "/upload", consumes = { "multipart/form-data" })
    private ResultData upload(@RequestParam("file") MultipartFile file) {
        // 将文件转字节-> 字符串
        String filePath = managerAppService.upload(file);
        String md5 = FileMd5Util.getFileMD5(filePath);
        Map<String,String> result = new HashMap<>(2);
        result.put("md5",md5);
        return ResultData.ok(md5);
    }

    @PostMapping(value = "/list")
    private Page<AppFile> list(@RequestBody RequestAppFile param) {
        return managerAppService.getPageByParam(param);
    }

    @PostMapping(value = "/delete")
    private ResultData delete(@RequestBody RequestAppFile param) {
        if(ObjectUtils.isEmpty(param.getId())){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        managerAppService.removeById(param.getId());
        return ResultData.ok();
    }

    @PostMapping(value = "/agenetList")
    private List<String> agenetList(@RequestBody RequestAppFile param) {
        return managerAppService.agenetList(param.getName());
    }

    @PostMapping(value = "/save")
    private ResultData save(@RequestBody RequestAppFile param) {
        managerAppService.saveByParam(param);
        return ResultData.ok();
    }


}
