package com.bonus.zlpt.home.controller;

import com.bonus.zlpt.common.core.utils.poi.ExcelUtil;
import com.bonus.zlpt.common.core.web.controller.BaseController;
import com.bonus.zlpt.common.core.web.domain.AjaxResult;
import com.bonus.zlpt.common.core.web.page.TableDataInfo;
import com.bonus.zlpt.common.security.utils.SecurityUtils;
import com.bonus.zlpt.home.pojo.BmTopicDto;
import com.bonus.zlpt.home.pojo.BmTopicInfo;
import com.bonus.zlpt.home.service.BmTopicInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 专题资讯表(BmTopicInfo)表控制层
 *
 * @author makejava
 * @since 2023-12-04 09:48:36
 */
@Api(value = "专题资讯表(BmTopicInfo)表控制层", tags = {"专题资讯表"})
@RestController
@RequestMapping("bmTopicInfo")
public class BmTopicInfoController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private BmTopicInfoService bmTopicInfoService;

    /**
     * 获取专题资讯列表
     * @param bmTopicDto
     * @return
     */
    @ApiOperation("获取专题资讯列表")
    @PostMapping("/list")
    public TableDataInfo getSpecialInfoList(@RequestBody BmTopicDto bmTopicDto)
    {
        startPage(bmTopicDto.getPageNum(), bmTopicDto.getPageSize());
        List<BmTopicInfo> list = bmTopicInfoService.selectSpecialInfoList(bmTopicDto);
        return getDataTable(list);
    }

    /**
     * 新增专题资讯信息
     * @param bmTopicInfo
     * @return
     */
    @ApiOperation("新增专题资讯信息")
    @PostMapping("/add")
    public AjaxResult addCarouselChartInfo(@RequestBody BmTopicInfo bmTopicInfo)
    {
        Long userId = SecurityUtils.getUserId();
        bmTopicInfo.setCreator(userId.intValue());
        return toAjax(bmTopicInfoService.insertConfig(bmTopicInfo));
    }

    /**
     * 根据id查询资讯信息
     * @param id
     * @return
     */
    @ApiOperation("根据id查询资讯信息")
    @GetMapping("/{id}")
    public AjaxResult getSpecialInfo(@PathVariable("id") String id)
    {
        BmTopicInfo bmTopicInfo = bmTopicInfoService.selectListById(id);
        Integer num = bmTopicInfo.getViewNum();
        AtomicInteger atomicInteger = new AtomicInteger(num);
        bmTopicInfoService.insertNum(id, atomicInteger.addAndGet(1));
        return success(bmTopicInfo);
    }

    /**
     * 编辑专题资讯信息
     * @param bmTopicInfo
     * @return
     */
    @ApiOperation("编辑专题资讯信息")
    @PutMapping("/update")
    public AjaxResult updateCarouselChartInfo(@RequestBody BmTopicInfo bmTopicInfo)
    {
        return toAjax(bmTopicInfoService.update(bmTopicInfo));
    }

    /**
     * 删除专题资讯信息
     * @param id
     * @return
     */
    @ApiOperation("删除专题资讯信息")
    @DeleteMapping("/{id}")
    public AjaxResult delSpecialInfo(@PathVariable("id") String id)
    {
        return toAjax(bmTopicInfoService.deleteById(id));
    }

    /**
     * 导出专题资讯信息
     * @param response
     * @param
     */
    @ApiOperation("导出专题资讯信息")
    @GetMapping("/export")
    public void expSpecialInfo(HttpServletResponse response) throws UnsupportedEncodingException {
        ExcelUtil<BmTopicInfo> util = new ExcelUtil<BmTopicInfo>(BmTopicInfo.class);
        util.exportExcel(response, bmTopicInfoService.selectSpecialInfoList(new BmTopicDto()), "专题资讯信息", "","专题资讯信息");
    }
}

