package com.yunzhi.marketing.redpack.controller;

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 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.common.StringUtils;
import com.yunzhi.marketing.redpack.entity.TaH5Template;
import com.yunzhi.marketing.redpack.service.ITaH5TemplateService;

/**
 * <p>
    * H5模板  前端控制器
    * </p>
 *
 * @author fxf
 * @since 2020-06-03
 */
@RestController
@RequestMapping("/api")
public class TaH5TemplateController extends BaseController {

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

    @Autowired
    public ITaH5TemplateService iTaH5TemplateService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/channel/taH5Template",method= RequestMethod.GET)
    public ResponseBean taH5TemplateList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
									    @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                        @RequestParam(value ="h5TemplateId", required = false) String h5TemplateId,
                                        @RequestParam(value ="name", required = false) String name){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TaH5Template> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaH5Template> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(!StringUtils.isEmpty(h5TemplateId), "h5_template_id", h5TemplateId);
            queryWrapper.like(!StringUtils.isEmpty(name), "name", name);
            queryWrapper.orderByDesc("create_date");

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

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

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

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

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/taH5Template/{id}",method= RequestMethod.GET)
    public ResponseBean taH5TemplateGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaH5TemplateService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taH5TemplateDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
