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

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

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

import com.uas.platform.b2b.dao.UserDao;
import com.uas.platform.b2b.mobile.dao.ClockAddressDao;
import com.uas.platform.b2b.mobile.dao.WorkDataDao;
import com.uas.platform.b2b.mobile.model.ClockAddress;
import com.uas.platform.b2b.mobile.model.WorkData;
import com.uas.platform.b2b.mobile.service.ClockSetCenterService;
import com.uas.platform.b2b.model.User;
import com.uas.platform.b2b.temporary.model.ClassObject;
import com.uas.platform.b2b.temporary.model.SignCardAddressInfo;
import com.uas.platform.b2b.temporary.model.WorkDataInfo;

@Service
public class ClockSetCenterServiceImpl implements ClockSetCenterService {


	@Autowired
	private ClockAddressDao clockAddressDao;

	@Autowired
	private WorkDataDao workDataDao;

	@Autowired
	private UserDao userDao;

	@Override
	public WorkDataInfo getSignCardInfoByEmcodeAndEnuu(Long emcode, Long enuu, String date) {
		// String sql = "select p.wd_id wd_id, p.wd_code wd_code, p.wd_name
		// wd_name, p.wd_recorder wd_recorder, p.wd_recorddate wd_recorddate,
		// p.wd_hours wd_hours, "
		// + "p.wd_degree wd_degree, p.wd_ondutyone wd_ondutyone,
		// p.wd_offdutyone wd_offdutyone, p.wd_ondutytwo wd_ondutytwo,
		// p.wd_offdutytwo wd_offdutytwo, "
		// + "p.wd_ondutythree wd_ondutythree, p.wd_offdutythree
		// wd_offdutythree, p.wd_emcode emcode, p.wd_man wd_man,
		// p.wd_defaultorcode wd_defaultorcode, "
		// + "p.wd_defaultor wd_defaultor, p.wd_day wd_day, p.wd_pcount
		// wd_pcount, p.wd_earlytime wd_earlytime, p.wd_enuu enuu from
		// mobile$workdata p where "
		// + "wd_emcode = " + emcode + " and wd_enuu = " + enuu
		// + "and to_date(to_char(wd_recorddate, 'yyyy mm dd'), 'yyyy mm dd') =
		// to_date ('" + date
		// + "','yyyy mm dd')";
		// List<WorkData> workdatas = commonDao.query(sql, WorkData.class);
		WorkData workdata = workDataDao.getWorkDataByEnuu(enuu);
		WorkDataInfo wd = new WorkDataInfo();
		List<User> users = userDao.findByEnUUAndUserUU(enuu, emcode);
		String[] days = workdata.getWd_day().split(",");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date today = null;
		try {
			today = sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(today);
		int weekday = c.get(Calendar.DAY_OF_WEEK) - 1;
		boolean isWork = false;
		for (String day : days) {
			if (Integer.valueOf(day).equals(weekday)) {
				isWork = true;
			}
		}
		if (!CollectionUtils.isEmpty(users) && isWork) {// 用户与企业有绑定关系, 并且今天上班
			if (workdata != null) {
				if (wd != null) {
					List<ClockAddress> address = clockAddressDao.getAddressByEnuu(enuu);
					List<SignCardAddressInfo> addrs = new ArrayList<SignCardAddressInfo>();
					for (ClockAddress add : address) {
						SignCardAddressInfo addr = new SignCardAddressInfo();
						addr.setCS_CODE(add.getCs_code());
						addr.setCS_ID(add.getCs_id());
						addr.setCS_INNERDISTANCE(add.getCs_innerdistance());
						addr.setCS_LATITUDE(add.getCs_latitude());
						addr.setCS_LONGITUDE(add.getCs_longitude());
						addr.setCS_RECORDATE(add.getCs_recorddate());
						addr.setCS_RECORDER(add.getCs_recorder());
						addr.setCS_VALIDRANGE(add.getCs_validrange());
						addr.setCS_WORKADDR(add.getCs_workaddr());
						addr.setEMCODE(add.getEmcode());
						addr.setENUU(add.getEnuu());
						addr.setSHORTNAME(add.getShortname());
						addrs.add(addr);
					}
					wd.setComAddressdata(addrs);
					ClassObject obj1 = new ClassObject();
					ClassObject obj2 = new ClassObject();
					ClassObject obj3 = new ClassObject();
					obj1.setWd_onduty(workdata.getWd_ondutyone());
					obj1.setWd_offduty(workdata.getWd_offdutyone());
					wd.setClass1(obj1);
					obj2.setWd_onduty(workdata.getWd_ondutytwo());
					obj2.setWd_offduty(workdata.getWd_offdutytwo());
					wd.setClass2(obj2);
					obj3.setWd_onduty(workdata.getWd_ondutythree());
					obj3.setWd_offduty(workdata.getWd_offdutythree());
					wd.setClass3(obj3);
					wd.setDay(workdata.getWd_day());
					wd.setCode(workdata.getWd_code());
					wd.setDefaultor(workdata.getWd_defaultor());
					wd.setDefaultorCode(workdata.getWd_defaultorCode());
					wd.setDegree(workdata.getWd_degree());
					wd.setEarlytime(workdata.getWd_earlytime());
					wd.setEmcode(workdata.getEmcode());
					wd.setEnuu(workdata.getEnuu());
					wd.setHours(workdata.getWd_hours());
					wd.setId(workdata.getWd_id());
					wd.setMan(workdata.getWd_man());
					wd.setName(workdata.getWd_name());
					wd.setPcount(workdata.getWd_pcount());
					wd.setRecorddate(workdata.getWd_recorddate());
					wd.setRecorder(workdata.getWd_recorder());
				}
			}
		}
		return wd;
	}

}
