package com.palmnest.service.virtProject.impl;

import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.model.estateserve.VirtualAccount;
import com.palmnest.model.estateserve.WxUser;
import com.palmnest.service.estateserve.ScWxuserManager;
import com.palmnest.service.virtProject.VirtualAccountService;
import com.palmnest.service.virtProject.manager.UserAccountManager;
import com.palmnest.service.virtProject.manager.VirtualAccountManager;
import com.palmnest.service.wechat.WeChatApiManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service("virtualAccountService")
public class VirtualAccountServiceImpl implements VirtualAccountService {


	@Autowired
	private VirtualAccountManager virtualAccountManager;
	@Autowired
	private UserAccountManager    userAccountManager;
	@Autowired
	private ScWxuserManager       scWxuserManager;
	@Autowired
	private WeChatApiManager      weChatApiManager;

	@Override
	public List<VirtualAccount> getVirAccountByAccount(String orgId, String account,
													   String password) {
		return virtualAccountManager.getVirAccountByAccount(orgId, account, password);
	}

	@Transactional
	public AppResultDto updateVirAccount(String orgId, String account, String oldPassword,
										 String newPassword, String openId) {
		List<VirtualAccount> list = virtualAccountManager.getVirAccountByAccount(orgId, account, oldPassword);
		if (list != null && list.size() > 0) {
			if (list.size() > 1) {
				return AppResultUtil.createErrorDto("100", "账号异常");
			}
			VirtualAccount va = list.get(0);
			va.setModifier(openId);
			va.setModifyTime(new Date());
			va.setPassword(newPassword);
			virtualAccountManager.updateModel(va);
			try {
				userAccountManager.updateUserAccountByAccId(va.getId());
			} catch (Exception e) {
				e.printStackTrace();
				log.error(e.getMessage());
				return AppResultUtil.createErrorDto("100", "修改企业帐号密码失败(用户登录记录)：" + e.getMessage());
			}
			return AppResultUtil.createSuccesDto();
		} else {
			return AppResultUtil.createErrorDto("100", "根据该密码找不到账号");
		}
	}

	@Override
	public VirtualAccount findById(Long id) {
		return virtualAccountManager.get(id);
	}

	@Async
	public void sendMeetingMessage(String openIds, String url,
								   String content) throws Exception {

		log.info("sendMeetingMessage/openIds:" + openIds);
		log.info("sendMeetingMessage/url:" + url);
		log.info("sendMeetingMessage/content:" + content);

		List<WxUser> wxList    = new ArrayList<WxUser>();
		String[]     openIdArr = openIds.split(";");

		if (openIdArr != null && openIdArr.length > 0) {

			for (int i = 0; i < openIdArr.length; i++) {
				WxUser wxUser = scWxuserManager.getScWxuserByOpenId(openIdArr[i]);
				if (wxUser != null) {
					if (!wxList.contains(wxUser)) {//去除重复的用户
						wxList.add(wxUser);
					}
				}
			}

			//获取有效的用户
			if (wxList != null && wxList.size() > 0) {
				for (int i = 0; i < wxList.size(); i++) {
					WxUser wxUser = wxList.get(i);
					log.info("sendMeetingMessage/wechat wxUserId:" + wxUser.getId());
					String str = weChatApiManager.sendServiceWithOpenId(wxUser.getDevelopId(), wxUser.getId(), content + "<a href='" + url + "'> 查看详情>></a>");
					log.info("sendMeetingMessage/result" + i + ":" + str);
				}
			}
		}
	}

}
