package com.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.platform.annotation.CanUserUpdateRecord;
import com.platform.entity.Result;
import com.platform.entity.TmSlideShow;
import com.platform.enums.ResultCodeEnum;
import com.platform.exception.CommonBaseException;
import com.platform.service.custom.TmSlideShowService;
import com.platform.utils.CommonDataUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author abnerhou
 * @date 2020/9/10 16:30
 * @desciption
 */
@Api(tags = "轮播图管理后台相关接口")
@RestController
@RequestMapping("/slideShow")
@Slf4j
public class TmSlideShowController {

    @Autowired
    private TmSlideShowService tmSlideShowService;

    @CanUserUpdateRecord
    @ApiOperation(value = "录入新的轮播图")
    @PostMapping(value = "/add")
    @ResponseBody
    public Result addNewSlideShow(@ApiParam(name = "TmSlideShow对象", value = "传入json格式", required = true) @RequestBody TmSlideShow tmSlideShow){

        if(null == tmSlideShow || !StringUtils.isNoneBlank(tmSlideShow.getTitle() , tmSlideShow.getCoverImage())){
            throw new CommonBaseException(ResultCodeEnum.D3004, "标题或者封面照片不能为空");
        }
        TmSlideShow dbSlideShow = tmSlideShowService.getSlideShowByTitle(tmSlideShow.getTitle());
        if(null != dbSlideShow){
            log.info("轮播图[{}]已经存在，无需重复添加" , tmSlideShow.getTitle());
            throw new CommonBaseException(ResultCodeEnum.D101, "轮播图已经存在，无需新增");
        }
        int insert = tmSlideShowService.insertSlideShow(tmSlideShow);
        if(insert != 1){
            log.error("新增轮播图[{}]失败" , tmSlideShow.getTitle());
            throw new CommonBaseException(ResultCodeEnum.D101, "新增轮播图失败");
        }
        return Result.success();
    }
    @ApiOperation(value = "获取轮播图列表，支持查询标题")
    @GetMapping(value = "/getAndQuery")
    @ResponseBody
    public Object getAndQuerySlideShow(String name,
                                       @RequestParam(name = "page") long page,
                                       @RequestParam(name = "limit") long limit){

        IPage<TmSlideShow> resultPage = tmSlideShowService.getSlideShowByPage(name , page , limit);
        if(null == resultPage){
            log.error("查询轮播图失败");
            throw new CommonBaseException(ResultCodeEnum.D101 , "查询轮播图失败");
        }

        return    CommonDataUtils.toResponsObject(0,"" , CommonDataUtils.assembleResultCustom(resultPage.getTotal() , resultPage.getPages() ,
                resultPage.getCurrent() ,limit , resultPage.getRecords()));
    }


    @ApiOperation(value = "根据ID获取轮播图")
    @GetMapping(value = "/getById")
    @ResponseBody
    public Object getSlideShowById(@RequestParam(name = "slideId") String slideId){
        if(StringUtils.isBlank(slideId)){
            log.error("轮播图ID不能为空");
            throw new CommonBaseException(ResultCodeEnum.D3001 , "轮播图ID不能为空");
        }
        TmSlideShow tmSlideShow = tmSlideShowService.getSlideShowById(slideId);
        return CommonDataUtils.toResponsObject(0 , "" , tmSlideShow);
    }

    @CanUserUpdateRecord
    @ApiOperation(value = "更新轮播图")
    @PostMapping(value = "/update")
    @ResponseBody
    public Object updateSlideShow( @ApiParam(name = "TmSlideShow对象", value = "传入json格式", required = true) @RequestBody TmSlideShow tmSlideShow) {

        if(null == tmSlideShow || StringUtils.isBlank(tmSlideShow.getSlideId())){
            log.error("轮播图id不能为空");
            throw new CommonBaseException(ResultCodeEnum.D3004 , "轮播图id不能为空");
        }
        TmSlideShow dbSlideShow = tmSlideShowService.getSlideShowById(tmSlideShow.getSlideId());
        if(null == dbSlideShow){
            log.error("轮播图[{}]不存在" , tmSlideShow.getSlideId());
            throw new CommonBaseException(ResultCodeEnum.D101 , "轮播图不存在");
        }
        int update = tmSlideShowService.updateSlideShow(tmSlideShow);
        if(update != 1){
            log.info("跟新轮播图[{}]失败" , tmSlideShow.getSlideId());
            throw new CommonBaseException(ResultCodeEnum.D101 , "轮播图更新失败");
        }
        return Result.success();
    }

}
