package com.qmth.qrzk.web.controller;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.qmth.qrzk.dao.repositories.PrintLogRepo;
import com.qmth.qrzk.repository.user.model.PrintLog;

/**
 * 查询打印日志
 * @author zhengmin
 *
 */
@RestController
@RequestMapping("printlogs")
public class PrintLogController {

	@Autowired private PrintLogRepo printLogRepo;
	
	@RequestMapping(method=RequestMethod.GET)
	public List<PrintLog> queryByBatchAndUser(@RequestParam int batchId,@RequestParam(required=false) int userId){
		Sort sort = new Sort(Direction.DESC,"createdTime");
		return printLogRepo.findByBatchIdAndUserId(batchId, userId, sort);
	}
	
	@RequestMapping(value="queryByBatchIdAndPrintDate/{batchId}/{createdDate}", method=RequestMethod.GET)
	public List<PrintLog> findByBatchIdAndPrintTime(@PathVariable int batchId, @PathVariable String createdDate){
		Sort sort = new Sort(Direction.DESC,"createdTime");
		if(null != createdDate && !"".equals(createdDate)){
			String from = createdDate + " 00:00:00";
			String to = createdDate + " 24:00:00";
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date dateFrom;
			Date dateTo;
			try {
				dateFrom = formatter.parse(from);
				dateTo = formatter.parse(to);
				return printLogRepo.findByBatchIdAndPrintTime(batchId, dateFrom, dateTo, sort);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return null;
	}
	
	@RequestMapping(value="queryPrintDates/{batchId}", method=RequestMethod.GET)
	public List<String> queryPrintDates(@PathVariable Integer batchId){
		return printLogRepo.queryPrintDates(batchId);
	}
}
