package org.jeecg.modules.oroqen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.oroqen.entity.OroqenUserFavorite;
import org.jeecg.modules.oroqen.service.IOroqenUserFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 用户收藏
 * @Author: jeecg-boot
 * @Date: 2025-01-08
 * @Version: V1.0
 */
@Tag(name = "用户收藏", description = "用户收藏")
@RestController
@RequestMapping("/oroqen/userFavorite")
@Slf4j
public class OroqenUserFavoriteController extends JeecgController<OroqenUserFavorite, IOroqenUserFavoriteService> {

    @Autowired
    private IOroqenUserFavoriteService oroqenUserFavoriteService;

    /**
     * 分页列表查询
     */
    @AutoLog(value = "用户收藏-分页列表查询")
    @Operation(summary = "用户收藏-分页列表查询", description = "用户收藏-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<OroqenUserFavorite>> queryPageList(OroqenUserFavorite oroqenUserFavorite,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                           HttpServletRequest req) {
        QueryWrapper<OroqenUserFavorite> queryWrapper = QueryGenerator.initQueryWrapper(oroqenUserFavorite, req.getParameterMap());
        Page<OroqenUserFavorite> page = new Page<OroqenUserFavorite>(pageNo, pageSize);
        IPage<OroqenUserFavorite> pageList = oroqenUserFavoriteService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 根据用户ID查询收藏
     */
    @AutoLog(value = "用户收藏-根据用户ID查询")
    @Operation(summary = "用户收藏-根据用户ID查询", description = "用户收藏-根据用户ID查询")
    @GetMapping(value = "/user/{userId}")
    public Result<List<OroqenUserFavorite>> getByUserId(@PathVariable String userId) {
        List<OroqenUserFavorite> list = oroqenUserFavoriteService.getByUserId(userId);
        return Result.OK(list);
    }

    /**
     * 根据用户ID和收藏类型查询收藏
     */
    @AutoLog(value = "用户收藏-根据用户ID和类型查询")
    @Operation(summary = "用户收藏-根据用户ID和类型查询", description = "用户收藏-根据用户ID和类型查询")
    @GetMapping(value = "/user/{userId}/type/{favoriteType}")
    public Result<List<OroqenUserFavorite>> getByUserIdAndType(@PathVariable String userId,
                                                               @PathVariable Integer favoriteType) {
        List<OroqenUserFavorite> list = oroqenUserFavoriteService.getByUserIdAndType(userId, favoriteType);
        return Result.OK(list);
    }

    /**
     * 添加收藏
     */
    @AutoLog(value = "用户收藏-添加收藏")
    @Operation(summary = "用户收藏-添加收藏", description = "用户收藏-添加收藏")
    @PostMapping(value = "/add")
    public Result<String> addFavorite(@RequestParam String userId,
                                      @RequestParam Integer favoriteType,
                                      @RequestParam String targetId) {
        oroqenUserFavoriteService.addFavorite(userId, favoriteType, targetId);
        return Result.OK("添加收藏成功！");
    }

    /**
     * 取消收藏
     */
    @AutoLog(value = "用户收藏-取消收藏")
    @Operation(summary = "用户收藏-取消收藏", description = "用户收藏-取消收藏")
    @PostMapping(value = "/remove")
    public Result<String> removeFavorite(@RequestParam String userId,
                                         @RequestParam Integer favoriteType,
                                         @RequestParam String targetId) {
        oroqenUserFavoriteService.removeFavorite(userId, favoriteType, targetId);
        return Result.OK("取消收藏成功！");
    }

    /**
     * 检查是否已收藏
     */
    @AutoLog(value = "用户收藏-检查收藏状态")
    @Operation(summary = "用户收藏-检查收藏状态", description = "用户收藏-检查收藏状态")
    @GetMapping(value = "/check")
    public Result<Boolean> isFavorited(@RequestParam String userId,
                                       @RequestParam Integer favoriteType,
                                       @RequestParam String targetId) {
        boolean isFavorited = oroqenUserFavoriteService.isFavorited(userId, favoriteType, targetId);
        return Result.OK(isFavorited);
    }

    /**
     * 编辑
     */
    @AutoLog(value = "用户收藏-编辑")
    @Operation(summary = "用户收藏-编辑", description = "用户收藏-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody OroqenUserFavorite oroqenUserFavorite) {
        oroqenUserFavoriteService.updateById(oroqenUserFavorite);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     */
    @AutoLog(value = "用户收藏-通过id删除")
    @Operation(summary = "用户收藏-通过id删除", description = "用户收藏-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        oroqenUserFavoriteService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     */
    @AutoLog(value = "用户收藏-批量删除")
    @Operation(summary = "用户收藏-批量删除", description = "用户收藏-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.oroqenUserFavoriteService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     */
    @AutoLog(value = "用户收藏-通过id查询")
    @Operation(summary = "用户收藏-通过id查询", description = "用户收藏-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<OroqenUserFavorite> queryById(@RequestParam(name = "id", required = true) String id) {
        OroqenUserFavorite oroqenUserFavorite = oroqenUserFavoriteService.getById(id);
        if (oroqenUserFavorite == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(oroqenUserFavorite);
    }

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OroqenUserFavorite oroqenUserFavorite) {
        return super.exportXls(request, oroqenUserFavorite, OroqenUserFavorite.class, "用户收藏");
    }

    /**
     * 通过excel导入数据
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OroqenUserFavorite.class);
    }
}