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


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.req.collection.ReqCloudUserCollection;
import com.zxdmy.excite.system.entity.req.lately.ReqCloudUserLately;
import com.zxdmy.excite.system.entity.vo.space.CloudFileVo;
import com.zxdmy.excite.system.service.cloud.collection.ICloudUserCollectionService;
import com.zxdmy.excite.system.service.cloud.lately.ICloudUserLatelyService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 最近文件表 前端控制器
 * </p>
 *
 * @author 沈松
 * @since 2026-01-29
 */
@Controller
@AllArgsConstructor
@RequestMapping("/cloud/lately")
public class CloudUserLatelyController extends BaseController {

    private ICloudUserLatelyService iCloudUserLatelyService;

    @RequestMapping("index")
    public String index() {
        return "cloud/lately/index";
    }


    @GetMapping("/list")
    @ResponseBody
    public BaseResult getPageList(Integer page, Integer limit, ReqCloudUserLately data) {
        try {
            data.setUserId(StpUtil.getLoginIdAsInt());
            PageList<CloudFileVo> dataPage = iCloudUserLatelyService.queryByPage(page, limit, data);
            return success("获取列表成功", dataPage.getList(), (int) dataPage.getTotal());
        } catch (Exception e) {
            return error(500, "发生错误:" + e.getMessage());
        }
    }


    @PostMapping("/addSave")
    @ResponseBody
    @AnnotationSaveReLog
    public BaseResult addSave(@Validated ReqCloudUserLately data) {
        try {
            data.setUserId(StpUtil.getLoginIdAsInt());
            if(null == iCloudUserLatelyService.getByData(data)){
                if(iCloudUserLatelyService.saveData(data)==1){
                    return success("操作成功");
                }else{
                    return error(400, "操作失败，系统错误");
                }
            }else{
                return error(400, "操作失败，数据已存在");
            }
        } catch (Exception e) {
            return error(500, "发生错误:" + e.getMessage());
        }
    }

}
