package org.linlinjava.jz.hm.service.impl;

import org.linlinjava.jz.db.dao.HmAppUserMapper;
import org.linlinjava.jz.db.dao.HmEmployRelationMapper;
import org.linlinjava.jz.db.domain.HmAppUser;
import org.linlinjava.jz.db.domain.HmAppUserExample;
import org.linlinjava.jz.db.domain.HmEmployRelation;
import org.linlinjava.jz.db.domain.HmEmployRelationExample;
import org.linlinjava.jz.hm.contants.AppUserContants;
import org.linlinjava.jz.hm.dto.UserRelationDto;
import org.linlinjava.jz.hm.service.HmAppUserSerevice;
import org.linlinjava.jz.hm.util.DateUtil;
import org.linlinjava.jz.service.HmButlerTaskParentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class HmAppUserSereviceImpl implements HmAppUserSerevice {

	@Autowired
	HmAppUserMapper hmAppUserMapper;

	@Autowired
	HmEmployRelationMapper employRelationMapper;

	@Autowired
	HmButlerTaskParentService hmButlerTaskParentService;

	public HmAppUser getUserInfo(String phone) {
		HmAppUserExample example = new HmAppUserExample();
		example.or().andPhoneEqualTo(phone).andIsInvalidEqualTo(1);// 有效的

		return hmAppUserMapper.selectOneByExample(example);
	}

	public HmAppUser getUserInfo(String phone, String type) {
		HmAppUserExample example = new HmAppUserExample();
		example.or().andPhoneEqualTo(phone).andUserTypeEqualTo(type).andIsInvalidEqualTo(1);// 有效的

		return hmAppUserMapper.selectOneByExample(example);
	}

	public void updateLastLoginTime(Long id, String lastLoginIp) {
		HmAppUser record = new HmAppUser();
		record.setLastLoginIp(lastLoginIp);
		record.setLastLoginTime(LocalDateTime.now());
		record.setId(id);

		hmAppUserMapper.updateByPrimaryKeySelective(record);
	}

	// 插入不存在的用户，并返回id
	public HmAppUser insertNotExitUser(String phone, String sysUserId, String type) {
		HmAppUser userServant = this.getUserInfo(phone, type);
		if (null == userServant) {
			userServant = new HmAppUser();
			userServant.setPhone(phone);
			userServant.setSysUserid(sysUserId);
			userServant.setUserType(type);
			hmAppUserMapper.insertSelective(userServant);
		}

		return userServant;
	}

	/**
	 * 创建小程序登录账号，关联三者关系
	 *
	 * @param dto
	 * @return
	 */
	@Transactional
	public Long insetUserRelation(UserRelationDto dto) {

		HmEmployRelationExample example = new HmEmployRelationExample();
		example.or().andOrderNoEqualTo(dto.getOrderNo()).andIsInvalidEqualTo(1);

		HmEmployRelation employ = employRelationMapper.selectOneByExample(example);

		// 保姆
		HmAppUser userServant = this.insertNotExitUser(dto.getUserServantPhone(), dto.getUserServantId(),
				AppUserContants.USER_TYPE_2);

		// 雇主
		HmAppUser userClient = this.insertNotExitUser(dto.getUserClientPhone(), dto.getUserClientId(),
				AppUserContants.USER_TYPE_1);

		// 管家
		HmAppUser userButler = this.insertNotExitUser(dto.getUserButlerPhone(), dto.getUserButlerId(),
				AppUserContants.USER_TYPE_3);

		if (null != employ) {
			// 如果是换保姆 ，更新三者关系中的用户id
			HmEmployRelation tempRelation = new HmEmployRelation();
			tempRelation.setId(employ.getId());
			tempRelation.setUserServantId(userServant.getId());
			tempRelation.setUserEmployerId(userClient.getId());
			tempRelation.setUserButlerId(userButler.getId());
			employRelationMapper.updateByPrimaryKeySelective(tempRelation);

			return employ.getId();

		} else {
			// 新增三者关系
			HmEmployRelation record = new HmEmployRelation();
			record.setOrderNo(dto.getOrderNo());
			record.setFirstServiceTime(DateUtil.strToLocalDateTime(dto.getFirstServiceTime()));// 首次服务时间
			record.setRestMode(dto.getRestMode());// 休方式
			record.setRestDay(dto.getRestDay());// 休息日 周，日
			record.setIsNeedClock(1);// 是否需要打卡1需要，0不需要 dto.getIsNeedClock()

			record.setUserServantId(userServant.getId());
			record.setUserEmployerId(userClient.getId());
			record.setUserButlerId(userButler.getId());

			record.setServantName(dto.getUserServantName());
			record.setEmployerName(dto.getUserClientName());
			record.setButlerName(dto.getUserButlerName());

			employRelationMapper.insertSelective(record);

			// 插入首次任务
			hmButlerTaskParentService.butlerFirstTask(dto.getOrderNo(), record.getId());
			// 插入7天任务
			hmButlerTaskParentService.butlerWeekTask(dto.getOrderNo(), record.getId());
			//插入月度任务 30天后
			hmButlerTaskParentService.butlerMonthTask(dto.getOrderNo(), record.getId(),30);
			return record.getId();
		}

	}

}