package com.yunzhi.marketing.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.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.entity.TdPosterTemplate;
import com.yunzhi.marketing.service.ITdPosterTemplateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
    *   前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-10-23
 */
@RestController
@RequestMapping("/api")
public class TdPosterTemplateController extends BaseController {

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

    @Autowired
    public ITdPosterTemplateService iTdPosterTemplateService;


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

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

    /**
     * 保存对象
     * @param tdPosterTemplate 实体对象
     * @return
     */
    @RequestMapping(value="/posterTemplate",method= RequestMethod.POST)
    public ResponseBean posterTemplateAdd(@RequestBody TdPosterTemplate tdPosterTemplate){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTdPosterTemplateService.save(tdPosterTemplate)){
                responseBean.addSuccess(tdPosterTemplate);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("posterTemplateAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

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

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/posterTemplate/{id}",method= RequestMethod.GET)
    public ResponseBean posterTemplateGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTdPosterTemplateService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("posterTemplateDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
    
    /**
     * 根据type获取数据
     * @param type
     * @return
     */
    @RequestMapping(value="/admin/posterTemplate",method= RequestMethod.GET)
    public ResponseBean posterTemplateList(@RequestParam(value ="type") String type, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            QueryWrapper<TdPosterTemplate> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("type",type);
            queryWrapper.orderByDesc("create_date");
            
            List<TdPosterTemplate> result = iTdPosterTemplateService.list(queryWrapper);
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("posterTemplateList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
