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


import cn.dev33.satoken.stp.StpUtil;
import com.zxdmy.excite.common.base.BaseController;
import com.zxdmy.excite.common.base.BaseResult;
import com.zxdmy.excite.common.entity.PageList;
import com.zxdmy.excite.framework.aop.AnnotationSaveReLog;
import com.zxdmy.excite.system.entity.cloud.role.CloudRole;
import com.zxdmy.excite.system.entity.cloud.role.CloudRoleUser;
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.role.ICloudRoleService;
import com.zxdmy.excite.system.service.cloud.role.ICloudRoleUserService;
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.*;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * <p>
 * 文件表 前端控制器
 * </p>
 *
 * @author ss
 * @since 2026-01-28
 */
@Controller
@AllArgsConstructor
@RequestMapping("/cloud/share")
public class CloudShareController extends BaseController {


    private ICloudFileService iCloudFileService;
    private ICloudRoleUserService iCloudRoleUserService;
    private ICloudRoleService iCloudRoleService;
    @RequestMapping("index")
    public String index() {
        return "cloud/share/index";
    }


    @GetMapping("/list")
    @ResponseBody
    public BaseResult getPageList(Integer page, Integer limit, ReqCloudFile data) {
        try {
            CloudRole cloudRole = iCloudRoleUserService.getCloudShareRole(StpUtil.getLoginIdAsInt());
            PageList<CloudFileVo> dataPage = iCloudFileService.queryByPage(page, limit, data);
            if(null!=cloudRole){
                if (cloudRole.getRoleDown()>0) {
                    for (CloudFileVo cloudFileVo:dataPage.getList()) {
                        cloudFileVo.setRoleDown(1);
                    }
                }
                if (cloudRole.getRoleUpload()>0) {
                    for (CloudFileVo cloudFileVo:dataPage.getList()) {
                        cloudFileVo.setRoleUpload(1);
                    }
                }
                if (cloudRole.getRoleDelete()>0) {
                    for (CloudFileVo cloudFileVo:dataPage.getList()) {
                        cloudFileVo.setRoleDelete(1);
                    }
                }
            }

            return success("获取数据成功", dataPage.getList(), (int) dataPage.getTotal());
        } catch (Exception e) {
            return error(500, "发生错误:" + e.getMessage());
        }
    }


    @GetMapping("/getData")
    @ResponseBody
    public BaseResult getData(String id) {
        try {
            CloudFileVo data = new CloudFileVo();
            CloudFile cloudFile = iCloudFileService.getById(id);
            if(null!=cloudFile){
                BeanUtils.copyProperties(data, cloudFile);
            }
            if(null!=cloudFile){
                return success("获取数据成功",data);
            }else{
                return error(400, "数据不存在！");
            }


        } catch (Exception e) {
            return error(500, "发生错误:" + e.getMessage());
        }
    }


}
