package com.palmnest.webapp.controller.dinner.wechat;

import cn.jiguang.common.utils.StringUtils;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.core.util.DateUtils;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.application.dto.DinnerPushSetVo;
import com.palmnest.model.User;
import com.palmnest.service.AppUserService;
import com.palmnest.service.dinner.service.OrderDinnerService;
import com.palmnest.service.dinner.service.PlaceAnOrderService;
import com.palmnest.service.estateserve.DateTypeInfoService;
import com.palmnest.webapp.controller.BaseFormController;
import lombok.extern.slf4j.Slf4j;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author longmeizhen
 * @version 创建时间：2019年3月1日 下午6:08:11
 */
@Slf4j
@Controller
@RequestMapping("/wechat/dinner/place/*")
public class WechatPlaceAnOrderController extends BaseFormController {


	@Autowired
	private PlaceAnOrderService placeAnOrderService;
	@Autowired
	private AppUserService      appUserService;
	@Autowired
	private OrderDinnerService  orderDinnerService;
	@Autowired
	private DateTypeInfoService dateTypeInfoService;

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * 获取该项目下整周的餐品
	 *
	 * @param orgId
	 * @return
	 */
	@RequestMapping(value = "getWeek")
	@ResponseBody
	public AppResultDto getWeek(String orgId, String wxUserId) {
		long beginTime = System.currentTimeMillis();
		System.out.println("beginTime:getWeek" + beginTime);
		log.info("beginTime:getWeek" + beginTime);
		Map<String, String> map = new HashMap<String, String>();
		map.put("orgId", orgId);
		String result = decideParameter(map);
		if (StringUtil.isNotEmpty(result)) {
			return AppResultUtil.createErrorDto("100", result);
		}
		return placeAnOrderService.getWeek(orgId, wxUserId);
	}

	/**
	 * 获取该项目下整周的餐品
	 *
	 * @param orgId
	 * @return
	 */
	@RequestMapping(value = "getWeekDate")
	@ResponseBody
	public AppResultDto getWeekDate(String orgId, String wxUserId, String dinnerTime) {
		if (!StringUtil.isNotEmpty(dinnerTime)) {
			return AppResultUtil.createErrorDto("100", "抱歉，请先选择日期！");
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("orgId", orgId);
		String result = decideParameter(map);
		if (StringUtil.isNotEmpty(result)) {
			return AppResultUtil.createErrorDto("100", result);
		}
		try {
			dinnerTime = sdf.format(sdf.parse(dinnerTime));
		} catch (Exception e) {
			log.error("数据格式异常：" + dinnerTime);
			return AppResultUtil.createErrorDto("100", "数据格式异常，请联系管理员");
		}
		return placeAnOrderService.getWeekDate(orgId, wxUserId, dinnerTime);
	}

	/**
	 * 获取该项目下该时间下所有用户
	 *
	 * @param orgId
	 * @param time  订餐时间
	 * @param type  1订餐时使用2我的界面使用
	 * @return
	 */
	@RequestMapping(value = "getAllUser")
	@ResponseBody
	public AppResultDto getAllUser(String orgId, String time, int type, String developId, String wxUserId) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("orgId", orgId);
		resMap.put("time", time);
		resMap.put("type", type);
		resMap.put("wxUserId", wxUserId);
		Map<String, String> map = new HashMap<String, String>();
		map.put("orgId", orgId);
		map.put("userId", wxUserId);
		String result = decideParameter(map);
		if (StringUtil.isNotEmpty(result)) {
			return AppResultUtil.createErrorDto("100", result);
		}
		try {
			if (type == 1) {
				if (!StringUtil.isNotEmpty(time)) {
					return AppResultUtil.createErrorDto("100", "数据格式异常，请联系管理员");
				} else {
					time = sdf.format(sdf.parse(time));
					String sysDate = DateUtils.formatDate(new Date(), "yyyy-MM-dd");
					if (sysDate.equals(time)) {
						//判断当前时间是否超出11点
						String str = orderDinnerService.getOrderTime(orgId, "orderTime");
						if (StringUtils.isNotEmpty(str)) {
							//超出
							return AppResultUtil.createErrorDto("100", "抱歉，当前时间已超过订餐截止时间（" + str + "），明天记得早点哦");
						}
					}
				}
			}
		} catch (Exception e) {
			log.error("数据格式异常：" + time);
			return AppResultUtil.createErrorDto("100", "数据格式异常，请联系管理员");
		}
		return placeAnOrderService.getUserBy(resMap);
	}

	/**
	 * 根据条件获取主食及份数(管理员还会需要展示所有对应的用户)
	 * 单品的时候主食为空，直接获取到了管理员时所对应下面的用户
	 *
	 * @param orgId
	 * @param openId
	 * @param mealId
	 * @param time
	 * @return
	 */
	@RequestMapping(value = "getFoodAndCount")
	@ResponseBody
	public AppResultDto getFoodAndCount(String orgId, String wxUserId, String mealId, String time) {
		Map<String, String> parements = new HashMap<String, String>();
		parements.put("orgId", orgId);
		parements.put("time", time);
		parements.put("mealId", mealId);
		parements.put("wxUserId", wxUserId);
		Map<String, String> map = new HashMap<String, String>();
		map.put("orgId", orgId);
		map.put("userId", wxUserId);
		String result = decideParameter(map);
		if (StringUtil.isNotEmpty(result)) {
			return AppResultUtil.createErrorDto("100", result);
		}
		try {
			if (!StringUtil.isNotEmpty(time)) {
				return AppResultUtil.createErrorDto("100", "数据格式异常，请联系管理员");
			} else {
				time = sdf.format(sdf.parse(time));
			}
		} catch (Exception e) {
			log.error("数据格式异常：" + time);
			return AppResultUtil.createErrorDto("100", "数据格式异常，请联系管理员");
		}
		return placeAnOrderService.getMealCarByMealId(parements);
	}

	/**
	 * 获取指定时间下的所有会议餐
	 *
	 * @param orgId
	 * @param openId
	 * @param time年月
	 * @return
	 */
	@RequestMapping(value = "getAllMeetFood")
	@ResponseBody
	public AppResultDto getAllMeetFood(String orgId, String wxUserId, String time) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("orgId", orgId);
		map.put("userId", wxUserId);
		String result = decideParameter(map);
		if (StringUtil.isNotEmpty(result)) {
			return AppResultUtil.createErrorDto("100", result);
		}
		try {
			if (!StringUtil.isNotEmpty(time)) {
				return AppResultUtil.createErrorDto("100", "数据格式异常，请联系管理员");
			}
		} catch (Exception e) {
			log.error("数据格式异常：" + time);
			return AppResultUtil.createErrorDto("100", "数据格式异常，请联系管理员");
		}
		return placeAnOrderService.getAllMeetFood(orgId, wxUserId, time);
	}

	/**
	 * 获取指定时间下的所有会议餐
	 *
	 * @param orgId
	 * @param openId
	 * @param time年月
	 * @return
	 */
	@RequestMapping(value = "getAllTempOrder")
	@ResponseBody
	public AppResultDto getAllTempOrder(String orgId, String wxUserId, String time) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("orgId", orgId);
		map.put("userId", wxUserId);
		String result = decideParameter(map);
		if (StringUtil.isNotEmpty(result)) {
			return AppResultUtil.createErrorDto("100", result);
		}
		try {
			if (!StringUtil.isNotEmpty(time)) {
				return AppResultUtil.createErrorDto("100", "数据格式异常，请联系管理员");
			}
		} catch (Exception e) {
			log.error("数据格式异常：" + time);
			return AppResultUtil.createErrorDto("100", "数据格式异常，请联系管理员");
		}
		return placeAnOrderService.getAllTempOrder(orgId, wxUserId, time);
	}

	/**
	 * 获取指定用户的所有积分记录
	 *
	 * @param orgId
	 * @param wxUserId
	 * @return
	 */
	@RequestMapping(value = "getAllIntegralRecodeList")
	@ResponseBody
	public AppResultDto getAllIntegralRecodeList(String orgId, String wxUserId) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("orgId", orgId);
		map.put("userId", wxUserId);
		String result = decideParameter(map);
		if (StringUtil.isNotEmpty(result)) {
			return AppResultUtil.createErrorDto("100", result);
		}
		return placeAnOrderService.getRecodeInfo(wxUserId, orgId);
	}

	/**
	 * 获取餐品下选中的用户
	 *
	 * @param orgId
	 * @param time
	 * @param mealId
	 * @param wxUserId
	 * @return
	 */
	@RequestMapping(value = "getMealUser")
	@ResponseBody
	public AppResultDto getMealUser(String orgId, String time, String mealId, String wxUserId) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("orgId", orgId);
		map.put("userId", wxUserId);
		String result = decideParameter(map);
		if (StringUtil.isNotEmpty(result)) {
			return AppResultUtil.createErrorDto("100", result);
		}
		if (!StringUtil.isNotEmpty(mealId) || !StringUtil.isNotEmpty(time)) {
			return AppResultUtil.createErrorDto("100", "参数不允许为空");
		}
		try {
			time = sdf.format(sdf.parse(time));
			String sysDate = DateUtils.formatDate(new Date(), "yyyy-MM-dd");
			if (sysDate.equals(time)) {
				//判断当前时间是否超出11点
				String str = orderDinnerService.getOrderTime(orgId, "orderTime");
				if (StringUtils.isNotEmpty(str)) {
					//超出
					return AppResultUtil.createErrorDto("100", "抱歉，当前时间已超过订餐截止时间（" + str + "），明天记得早点哦");
				}
			}
		} catch (Exception e) {
			log.error("数据格式异常：" + time);
			return AppResultUtil.createErrorDto("100", "数据格式异常，请联系管理员");
		}
		Map<String, String> parements = new HashMap<String, String>();
		parements.put("orgId", orgId);
		parements.put("time", time);
		parements.put("mealId", mealId);
		parements.put("userId", wxUserId);
		return placeAnOrderService.getMealUser(parements);
	}

	/**
	 * 根据项目获取该项目的单日积分设置
	 *
	 * @param orgId
	 * @return
	 */
	@RequestMapping(value = "getIntegralByOrgId")
	@ResponseBody
	public AppResultDto getIntegralByOrgId(String orgId) {
		if (!StringUtil.isNotEmpty(orgId)) {
			return AppResultUtil.createErrorDto("100", "项目不允许为空");
		}
		return placeAnOrderService.getIntegralByOrgId(orgId);
	}

	/**
	 * 根据项目获取该项目的单日积分设置
	 *
	 * @param orgId
	 * @return
	 */
	@RequestMapping(value = "getTempOpen")
	@ResponseBody
	public AppResultDto getTempOpen(String orgId) {
		int open = 0;
		if (!StringUtil.isNotEmpty(orgId)) {
			return AppResultUtil.createSuccesDto(open);
		}
		open = placeAnOrderService.getTempOpen(orgId);
		return AppResultUtil.createSuccesDto(open);
	}


	/**
	 * 根据项目获取该项目的单日积分设置
	 *
	 * @param orgId
	 * @return
	 */
	@RequestMapping(value = "sendMessage")
	@ResponseBody
	public AppResultDto sendMessage(String orgId, String openId) {
		placeAnOrderService.sendMessageByEveryDay(openId, orgId);
		return AppResultUtil.createSuccesDto();
	}

	/**
	 * 根据项目获取该项目的单日积分设置
	 *
	 * @param orgId
	 * @return
	 */
	@RequestMapping(value = "getMealRanking")
	@ResponseBody
	public AppResultDto getMealRanking(String orgId) {
		if (!StringUtil.isNotEmpty(orgId)) {
			return AppResultUtil.createErrorDto("100", "项目不允许为空");
		}
		return orderDinnerService.getMealRanking(orgId);
	}

	/**
	 * 测试发送短信
	 *
	 * @param orgId
	 * @param phongs
	 * @return
	 */
	@RequestMapping(value = "sendMobile")
	@ResponseBody
	public AppResultDto sendMobile(String orgId, String mobile) {
		placeAnOrderService.sendMessageByOrgSet(orgId, mobile);
		return AppResultUtil.createSuccesDto();
	}

	/**
	 * 更新日期类型
	 *
	 * @param year
	 * @param month
	 * @return
	 */
	@RequestMapping(value = "saveDateType")
	@ResponseBody
	public AppResultDto saveDateType(String year, String month) {
		String res = dateTypeInfoService.saveDateType(year, month);
		if ("SUCCESS".equals(res)) {
			String s = dateTypeInfoService.saveDateTypeAddOne(year, month);
			if ("SUCCESS".equals(s)) {
				return AppResultUtil.createSuccesDto();
			}
			return AppResultUtil.createErrorDto("100", "更新失败");
		} else {
			return AppResultUtil.createErrorDto("100", "更新失败");
		}

	}


	/**
	 * 获取导出的数据
	 *
	 * @param orgId
	 * @return
	 */
	@RequestMapping(value = "getOrderInfoTask2")
	@ResponseBody
	public AppResultDto getOrderInfoTask2(@RequestParam(value = "orgId",required = false) String orgId,@RequestParam(value = "date",required = false) String date) {
		orderDinnerService.getOrderInfoTask2(orgId,date);
		return AppResultUtil.createSuccesDto();
	}

	@RequestMapping(value = "getOrderInfoTask")
	@ResponseBody
	public AppResultDto getOrderInfoTask(@RequestParam(value = "orgId",required = false) String orgId) {
		orderDinnerService.getOrderInfoTask(orgId);
		return AppResultUtil.createSuccesDto();
	}

	/**
	 * 保存推送设置
	 *
	 * @param request
	 * @param dinnerPushSetVo
	 * @return
	 */
	@RequestMapping(value = "saveAndPushSet")
	@ResponseBody
	public AppResultDto saveAndPushSet(HttpServletRequest request, DinnerPushSetVo dinnerPushSetVo) {
		return placeAnOrderService.saveAndPushSet(dinnerPushSetVo);
	}

	@RequestMapping(value = "getPushSetByOrgId")
	@ResponseBody
	public AppResultDto getPushSetByOrgId(Integer orgId) {
		if (null == orgId) {
			return AppResultUtil.createErrorDto("100", "项目不允许为空");
		}
		return placeAnOrderService.getPushSet(orgId);
	}

	@RequestMapping(value = "getPushSetByUserId")
	@ResponseBody
	public AppResultDto getPushSetByUserId(Integer userId) {
		if (null == userId) {
			return AppResultUtil.createErrorDto("100", "用户不允许为空");
		}
		return placeAnOrderService.getPushSetByUserId(userId);
	}

	@RequestMapping(value = "sendTempMsg")
	@ResponseBody
	public AppResultDto sendTempMsg(@RequestParam(value = "openId",required = false) String openId,@RequestParam(value = "orgId",required = false) String orgId,@RequestParam(value = "date",required = false) String date) {
		placeAnOrderService.sendTempMsg(openId,orgId,date);
		return AppResultUtil.createSuccesDto();
	}
}
