package com.palmnest.webapp.controller.backstage;

import com.alibaba.fastjson.JSONObject;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.core.util.HttpKit;
import com.palmnest.application.core.util.MD5;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.application.dto.OrgDto;
import com.palmnest.application.dto.OrgMeetDto;
import com.palmnest.model.User;
import com.palmnest.model.notice.EmailConfigModel;
import com.palmnest.model.notice.MobileConfigModel;
import com.palmnest.model.notice.WxtemplateMessageConfigModel;
import com.palmnest.model.notice.WxtemplateMessageTypeModel;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.service.AppUserService;
import com.palmnest.service.OrganizationManager;
import com.palmnest.service.dinner.manager.DeptManager;
import com.palmnest.service.estateserve.PublicNoManager;
import com.palmnest.service.estateserve.VirtualArchManager;
import com.palmnest.service.notice.EmailConfigManager;
import com.palmnest.service.notice.MobileConfigManager;
import com.palmnest.service.notice.WxtemplateMessageConfigManager;
import com.palmnest.service.notice.WxtemplateMessageTypeManager;
import com.palmnest.webapp.controller.BaseFormController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Slf4j
@Controller
@RequestMapping(value = "/admin/templ/*")
public class TempInfoController extends BaseFormController {


	@Autowired
	private EmailConfigManager             emailConfigManager;
	@Autowired
	private WxtemplateMessageTypeManager   wxtemplateMessageTypeManager;
	@Autowired
	private WxtemplateMessageConfigManager wxtemplateMessageConfigManager;
	@Autowired
	private OrganizationManager            organizationManager;
	@Autowired
	private AppUserService                 appUserService;
	@Autowired
	private PublicNoManager                publicNoManager;
	@Autowired
	private MobileConfigManager            mobileConfigManager;
	@Autowired
	private VirtualArchManager             virtualArchManager;
	@Autowired
	private DeptManager                    deptManager;

	@RequestMapping(value = "jumPwxTemplate")
	public ModelAndView jumPwxTemplate(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Model        model = new ExtendedModelMap();
		User         user  = (User) request.getSession().getAttribute("UserCache");
		List<OrgDto> dtos  = appUserService.getUserOrgs(user.getId());
		model.addAttribute("orgs", dtos);
		return new ModelAndView("publicno/wxTemplate", model.asMap());
	}

	@RequestMapping(value = "jumpEmailSetting")
	public ModelAndView jumpVirModel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Model        model = new ExtendedModelMap();
		User         user  = (User) request.getSession().getAttribute("UserCache");
		List<OrgDto> dtos  = appUserService.getUserOrgs(user.getId());
		model.addAttribute("orgs", dtos);
		return new ModelAndView("admin/emailSetting/emailSetting", model.asMap());
	}

	@RequestMapping(value = "/getOrgList")
	@ResponseBody
	public AppResultDto getPublicList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			User         user = (User) request.getSession().getAttribute("UserCache");
			List<OrgDto> dtos = appUserService.getUserOrgs(user.getId());
			return AppResultUtil.createSuccesDto(dtos);
		} catch (Exception e) {
			e.printStackTrace();
			return AppResultUtil.createErrorDto("100", "获取异常：" + e.getMessage());
		}


	}

	/**
	 * 根据项目名称模糊搜索
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getOrgListByName")
	@ResponseBody
	public AppResultDto getOrgListByName(HttpServletRequest request, String orgName) throws Exception {
		User             user = (User) request.getSession().getAttribute("UserCache");
		List<OrgMeetDto> list = appUserService.getUserOrgsByName(user.getId(), orgName);
//			ListPage page=PageUtils.getListPage(list, pageNo, pageSize);
//			WebPageResultDto dto=new WebPageResultDto("200","成功",page.getDataList());
//			if(list!=null&&list.size()>0){
//				dto.setCount(Long.parseLong(String.valueOf(list.size())));
//			}else{
//				dto.setCount(Long.parseLong("0"));
//			}
		return AppResultUtil.createSuccesDto(list);
	}

	/**
	 * 根据公司名称获取公司
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getVirList")
	@ResponseBody
	public AppResultDto getVirList(HttpServletRequest request, Integer virtualId, Integer level, String orgId, String virName) {
		if (!StringUtil.isNotEmpty(orgId)) {
			return AppResultUtil.createErrorDto("100", "项目不允许为空");
		}
		List<Map<String, String>> list = virtualArchManager.getVirByOrgIdAndVirName(virtualId, level, Integer.parseInt(orgId), virName);
		return AppResultUtil.createSuccesDto(list);
	}

	/**
	 * 根据公司名称获取普通未代理公司
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getVirListByNoAgant")
	@ResponseBody
	public AppResultDto getVirListByNoAgant(HttpServletRequest request, Integer id, String orgId, String virName) {
		if (!StringUtil.isNotEmpty(orgId)) {
			return AppResultUtil.createErrorDto("100", "项目不允许为空");
		}
		List<Map<String, String>> list = virtualArchManager.getVirByOrgIdAndNoAgant(id, Integer.parseInt(orgId), virName);
		return AppResultUtil.createSuccesDto(list);
	}


	/**
	 * 根据部门名称获取部门
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getDept")
	@ResponseBody
	public AppResultDto getDept(HttpServletRequest request, String orgId, String virId, String deptName) {
		if (!StringUtil.isNotEmpty(orgId)) {
			return AppResultUtil.createErrorDto("100", "项目不允许为空");
		}
		/*if (!StringUtil.isNotEmpty(virId)){
			return AppResultUtil.createErrorDto("100", "企业不允许为空");
		}*/
		List<Map<String, String>> list = deptManager.getDeptByVirIdAndDeptName(Integer.parseInt(virId), deptName);
		return AppResultUtil.createSuccesDto(list);
	}

	/**
	 * 获取所有有效的邮箱配置
	 *
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "getEmailSettingList")
	public void getEmailSettingList(HttpServletRequest request, HttpServletResponse response) {
		JSONObject   json     = new JSONObject();
		String       email    = request.getParameter("emailAddress");
		String       orgIds   = request.getParameter("orgIds");
		User         user     = (User) request.getSession().getAttribute("UserCache");
		List<OrgDto> dtos     = appUserService.getUserOrgs(user.getId());
		int          pageNo   = Integer.parseInt(request.getParameter("page"));
		int          pageSize = Integer.parseInt(request.getParameter("limit"));
		try {
			if (StringUtils.isEmpty(orgIds) || "0".equals(orgIds)) {
				if (dtos != null && dtos.size() > 0) {
					orgIds = "";
					for (int i = 0; i < dtos.size(); i++) {
						OrgDto dto = dtos.get(i);
						orgIds = orgIds + dto.getId() + ";";
					}
				}
			}
			Page page = emailConfigManager.getEmailConfigList(orgIds, email, pageNo, pageSize);
			json.put("data", page.getItems());
			json.put("count", page.getTotalCount());
			json.put("code", "200");
			json.put("msg", "");
			StringUtil.charSet(json, request, response);
		} catch (Exception e) {
			log.error(e.getMessage());
			json.put("data", "");
			json.put("count", 0);
			json.put("code", "100");
			json.put("msg", "获取有效的邮箱配置信息失败:+" + e.getMessage());
			StringUtil.charSet(json, request, response);
		}
	}

	/**
	 * 获取邮箱配置单条的配置信息
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "getEmailSetInfo")
	public void getEmailSetInfo(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		String     id   = request.getParameter("emailCongifId");
		if (StringUtils.isEmpty(id)) {
			json.put("data", "");
			json.put("code", "100");
			json.put("msg", "邮箱配置Id不允许为空");
			StringUtil.charSet(json, request, response);
		} else {
			EmailConfigModel emailInfo = emailConfigManager.get(Integer.parseInt(id));
			if (emailInfo != null) {
				TbOrganization organization = organizationManager.getOrganizationById(emailInfo.getOrgId());
				emailInfo.setOrgName(organization.getName());
				json.put("data", emailInfo);
				json.put("code", "200");
				json.put("msg", "");
				StringUtil.charSet(json, request, response);
			} else {
				json.put("data", "");
				json.put("code", "100");
				json.put("msg", "查询的邮箱配置对象为空");
				StringUtil.charSet(json, request, response);
			}
		}

	}

	/**
	 * 保存修改邮箱配置信息
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "saveAndUpdateEmailConfig")
	public void saveAndUpdateEmailConfig(@ModelAttribute("emailConfigModel") EmailConfigModel emailConfigModel, HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		User       user = (User) request.getSession().getAttribute("UserCache");
		if (emailConfigModel == null) {
			json.put("data", "");
			json.put("code", "100");
			json.put("msg", "邮箱配置对象为空");
			StringUtil.charSet(json, request, response);
		} else {
			if (emailConfigModel.getId() == 0) {//添加邮箱配置界面
				emailConfigModel.setIsEnabled("1");
				emailConfigModel.setCreateTime(new Date());
				emailConfigModel.setCreator(user.getUsername());
				EmailConfigModel emailInfo = emailConfigManager.save(emailConfigModel);
				json.put("data", emailInfo);
				json.put("code", "200");
				json.put("msg", "添加成功");
				StringUtil.charSet(json, request, response);
			} else {
				EmailConfigModel model = emailConfigManager.get(emailConfigModel.getId());
				emailConfigModel.setIsEnabled(model.getIsEnabled());
				emailConfigModel.setCreateTime(model.getCreateTime());
				emailConfigModel.setCreator(model.getCreator());
				emailConfigModel.setModifier(user.getUsername());
				emailConfigModel.setModifyTime(new Date());
				emailConfigManager.updateModel(emailConfigModel);
				json.put("data", emailConfigModel);
				json.put("code", "200");
				json.put("msg", "修改成功");
				StringUtil.charSet(json, request, response);
			}
		}

	}

	/**
	 * 删除邮箱配置信息
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "deleteEmailConfig")
	public void deleteEmailConfig(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		String     id   = request.getParameter("emailCongifId");
		if (StringUtils.isEmpty(id)) {
			json.put("data", "");
			json.put("code", "100");
			json.put("msg", "邮箱配置Id不允许为空");
			StringUtil.charSet(json, request, response);
		} else {
			EmailConfigModel emailInfo = emailConfigManager.get(Integer.parseInt(id));
			emailInfo.setIsEnabled("0");
			emailConfigManager.updateModel(emailInfo);
			json.put("data", "");
			json.put("code", "200");
			json.put("msg", "删除成功");
			StringUtil.charSet(json, request, response);
		}

	}


	/**
	 * 获取所有有效的微信模板配置信息
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "getWxTemplList")
	public void getWxTemplList(HttpServletRequest request, HttpServletResponse response) {
		JSONObject   json            = new JSONObject();
		String       orgIds          = request.getParameter("orgIds");
		User         user            = (User) request.getSession().getAttribute("UserCache");
		List<OrgDto> dtos            = appUserService.getUserOrgs(user.getId());
		String       pub             = request.getParameter("wechatAccountId");
		int          wechatAccountId = 0;//微信公众号，显示全部的就传0
		if (StringUtils.isNotEmpty(pub)) {
			wechatAccountId = Integer.parseInt(pub);
		}
		int pageNo   = Integer.parseInt(request.getParameter("page"));
		int pageSize = Integer.parseInt(request.getParameter("limit"));
		try {
			if (StringUtils.isEmpty(orgIds) || "0".equals(orgIds)) {
				if (dtos != null && dtos.size() > 0) {
					orgIds = "";
					for (int i = 0; i < dtos.size(); i++) {
						OrgDto dto = dtos.get(i);
						orgIds = orgIds + dto.getId() + ";";
					}
				}
			}
			Page page = wxtemplateMessageTypeManager.getTemplateMessageTypeList(orgIds, wechatAccountId, pageSize, pageNo);
			json.put("data", page.getItems());
			json.put("count", page.getTotalCount());
			json.put("code", "200");
			json.put("msg", "");
			StringUtil.charSet(json, request, response);
		} catch (Exception e) {
			log.error(e.getMessage());
			json.put("data", "");
			json.put("count", 0);
			json.put("code", "100");
			json.put("msg", "获取有效的微信模板配置信息失败:+" + e.getMessage());
			StringUtil.charSet(json, request, response);
		}
	}


	/**
	 * 获取微信模板配置单条的配置信息
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "getWxTemplInfo")
	public void getWxTemplInfo(HttpServletRequest request, HttpServletResponse response) {
		JSONObject          json = new JSONObject();
		Map<String, Object> map  = new HashMap<String, Object>();
		String              id   = request.getParameter("wxTemplId");
		if (StringUtils.isEmpty(id)) {
			json.put("data", "");
			json.put("code", "100");
			json.put("msg", "微信模板配置Id不允许为空");
			StringUtil.charSet(json, request, response);

		} else {
			WxtemplateMessageTypeModel wxTemplInfo = wxtemplateMessageTypeManager.get(Integer.parseInt(id));
			if (wxTemplInfo != null) {
				try {
					List<WxtemplateMessageConfigModel> list = wxtemplateMessageConfigManager.getWxTemplConfigByTypeId(wxTemplInfo.getId());
					map.put("paramList", list);
					map.put("wxTemplInfo", wxTemplInfo);
					List<Map<String, Object>> orgPuList = publicNoManager.getWxPubOrgRelations(wxTemplInfo.getWechat_account_id());
					String                    orgId     = "";
					if (orgPuList != null && orgPuList.size() > 0) {
						Map<String, Object> pubOrg = orgPuList.get(0);
						orgId = pubOrg.get("organizationId") == null ? "" : pubOrg.get("organizationId").toString();
					}
					map.put("orgId", orgId);
					if (org.apache.commons.lang3.StringUtils.isNotBlank(orgId)) {
						TbOrganization organization = organizationManager.getOrganizationById(Integer.parseInt(orgId));
						map.put("orgName", organization.getName());
					}
					json.put("data", map);
					json.put("code", "200");
					json.put("msg", "");
					StringUtil.charSet(json, request, response);
				} catch (Exception e) {
					log.error(e.getMessage());
					json.put("data", "");
					json.put("code", "100");
					json.put("msg", "查看详情失败：" + e.getMessage());
					StringUtil.charSet(json, request, response);
				}

			} else {
				json.put("data", "");
				json.put("code", "100");
				json.put("msg", "查询的微信模板配置对象为空");
				StringUtil.charSet(json, request, response);
			}
		}

	}

	/**
	 * 新增修改微信模板配置信息
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "saveAndUpdateWxTempl")
	public void saveAndUpdateWxTempl(@ModelAttribute("wxtemplateMessageTypeModel") WxtemplateMessageTypeModel wxtemplateMessageTypeModel, HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		User       user = (User) request.getSession().getAttribute("UserCache");
		try {
			if (wxtemplateMessageTypeModel == null) {
				json.put("data", "");
				json.put("code", "100");
				json.put("msg", "微信模板配置对象为空");
				StringUtil.charSet(json, request, response);
			} else {
				wxtemplateMessageTypeModel.setTemplateid(wxtemplateMessageTypeModel.getTemplateid().trim());
				if (wxtemplateMessageTypeModel.getId() != null && wxtemplateMessageTypeModel.getId() != 0) {

					wxtemplateMessageTypeModel.setModifier(user.getUsername());
					wxtemplateMessageTypeModel.setModifyTime(new Date());
					WxtemplateMessageTypeModel model = wxtemplateMessageTypeManager.get(wxtemplateMessageTypeModel.getId());
					wxtemplateMessageTypeModel.setIsEnabled(model.getIsEnabled());
					wxtemplateMessageTypeModel.setCreateTime(model.getCreateTime());
					wxtemplateMessageTypeModel.setCreator(model.getCreator());
					wxtemplateMessageConfigManager.delByTemplateId(wxtemplateMessageTypeModel.getId());

				} else {
					wxtemplateMessageTypeModel.setIsEnabled("1");
					wxtemplateMessageTypeModel.setCreateTime(new Date());
					wxtemplateMessageTypeModel.setCreator(user.getUsername());
				}
				Map<String, Object> mapInfo = new HashMap<String, Object>();

				mapInfo = wxtemplateMessageTypeManager.saveOrUpdate(wxtemplateMessageTypeModel);
				//参数表
				List<WxtemplateMessageConfigModel> templateParams = new ArrayList<WxtemplateMessageConfigModel>();
				Map                                map            = WebUtils.getParametersStartingWith(request, "param");
				for (int i = 1; i <= map.size(); i++) {
					if (StringUtils.isNotEmpty(request.getParameter("key" + i))) {
						WxtemplateMessageConfigModel param = new WxtemplateMessageConfigModel();
						param.setName(request.getParameter("param" + i));
						param.setParam_key(request.getParameter("key" + i));
						templateParams.add(param);
					}
				}
				if (mapInfo != null && mapInfo.size() > 0) {
					wxtemplateMessageTypeModel = (WxtemplateMessageTypeModel) mapInfo.get("data");
				}
				for (WxtemplateMessageConfigModel param : templateParams) {
					param.setWechat_template_id(wxtemplateMessageTypeModel.getId());
					param.setCreateTime(new Date());
					param.setCreator(user.getUsername());
					param.setIsEnabled("1");
					wxtemplateMessageConfigManager.saveOrUpdate(param);
				}
				json.put("data", mapInfo.get("data"));
				json.put("code", mapInfo.get("code"));
				json.put("msg", mapInfo.get("msg"));
				StringUtil.charSet(json, request, response);
			}

		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			json.put("data", "");
			json.put("code", "100");
			json.put("msg", "新增或修改失败");
			StringUtil.charSet(json, request, response);
		}

	}

	/**
	 * 删除微信模板配置信息
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "deleteWxTemplConfig")
	public void deleteWxTemplConfig(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		String     id   = request.getParameter("wxTemplId");
		if (StringUtils.isEmpty(id)) {
			json.put("data", "");
			json.put("code", "100");
			json.put("msg", "微信模板配置Id不允许为空");
			StringUtil.charSet(json, request, response);
		} else {
			WxtemplateMessageTypeModel wxTemplInfo = wxtemplateMessageTypeManager.get(Integer.parseInt(id));
			wxTemplInfo.setIsEnabled("0");
			wxtemplateMessageTypeManager.updateModel(wxTemplInfo);
			json.put("data", "");
			json.put("code", "200");
			json.put("msg", "删除成功");
			StringUtil.charSet(json, request, response);
		}

	}

	/**
	 * 短信账号配置页面
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "jumpMessageAccount")
	public ModelAndView jumpMessageAccount(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Model        model = new ExtendedModelMap();
		User         user  = (User) request.getSession().getAttribute("UserCache");
		List<OrgDto> dtos  = appUserService.getUserOrgs(user.getId());
		model.addAttribute("orgs", dtos);
		return new ModelAndView("admin/emailSetting/messageAccount", model.asMap());
	}

	/**
	 * 保存修改短信账号信息
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "saveAndUpdateMobileConfig")
	public void saveAndUpdateMobileConfig(@ModelAttribute("mobileConfigModel") MobileConfigModel mobileConfigModel,
										  HttpServletRequest request, HttpServletResponse response) throws Exception {

		JSONObject json = new JSONObject();
		User       user = (User) request.getSession().getAttribute("UserCache");
		if (mobileConfigModel == null) {
			json.put("data", "");
			json.put("code", "100");
			json.put("msg", "配置对象为空");
			StringUtil.charSet(json, request, response);
		} else {
			if (mobileConfigModel.getId() == null) {//新增短信账号
				//一个项目只可以配置一个短信账号
				List<MobileConfigModel> list = mobileConfigManager.getMobileConfigList(mobileConfigModel.getOrgId() + "", 1, 10).getItems();
				if (null != list && list.size() > 0) {
					json.put("code", "100");
					json.put("msg", "已经配置过账号");
					StringUtil.charSet(json, request, response);
				} else {
					mobileConfigModel.setIsEnable(1);
					mobileConfigModel.setCreateTime(new Date());
					mobileConfigModel.setCreator(user.getId());
					MobileConfigModel configModel = mobileConfigManager.save(mobileConfigModel);
					json.put("data", configModel);
					json.put("code", "200");
					json.put("msg", "添加成功");
					StringUtil.charSet(json, request, response);
				}
			} else {//修改短信账号
				MobileConfigModel model = mobileConfigManager.get(mobileConfigModel.getId());
				mobileConfigModel.setIsEnable(model.getIsEnable());
				mobileConfigModel.setCreateTime(model.getCreateTime());
				mobileConfigModel.setCreator(model.getCreator());
				mobileConfigModel.setModify(user.getId());
				mobileConfigModel.setModifyTime(new Date());
				mobileConfigManager.updateModel(mobileConfigModel);
				json.put("data", mobileConfigModel);
				json.put("code", "200");
				json.put("msg", "修改成功");
				StringUtil.charSet(json, request, response);
			}
		}

	}

	/**
	 * 删除短信账号
	 *
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "deleteMobileConfig")
	public void deleteMobileConfig(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		String     id   = request.getParameter("mobileId");
		if (StringUtils.isEmpty(id)) {
			json.put("data", "");
			json.put("code", "100");
			json.put("msg", "Id不允许为空");
			StringUtil.charSet(json, request, response);
		} else {
			MobileConfigModel model = mobileConfigManager.get(Long.parseLong(id));
			model.setIsEnable(0);
			mobileConfigManager.updateModel(model);
			json.put("data", "");
			json.put("code", "200");
			json.put("msg", "删除成功");
			StringUtil.charSet(json, request, response);
		}
	}


	/**
	 * 获取短信账号列表
	 *
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "getMobileList")
	public void getMobileList(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json   = new JSONObject();
		String     orgIds = request.getParameter("orgId");//项目id
		User       user   = (User) request.getSession().getAttribute("UserCache");

		if (StringUtils.isBlank(orgIds)) {
			orgIds = getUserManageOrgIds(user);//如果前台未传项目id,则查询该用户所管辖所有项目
		}

		String pageNo   = request.getParameter("page");
		String pageSize = request.getParameter("limit");

		if (StringUtils.isBlank(pageNo)) {
			pageNo = "1";
		}
		if (StringUtils.isBlank(pageSize)) {
			pageSize = "10";
		}

		try {

			Page page = mobileConfigManager.getMobileConfigList(orgIds, Integer.parseInt(pageNo), Integer.parseInt(pageSize));
			json.put("data", page.getItems());
			json.put("count", page.getTotalCount());
			json.put("code", "200");
			json.put("msg", "");
			json.put("userType", user.getUserType());
			StringUtil.charSet(json, request, response);
		} catch (Exception e) {
			log.error(e.getMessage());
			json.put("data", "");
			json.put("count", 0);
			json.put("code", "100");
			json.put("msg", "获取信息失败:+" + e.getMessage());
			StringUtil.charSet(json, request, response);
		}
	}

	/**
	 * 查询短信剩余量
	 *
	 * @param request
	 */
	@RequestMapping("getMobileResidueCount")
	@ResponseBody
	public AppResultDto getMobileResidue(HttpServletRequest request) throws IOException {
		String mobileId = request.getParameter("mobileId");

		if (StringUtils.isBlank(mobileId)) {
			return AppResultUtil.createErrorDto("100", "参数id为空!");
		}

		MobileConfigModel model    = mobileConfigManager.get(Long.parseLong(mobileId));
		String            username = model.getMobileNum();
		//密码加密
		String pwd = MD5.sign(model.getPassword(), "", "utf-8");

		String urlString   = "http://api.shgmnets.com/sms/balance";
		String paramString = "user=" + username + "&pwd=" + pwd;
		String res         = HttpKit.Post(paramString, urlString);

		String result = "0";
		if (StringUtils.isNotBlank(res) && res.split(",")[1].equals("0")) {
			result = res.split(",")[2];
		}
		return AppResultUtil.createSuccesDto("成功", result);
	}

	/**
	 * 查询短信账号详情
	 *
	 * @param request
	 * @param response
	 */
	@RequestMapping("getMobileDetail")
	@ResponseBody
	public AppResultDto getMobileDetail(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String mobileId = request.getParameter("mobileId");

		if (StringUtils.isBlank(mobileId)) {
			return AppResultUtil.createErrorDto("100", "参数id为空!");
		}

		MobileConfigModel model = mobileConfigManager.get(Long.parseLong(mobileId));
		return AppResultUtil.createSuccesDto("200", model);
	}

	/**
	 * 校验是否配置短信账号
	 *
	 * @param request
	 * @param response
	 */
	@RequestMapping("checkHasMobileAccount")
	@ResponseBody
	public AppResultDto checkHasMobileAccount(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String orgId  = request.getParameter("orgId");
		String result = "false";
		if (StringUtils.isNotBlank(orgId)) {
			List<MobileConfigModel> list = mobileConfigManager.getMobileConfigList(orgId);
			if (null != list && list.size() > 0) {
				result = "true";
			}
		}
		return AppResultUtil.createSuccesDto("成功", result);
	}
}
