package com.jeesite.modules.yinet.web;

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

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.alibaba.fastjson.JSONValidator;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.yinet.entity.YinetMessageType;
import com.jeesite.modules.yinet.service.YinetMessageTypeService;

/**
 * 消息类型Controller
 * @author xqx
 * @version 2022-06-18
 */
@Controller
@RequestMapping(value = "${adminPath}/yinet/yinetMessageType")
public class YinetMessageTypeController extends BaseController {

	@Autowired
	private YinetMessageTypeService yinetMessageTypeService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public YinetMessageType get(Long messageTypeId, boolean isNewRecord) {
		return yinetMessageTypeService.get(String.valueOf(messageTypeId), isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("yinet:yinetMessageType:view")
	@RequestMapping(value = {"list", ""})
	public String list(YinetMessageType yinetMessageType, Model model) {
		model.addAttribute("yinetMessageType", yinetMessageType);
		return "modules/yinet/yinetMessageTypeList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("yinet:yinetMessageType:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<YinetMessageType> listData(YinetMessageType yinetMessageType, HttpServletRequest request, HttpServletResponse response) {
		yinetMessageType.setPage(new Page<>(request, response));
		Page<YinetMessageType> page = yinetMessageTypeService.findPage(yinetMessageType);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("yinet:yinetMessageType:view")
	@RequestMapping(value = "form")
	public String form(YinetMessageType yinetMessageType, Model model) {
		model.addAttribute("yinetMessageType", yinetMessageType);
		return "modules/yinet/yinetMessageTypeForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("yinet:yinetMessageType:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated YinetMessageType yinetMessageType) {
		yinetMessageTypeService.save(yinetMessageType);
		return renderResult(Global.TRUE, text("保存消息类型成功！"));
	}

	/**
	 * 删除数据
	 */
	@RequiresPermissions("yinet:yinetMessageType:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(YinetMessageType yinetMessageType) {
		yinetMessageTypeService.delete(yinetMessageType);
		return renderResult(Global.TRUE, text("删除消息类型成功！"));
	}

	/**
	 * 列表选择对话框
	 */
	@RequiresPermissions("yinet:yinetMessageType:view")
	@RequestMapping(value = "yinetMessageTypeSelect")
	public String yinetMessageTypeSelect(YinetMessageType yinetMessageType, String selectData, Model model) {
		String selectDataJson = EncodeUtils.decodeUrl(selectData);
		if (selectDataJson != null && JSONValidator.from(selectDataJson).validate()){
			model.addAttribute("selectData", selectDataJson);
		}
		model.addAttribute("yinetMessageType", yinetMessageType);
		return "modules/yinet/yinetMessageTypeSelect";
	}

}
