package com.ibeetl.admin.flow.controller;

import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.annotation.LogController;
import com.ibeetl.admin.core.web.JsonResult;
import com.ibeetl.admin.flow.entity.DirectedSmsTemplateEntity;
import com.ibeetl.admin.flow.query.QueryDirectedSmstemplate;
import com.ibeetl.admin.flow.service.DirectedSmsTemplateService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class DirectedSmsTemplateController {

    private static final String MODEL = "/directed/smstemplate";
    private final Log log = LogFactory.getLog(this.getClass());

    @Autowired
    private DirectedSmsTemplateService service;

    @GetMapping(MODEL + "/index.do")
    @Function("directedsms.index")
    @LogController(content = "短信模板", type = "2", description = "")
    // 用户免费资源查询
    public ModelAndView index(HttpServletRequest request,
                              HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("/directed/smstemplate/index.html");
        mv.addObject("search", DirectedSmsTemplateEntity.class.getName());
        return mv;
    }

    @PostMapping(MODEL + "/list.json")
    @Function("directedsms.query")
    @ResponseBody
    @LogController(content = "短信模板查看", type = "2", description = "")
    public JsonResult<PageQuery<DirectedSmsTemplateEntity>> index(QueryDirectedSmstemplate condition) {
        PageQuery<DirectedSmsTemplateEntity> page = condition.getPageQuery();

        service.search(page);
        return JsonResult.success(page);
    }

    @GetMapping(MODEL + "/add.do")
    @Function("directedsms.add")
    @LogController(content="新增短信模板",type="2",description="")
    public ModelAndView add() {
        ModelAndView view = new ModelAndView("/directed/smstemplate/add.html");
        return view;
    }

    @PostMapping(MODEL + "/add.json")
    @Function("directedsms.add")
    @ResponseBody
    @LogController(content = "新增短信模板", type = "2", description = "")
    public JsonResult add(DirectedSmsTemplateEntity entity) {

        if (StringUtils.isEmpty(entity.getContent())) {
            return JsonResult.failMessage("短信模板内容不能为空");
        }

        if (entity.getContent().endsWith(" ")) {
            return JsonResult.failMessage("短信模板不能以空格结尾");
        }

        if (StringUtils.isEmpty(entity.getCpid())) {
            return JsonResult.failMessage("请选择合作方！");
        }

        service.saveSmstemplate(entity);

        return JsonResult.success();
    }

    @GetMapping(MODEL + "/edit.do")
    @Function("directedsms.edit")
    @LogController(content="修改短信模板",type="2",description="")
    public ModelAndView edit(String smstype) {
        ModelAndView view = new ModelAndView("/directed/smstemplate/edit.html");
        view.addObject("entity", service.findSmsTemplateByCpSmstype(smstype));
        return view;
    }

    @PostMapping(MODEL + "/edit.json")
    @Function("directedsms.edit")
    @ResponseBody
    @LogController(content = "修改短信模板", type = "2", description = "")
    public JsonResult edit(DirectedSmsTemplateEntity entity) {
        if (StringUtils.isEmpty(entity.getContent())) {
            return JsonResult.failMessage("短信模板内容不能为空");
        }

        if (entity.getContent().endsWith(" ")) {
            return JsonResult.failMessage("短信模板不能以空格结尾");
        }

        if (StringUtils.isEmpty(entity.getCpid())) {
            return JsonResult.failMessage("请选择合作方！");
        }
        service.updateSmstemplate(entity);
        return JsonResult.success();
    }

    @PostMapping(MODEL + "/del.json")
    @Function("directedsms.del")
    @ResponseBody
    @LogController(content = "删除短信模板", type = "2", description = "")
    public JsonResult delete(DirectedSmsTemplateEntity entity) {

        service.deleteSmstemplate(entity);
        return JsonResult.success();
    }

    public static void main(String[] args) {
        System.out.println("asdf ".endsWith(" "));
    }
}
