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.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.post.domain.PostPublisher;
import com.ruoyi.post.service.IPostPublisherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * @Author HuiANan
 * @Date 2023/1/25 20:33
 * @Exegesis:
 */
@RestController
@RequestMapping("/post/publisher")
public class PostPublisherController extends BaseController
{
    @Autowired
    private IPostPublisherService postPublisherService;

    /**
     * 查询邮贝发行方信息;列表
     */
    @PreAuthorize("@ss.hasPermi('system:publisher:list')")
    @GetMapping("/list")
    public TableDataInfo list(PostPublisher postPublisher)
    {
        startPage();
        List<PostPublisher> list = postPublisherService.selectPostPublisherList(postPublisher);
        return getDataTable(list);
    }

    /**
     * 导出邮贝发行方信息;列表
     */
    @PreAuthorize("@ss.hasPermi('system:publisher:export')")
    @Log(title = "邮贝发行方信息;", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PostPublisher postPublisher)
    {
        List<PostPublisher> list = postPublisherService.selectPostPublisherList(postPublisher);
        ExcelUtil<PostPublisher> util = new ExcelUtil<PostPublisher>(PostPublisher.class);
        util.exportExcel(response, list, "邮贝发行方信息;数据");
    }

    /**
     * 获取邮贝发行方信息;详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:publisher:query')")
    @GetMapping(value = "/{publisherId}")
    public AjaxResult getInfo(@PathVariable("publisherId") Long publisherId)
    {
        return success(postPublisherService.selectPostPublisherByPublisherId(publisherId));
    }

    /**
     * 新增邮贝发行方信息;
     */
    @PreAuthorize("@ss.hasPermi('system:publisher:add')")
    @Log(title = "邮贝发行方信息;", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PostPublisher postPublisher)
    {
        if(UserConstants.NOT_UNIQUE.equals(postPublisherService.checkPublisherName(postPublisher)))
        {
            return AjaxResult.error("新增发行方"+postPublisher.getPublisherName()+"失败，发行方名称已经存在");
        }
        else if(UserConstants.NOT_UNIQUE.equals(postPublisherService.checkImage(postPublisher)))
        {
            return AjaxResult.error("新增发行方"+postPublisher.getPublisherImage()+"失败，发行方头像已经存在");
        }
        postPublisher.setCreateBy(getUsername());
        return toAjax(postPublisherService.insertPostPublisher(postPublisher));
    }

    /**
     * 修改邮贝发行方信息;
     */
    @PreAuthorize("@ss.hasPermi('system:publisher:edit')")
    @Log(title = "邮贝发行方信息;", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PostPublisher postPublisher)
    {
        if(UserConstants.NOT_UNIQUE.equals(postPublisherService.checkPublisherName(postPublisher)))
        {
            return AjaxResult.error("新增发行方"+postPublisher.getPublisherName()+"失败，发行方名称已经存在");
        }
        else if(UserConstants.NOT_UNIQUE.equals(postPublisherService.checkImage(postPublisher)))
        {
            return AjaxResult.error("新增发行方"+postPublisher.getPublisherImage()+"失败，发行方头像已经存在");
        }
        postPublisher.setCreateBy(getUsername());
        return toAjax(postPublisherService.updatePostPublisher(postPublisher));
    }

    /**
     * 删除邮贝发行方信息;
     */
    @PreAuthorize("@ss.hasPermi('system:publisher:remove')")
    @Log(title = "邮贝发行方信息;", businessType = BusinessType.DELETE)
    @DeleteMapping("/{publisherIds}")
    public AjaxResult remove(@PathVariable Long[] publisherIds)
    {
        return toAjax(postPublisherService.deletePostPublisherByPublisherIds(publisherIds));
    }
}