package com.jpsoft.shinestar.modules.business.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.annotation.Resource;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.jpsoft.shinestar.modules.base.dao.CanteenDeviceDAO;
import com.jpsoft.shinestar.modules.base.dao.CanteenDiningDAO;
import com.jpsoft.shinestar.modules.base.dao.DeviceInfoDAO;
import com.jpsoft.shinestar.modules.base.entity.CanteenDevice;
import com.jpsoft.shinestar.modules.base.entity.CanteenDining;
import com.jpsoft.shinestar.modules.base.entity.DeviceInfo;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.jpsoft.shinestar.modules.business.dao.CanteenRecordAttendanceDAO;
import com.jpsoft.shinestar.modules.business.entity.CanteenRecordAttendance;
import com.jpsoft.shinestar.modules.business.service.CanteenRecordAttendanceService;

import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.github.pagehelper.PageHelper;

@Transactional
@Component(value="canteenRecordAttendanceService")
public class CanteenRecordAttendanceServiceImpl implements CanteenRecordAttendanceService {
	@Resource(name="canteenRecordAttendanceDAO")
	private CanteenRecordAttendanceDAO canteenRecordAttendanceDAO;

	@Resource(name="deviceInfoDAO")
	private DeviceInfoDAO deviceInfoDAO;

	@Resource(name="canteenDeviceDAO")
	private CanteenDeviceDAO canteenDeviceDAO;

	@Resource(name="canteenDiningDAO")
	private CanteenDiningDAO canteenDiningDAO;

	@Override
	public CanteenRecordAttendance get(String id) {
		// TODO Auto-generated method stub
		return canteenRecordAttendanceDAO.get(id);
	}

	@Override
	public int insert(CanteenRecordAttendance model) {
		// TODO Auto-generated method stub
		//model.setId(UUID.randomUUID().toString());
		
		return canteenRecordAttendanceDAO.insert(model);
	}

	@Override
	public int update(CanteenRecordAttendance model) {
		// TODO Auto-generated method stub
		return canteenRecordAttendanceDAO.update(model);		
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return canteenRecordAttendanceDAO.delete(id);
	}

	@Override
	public boolean exist(String id) {
		// TODO Auto-generated method stub
		int count = canteenRecordAttendanceDAO.exist(id);
		
		return count > 0 ? true : false;
	}
	
	@Override
	public List<CanteenRecordAttendance> list() {
		// TODO Auto-generated method stub
		return canteenRecordAttendanceDAO.list();
	}

	@Override
	public boolean punchIn(String deviceNo, Long personId, Date recordTime) throws ParseException {
		boolean result = false;

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		String recordDate = sdf.format(recordTime);

		Date now = new Date();

		DeviceInfo deviceInfo = deviceInfoDAO.findByDeviceNo(deviceNo);
		if(deviceInfo!=null){
			CanteenDevice canteenDevice = canteenDeviceDAO.findByDeviceId(deviceInfo.getId());
			if(canteenDevice!=null){
				List<CanteenDining> list = canteenDiningDAO.findByCanteenId(canteenDevice.getCanteenId());

				for (CanteenDining canteenDining:list) {
					String diningTime = canteenDining.getDiningTime();
					JSONObject jsonObject = JSONUtil.parseObj(diningTime);
					String start = (String)jsonObject.get("start");
					String end = (String)jsonObject.get("end");

					String startDateStr = recordDate + " " + start + ":00";
					String endDateStr = recordDate + " " + end + ":00";

					Date startTime = sdf1.parse(startDateStr);
					Date endTime = sdf1.parse(endDateStr);

					if(startTime.compareTo(endTime) > 0){
						calendar.setTime(recordTime);
						calendar.add(Calendar.DATE, 1);
						Date newDate = calendar.getTime();
						String newDateStr = sdf.format(newDate);
						endDateStr = newDateStr + " " + end + ":00";
						endTime = sdf1.parse(endDateStr);
					}

					if(recordTime.compareTo(startTime) >= 0 && recordTime.compareTo(endTime) <= 0){
						CanteenRecordAttendance canteenRecordAttendance = canteenRecordAttendanceDAO.findByPersonIdAndRecordTime(personId,startTime,endTime);
						if(canteenRecordAttendance==null){
							//此时段无刷卡记录
							canteenRecordAttendance = new CanteenRecordAttendance();
							canteenRecordAttendance.setId(UUID.randomUUID().toString());
							canteenRecordAttendance.setDelFlag(false);
							canteenRecordAttendance.setCreateTime(now);
							canteenRecordAttendance.setRecordTime(recordTime);
							canteenRecordAttendance.setDeviceNo(deviceNo);
							canteenRecordAttendance.setPersonId(personId);
							canteenRecordAttendance.setAttendanceDate(recordDate);

							int count = canteenRecordAttendanceDAO.insert(canteenRecordAttendance);

							if(count>0){
								result = true;
								break;
							}
						}
					}
				}
			}
		}

		return result;
	}

	@Override
	public Page<CanteenRecordAttendance> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize,boolean count,List<Sort> sortList) {
		Page<CanteenRecordAttendance> page = PageHelper.startPage(pageNumber,pageSize,count).doSelectPage(()->{
			canteenRecordAttendanceDAO.search(searchParams,sortList);
		});

		return page;
	}
}