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

import java.time.LocalDateTime;
import java.util.List;

import org.linlinjava.jz.db.dao.HmButlerTaskLogMapper;
import org.linlinjava.jz.db.dao.HmButlerTaskParentMapper;
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.HmButlerTaskParentExample;
import org.linlinjava.jz.db.util.hm.CalendarUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("ButlerTaskParentService")
public class HmButlerTaskParentService {

	@Autowired
	HmButlerTaskParentMapper butlerTaskParentMapper;

	@Autowired
	HmButlerTaskLogService butlerTaskLogService;

	public List<HmButlerTaskParent> getEmployeeRelationTask(Long employRelationId) {
		HmButlerTaskParentExample example = new HmButlerTaskParentExample();
		example.or().andEmployRelationIdEqualTo(employRelationId);
		List<HmButlerTaskParent> list = butlerTaskParentMapper.selectByExample(example);
		for (HmButlerTaskParent t : list) {
			// 任务记录
			List<HmButlerTaskLog> hmButlerTaskLogList = butlerTaskLogService.getTaskLog(t.getId());
			t.setButlerTaskLogList(hmButlerTaskLogList);
		}
		return list;

	}

	public  HmButlerTaskParent  queryTaskLogByParentId(Long id) {
		HmButlerTaskParent bt = butlerTaskParentMapper.selectByPrimaryKey(id);

		// 任务记录
		List<HmButlerTaskLog> hmButlerTaskLogList = butlerTaskLogService.getTaskLog(bt.getId());
		bt.setButlerTaskLogList(hmButlerTaskLogList);

		return bt;

	}

	public List<HmButlerTaskParent> selectByYeahMonth(Long employRelationId, String yearMonth) {
		LocalDateTime st = LocalDateTime.parse(CalendarUtil.getFirstDayOfMonth(yearMonth) + " 00:00:00",
				CalendarUtil.df);
		LocalDateTime ed = LocalDateTime.parse(CalendarUtil.getLastDayOfMonth(yearMonth) + " 23:59:59",
				CalendarUtil.df);

		HmButlerTaskParentExample example = new HmButlerTaskParentExample();
		example.or().andEmployRelationIdEqualTo(employRelationId).andCreateTimeBetween(st, ed);
		List<HmButlerTaskParent> list = butlerTaskParentMapper.selectByExample(example);
		for (HmButlerTaskParent t : list) {
			// 任务记录
			List<HmButlerTaskLog> hmButlerTaskLogList = butlerTaskLogService.getTaskLog(t.getId());
			t.setButlerTaskLogList(hmButlerTaskLogList);
		}
		return list;

	}
}
