package com.ruoyi.web.controller.cp;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.CpsContentInfo;
import com.ruoyi.system.service.ICpsContentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 发布内容Controller
 *
 * @author ruoyi
 * @date 2024-08-14
 */
@RestController
@CrossOrigin
@RequestMapping("/cp/contentInfo")
@Anonymous
public class CpsContentInfoController extends BaseController {
    @Autowired
    private ICpsContentInfoService cpsContentInfoService;

    /**
     * 查询发布内容列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CpsContentInfo cpsContentInfo) {
        startPage();
        List<CpsContentInfo> list = cpsContentInfoService.selectCpsContentInfoList(cpsContentInfo);
        return getDataTable(list);
    }


    /**
     * 导出发布内容列表
     */
    @Log(title = "发布内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CpsContentInfo cpsContentInfo) {
        List<CpsContentInfo> list = cpsContentInfoService.selectCpsContentInfoList(cpsContentInfo);
        ExcelUtil<CpsContentInfo> util = new ExcelUtil<CpsContentInfo>(CpsContentInfo.class);
        util.exportExcel(response, list, "发布内容数据");
    }

    /**
     * 获取发布内容详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(cpsContentInfoService.selectCpsContentInfoById(id));
    }

    /**
     * 新增发布内容
     */
    @Log(title = "发布内容", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CpsContentInfo cpsContentInfo) {
        return toAjax(cpsContentInfoService.insertCpsContentInfo(cpsContentInfo));
    }

    /**
     * 修改发布内容
     */
    @Log(title = "发布内容", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CpsContentInfo cpsContentInfo) {
        return toAjax(cpsContentInfoService.updateCpsContentInfo(cpsContentInfo));
    }

    /**
     * 删除发布内容
     */
    @Log(title = "发布内容", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cpsContentInfoService.deleteCpsContentInfoByIds(ids));
    }

    @Log(title = "点赞内容", businessType = BusinessType.UPDATE)
    @PostMapping("/{openId}/like")
    public AjaxResult likeContent(@RequestBody CpsContentInfo cpsContentInfo, @PathVariable String openId) {
        cpsContentInfoService.updateLikeContent(cpsContentInfo.getId(), openId);
        return AjaxResult.success();
    }

    @Log(title = "点赞内容", businessType = BusinessType.UPDATE)
    @PostMapping("/{openId}/unlike")
    public AjaxResult unlikeContent(@RequestBody CpsContentInfo cpsContentInfo, @PathVariable String openId) {
        cpsContentInfoService.updateDisLikeContent(cpsContentInfo.getId(), openId);
        return AjaxResult.success();
    }

    @Log(title = "收藏内容", businessType = BusinessType.UPDATE)
    @PostMapping("/{openId}/collect")
    public AjaxResult collectContent(@RequestBody CpsContentInfo cpsContentInfo, @PathVariable String openId) {
        cpsContentInfoService.updateCollectContent(cpsContentInfo.getId(), openId);
        return AjaxResult.success();
    }

    @Log(title = "收藏内容", businessType = BusinessType.UPDATE)
    @PostMapping("/{openId}/uncollect")
    public AjaxResult uncollectContent(@RequestBody CpsContentInfo cpsContentInfo, @PathVariable String openId) {
        cpsContentInfoService.updateDisCollectContent(cpsContentInfo.getId(), openId);
        return AjaxResult.success();
    }
}
