package com.uas.platform.b2b.mobile.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import com.uas.platform.b2b.dao.CommonDao;
import com.uas.platform.b2b.dao.UserBaseInfoDao;
import com.uas.platform.b2b.mobile.dao.OutPlanDetailDao;
import com.uas.platform.b2b.mobile.dao.UserCenterDao;
import com.uas.platform.b2b.mobile.dao.VisitRecordDao;
import com.uas.platform.b2b.mobile.enumeration.MobileStatus;
import com.uas.platform.b2b.mobile.model.OutPlan;
import com.uas.platform.b2b.mobile.model.OutPlanDetail;
import com.uas.platform.b2b.mobile.model.UserCenter;
import com.uas.platform.b2b.mobile.model.VisitRecord;
import com.uas.platform.b2b.mobile.model.WorkSchedule;
import com.uas.platform.b2b.mobile.service.WorkScheduleService;
import com.uas.platform.b2b.model.OfficeTask;
import com.uas.platform.b2b.temporary.model.OutPlanInfo;
import com.uas.platform.core.model.Constant;

@Service
public class WorkScheduleServiceImpl implements WorkScheduleService {

	@Autowired
	private UserCenterDao userCenterDao;

	@Autowired
	private UserBaseInfoDao userBaseInfoDao;

	@Autowired
	private VisitRecordDao visitRecordDao;

	@Autowired
	private CommonDao commonDao;

	@Autowired
	private OutPlanDetailDao outPlanDetailDao;

	@Override
	public ModelMap getWorkSchedule(Long emcode, Long enuu, String date) {
		ModelMap map = new ModelMap();
		UserCenter user = userCenterDao.getUserInfoByEmcodeAndEnuu(emcode, enuu);
		if (user != null) {
			WorkSchedule workSchedule = new WorkSchedule();
			String sql = "select o.op_id mp_id, o.op_recorder mp_recorder, o.op_recordercode recordercode, o.op_code mp_code, "
					+ "o.op_address mp_address, o.op_visittime mp_visittime, o.op_enuu enuu, o.op_recorddate mp_recorddate, "
					+ "o.op_arrivedate mp_arrivedate from mobile$outplan o where exists (select d.opd_id mpd_id, d.opd_code pd_code, "
					+ "d.opd_recordercode mpd_recordercode, d.opd_recorder mpd_recorder, d.opd_address mpd_address, d.opd_kind mpd_kind, "
					+ "d.opd_detno mpd_detno, d.opd_remark mpd_remark, d.opd_location mpd_location, d.opd_enuu enuu, d.opd_arrivedate mpd_arrivedate, "
					+ "d.opd_actdate mpd_actdate, d.opd_outdate mpd_outdate, d.opd_recorddate mpd_recorddate, d.opd_status mpd_status, d.opd_statuscode mpd_statuscode, "
					+ "d.opd_distance mpd_distance, d.opd_longitude mpd_longitude, d.opd_latitude mpd_latitude, d.opd_company mpd_company, d.opd_firsttime mpd_firsttime, "
					+ "d.opd_lasttime mpd_lasttime from mobile$outplandetail d where d.opd_opid = o.op_id and o.op_enuu = "
					+ enuu + " and o.op_recordercode = " + emcode + " and date_format(d.opd_arrivedate,'%Y%m') = '" + date
					+ "')";
			List<OutPlan> meOutPlans = commonDao.query(sql, OutPlan.class);
			List<OutPlanInfo> planInfos = new ArrayList<OutPlanInfo>();
			if (!CollectionUtils.isEmpty(meOutPlans)) {
				for (OutPlan ou : meOutPlans) {
					OutPlanInfo info = new OutPlanInfo();
					info.setMp_recorder(ou.getMp_recorder());
					info.setMp_code(ou.getMp_code());
					info.setMp_recordercode(ou.getRecorderCode());
					List<OutPlanDetail> details = outPlanDetailDao.findByOutPlan(ou);
					if (!CollectionUtils.isEmpty(details)) {
						for (OutPlanDetail detail : details) {
							info.setMpd_distance(detail.getMpd_distance());
							info.setMpd_address(detail.getMpd_address());
							info.setMpd_actdate(detail.getMpd_actdate());
							info.setMpd_company(detail.getMpd_company());
							info.setMpd_id(detail.getMpd_id());
							info.setMpd_remark(detail.getMpd_remark());
							info.setMpd_status(detail.getMpd_status());
							info.setMpd_outdate(detail.getMpd_outdate());
							info.setMpd_arrivedate(detail.getMpd_arrivedate());
							info.setMp_firsttime(detail.getMpd_firsttime());
							info.setMp_lasttime(detail.getMpd_lasttime());
						}
					}
					planInfos.add(info);
				}
			}
			// 执行人是自己的所有任务
			String taskSql = "select t.ta_id id, t.ta_code code, t.ta_detail detail, t.ta_recordercode recordercode, t.ta_recorder recorder, "
					+ "t.ta_doman doman, t.ta_domancode domancode, t.ta_startdate startdate, t.ta_enddate enddate, t.ta_status status, "
					+ "t.ta_statuscode statuscode, t.ta_recorddate recorddate, t.ta_uu uu, t.ta_taskname taskname  from office$task t where "
					+ "ta_uu = " + enuu + " and ta_domancode = " + emcode
					+ " and  date_format(ta_startdate,'%Y%m') = '" + date + "'";
			// 录入人是自己而且是待确认的
			String metaskSql = "select t.ta_id id, t.ta_code code, t.ta_detail detail, t.ta_recordercode recordercode, t.ta_recorder recorder, "
					+ "t.ta_doman doman, t.ta_domancode domancode, t.ta_startdate startdate, t.ta_enddate enddate, t.ta_status status, "
					+ "t.ta_statuscode statuscode, t.ta_recorddate recorddate, t.ta_uu uu, t.ta_taskname taskname  from office$task t where "
					+ "ta_uu = " + enuu + " and ta_recordercode = " + emcode + " and ta_domancode <>" + emcode
					+ " and ta_status = '待确认'" + " and  date_format(ta_startdate,'%Y%m') = '" + date + "'";
			List<OfficeTask> task = commonDao.query(taskSql, OfficeTask.class);
			List<OfficeTask> meTask = commonDao.query(metaskSql, OfficeTask.class);
			task.addAll(meTask);
			List<VisitRecord> records = getRecordsInOneMonth(emcode, enuu, date);
			workSchedule.setTask(task);
			workSchedule.setOutPlan(planInfos);
			workSchedule.setVisitRecord(records);
			map.put("me", workSchedule);
			if (user.getIsAdmin().equals(Constant.YES)) {// 管理员,获取下属任务等
				WorkSchedule workSchedules = new WorkSchedule();
				String outsql = "select o.op_id mp_id, o.op_recorder mp_recorder, o.op_recordercode recordercode, o.op_code mp_code, "
						+ "o.op_address mp_address, o.op_visittime mp_visittime, o.op_enuu enuu, o.op_recorddate mp_recorddate, "
						+ "o.op_arrivedate mp_arrivedate from mobile$outplan o where exists (select d.opd_id mpd_id, d.opd_code pd_code, "
						+ "d.opd_recordercode mpd_recordercode, d.opd_recorder mpd_recorder, d.opd_address mpd_address, d.opd_kind mpd_kind, "
						+ "d.opd_detno mpd_detno, d.opd_remark mpd_remark, d.opd_location mpd_location, d.opd_enuu enuu, d.opd_arrivedate mpd_arrivedate, "
						+ "d.opd_actdate mpd_actdate, d.opd_outdate mpd_outdate, d.opd_recorddate mpd_recorddate, d.opd_status mpd_status, d.opd_statuscode mpd_statuscode, "
						+ "d.opd_distance mpd_distance, d.opd_longitude mpd_longitude, d.opd_latitude mpd_latitude, d.opd_company mpd_company, d.opd_firsttime mpd_firsttime, "
						+ "d.opd_lasttime mpd_lasttime from mobile$outplandetail d where d.opd_opid = o.op_id and o.op_enuu = "
						+ enuu + " and date_format(d.opd_arrivedate,'%Y%m') = '" + date + "')";
				List<OutPlan> outPlans = commonDao.query(outsql, OutPlan.class);
				List<OutPlanInfo> othOutPlans = new ArrayList<OutPlanInfo>();
				if (!CollectionUtils.isEmpty(outPlans)) {
					for (OutPlan plan : outPlans) {
						if (!plan.getRecorderCode().equals(emcode)) {
							OutPlanInfo info = new OutPlanInfo();
							info.setMp_recorder(plan.getMp_recorder());
							info.setMp_code(plan.getMp_code());
							info.setMp_recordercode(plan.getRecorderCode());
							List<OutPlanDetail> outplandetails = outPlanDetailDao.findByOutPlan(plan);
							if (!CollectionUtils.isEmpty(outplandetails)) {
								for (OutPlanDetail detail : outplandetails) {
									info.setMpd_distance(detail.getMpd_distance());
									info.setMpd_address(detail.getMpd_address());
									info.setMpd_actdate(detail.getMpd_actdate());
									info.setMpd_company(detail.getMpd_company());
									info.setMpd_id(detail.getMpd_id());
									info.setMpd_remark(detail.getMpd_remark());
									info.setMpd_status(detail.getMpd_status());
									info.setMpd_outdate(detail.getMpd_outdate());
									info.setMpd_arrivedate(detail.getMpd_arrivedate());
									info.setMp_firsttime(detail.getMpd_firsttime());
									info.setMp_lasttime(detail.getMpd_lasttime());
									othOutPlans.add(info);
								}
							}
						}
					}
				}
				String othtaSql = "select o.ta_id id, o.ta_code code, o.ta_detail detail, o.ta_recordercode recordercode, "
						+ "o.ta_recorder recorder, o.ta_doman doman, o.ta_domancode domancode, o.ta_startdate startdate, "
						+ "o.ta_enddate enddate,o.ta_status status, o.ta_recorddate recorddate,o.ta_uu uu, o.ta_taskname taskname from office$task o where o.ta_uu = "
						+ enuu + " and date_format(ta_startdate,'%Y%m') = '" + date + "'";
				List<OfficeTask> tasks = commonDao.query(othtaSql, OfficeTask.class);
				List<OfficeTask> othTasks = new ArrayList<OfficeTask>();
				if (!CollectionUtils.isEmpty(tasks)) {
					for (OfficeTask ta : tasks) {
						if (ta.getDomancode() != null) {
							if (!ta.getDomancode().equals(emcode)) {
								othTasks.add(ta);
							}
						}
					}
				}
				List<VisitRecord> othrecords = getRecordsInOneMonth(enuu, date);
				List<VisitRecord> virecords = new ArrayList<VisitRecord>();
				if (!CollectionUtils.isEmpty(records)) {
					for (VisitRecord record : othrecords) {
						if (!Objects.equals(record.getEmuu(), emcode)) {
							virecords.add(record);
						}
					}
				}
				workSchedules.setTask(othTasks);
				workSchedules.setOutPlan(othOutPlans);
				workSchedules.setVisitRecord(virecords);
				map.put("other", workSchedules);
			}
		}
		return map;
	}

	@Override
	public ModelMap saveVisitRecord(VisitRecord visitRecord) {
		ModelMap map = new ModelMap();
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddss");
		visitRecord.setStatuscode(MobileStatus.COMMITED.getPhrase());
		visitRecord.setStatuscode(MobileStatus.COMMITED.name());
		visitRecord.setCode("VR" + sdf.format(new Date()));
		visitRecord.setVr_emname(userBaseInfoDao.findByUserUU(visitRecord.getEmuu()).getUserName());
		visitRecord.setVr_date(new Date());
		visitRecord = visitRecordDao.save(visitRecord);
		if (visitRecord.getVr_id() != null) {
			map.put("vr_id", visitRecord.getVr_id());
			map.put("success", true);
		} else {
			map.put("success", false);
		}
		return map;
	}

	@Override
	public List<VisitRecord> getRecordsInOneMonth(Long emuu, Long enuu, String date) {
		String sql = "select p.vr_detail vr_detail, p.vr_emname vr_emname, p.vr_id vr_id, p.vr_startdate vr_startdate, p.vr_cuaddress vr_cuaddress, "
				+ "p.vr_title vr_title, p.vr_enddate vr_enddate, p.vr_cucontact vr_cucontact, p.vr_cuname vr_cuname, p.vr_date vr_date from mobile$visitrecord p where "
				+ "vr_emuu = " + emuu + " and vr_enuu = " + enuu + " and (date_format(vr_startdate,'%Y%m') = '" + date
				+ "' or ( vr_startdate is null and date_format(vr_enddate,'%Y%m') = '" + date
				+ "') or (vr_startdate is null and vr_enddate is null and date_format(vr_date,'%Y%m') = '" + date + "'))";
		List<VisitRecord> records = commonDao.query(sql, VisitRecord.class);
		return records;
	}

	@Override
	public List<VisitRecord> getRecordsInOneMonth(Long enuu, String date) {
		String sql = "select p.vr_detail vr_detail, p.vr_emname vr_emname, p.vr_id vr_id, p.vr_startdate vr_startdate, p.vr_cuaddress vr_cuaddress, "
				+ "p.vr_title vr_title, p.vr_enddate vr_enddate, p.vr_cucontact vr_cucontact, p.vr_cuname vr_cuname,  p.vr_date vr_date  from mobile$visitrecord p where "
				+ "vr_enuu = " + enuu + "and (date_format(vr_startdate,'%Y%m') = '" + date
				+ "' or ( vr_startdate is null and date_format(vr_enddate,'%Y%m') = '" + date
				+ "') or (vr_startdate is null and vr_enddate is null and date_format(vr_date,'%Y%m') = '" + date + "'))";
		List<VisitRecord> records = commonDao.query(sql, VisitRecord.class);
		return records;
	}

}
