package com.zxdmy.excite.admin.controller.cloud.upload;

import com.zxdmy.excite.common.base.BaseController;
import com.zxdmy.excite.common.base.BaseResult;
import com.zxdmy.excite.framework.aop.AnnotationSaveReLog;
import com.zxdmy.excite.system.entity.cloud.space.CloudFile;
import com.zxdmy.excite.system.entity.req.space.ReqCloudFile;
import com.zxdmy.excite.system.entity.vo.space.CloudFileVo;
import com.zxdmy.excite.system.service.cloud.space.ICloudFileService;
import lombok.AllArgsConstructor;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.InvocationTargetException;

/**
 * 系统菜单/权限表 前端控制器
 *
 * @author 沈松
 * @since 2021-09-12
 */
@Controller
@AllArgsConstructor
@RequestMapping("/cloud/upload/")
public class UploadController extends BaseController {

    private ICloudFileService iCloudFileService;


    @RequestMapping("index/{id}")
    public String index(@PathVariable String id, ModelMap map) throws InvocationTargetException, IllegalAccessException {
        map.put("id", id);
        return "cloud/upload/index";
    }
    @PostMapping("/addSave")
    @ResponseBody
    @AnnotationSaveReLog
    public BaseResult addSave(@Validated ReqCloudFile data) {
        try {
            String oldName = data.getFileName();
            if(null!=data.getFileName()){
                String[] file = data.getFileName().split("\\.");
                if(file.length>0) {
                    data.setFileName(file[0].toString());
                }
            }

            if(null == iCloudFileService.getByData(data)){
                data.setFileName(oldName);
                if(iCloudFileService.saveFileData(data)==1){
                    return success("操作成功");
                }else{
                    return error(400, "操作失败，系统错误");
                }
            }else{
                return error(400, "操作失败，数据已存在");
            }
        } catch (Exception e) {
            return error(500, "发生错误:" + e.getMessage());
        }
    }

    @PostMapping("/editSave")
    @ResponseBody
    @AnnotationSaveReLog
    public BaseResult editSave(@Validated ReqCloudFile data) {
        try {

            if(iCloudFileService.updateFileData(data)==1){
                return success("操作成功");
            }else{
                return error(400, "操作失败，数据已存在");
            }
        } catch (Exception e) {
            return error(500, "发生错误:" + e.getMessage());
        }
    }

    @RequestMapping("changeIndex/{id}")
    public String changeIndex(@PathVariable String id, ModelMap map) throws InvocationTargetException, IllegalAccessException {
        map.put("id", id);
        return "cloud/upload/changeIndex";
    }
}
