package com.ruoyi.web.controller.system;

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.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.PoNews;
import com.ruoyi.system.domain.PoUser;
import com.ruoyi.system.service.IPoNewsService;
import com.ruoyi.system.service.impl.PoUserServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
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.Date;
import java.util.List;

/**
 * 消息模块Controller
 *
 * @date 2023-01-15
 */
@RestController
@RequestMapping("/system/news")
public class PoNewsController extends BaseController
{
    @Autowired
    private IPoNewsService poNewsService;

    @Autowired
    private PoUserServiceImpl poUserService;

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

    /**
     * 查看详情
     * 点击详情获取一个消息详细内容根据newsId
     */
    @ApiOperation("点击详情获取一个消息详细内容根据newsId")
    @Log(title = "获取一个消息详细内容")
    @PreAuthorize("@ss.hasPermi('system:news:querycontent')")
    @GetMapping("/content/{newsId}")
    public AjaxResult getContent(@PathVariable("newsId")String newsId){
        return success(poNewsService.selectContentByNewsId(newsId));
    }

    /**
     * 标题和时间搜索消息列表
     */
    @ApiOperation("标题和时间搜索消息列表")
    @Log(title = "标题和时间搜索")
    @PreAuthorize("@ss.hasPermi('system:news:querynews')")
    @GetMapping("/queryNews")
    public TableDataInfo list(@RequestParam(value="title",required = false)String title,
                              @RequestParam(value="newsTimeStart",required = false)Date newsTimeStart,
                              @RequestParam(value="newsTimeEnd",required = false) Date newsTimeEnd)
    {
        startPage();
        List<PoNews> list= poNewsService.selectListByTitleAndTime(title,newsTimeStart,newsTimeEnd);
        return getDataTable(list);
    }

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

    /**
     * 导出消息列表
     */
    @ApiOperation("导出消息列表")
    @PreAuthorize("@ss.hasPermi('system:news:export')")
    @Log(title = "导出消息列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PoNews poNews)
    {
        List<PoNews> list = poNewsService.selectPoNewsList(poNews);
        ExcelUtil<PoNews> util = new ExcelUtil<PoNews>(PoNews.class);
        util.exportExcel(response, list, "消息数据");
    }

    /**
     * 获取消息详细信息
     */
    @ApiOperation("获取消息详细内容")
    @Log(title = "获取所有消息详情")
    @PreAuthorize("@ss.hasPermi('system:news:query')")
    @GetMapping(value = "/{newsId}")
    public AjaxResult getInfo(@PathVariable("newsId") String newsId)
    {
        return success(poNewsService.selectPoNewsByNewsId(newsId));
    }

    /**
     * 新增消息
     */
    @ApiOperation("新增消息")
    @PreAuthorize("@ss.hasPermi('system:news:add')")
    @Log(title = "新增消息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody @Param("poNews") PoNews poNews, @RequestBody @Param("poUser") PoUser poUser)
    {
        //判断当前用户是否有操作的权限
        poUserService.checkUserAllowed(poUser);

        if (UserConstants.NOT_UNIQUE.equals(poNewsService.checkPostNewsTitleUnique(poNews))){
            return AjaxResult.error("新增失败"+poNews.getNewsTitle()+"已经存在");
        }
        if(UserConstants.NOT_UNIQUE.equals(poNewsService.checkPostNewsImageUnique(poNews))){
            return AjaxResult.error("新增失败"+poNews.getImage()+"已经存在");
        }
        return toAjax(poNewsService.insertPoNews(poNews));
    }

    /**
     * 修改消息
     */
    @ApiOperation("修改消息")
    @PreAuthorize("@ss.hasPermi('system:news:edit')")
    @Log(title = "修改消息", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody PoNews poNews ,@RequestBody PoUser poUser)
    {
        //校验是否有操作的权限
        poUserService.checkUserAllowed(poUser);

        //校验是否可以访问到数据
        poUserService.checkUserDataScope(poUser.getUserId());

        if (UserConstants.NOT_UNIQUE.equals(poNewsService.checkPostNewsTitleUnique(poNews))
            ){
            return AjaxResult.error("修改失败"+poNews.getNewsTitle()+"已经存在");
        }
        return toAjax(poNewsService.updatePoNews(poNews));
    }

    /**
     * 删除消息
     */
    @ApiOperation("删除消息")
    @PreAuthorize("@ss.hasPermi('system:news:remove')")
    @Log(title = "删除消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{newsIds}")
    public AjaxResult remove(@PathVariable String[] newsIds)
    {
        if (!getUsername().equals("admin")){
            return AjaxResult.error("删除消息失败当前用户不是管理员");
        }
        return toAjax(poNewsService.deletePoNewsByNewsIds(newsIds));
    }
}

