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

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;

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.enumeration.MobileStatus;
import com.uas.platform.b2b.mobile.model.OutAddress;
import com.uas.platform.b2b.mobile.model.OutPlan;
import com.uas.platform.b2b.mobile.model.OutPlanDetail;
import com.uas.platform.b2b.mobile.service.OutPlanService;
import com.uas.platform.b2b.temporary.model.OutPlanInfo;

@Service
public class OutPlanServiceImpl implements OutPlanService {

	@Autowired
	private OutPlanDetailDao outPlanDetailDao;

	@Autowired
	private CommonDao commonDao;

	@Autowired
	private UserBaseInfoDao userBaseInfoDao;

	@Override
	public ModelMap saveOutPlan(OutPlan outPlan, List<OutPlanDetail> outPlanDetails) {
		ModelMap map = new ModelMap();
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMhhss");
		outPlan.setMp_code("OU" + sdf.format(new Date()));
		outPlan.setMp_recorder(userBaseInfoDao.findByUserUU(outPlan.getRecorderCode()).getUserName());
		Set<OutPlanDetail> outDetails = new HashSet<OutPlanDetail>();
		if (!CollectionUtils.isEmpty(outPlanDetails)) {
			for (OutPlanDetail outPlanDetail : outPlanDetails) {
				outPlanDetail.setMpd_code("OUT" + sdf.format(new Date()));
				outPlanDetail.setMpd_recorddate(new Date());
				outPlanDetail.setMpd_status(MobileStatus.ENTERING.getPhrase());
				outPlanDetail.setMpd_statuscode(MobileStatus.ENTERING.name());
				outPlanDetail.setOutPlan(outPlan);
				outDetails.add(outPlanDetail);
			}
		}
		outPlanDetails = outPlanDetailDao.save(outDetails);
		for (OutPlanDetail outPlanInfo : outPlanDetails) {
			map.put("mpd_id", outPlanInfo.getMpd_id());
			map.put("mp_id", outPlanInfo.getMpd_id());
			map.put("success", true);
			map.put("mp_code", outPlanInfo.getMpd_code());
			map.put("data", outPlanDetails);
		}
		return map;
	}

	@Override
	public ModelMap getOutPlan(Long emcode, Long enuu) {
		ModelMap map = new ModelMap();
		String sql = "select p.op_id mp_id,p.op_recorder mp_recorder,p.op_recordercode recorderCode,p.op_code mp_code,p.op_address mp_address,"
				+ "p.op_visittime mp_visittime,p.op_enuu enuu from mobile$outplan p where op_enuu = " + enuu
				+ " and op_recordercode = " + emcode + " order by op_id desc";
		List<OutPlan> outPlans = commonDao.query(sql, OutPlan.class);
		List<OutPlanInfo> infos = new ArrayList<OutPlanInfo>();
		for (OutPlan outPlan : outPlans) {
			String detailSql = "select p.opd_id mpd_id, p.opd_code mpd_code, p.opd_recordercode mpd_recordercode, p.opd_recorder mpd_recorder, p.opd_address mpd_address,"
					+ "p.opd_kind mpd_kind, p.opd_detno mpd_detno, p.opd_remark mpd_remark, p.opd_location mpd_location, p.opd_enuu mpd_enuu, p.opd_arrivedate mpd_arrivedate,"
					+ "p.opd_actdate mpd_actdate, p.opd_outdate mpd_outdate, p.opd_recorddate mpd_recorddate, p.opd_status mpd_status, p.opd_statuscode mpd_statuscode, p.opd_longitude longitude,p.opd_latitude latitude, p.opd_distance mpd_distance, p.opd_company mpd_company, p.opd_firsttime mpd_firsttime, p.opd_lasttime mpd_lasttime from mobile$outplandetail p where opd_opid = ?"
					+ " and opd_arrivedate>=now() and opd_arrivedate<adddate(now(), interval 1 day) order by opd_id desc";
			List<OutPlanDetail> details = commonDao.query(detailSql, OutPlanDetail.class, outPlan.getMp_id());
			for (OutPlanDetail detail : details) {
				String addSql = "select p.oad_longitude md_longitude, p.oad_latitude md_latitude from mobile$outaddress p where oad_address = '"
						+ detail.getMpd_address() + "'";
				List<OutAddress> addrs = commonDao.query(addSql, OutAddress.class);
				if (!CollectionUtils.isEmpty(addrs)) {
					OutAddress addr = addrs.get(0);
					OutPlanInfo info = new OutPlanInfo();
					info.setEnuu(detail.getEnuu());
					info.setMd_latitude(addr.getMd_latitude());
					info.setMd_longitude(addr.getMd_longitude());
					info.setMp_id(outPlan.getMp_id());
					info.setMpd_mpid(outPlan.getMp_id());
					info.setMpd_actdate(detail.getMpd_actdate());
					info.setMpd_address(detail.getMpd_address());
					info.setMpd_arrivedate(detail.getMpd_arrivedate());
					info.setMpd_distance(detail.getMpd_distance());
					info.setMpd_id(detail.getMpd_id());
					info.setMpd_kind(detail.getMpd_kind());
					info.setMpd_location(detail.getMpd_location());
					info.setMpd_outdate(detail.getMpd_outdate());
					info.setMpd_recorddate(detail.getMpd_recorddate());
					info.setMpd_remark(detail.getMpd_remark());
					info.setMpd_status(detail.getMpd_status());
					info.setMpd_company(detail.getMpd_company());
					info.setMp_firsttime(detail.getMpd_firsttime());
					info.setMp_lasttime(detail.getMpd_lasttime());
					infos.add(info);
				}
			}
			Set<OutPlanDetail> planDetails = new HashSet<OutPlanDetail>();
			planDetails.addAll(details);
			outPlan.setOutPlanDetail(planDetails);
		}
		map.put("data", infos);
		map.put("success", true);
		return map;
	}

	@Override
	public ModelMap updateStatus(Long id, String statuscode) {
		ModelMap map = new ModelMap();
		OutPlanDetail detail = outPlanDetailDao.findOne(id);
		detail.setMpd_statuscode(statuscode);
		if (statuscode.equals(MobileStatus.CHECKOUT.name())) {
			detail.setMpd_status(MobileStatus.CHECKOUT.getPhrase());
		}
		if (statuscode.equals(MobileStatus.done.name())) {
			detail.setMpd_status(MobileStatus.done.getPhrase());
		}
		outPlanDetailDao.save(detail);
		map.put("success", true);
		return map;
	}

}
