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.dao.UserBaseInfoDao;
import com.uas.platform.b2b.mobile.dao.ClockAddressDao;
import com.uas.platform.b2b.mobile.model.ClockAddress;
import com.uas.platform.b2b.mobile.service.ClockAddressService;

@Service
public class ClockAddressServiceImpl implements ClockAddressService {

	@Autowired
	private ClockAddressDao clockAddressDao;

	@Autowired
	private UserBaseInfoDao userBaseInfoDao;

	@Override
	public ModelMap getSignCardAddress(Long enuu) {
		ModelMap map = new ModelMap();
		map.put("data", clockAddressDao.getAddressByEnuu(enuu));
		return map;
	}

	@Override
	public ModelMap saveSignCardAddress(ClockAddress address) {
		ModelMap map = new ModelMap();
		if (address.getCs_id() == null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyMMHHss");
			address.setCs_code(sdf.format(new Date()));
			address.setCs_recorddate(new Date());
			address.setCs_recorder(userBaseInfoDao.findByUserUU(address.getEmcode()).getUserName());
			address = clockAddressDao.save(address);
			if (address.getCs_id() != null) {
				map.put("success", true);
			} else {
				map.put("success", false);
			}
		} else {
			ClockAddress addr = clockAddressDao.findOne(address.getCs_id());
			addr.setCs_recorddate(new Date());
			addr.setCs_recorder(userBaseInfoDao.findByUserUU(address.getEmcode()).getUserName());
			addr.setCs_validrange(address.getCs_validrange());
			addr.setCs_innerdistance(address.getCs_innerdistance());
			addr.setCs_latitude(address.getCs_latitude());
			addr.setCs_longitude(address.getCs_longitude());
			addr.setShortname(address.getShortname());
			addr.setCs_workaddr(address.getCs_workaddr());
			addr.setEmcode(address.getEmcode());
			addr.setEnuu(address.getEnuu());
			clockAddressDao.save(addr);
			map.put("success", true);
		}
		return map;
	}

	@Override
	public ModelMap deleteById(Long id) {
		ModelMap map = new ModelMap();
		clockAddressDao.delete(id);
		map.put("success", true);
		return map;
	}

}
