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.common.StringUtils;
import com.yunzhi.marketing.entity.TaMiniappTemplate;
import com.yunzhi.marketing.service.ITaMiniappTemplateService;
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;

/**
 * <p>
    * 小程序消息模板  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-09-11
 */
@RestController
@RequestMapping("/")
public class TaMiniappTemplateController extends BaseController {

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

    @Autowired
    public ITaMiniappTemplateService iTaMiniappTemplateService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/center/taMiniappTemplate/{miniAppId}",method= RequestMethod.GET)
    public ResponseBean taMiniappTemplateList(@PathVariable String miniappId,
                                              @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                              @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TaMiniappTemplate> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaMiniappTemplate> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("miniapp_id",miniappId);
            queryWrapper.orderByDesc("create_date");

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

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

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

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

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

    @GetMapping("/wx/template/of/{type}")
    public ResponseBean getTemplateByType(@PathVariable String type, HttpServletRequest request) {
        String appid = request.getHeader("appid");

        if (StringUtils.isEmpty(type) || StringUtils.isEmpty(appid)) {
            return ResponseBean.error("参数非法", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        TaMiniappTemplate tpl = iTaMiniappTemplateService.getTplByType(type, appid);
        return ResponseBean.success(tpl);
    }
}
