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

import java.text.SimpleDateFormat;
import java.util.Date;

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

import com.uas.platform.b2b.mobile.dao.WorkDataDao;
import com.uas.platform.b2b.mobile.model.WorkData;
import com.uas.platform.b2b.mobile.service.WorkDataService;

@Service
public class WorkDataServiceImpl implements WorkDataService {

	@Autowired
	private WorkDataDao workDataDao;

	@Override
	public ModelMap getWorkDataByEnuu(Long enuu) {
		ModelMap map = new ModelMap();
		WorkData wd = workDataDao.getWorkDataByEnuu(enuu);
		if(wd != null) {
			map.put("data", wd);
		} else {// 如果是新企业，给个默认的数据返回回去
			WorkData wod = new WorkData();
			wod.setWd_name("默认班次");
			wod.setWd_earlytime(2);
			wod.setWd_degree(2);
			wod.setWd_ondutyone("08:30");
			wod.setWd_offdutyone("12:00");
			wod.setWd_ondutytwo("13:30");
			wod.setWd_offdutytwo("18:00");
			wod.setWd_ondutythree("");
			wod.setWd_offdutythree("");
			wod.setWd_day("1,2,3,4,5,6,7");
			map.put("data", wod);
		}
		return map;
	}

	@Override
	public ModelMap deleteById(Long id) {
		ModelMap map = new ModelMap();
		if (null == id) {
			map.put("error", "id不能为空");
		} else {
			WorkData workData = workDataDao.findOne(id);
			if (null == workData) {
				map.put("error", "该班次不存在");
			} else {
				workDataDao.delete(id);
				map.put("success", "该班次已成功删除");
			}
		}
		return map;
	}

	@Override
	public ModelMap saveWorkData(WorkData workData) {
		ModelMap map = new ModelMap();
		if (workData.getEnuu() != null) {
			WorkData wd = workDataDao.getWorkDataByEnuu(workData.getEnuu());
			if (wd != null) {
				wd.setWd_earlytime(workData.getWd_earlytime());
				wd.setWd_offdutyone(workData.getWd_offdutyone());
				wd.setWd_offdutytwo(workData.getWd_offdutytwo());
				wd.setWd_offdutythree(workData.getWd_offdutythree());
				wd.setWd_ondutyone(workData.getWd_ondutyone());
				wd.setWd_ondutytwo(workData.getWd_ondutytwo());
				wd.setWd_ondutythree(workData.getWd_ondutythree());
				wd.setWd_recorddate(new Date());
				workDataDao.save(wd);
				map.put("success", true);
				map.put("code", workData.getWd_code());
			} else {
				if (workData.getWd_code().length() != 0) {
					SimpleDateFormat sdf = new SimpleDateFormat("yyMMddss");
					workData.setWd_code("WD" + sdf.format(new Date()));
					workData.setWd_recorddate(new Date());
				}
				workData = workDataDao.save(workData);
				if (workData.getWd_id() != null) {
					map.put("id", workData.getWd_id());
					map.put("success", true);
					if (workData.getWd_code().length() != 0) {
						map.put("code", workData.getWd_code());
					}
				} else {
					map.put("success", false);
				}
			}
		}
		return map;
	}

}
