package org.linlinjava.jz.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.linlinjava.jz.db.dao.HmButlerTaskChildMapper;
import org.linlinjava.jz.db.dao.HmButlerTaskClockMapper;
import org.linlinjava.jz.db.domain.HmButlerTaskChild;
import org.linlinjava.jz.db.domain.HmButlerTaskChildExample;
import org.linlinjava.jz.db.domain.HmButlerTaskClock;
import org.linlinjava.jz.db.domain.HmButlerTaskClockExample;
import org.linlinjava.jz.db.domain.HmEmployRelation;
import org.linlinjava.jz.db.dto.hm.HmVisitClockDto;
import org.linlinjava.jz.db.dto.hm.HmbutlerTaskClockDto;
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.TicketStatusConstant;
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;

@Service
@Transactional
public class  HmButlerTaskClockService {

	@Autowired
	HmButlerTaskClockMapper butlerTaskClockMapper;

	@Autowired
	HmButlerTaskChildMapper butlerTaskChildMapper;

	@Autowired
	HmButlerTaskChildService hmButlerTaskChildService;
	
	@Autowired
	HmEmployRelationService employRelationService;
	
	@Autowired
	NoticeService noticeService;

	/**
	 *首次 上门拜访打卡记录
	 * 
	 * @param dto
	 * @return
	 */
	public Object visitClock(HmbutlerTaskClockDto dto) {
		HmButlerTaskChild taskChild = butlerTaskChildMapper.selectByPrimaryKey(dto.getTaskChildId());

		if (null == taskChild) {
			return HmResponseUtil.fail("打卡数据异常");
		}

		if (taskChild.getStatus().equals(1)) {
			return HmResponseUtil.fail("你已完成上门打卡任务，不能多次打卡");
		}

		HmButlerTaskClock record = new HmButlerTaskClock();
		record.setAddress(dto.getAddress());
		record.setComment(dto.getComment());
		record.setEmployRelationId(taskChild.getEmployRelationId());
		record.setLatLgt(dto.getLatLgt());
		record.setOrderNo(taskChild.getOrderNo());
		record.setTaskChildId(taskChild.getId());
		record.setTaskParentId(taskChild.getTaskParentId());
		record.setIsFollowup(0);//是否属于跟进类的任务
		butlerTaskClockMapper.insertSelective(record);

		String operateContent = HmButlerTaskLogContans.TASK_LOG_DOORVISITCLOCK;
		hmButlerTaskChildService.completeChildTask(dto.getTaskChildId(), record.getId(), operateContent);

		return HmResponseUtil.ok();
	}

	 /**
	  * 拜访打卡信息详情
	  * @param taskChildId
	  * @return
	  */
	public Object getVisitClockInfo(Long taskChildId) {
		HmButlerTaskChild taskChild = butlerTaskChildMapper.selectByPrimaryKey(taskChildId);

		HmButlerTaskClockExample example = new HmButlerTaskClockExample();
		example.or().andTaskChildIdEqualTo(taskChildId);
		HmButlerTaskClock taskClock = butlerTaskClockMapper.selectOneByExample(example);

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

		return HmResponseUtil.ok(data);
	}
	
	/**
	 * 跟进类任务 添加拜访打卡记录
	 * @param dto
	 * @return
	 */
	public Object visitClockFollowUp(HmVisitClockDto dto) {
		//添加拜访记录， 插入通知notice_user
		HmEmployRelation er = employRelationService.queryById(dto.getEmployRelationId());
		if(null==er) {
			return HmResponseUtil.fail_null();
		}
		 
		HmButlerTaskClock record = new HmButlerTaskClock();
		record.setAddress(dto.getAddress());
		record.setComment(dto.getComment());
		record.setEmployRelationId(dto.getEmployRelationId());
		record.setLatLgt(dto.getLatLgt());
		record.setOrderNo(er.getOrderNo());
//		record.setTaskChildId(taskChild.getId());
//		record.setTaskParentId(taskChild.getTaskParentId());
		record.setIsFollowup(1);//是否属于跟进类的任务
		butlerTaskClockMapper.insertSelective(record);

		noticeService.addNoticeUser(dto.getNoticeId(), "添加了拜访记录", TicketStatusConstant.STATUS_PROCESSING.toString());

		return HmResponseUtil.ok();
	}
}
