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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import org.apache.commons.lang3.StringUtils;
import org.linlinjava.jz.db.dao.HmButlerTaskChildMapper;
import org.linlinjava.jz.db.dao.HmButlerTaskTeachMapper;
import org.linlinjava.jz.db.dao.HmEmployTaskMapper;
import org.linlinjava.jz.db.dao.HmEmployTaskSetMapper;
import org.linlinjava.jz.db.dao.HmTaskItemMapper;
import org.linlinjava.jz.db.domain.HmButlerTaskChild;
import org.linlinjava.jz.db.domain.HmButlerTaskTeach;
import org.linlinjava.jz.db.domain.HmButlerTaskTeachExample;
import org.linlinjava.jz.db.domain.HmEmployTask;
import org.linlinjava.jz.db.domain.HmEmployTaskExample;
import org.linlinjava.jz.db.domain.HmEmployTaskSet;
import org.linlinjava.jz.db.domain.HmEmployTaskSetExample;
import org.linlinjava.jz.db.domain.HmTaskItem;
import org.linlinjava.jz.db.dto.hm.ButlerTaskTeachAddDto;
import org.linlinjava.jz.db.dto.hm.ButlerTaskTeachDto;
import org.linlinjava.jz.db.util.HmResponseUtil;
import org.linlinjava.jz.db.util.JsonUtil;
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 HmButlerTaskTeachService {

	@Autowired
	HmButlerTaskTeachMapper butlerTaskTeachMapper;

	@Autowired
	HmButlerTaskChildMapper butlerTaskChildMapper;

	@Autowired
	HmEmployTaskMapper employTaskMapper;

	@Autowired
	HmTaskItemMapper hmTaskItemMapper;
	
	@Autowired
	HmButlerTaskChildService butlerTaskChildService;
	
	@Autowired
	HmEmployTaskSetMapper employTaskSetMapper;

	/**
	 * 管家--查询跟教记录
	 * 
	 * @param taskChildId
	 * @return
	 */
	public Map<String, Object> getServantTaskTeach(Long taskChildId) {
		// 跟教 可能完成，可能没完成
		HmButlerTaskChild taskChild = butlerTaskChildMapper.selectByPrimaryKey(taskChildId);
		if (null == taskChild) {
//			return HmResponseUtil.fail_null();
			throw new RuntimeException("数据异常");
		}


		Map<String, Object> data = new HashMap<String, Object>();
		data.put("taskChild", taskChild);

		// 跟教任务已完成
		if (taskChild.getStatus().equals(1)) {
			// 直接拿HmButlerTaskTeach解析
					
			HmButlerTaskTeachExample exampleTeach = new HmButlerTaskTeachExample();
			exampleTeach.or().andEmployRelationIdEqualTo(taskChild.getEmployRelationId());

			List<HmButlerTaskTeach> taskTeachList = butlerTaskTeachMapper.selectByExample(exampleTeach);
			if(taskTeachList.size()>0) {
				HmButlerTaskTeach taskTeach=taskTeachList.get(taskTeachList.size()-1); 
				
				String taskJson = taskTeach.getTaskJson();
				List<ButlerTaskTeachDto> butlerTaskTeach = JsonUtil.jsonToList(taskJson, ButlerTaskTeachDto.class);
				data.put("butlerTaskTeach", butlerTaskTeach);
				data.put("abnormalRecord", taskTeach.getAbnormalRecord());
				data.put("teachRecord", taskTeach.getTeachRecord());
			}
		} else {
			// 还没完成跟教，需判断是否配置保姆任务
			HmEmployTaskSetExample example = new HmEmployTaskSetExample();
			example.or().andEmployRelationIdEqualTo(taskChild.getEmployRelationId());
			List<HmEmployTaskSet> employTaskSet = employTaskSetMapper.selectByExample(example);
			if (employTaskSet.size() == 0) {
//				return HmResponseUtil.fail("该保姆还未设置任务");
				throw new RuntimeException("该保姆还未设置任务");
			}

			List<ButlerTaskTeachDto> butlerTaskTeach = new ArrayList<ButlerTaskTeachDto>();
			for (HmEmployTaskSet h : employTaskSet) {
				HmTaskItem item = hmTaskItemMapper.selectByPrimaryKeySelective(h.getItemId(),
						HmTaskItem.Column.itemName);
				ButlerTaskTeachDto td = new ButlerTaskTeachDto();
				td.setTaskItemId(h.getItemId());
				td.setTaskItemName(item.getItemName());
				td.setIsChoose(false);
				td.setEmployTaskComment(" ");
				butlerTaskTeach.add(td);
			}

			data.put("butlerTaskTeach", butlerTaskTeach);
		}

		return data;
	}

	@Transactional
	public void addServantTaskTeach(ButlerTaskTeachAddDto dto) {
		//插入跟教记录，修改子任务 跟教状态,插入日志
		HmButlerTaskChild taskChild = butlerTaskChildMapper.selectByPrimaryKey(dto.getTaskChildId());
		if (null == taskChild) {
//			return HmResponseUtil.fail_null();
			throw new RuntimeException("数据异常");
		}
		
		if (taskChild.getStatus()==1) {
//			return HmResponseUtil.fail("你已完成首次保姆跟教，不能重复操作");
			throw new RuntimeException("你已完成首次保姆跟教，不能重复操作");
		}
		
		HmButlerTaskTeach record=new HmButlerTaskTeach();
		record.setAbnormalRecord(dto.getAbnormalRecord());
		record.setTeachRecord(dto.getTeachRecord());
		record.setTaskJson(JsonUtil.getJson(dto.getTaskTeah()));
		
		record.setTaskParentId(taskChild.getTaskParentId());
		record.setTaskChildId(dto.getTaskChildId());
		record.setOrderNo(taskChild.getOrderNo());
		record.setEmployRelationId(taskChild.getEmployRelationId());
		 
		List<Long> tempId=new ArrayList<Long>();
		dto.getTaskTeah().forEach( t->{
			tempId.add(t.getTaskItemId());
		});
		String join = StringUtils.join(tempId,',');
		record.setTaskItemId(join);
		butlerTaskTeachMapper.insertSelective(record);
		
		String operateContent=HmButlerTaskLogContans.TASK_LOG_FOLLOWTEACHER+(record.getTeachRecord()==null?record.getAbnormalRecord():record.getTeachRecord());
		butlerTaskChildService.completeChildTask(dto.getTaskChildId(), record.getId(), operateContent);
		
		 
	}

}
