package com.palmnest.webapp.controller.dinner;

import cn.jiguang.common.utils.StringUtils;

import com.palmnest.application.AppConstants;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.application.dto.WebPageResultDto;
import com.palmnest.application.dto.input.OrderDinnerInputDto;
import com.palmnest.model.User;
import com.palmnest.service.dinner.service.OrderDinnerService;
import com.palmnest.service.dinner.service.PlaceAnOrderService;
import com.palmnest.webapp.controller.BaseFormController;
import com.palmnest.webapp.quartz.OrderDinnerRechargeTimerTask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

/**
 * @author longmeizhen
 */
@Slf4j
@Controller
@RequestMapping("/admin/dinner/*")
public class OrderDinnerController extends BaseFormController {

	@Autowired
	private OrderDinnerService orderDinnerService;
	
	@Autowired
	private PlaceAnOrderService placeAnOrderService;

	/**
	 * 获取我的订单列表
	 *
	 * @param consId
	 * @param status 0代表全部 1代表待送餐 2代表待评价 3代表已完成4已取消
	 *               (状态为待送餐时允许取消订单[取消订单的时间必须在指定时间之前])
	 * @return
	 */
	@RequestMapping(value = "getOrderDinnerList")
	@ResponseBody
	public WebPageResultDto getOrderDinnerList(HttpServletRequest request, OrderDinnerInputDto dto, int pageSize, int pageNo) {
		User user = (User) request.getSession().getAttribute("UserCache");
//		if(StringUtils.isEmpty(dto.getOrgId())){
//			dto.setOrgId(getUserManageOrgIds(user));
//		}
		String orgIds = getUserManageOrgIds(user);
		if (StringUtils.isEmpty(dto.getOrgId()) && StringUtils.isNotEmpty(orgIds)) {
			dto.setOrgId(orgIds);
		}
		Page             page    = orderDinnerService.findList(dto, pageSize, pageNo);
		WebPageResultDto pageDto = new WebPageResultDto(AppConstants.SUCCESS_CODE, "成功", page == null ? null : page.getItems());
		if (page == null) {
			pageDto.setCount(Long.parseLong("0"));
		} else {
			pageDto.setCount(page.getTotalCount());
		}
		return pageDto;
	}

	/**
	 * 获取详情
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "getOrderDinnerInfo")
	@ResponseBody
	public AppResultDto getOrderDinnerInfo(Long id) {
		if (null == id) {
			return AppResultUtil.createErrorDto("100", "id不能为空");
		}
		return orderDinnerService.findById(id);
	}

	/**
	 * 导出今日订餐汇总
	 *
	 * @param orgId
	 * @return
	 */
	@RequestMapping(value = "exportExcel")
	@ResponseBody
	public AppResultDto exportExcel(HttpServletRequest request, String orgId) {
		if (StringUtils.isEmpty(orgId)) {
			return AppResultUtil.createErrorDto("100", "项目不允许为空");
		}
		String res = orderDinnerService.getOrderTime(orgId, "importTime");
		if (StringUtils.isEmpty(res)) {
			//还没有到规定时间
			return AppResultUtil.createErrorDto("100", "抱歉，还未到汇总时间");
		}
		return AppResultUtil.createSuccesDto();
	}

	/**
	 * 数据统计
	 *
	 * @param request
	 * @param orgId         项目
	 * @param type          类型   1订餐统计、2评价统计、3部门统计、4个人统计
	 * @param dateType      统计周期 1年2季度3月
	 * @param year          年份
	 * @param time          具体时间 (年份时不需要传)
	 * @param operationType 操作类型 1 查询 2导出
	 * @return
	 */
	@RequestMapping(value = "getDataStatistics")
	@ResponseBody
	public WebPageResultDto getDataStatistics(HttpServletRequest request, String orgId, int type, int dateType, String year, String time, int operationType, int pageSize, int pageNo) {
		User user = (User) request.getSession().getAttribute("UserCache");
		if (StringUtils.isEmpty(orgId)) {
			orgId = getUserManageOrgIds(user);
		}
		if (StringUtils.isEmpty(orgId)) {
			return new WebPageResultDto("100", "项目不允许为空", null);
		}
		return orderDinnerService.getDataStatistics(orgId, type, dateType, year, time, operationType, pageSize, pageNo);
	}

	/**
	 * 获取最开始新增数据的时间
	 *
	 * @param request
	 * @param orgId
	 * @return
	 */
	@RequestMapping(value = "getTopCreateTime")
	@ResponseBody
	public AppResultDto getTopCreateTime(HttpServletRequest request, String orgId) {
		User user = (User) request.getSession().getAttribute("UserCache");
		if (!StringUtil.isNotEmpty(orgId)) {
			orgId = getUserManageOrgIds(user);
		}
		if (!StringUtil.isNotEmpty(orgId)) {
			return AppResultUtil.createErrorDto("100", "项目不允许为空");
		}
		String time = orderDinnerService.getTopCreateTime(orgId);
		return AppResultUtil.createSuccesDto(time);
	}
	
	/**
	 * 获取最开始新增数据的时间
	 *
	 * @param request
	 * @param orgId
	 * @return
	 */
	@RequestMapping(value = "automaticRecharge")
	@ResponseBody
	public AppResultDto automaticRecharge(HttpServletRequest request) {
		
		
		log.info("手动调用定时任务placeAnOrderService.automaticRecharge() start===>");
		
		placeAnOrderService.automaticRecharge();
		
		
		log.info("手动调用定时任务placeAnOrderService.automaticRecharge() end===>");
		
		return AppResultUtil.createSuccesDto();
	}

}
