package org.linlinjava.jz.service;

import org.linlinjava.jz.core.compent.SendSms;
import org.linlinjava.jz.db.dao.HmAppUserMapper;
import org.linlinjava.jz.db.dao.HmButlerTaskChildMapper;
import org.linlinjava.jz.db.dao.HmEmployRelationMapper;
import org.linlinjava.jz.db.dao.HmSmslogMapper;
import org.linlinjava.jz.db.domain.HmAppUser;
import org.linlinjava.jz.db.domain.HmButlerTaskChild;
import org.linlinjava.jz.db.domain.HmEmployRelation;
import org.linlinjava.jz.db.service.hm.HmButlerTaskChildService;
import org.linlinjava.jz.db.service.hm.HmEmployRelationService;
import org.linlinjava.jz.db.util.HmResponseUtil;
import org.linlinjava.jz.db.util.hm.HmButlerTaskLogContans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HmSmsLogService {

	@Autowired
	HmSmslogMapper hmSmslogMapper;

	@Autowired
	SendSms sendSms;

	@Autowired
	HmButlerTaskChildMapper hmButlerTaskChildMapper;

//	@Autowired
//	HmEmployRelationMapper employRelationMapper;

	@Autowired
	HmAppUserMapper appUserMapper;

	@Autowired
	HmButlerTaskChildService butlerTaskChildService;

	@Autowired
	HmEmployRelationService employRelationService;

	/**
	 * 邀请主顾双方使用小程序发送短信 给雇主和保姆
	 * 
	 * @param taskChildId
	 */
	public String invitationUseApp(Long taskChildId) {

		// 发送短信，记录短信日志
		// 完成子任务
		HmButlerTaskChild taskChild = hmButlerTaskChildMapper.selectByPrimaryKey(taskChildId);
		if (taskChild.getStatus() == 1) {
//			return HmResponseUtil.fail("你已邀请雇主双方使用小程序");
			return "你已邀请雇主双方使用小程序";
		}

		HmEmployRelation relation = employRelationService.queryById(taskChild.getEmployRelationId());

		HmAppUser servantUser = appUserMapper.selectByPrimaryKey(relation.getUserServantId());// 保姆
		HmAppUser emplyoerUser = appUserMapper.selectByPrimaryKey(relation.getUserEmployerId());// 雇主

		Boolean booServant = sendSms.sendMsServant(servantUser.getPhone(), relation.getServantName(),
				relation.getButlerName());

		if (!booServant) {
			return "发送邀请短信给保姆失败";
		}

		Boolean booEmploye = sendSms.sendMsCustomer(emplyoerUser.getPhone(), relation.getEmployerName(),
				relation.getButlerName());
		if (!booEmploye) {
			return "发送邀请短信给雇主失败";
		}

		String operateContent = HmButlerTaskLogContans.TASK_LOG_INVITETOUSE;
		butlerTaskChildService.completeChildTask(taskChildId, taskChildId, operateContent);

		return null;
	}

	/**
	 * 邀请主顾双方使用小程序发送短信 给雇主和保姆
	 * 
	 * @param taskChildId
	 */
	public String invitationUseApps(String orderNo) {

		HmEmployRelation relation = employRelationService.queryByOrderNo(orderNo);

		HmAppUser servantUser = appUserMapper.selectByPrimaryKey(relation.getUserServantId());// 保姆
		HmAppUser emplyoerUser = appUserMapper.selectByPrimaryKey(relation.getUserEmployerId());// 雇主

		Boolean booServant = sendSms.sendMsServant(servantUser.getPhone(), relation.getServantName(),
				relation.getButlerName());

		if (!booServant) {
			return "发送邀请短信给保姆失败";
		}

		Boolean booEmploye = sendSms.sendMsCustomer(emplyoerUser.getPhone(), relation.getEmployerName(),
				relation.getButlerName());
		if (!booEmploye) {
			return "发送邀请短信给雇主失败";
		}

		return null;
	}

}
