package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.vo.PostCollectionsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PostCollections;
import com.ruoyi.system.service.IPostCollectionsService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 藏品Controller
 *
 * @author ruoyi
 * @date 2023-02-15
 */
@Api(tags = "PostCollectionsController",description = "藏品")
@RestController
@RequestMapping("/system/collections")
public class PostCollectionsController extends BaseController
{
    @Autowired
    private IPostCollectionsService postCollectionsService;

    /**
     * 查询藏品列表
     */
    @ApiOperation("查询藏品列表")
    @PreAuthorize("@ss.hasPermi('system:collections:list')")
    @GetMapping(value = "/list/{id}")
    public TableDataInfo list(@PathVariable("id") Long id)
    {
        startPage();
        List<PostCollections> list = postCollectionsService.selectPostCollectionsList(id);
        return getDataTable(list);
    }


    /**
     * 获取藏品详细信息
     */
    @ApiOperation("获取藏品详细信息")
    @PreAuthorize("@ss.hasPermi('system:collections:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(postCollectionsService.selectPostCollectionsById(id));
    }

    /**
     * 新增藏品
     */
    @ApiOperation("新增藏品")
    @PreAuthorize("@ss.hasPermi('system:collections:add')")
    @Log(title = "藏品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PostCollectionsVo postCollectionsVo)
    {
        //截止时间早于起售时间,套系售卖截止时间早于创建时间
        if (postCollectionsVo.getStartTime().after(postCollectionsVo.getEndTime())||
                postCollectionsVo.getEndTime().before(DateUtils.getNowDate())){
            return warn("请设置正确的套系时间");
        }
        //判断套系名称是否重复
        if (postCollectionsService.selectPostCollectionsByName(postCollectionsVo) > 0){
            return warn("该套系名称已存在");
        }
        return postCollectionsService.insertPostCollections(postCollectionsVo);
    }

    /**
     * 修改藏品
     */
    @ApiOperation("修改藏品")
    @PreAuthorize("@ss.hasPermi('system:collections:edit')")
    @Log(title = "藏品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PostCollections postCollections)
    {
        return toAjax(postCollectionsService.updatePostCollections(postCollections));
    }

    /**
     * 删除藏品
     */
    @ApiOperation("删除藏品")
    @PreAuthorize("@ss.hasPermi('system:collections:remove')")
    @Log(title = "藏品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(postCollectionsService.deletePostCollectionsByIds(ids));
    }


}
