package com.huijy.web.controller.management;

import java.util.List;

import com.huijy.common.core.domain.entity.SysUser;
import com.huijy.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.huijy.common.annotation.Log;
import com.huijy.common.core.controller.BaseController;
import com.huijy.common.core.domain.AjaxResult;
import com.huijy.common.enums.BusinessType;
import com.huijy.management.domain.Content;
import com.huijy.management.service.IContentService;
import com.huijy.common.utils.poi.ExcelUtil;
import com.huijy.common.core.page.TableDataInfo;

/**
 * 内容管理Controller
 *
 * @author luobo
 * @date 2021-09-12
 */
@RestController
@RequestMapping("/management/content")
public class ContentController extends BaseController
{
    @Autowired
    private IContentService contentService;

    /**
     * 查询内容管理列表
     */
    @PreAuthorize("@ss.hasPermi('management:content:list')")
    @GetMapping("/list")
    public TableDataInfo list(Content content)
    {
        startPage();
        List<Content> list = contentService.selectContentList(content);
        return getDataTable(list);
    }

    /**
     * 导出内容管理列表
     */
    @PreAuthorize("@ss.hasPermi('management:content:export')")
    @Log(title = "内容管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Content content)
    {
        List<Content> list = contentService.selectContentList(content);
        ExcelUtil<Content> util = new ExcelUtil<Content>(Content.class);
        return util.exportExcel(list, "内容管理数据");
    }

    /**
     * 获取内容管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('management:content:query')")
    @GetMapping(value = "/{contentId}")
    public AjaxResult getInfo(@PathVariable("contentId") Long contentId)
    {
        return AjaxResult.success(contentService.selectContentByContentId(contentId));
    }

    /**
     * 新增内容管理
     */
    @PreAuthorize("@ss.hasPermi('management:content:add')")
    @Log(title = "内容管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Content content)
    {

        content.setCreateAt(getUserId());
        return toAjax(contentService.insertContent(content));
    }

    /**
     * 修改内容管理
     */
    @PreAuthorize("@ss.hasPermi('management:content:edit')")
    @Log(title = "内容管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Content content)
    {
        return toAjax(contentService.updateContent(content));
    }

    /**
     * 删除内容管理
     */
    @PreAuthorize("@ss.hasPermi('management:content:remove')")
    @Log(title = "内容管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{contentIds}")
    public AjaxResult remove(@PathVariable Long[] contentIds)
    {
        return toAjax(contentService.deleteContentByContentIds(contentIds));
    }

    @PreAuthorize("@ss.hasPermi('management:content:audit')")
    @Log(title = "初审内容", businessType = BusinessType.UPDATE)
    @PostMapping("/audit")
    public AjaxResult updateAudit(@RequestBody Content content){
        contentService.updateAudit(content.getContentId(),content.getAuditFlag(),content.getAuditMsg(),getUserId());
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('management:content:endAudit')")
    @Log(title = "终审内容", businessType = BusinessType.UPDATE)
    @PostMapping("/endAudit")
    public AjaxResult updateEndAudit(@RequestBody Content content){
        contentService.updateAudit(content.getContentId(),content.getAuditFlag(),content.getEndAuditMsg(),getUserId());
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('management:content:release')")
    @Log(title = "发布内容", businessType = BusinessType.UPDATE)
    @PostMapping("/release")
    public AjaxResult updateRelease(@RequestBody Content content){
        contentService.updateRelease(content.getContentId(),content.getReleaseFlag(),getUserId());
        return AjaxResult.success();
    }
}
