package com.huiju.foyo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiju.foyo.common.BaseController;
import com.huiju.foyo.common.ConstantFoyo;
import com.huiju.foyo.common.ResponseBean;
import com.huiju.foyo.model.TaCover;
import com.huiju.foyo.service.ITaCoverService;
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.List;

/**
 * <p>
    * 案例首页封面图  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-08-21
 */
@RestController
@RequestMapping("/")
public class TaCoverController extends BaseController {

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

    @Autowired
    public ITaCoverService iTaCaseIndexCoverService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/cover", method= RequestMethod.GET)
    public ResponseBean taCaseIndexCoverList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                             @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                             @RequestParam(required = false) String type
                                             ){
        ResponseBean responseBean = new ResponseBean();
        try {
            QueryWrapper<TaCover> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(null != type, "type", type);
            queryWrapper.orderByDesc("create_time");

            responseBean.addSuccess(iTaCaseIndexCoverService.list(queryWrapper));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCaseIndexCoverList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     * @param taCaseIndexCover 实体对象
     * @return
     */
    @RequestMapping(value="/admin/cover/add", method= RequestMethod.POST)
    public ResponseBean taCaseIndexCoverAdd(
            @RequestBody TaCover taCaseIndexCover){
        ResponseBean responseBean = new ResponseBean();
        try {
            QueryWrapper<TaCover> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("type", taCaseIndexCover.getType());
            List<TaCover> taCaseIndexCoverList = iTaCaseIndexCoverService.list(queryWrapper);
            if (taCaseIndexCoverList.size() > 0){
                TaCover taCaseIndexCoverUpdate = taCaseIndexCoverList.get(0);
                taCaseIndexCoverUpdate.setUpdateTime(LocalDateTime.now());
                taCaseIndexCoverUpdate.setCoverImg(taCaseIndexCover.getCoverImg());
                if (iTaCaseIndexCoverService.updateById(taCaseIndexCoverUpdate)){
                    responseBean.addSuccess(taCaseIndexCover);
                }else {
                    responseBean.addError("fail");
                }
            }else {
                taCaseIndexCover.setStatus(ConstantFoyo.STATUS_EFFECTIVE);
                taCaseIndexCover.setCreateBy(ConstantFoyo.USER_ADMIN);
                taCaseIndexCover.setCreateTime(LocalDateTime.now());
                if (iTaCaseIndexCoverService.save(taCaseIndexCover)){
                    responseBean.addSuccess(taCaseIndexCover);
                }else {
                    responseBean.addError("fail");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCaseIndexCoverAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @ResponseBody
    @RequestMapping(value="/taCaseIndexCover/{id}", method= RequestMethod.DELETE)
    public ResponseBean taCaseIndexCoverDelete(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            if(iTaCaseIndexCoverService.removeById(id)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCaseIndexCoverDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/taCaseIndexCover/{id}",method= RequestMethod.GET)
    public ResponseBean taCaseIndexCoverGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaCaseIndexCoverService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCaseIndexCoverDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
    
    /**
     * 分页查询列表
     * @return
     */
    @RequestMapping(value="/pc/cover",method= RequestMethod.GET)
    public ResponseBean pcCaseIndexCoverList(
            @RequestParam(required = false) String type
    ){
        ResponseBean responseBean = new ResponseBean();
        try {
            QueryWrapper<TaCover> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(null != type, "type", type);
            queryWrapper.last(" limit 1");
            responseBean.addSuccess(iTaCaseIndexCoverService.list(queryWrapper));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCaseIndexCoverList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
