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.OroqenCultureContent;
import org.jeecg.modules.oroqen.service.IOroqenCultureContentService;
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/cultureContent")
@Slf4j
public class OroqenCultureContentController extends JeecgController<OroqenCultureContent, IOroqenCultureContentService> {

    @Autowired
    private IOroqenCultureContentService oroqenCultureContentService;

    /**
     * 分页列表查询
     */
    @AutoLog(value = "文化内容-分页列表查询")
    @Operation(summary = "文化内容-分页列表查询", description = "文化内容-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<OroqenCultureContent>> queryPageList(OroqenCultureContent oroqenCultureContent,
                                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                             HttpServletRequest req) {
        QueryWrapper<OroqenCultureContent> queryWrapper = QueryGenerator.initQueryWrapper(oroqenCultureContent, req.getParameterMap());
        Page<OroqenCultureContent> page = new Page<OroqenCultureContent>(pageNo, pageSize);
        IPage<OroqenCultureContent> pageList = oroqenCultureContentService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 根据分类ID查询文化内容
     */
    @AutoLog(value = "文化内容-根据分类ID查询")
    @Operation(summary = "文化内容-根据分类ID查询", description = "文化内容-根据分类ID查询")
    @GetMapping(value = "/category/{categoryId}")
    public Result<List<OroqenCultureContent>> getByCategoryId(@PathVariable String categoryId) {
        List<OroqenCultureContent> list = oroqenCultureContentService.getByCategoryId(categoryId);
        return Result.OK(list);
    }

    /**
     * 查询推荐文化内容
     */
    @AutoLog(value = "文化内容-查询推荐内容")
    @Operation(summary = "文化内容-查询推荐内容", description = "文化内容-查询推荐内容")
    @GetMapping(value = "/recommended")
    public Result<List<OroqenCultureContent>> getRecommended() {
        List<OroqenCultureContent> list = oroqenCultureContentService.getRecommended();
        return Result.OK(list);
    }

    /**
     * 添加
     */
    @AutoLog(value = "文化内容-添加")
    @Operation(summary = "文化内容-添加", description = "文化内容-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody OroqenCultureContent oroqenCultureContent) {
        oroqenCultureContentService.save(oroqenCultureContent);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     */
    @AutoLog(value = "文化内容-编辑")
    @Operation(summary = "文化内容-编辑", description = "文化内容-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody OroqenCultureContent oroqenCultureContent) {
        oroqenCultureContentService.updateById(oroqenCultureContent);
        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) {
        oroqenCultureContentService.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.oroqenCultureContentService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     */
    @AutoLog(value = "文化内容-通过id查询")
    @Operation(summary = "文化内容-通过id查询", description = "文化内容-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<OroqenCultureContent> queryById(@RequestParam(name = "id", required = true) String id) {
        OroqenCultureContent oroqenCultureContent = oroqenCultureContentService.getById(id);
        if (oroqenCultureContent == null) {
            return Result.error("未找到对应数据");
        }
        // 更新浏览次数
        oroqenCultureContentService.updateViewCount(id);
        return Result.OK(oroqenCultureContent);
    }

    /**
     * 点赞
     */
    @AutoLog(value = "文化内容-点赞")
    @Operation(summary = "文化内容-点赞", description = "文化内容-点赞")
    @PostMapping(value = "/like/{id}")
    public Result<String> like(@PathVariable String id) {
        oroqenCultureContentService.updateLikeCount(id, 1);
        return Result.OK("点赞成功!");
    }

    /**
     * 取消点赞
     */
    @AutoLog(value = "文化内容-取消点赞")
    @Operation(summary = "文化内容-取消点赞", description = "文化内容-取消点赞")
    @PostMapping(value = "/unlike/{id}")
    public Result<String> unlike(@PathVariable String id) {
        oroqenCultureContentService.updateLikeCount(id, -1);
        return Result.OK("取消点赞成功!");
    }

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OroqenCultureContent oroqenCultureContent) {
        return super.exportXls(request, oroqenCultureContent, OroqenCultureContent.class, "文化内容");
    }

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