package com.gxwebsoft.project.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.project.service.ProjectCollectionService;
import com.gxwebsoft.project.entity.ProjectCollection;
import com.gxwebsoft.project.param.ProjectCollectionParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 我的收藏控制器
 *
 * @author 科技小王子
 * @since 2025-03-16 12:12:13
 */
@Tag(name = "我的收藏管理")
@RestController
@RequestMapping("/api/project/project-collection")
public class ProjectCollectionController extends BaseController {
    @Resource
    private ProjectCollectionService projectCollectionService;

    @Operation(summary = "分页查询我的收藏")
    @GetMapping("/page")
    public ApiResult<PageResult<ProjectCollection>> page(ProjectCollectionParam param) {
        // 使用关联查询
        return success(projectCollectionService.pageRel(param));
    }

    @Operation(summary = "查询全部我的收藏")
    @GetMapping()
    public ApiResult<List<ProjectCollection>> list(ProjectCollectionParam param) {
        // 使用关联查询
        return success(projectCollectionService.listRel(param));
    }

    @Operation(summary = "是否收藏")
    @GetMapping("/{id}")
    public ApiResult<Boolean> get(@PathVariable("id") Integer id) {
      final ProjectCollection projectCollection = projectCollectionService.getOne(new LambdaQueryWrapper<ProjectCollection>().eq(ProjectCollection::getAppId, id).eq(ProjectCollection::getUserId, getLoginUserId()).last("limit 1"));
      if(ObjectUtil.isNotEmpty(projectCollection)){
        return success(true);
      }
      return success(false);
    }

    @OperationLog
    @Operation(summary = "加入收藏")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ProjectCollection projectCollection) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
         projectCollection.setUserId(loginUser.getUserId());
        }
        if (projectCollectionService.save(projectCollection)) {
            return success("收藏成功");
        }
        return fail("操作失败");
    }

    @OperationLog
    @Operation(summary = "修改我的收藏")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ProjectCollection projectCollection) {
        if (projectCollectionService.updateById(projectCollection)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @OperationLog
    @Operation(summary = "取消收藏")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (projectCollectionService.remove(new LambdaQueryWrapper<ProjectCollection>().eq(ProjectCollection::getAppId,id).eq(ProjectCollection::getUserId,getLoginUserId()))) {
            return success("已取消收藏");
        }
        return fail("操作失败");
    }

    @PreAuthorize("hasAuthority('project:projectCollection:save')")
    @OperationLog
    @Operation(summary = "批量添加我的收藏")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ProjectCollection> list) {
        if (projectCollectionService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('project:projectCollection:update')")
    @OperationLog
    @Operation(summary = "批量修改我的收藏")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ProjectCollection> batchParam) {
        if (batchParam.update(projectCollectionService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('project:projectCollection:remove')")
    @OperationLog
    @Operation(summary = "批量删除我的收藏")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (projectCollectionService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
