package com.ruoyi.web.controller.post;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.post.domain.PostNews;
import com.ruoyi.post.service.IPostNewsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * @Author HuiANan
 * @Date 2023/1/15 18:40
 * @Exegesis:
 */
@RestController
@RequestMapping("/post/news")
public class PostNewsController extends BaseController
{
    @Autowired
    private IPostNewsService postNewsService;

    /**
     * 查询消息;
     */
    @ApiOperation("查询消息列表")
    @Log(title = "分页查询消息")
    @PreAuthorize("@ss.hasPermi('system:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(PostNews postNews)
    {
        startPage();
        List<PostNews> list = postNewsService.selectPostNewsList(postNews);
        return getDataTable(list);
    }

    /**
     * 导入用户手机号
     */
    @Log(title = "用户手机导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport,PostNews postNews) throws Exception
    {
        ExcelUtil<PostNews> util = new ExcelUtil<PostNews>(PostNews.class);
        List<PostNews> phoneList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = postNewsService.importPhone(phoneList, updateSupport, operName,postNews);
        return success(message);
    }

    /**
     * 导出消息;
     */
    @PreAuthorize("@ss.hasPermi('system:news:export')")
    @Log(title = "通知消息管理;", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PostNews postNews)
    {
        List<PostNews> list = postNewsService.selectPostNewsList(postNews);
        ExcelUtil<PostNews> util = new ExcelUtil<PostNews>(PostNews.class);
        util.exportExcel(response, list, "通知消息管理; ");
    }

    /**
     * 获取消息;
     */
    @ApiOperation("点击详情")
    @Log()
    @PreAuthorize("@ss.hasPermi('system:news:query')")
    @GetMapping(value = "/{newsId}")
    public AjaxResult getInfo(@PathVariable("newsId") Integer newsId)
    {
        return success(postNewsService.selectPostNewsByNewsId(newsId));
    }

    /**
     * 新增消息;
     */
    @PreAuthorize("@ss.hasPermi('system:news:add')")
    @Log(title = "通知消息管理; ", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PostNews postNews)
    {
        if(UserConstants.NOT_UNIQUE.equals(postNewsService.checkTitle(postNews)))
        {
            return AjaxResult.error("新增消息"+postNews.getNewsTitle()+"失败，消息标题已经存在");
        }
        else if(UserConstants.NOT_UNIQUE.equals(postNewsService.checkContent(postNews)))
        {
            return AjaxResult.error("新增消息"+postNews.getNewsContent()+"失败，消息内容已经存在");
        }
        postNews.setCreateBy(getUsername());
        return toAjax(postNewsService.insertPostNews(postNews));
    }

    /**
     * 修改消息;
     */
    @PreAuthorize("@ss.hasPermi('system:news:edit')")
    @Log(title = "通知消息管理;", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody PostNews postNews)
    {
        if(UserConstants.NOT_UNIQUE.equals(postNewsService.checkTitle(postNews)))
        {
            return AjaxResult.error("修改消息"+postNews.getNewsTitle()+"失败，消息标题已经存在");
        }
        else if(UserConstants.NOT_UNIQUE.equals(postNewsService.checkContent(postNews)))
        {
            return AjaxResult.error("修改消息"+postNews.getNewsContent()+"失败，消息内容已经存在");
        }
        postNews.setCreateBy(getUsername());
        return toAjax(postNewsService.insertPostNews(postNews));
    }

    /**
     * 删除消息;
     */
    @PreAuthorize("@ss.hasPermi('system:news:remove')")
    @Log(title = "通知消息管理; ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{newsIds}")
    public AjaxResult remove(@PathVariable Integer[] newsIds)
    {

        return toAjax(postNewsService.deletePostNewsByNewsIds(newsIds));
    }

}