package com.huiju.foyo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huiju.foyo.common.BaseController;
import com.huiju.foyo.common.ConstantFoyo;
import com.huiju.foyo.common.ResponseBean;
import com.huiju.foyo.model.TaCarouselPicture;
import com.huiju.foyo.service.ITaCarouselPictureService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
    * 轮播图  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-08-21
 */
@RestController
@RequestMapping("/")
public class TaCarouselPictureController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(TaCarouselPictureController.class);

    @Autowired
    public ITaCarouselPictureService iTaCarouselPictureService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/carousel/list",method= RequestMethod.GET)
    public ResponseBean taCarouselPictureList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                              @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TaCarouselPicture> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaCarouselPicture> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("create_time");

            IPage<TaCarouselPicture> result = iTaCarouselPictureService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCarouselPictureList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     * @param taCarouselPicture 实体对象
     * @return
     */
    @RequestMapping(value="/admin/carousel/add",method= RequestMethod.POST)
    public ResponseBean taCarouselPictureAdd(@RequestBody TaCarouselPicture taCarouselPicture){
        ResponseBean responseBean = new ResponseBean();
        try {
            taCarouselPicture.setCreateBy(ConstantFoyo.USER_ADMIN);
            taCarouselPicture.setCreateTime(LocalDateTime.now());
            taCarouselPicture.setStatus(ConstantFoyo.STATUS_EFFECTIVE);
            if (iTaCarouselPictureService.save(taCarouselPicture)){
                responseBean.addSuccess(taCarouselPicture);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCarouselPictureAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     */
    @ResponseBody
    @RequestMapping(value="/admin/carousel/delete", method= RequestMethod.DELETE)
    public ResponseBean taCarouselPictureDelete(@RequestBody String[] ids){
        ResponseBean responseBean = new ResponseBean();
        try {
            List<String> idList = Arrays.asList(ids);
            if(iTaCarouselPictureService.removeByIds(idList)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCarouselPictureDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param taCarouselPicture 实体对象
     * @return
     */
    @RequestMapping(value="/admin/taCarouselPicture/{id}",method= RequestMethod.PUT)
    public ResponseBean taCarouselPictureUpdate(@PathVariable Integer id,
                                        @RequestBody TaCarouselPicture taCarouselPicture){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaCarouselPictureService.updateById(taCarouselPicture)){
                responseBean.addSuccess(taCarouselPicture);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCarouselPictureUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/admin/taCarouselPicture/{id}",method= RequestMethod.GET)
    public ResponseBean taCarouselPictureGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaCarouselPictureService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCarouselPictureDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
    
    /**
     * 分页查询列表
     * @return
     */
    @RequestMapping(value="/pc/carousel/list",method= RequestMethod.GET)
    public ResponseBean pcCaseList(){
        ResponseBean responseBean = new ResponseBean();
        try {
            QueryWrapper<TaCarouselPicture> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status", ConstantFoyo.STATUS_EFFECTIVE);
            queryWrapper.orderByDesc("sort");
            queryWrapper.orderByDesc("create_time");
            queryWrapper.last("limit 3");
            responseBean.addSuccess(iTaCarouselPictureService.list(queryWrapper));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCaseList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
