package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
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.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.PostCollectionsSystem;
import com.ruoyi.system.domain.vo.CollectionsVo;
import com.ruoyi.system.service.IPostCollectionsService;
import com.ruoyi.system.service.IPostCollectionsSystemService;
import com.ruoyi.system.utils.CollectionStatusJudgment;
import io.swagger.annotations.Api;
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 javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

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

    /**
     * 查询藏品套系列表
     */
    @ApiOperation("查询藏品套系列表")
    @PreAuthorize("@ss.hasPermi('system:system:list')")
    @GetMapping("/list")
    public TableDataInfo list(PostCollectionsSystem postCollectionsSystem)
    {
        startPage();
        List<CollectionsVo> list = postCollectionsSystemService.selectPostCollectionsSystemListPage(postCollectionsSystem);
        return getDataTable(list);
    }

    /**
     * 导出藏品套系列表
     */
    @ApiOperation("导入藏品套系列表")
    @PreAuthorize("@ss.hasPermi('system:system:export')")
    @Log(title = "藏品套系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PostCollectionsSystem postCollectionsSystem)
    {
        List<PostCollectionsSystem> list = postCollectionsSystemService.selectPostCollectionsSystemList(postCollectionsSystem);
        ExcelUtil<PostCollectionsSystem> util = new ExcelUtil<PostCollectionsSystem>(PostCollectionsSystem.class);
        util.exportExcel(response, list, "藏品套系数据");
    }

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

    /**
     * 新增藏品套系
     */
    @ApiOperation("新增藏品套系")
    @PreAuthorize("@ss.hasPermi('system:system:add')")
    @Log(title = "藏品套系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PostCollectionsSystem postCollectionsSystem)
    {
        if (!getUsername().equals("admin")){
            //warn：601
            return warn("仅管理员拥有该权限");
        }
        //截止时间早于起售时间
        if (postCollectionsSystem.getStartTime().after(postCollectionsSystem.getEndTime())){
            return warn("请设置正确的套系时间");
        }
        //当套系售卖截止时间早于创建时间
        if (postCollectionsSystem.getEndTime().before(DateUtils.getNowDate())){
            return warn("请设置正确的套系时间");
        }
        //判断套系名称是否重复
        if (postCollectionsSystemService.selectPostCollectionsSystemByName(postCollectionsSystem) > 0){
            return warn("该套系名称已存在");
        }
        return toAjax(postCollectionsSystemService.insertPostCollectionsSystem(postCollectionsSystem));
    }

    /**
     * 修改藏品套系
     */
    @ApiOperation("修改藏品套系")
    @PreAuthorize("@ss.hasPermi('system:system:edit')")
    @Log(title = "藏品套系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PostCollectionsSystem postCollectionsSystem)
    {
        return toAjax(postCollectionsSystemService.updatePostCollectionsSystem(postCollectionsSystem));

    }

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

    /**
     * 根据套系名称进行查询
     * @param title
     * @param TimeLeft
     * @param TimeRight
     * @return
     */
    @ApiOperation("根据套系名称进行查询")
    @PreAuthorize("@ss.hasPermi('system:collections:queryCollections')")
    @GetMapping("/queryCollections")
    public TableDataInfo list(@RequestParam(value="title",required = false)String title,
                              @RequestParam(value="TimeLeft",required = false) Date TimeLeft,
                              @RequestParam(value="TimeRight",required = false) Date TimeRight)
    {
        startPage();
        List<PostCollectionsSystem> list= postCollectionsSystemService.selectByTitleAndTime(title,TimeLeft,TimeRight);
        return getDataTable(list);
    }

    /**
     *  更改套系状态（上架 /下架）
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("更改套系状态（上架 /下架）")
    @PostMapping("/status/{status}")
    public AjaxResult changeStatus(@RequestBody List<Long> id, @PathVariable String status){
        List<PostCollectionsSystem> postCollectionsSystemList = postCollectionsSystemService.listByIds(id);
        postCollectionsSystemList = postCollectionsSystemList.stream().map(
                (item) ->{
                    item.setStatus(status);
                    item.setUpdateBy(getUsername());
                    item.setUpdateTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.getTime()));
                    return item;
                }
        ).collect(Collectors.toList());
        //更改藏品的状态
        CollectionStatusJudgment.JudgmentStatus(id,status);
        return toAjax(postCollectionsSystemService.updateBatchById(postCollectionsSystemList));
    }


}
