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.CpsUsrContentRel;
import com.ruoyi.system.service.ICpsUsrContentRelService;
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-22
 */
@RestController
@CrossOrigin
@Anonymous
@RequestMapping("/cp/usrContentRel")
public class CpsUsrContentRelController extends BaseController {
    @Autowired
    private ICpsUsrContentRelService cpsUsrContentRelService;

    /**
     * 查询用户资讯信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CpsUsrContentRel cpsUsrContentRel) {
        startPage();
        List<CpsUsrContentRel> list = cpsUsrContentRelService.selectCpsUsrContentRelList(cpsUsrContentRel);
        return getDataTable(list);
    }

    /**
     * 导出用户资讯信息列表
     */
    @Log(title = "用户资讯信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CpsUsrContentRel cpsUsrContentRel) {
        List<CpsUsrContentRel> list = cpsUsrContentRelService.selectCpsUsrContentRelList(cpsUsrContentRel);
        ExcelUtil<CpsUsrContentRel> util = new ExcelUtil<CpsUsrContentRel>(CpsUsrContentRel.class);
        util.exportExcel(response, list, "用户资讯信息数据");
    }

    /**
     * 获取用户资讯信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(cpsUsrContentRelService.selectCpsUsrContentRelById(id));
    }

    @GetMapping(value = "/{openId}/{id}")
    public AjaxResult getInfoByOpenId(@PathVariable("openId") String openId, @PathVariable("id") Long id) {
        return success(cpsUsrContentRelService.selectCpsUsrContentRelList(new CpsUsrContentRel() {{
            setOpenId(openId);
            setContentId(id);
        }}));
    }

    /**
     * 修改用户资讯信息
     */
    @Log(title = "用户资讯信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CpsUsrContentRel cpsUsrContentRel) {
        return toAjax(cpsUsrContentRelService.updateCpsUsrContentRel(cpsUsrContentRel));
    }

    /**
     * 删除用户资讯信息
     */
    @Log(title = "用户资讯信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cpsUsrContentRelService.deleteCpsUsrContentRelByIds(ids));
    }
}
