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

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.account.CloudCompany;
import com.zxdmy.excite.system.entity.cloud.space.CloudFile;
import com.zxdmy.excite.system.entity.req.account.ReqCompany;
import com.zxdmy.excite.system.entity.req.collection.ReqCloudUserCollection;
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.space.ICloudFileService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

    private ICloudUserCollectionService iCloudUserCollectionService;

    /**
     * @return 页面：【菜单管理】页面入口
     */
    @RequestMapping("index")
    public String index() {
        return "cloud/collect/index";
    }


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


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


    @PostMapping("/removeCollection")
    @ResponseBody
    @AnnotationSaveReLog
    public BaseResult removeCollection(String id) {
        try {
            if(iCloudUserCollectionService.deleteById(id)==1){
                return success("操作成功");
            }else{
                return error(400, "操作失败，系统错误");
            }
        } catch (Exception e) {
            return error(500, "发生错误:" + e.getMessage());
        }
    }
}
