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

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.time.DateUtils;
import org.linlinjava.jz.db.dao.HmButlerTaskChildMapper;
import org.linlinjava.jz.db.dao.HmButlerTaskLogMapper;
import org.linlinjava.jz.db.dao.HmButlerTaskParentMapper;
import org.linlinjava.jz.db.dao.HmEmployRelationMapper;
import org.linlinjava.jz.db.domain.HmButlerTaskChild;
import org.linlinjava.jz.db.domain.HmButlerTaskChildExample;
import org.linlinjava.jz.db.domain.HmButlerTaskLog;
import org.linlinjava.jz.db.domain.HmButlerTaskLogExample;
import org.linlinjava.jz.db.domain.HmButlerTaskParent;
import org.linlinjava.jz.db.domain.HmEmployRelation;
import org.linlinjava.jz.db.util.hm.HmButlerTaskLogContans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 拜访类子任务
 * 
 * @author 36952
 *
 */
@Service
public class HmButlerTaskChildService {

	@Autowired
	HmButlerTaskChildMapper hmButlerTaskChildMapper;

	@Autowired
	HmButlerTaskParentMapper hmButlerTaskParentMapper;

	@Autowired
	HmButlerTaskLogMapper hmButlerTaskLogMapper;

	@Autowired
	HmButlerTaskLogService hmButlerTaskLogService;

	@Autowired
	HmEmployRelationMapper employRelationMapper;

	private final DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

	/**
	 * 拜访类-任务详情
	 * 
	 * @param taskParentId 主任务id
	 * @return
	 */
	public Map<String, Object> selectByTaskParentId(Long taskParentId) {
		// 返回 主任务，子任务，任务操作日志
		HmButlerTaskLogExample logexample = new HmButlerTaskLogExample();
		logexample.or().andTaskParentIdEqualTo(taskParentId);
		logexample.setOrderByClause(" id desc ");
		List<HmButlerTaskLog> hmButlerTaskLogList = hmButlerTaskLogMapper.selectByExample(logexample);

		boolean boo = true;
		for (HmButlerTaskLog e : hmButlerTaskLogList) {
			e.setTitle(e.getOperateContent());
			e.setDescription(df.format(e.getCreateTime()));
			if (boo) {
				e.setSize("14");
				boo = false;
			} else {
				e.setSize("8");
			}
		}

		HmButlerTaskChildExample childexample = new HmButlerTaskChildExample();
		childexample.or().andTaskParentIdEqualTo(taskParentId);
		List<HmButlerTaskChild> hmButlerTaskChildList = hmButlerTaskChildMapper.selectByExample(childexample);

		HmButlerTaskParent task = hmButlerTaskParentMapper.selectByPrimaryKey(taskParentId);

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("task", task);
		data.put("hmButlerTaskChildList", hmButlerTaskChildList);
		data.put("hmButlerTaskLogList", hmButlerTaskLogList);
		data.put("isAllComplete", false);

		int c = 0;
		for (HmButlerTaskChild t : hmButlerTaskChildList) {
			if (t.getStatus().equals(1)) {// 都是已完成
				c = c + 1;
			}
		}
		if (c == hmButlerTaskChildList.size()) {
			data.put("isAllComplete", true);// 拜访类子任务都是完成状态
		}

		return data;
	}

	/**
	 * 子任务完成 不能用于修改拜访时间
	 * 
	 * @param taskChildId
	 * @param businessId     当没有其他业务id或不好取时，用taskChildId代替
	 * @param operateContent
	 * @return
	 */
	public void completeChildTask(Long taskChildId, Long businessId, String operateContent) {
		HmButlerTaskChild tc = hmButlerTaskChildMapper.selectByPrimaryKey(taskChildId);
		if (tc.getStatus() == 0) {// 如果状态已经完成了，就跳过
			HmButlerTaskChild record = new HmButlerTaskChild();
			record.setId(taskChildId);
			record.setStatus(1);//
			record.setBusinessId(businessId);
			hmButlerTaskChildMapper.updateByPrimaryKeySelective(record);
		}

		// 插入操作日志
		hmButlerTaskLogService.insertButlerTaskLog(taskChildId, businessId, operateContent);

	}

	/**
	 * 修改拜访时间
	 * 
	 * @param taskChildId
	 * @param date        yyyy-MM-dd
	 * @return
	 */
	@Transactional
	public int updateVisitTime(Long taskChildId, String date) {
		// 先修改三者关系里的时间
		// 再修改子任务的时间，拼接上去，改个状态，和状态
		// 记录日志
//		employRelationMapper
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		HmButlerTaskChild tc = hmButlerTaskChildMapper.selectByPrimaryKey(taskChildId);

		HmEmployRelation ert = employRelationMapper.selectByPrimaryKey(tc.getEmployRelationId());
		HmEmployRelation tempert = new HmEmployRelation();
		tempert.setId(ert.getId());
		tempert.setFirstServiceTime(LocalDateTime.parse(date + " 00:00:00", df));
		employRelationMapper.updateByPrimaryKeySelective(tempert);

		// 拜访时间：2020-02-02
		String str = ":";
		if (tc.getTaskChildName().indexOf(str) != -1) {
			String[] split = tc.getTaskChildName().split(str);
			tc.setTaskChildName(split[0] + str + date);
		} else {
			tc.setTaskChildName(tc.getTaskChildName() + str + date);
		}

		tc.setStatus(1);
		tc.setUpdateTime(LocalDateTime.now());
		hmButlerTaskChildMapper.updateByPrimaryKeySelective(tc);

		String operateContent = HmButlerTaskLogContans.TASK_LOG_VISITINGTIME + date;
		return hmButlerTaskLogService.insertButlerTaskLog(taskChildId, ert.getId(), operateContent);
	}

	public static void main(String[] args) {
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime.parse("2020-08-20 00:00:00", df);
	}

}
