package com.palmnest.webapp.controller.wechat;

import com.palmnest.application.AppConstants;
import com.palmnest.application.CodeConstants;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.model.estateserve.WxPublicno;
import com.palmnest.model.notice.MobileConfigModel;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.service.MeetingService;
import com.palmnest.service.estateserve.PublicNoManager;
import com.palmnest.service.estateserve.SmsAuthCodeManager;
import com.palmnest.service.notice.MobileConfigManager;
import com.palmnest.service.org.OrganizationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping(value = "/wechat/meeting")
public class MeetingClientController {

	@Autowired
	private OrganizationService organizationService;
	@Autowired
	private MobileConfigManager mobileConfigManager;
	@Autowired
	private PublicNoManager     publicNoManager;
	@Autowired
	private SmsAuthCodeManager  smsAuthCodeManager;
	@Autowired
	private MeetingService      meetingService;

	/**
	 * 根据客户端编码获取短信配置信息
	 *
	 * @param clientCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getMobileConfigInfo")
	@ResponseBody
	public AppResultDto getMobileConfig(@RequestParam String clientCode) throws Exception {
		//根据客户端编码查询项目下的短信账号配置信息
		try {
			TbOrganization org = organizationService.getByMettingClient(clientCode);
			if (org != null) {
				List<MobileConfigModel> list = mobileConfigManager.getMobileConfigList(org.getId().toString());
				if (list != null && list.size() > 0) {
					MobileConfigModel mobile = list.get(0);
					return AppResultUtil.createSuccesDto(mobileConfigManager.convertToDto(mobile));
				}
			}
			return AppResultUtil.createErrorDto(CodeConstants.MEETING_CLIENT_NO_EXSITS, "编码对应项目不存在!");
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return AppResultUtil.createErrorDto("100", "返回信息：" + e.getMessage());
		}
	}

	@RequestMapping(value = "/getWxPayConfig")
	@ResponseBody
	public AppResultDto getWxPayConfig(@RequestParam String clientCode) throws Exception {
		//根据客户端编码查询项目下的微信商户配置信息
		try {
			WxPublicno wxPublic = publicNoManager.getByOrgClientCode(clientCode);
			if (wxPublic != null) {
				return AppResultUtil.createSuccesDto(publicNoManager.convertToDto(wxPublic));
			}
			return AppResultUtil.createErrorDto(CodeConstants.MEETING_WX_NO_EXSITS, "编码对应商户信息不存在!");
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return AppResultUtil.createErrorDto("100", "返回信息：" + e.getMessage());
		}
	}

	/**
	 * 给会议室提供的发送短信的接口
	 *
	 * @param clientCode
	 * @param content
	 * @param phones
	 * @return
	 */
	@RequestMapping(value = "/sendMsgMeeting")
	@ResponseBody
	public AppResultDto sendMsgMeeting(@RequestParam String clientCode, @RequestParam String content, @RequestParam String phones) {
		if (!StringUtil.isNotEmpty(clientCode)) {
			return AppResultUtil.createErrorDto("100", "项目不能为空");
		}
		if (!StringUtil.isNotEmpty(phones)) {
			return AppResultUtil.createErrorDto("100", "电话号码不能为空");
		}
		if (!StringUtil.isNotEmpty(content)) {
			return AppResultUtil.createErrorDto("100", "内容不能为空");
		}
		try {
			TbOrganization org = organizationService.getByMettingClient(clientCode);
			if (null == org) {
				return AppResultUtil.createErrorDto("100", "项目不能为空");
			}
			String result = smsAuthCodeManager.smsSendByOrgId(org.getId() + "", content, phones, "", CodeConstants.SMS_MEETING_TYPE);
			if ("Success".equals(result)) {
				return AppResultUtil.createSuccesDto();
			} else if ("3".equalsIgnoreCase(result)) {
				return AppResultUtil.createErrorDto(AppConstants.ERROR_CODE, "该项目下未配置短信账号");
			} else {
				return AppResultUtil.createErrorDto(AppConstants.ERROR_CODE, "验证码发送失败");
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return AppResultUtil.createErrorDto("100", "返回信息：" + e.getMessage());
		}
	}

	/**
	 * 获取会议室微信端的参会人
	 *
	 * @param orgId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getVirtDeptUser")
	@ResponseBody
	public AppResultDto getVirtDeptUser(@RequestParam Integer virtId, @RequestParam String name) throws Exception {
		if (null == virtId) {
			return AppResultUtil.createErrorDto("100", "企业不能为空");
		}
		try {
			Map<String, Object> map = meetingService.getVirtDeptUser(virtId, name);
			return AppResultUtil.createSuccesDto(map);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return AppResultUtil.createErrorDto("100", "返回信息：" + e.getMessage());
		}
	}
}
