package com.palmnest.webapp.controller.dinner;

import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.application.dto.DinnerPushSetVo;
import com.palmnest.application.dto.OrgDto;
import com.palmnest.application.dto.RechargeSetDto;
import com.palmnest.application.dto.WebPageResultDto;
import com.palmnest.model.User;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.service.AppUserService;
import com.palmnest.service.dinner.service.PlaceAnOrderService;
import com.palmnest.service.property.PropertyManager;
import com.palmnest.webapp.controller.BaseFormController;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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


	@Autowired
	private PlaceAnOrderService placeAnOrderService;
	@Autowired
	private AppUserService      appUserService;
	@Autowired
	private PropertyManager     propertyManager;

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

	/**
	 * 保存自动充值积分设置
	 *
	 * @param request
	 * @param reSetDto
	 * @return
	 */
	@RequestMapping(value = "saveAndRechargeSet")
	@ResponseBody
	public AppResultDto saveAndRechargeSet(HttpServletRequest request, RechargeSetDto reSetDto) {
		User   user   = (User) request.getSession().getAttribute("UserCache");
		String userId = "";
		if (user != null) {
			userId = user.getId().toString();
		} else {
			return AppResultUtil.createErrorDto("100", "用户没有登录");
		}
		boolean bool = (reSetDto.getDayCount() <= 0 || reSetDto.getDayCount() == null);
		if (reSetDto != null && bool) {
			return AppResultUtil.createErrorDto("100", "单日数量必须大于0");
		}
		return placeAnOrderService.saveAndRechargeSet(userId, reSetDto);
	}


	/**
	 * 获取该项目下的推送设置
	 *
	 * @param orgId
	 * @return
	 */
	@RequestMapping(value = "getPushSetByOrgId")
	@ResponseBody
	public AppResultDto getPushSetByOrgId(Integer orgId) {
		if (null == orgId) {
			return AppResultUtil.createErrorDto("100", "项目不允许为空");
		}
		return placeAnOrderService.getPushSet(orgId);
	}

	/**
	 * 保存推送设置
	 *
	 * @param request
	 * @param dinnerPushSetVo
	 * @return
	 */
	@RequestMapping(value = "saveAndPushSet")
	@ResponseBody
	public AppResultDto saveAndPushSet(HttpServletRequest request, DinnerPushSetVo dinnerPushSetVo) {
		User   user   = (User) request.getSession().getAttribute("UserCache");
		String userId = "";
		if (user != null) {
			userId = user.getId().toString();
		} else {
			return AppResultUtil.createErrorDto("100", "用户没有登录");
		}
		return placeAnOrderService.saveAndPushSet(dinnerPushSetVo);
	}

	/**
	 * 修改选中用户积分
	 *
	 * @param orgId
	 * @return
	 */
	@RequestMapping(value = "updateAppUserList")
	@ResponseBody
	public AppResultDto updateAppUserList(HttpServletRequest request, String orgId, Integer integral, String userIds) {
		if (!StringUtil.isNotEmpty(orgId)) {
			return AppResultUtil.createErrorDto("100", "项目不允许为空");
		}
		if (integral == null || integral == 0 || integral < 0) {
			return AppResultUtil.createErrorDto("100", "充值数量必须大于0");
		}
		if (!StringUtil.isNotEmpty(userIds)) {
			return AppResultUtil.createErrorDto("100", "充值账号不能为空");
		}
		User   user   = (User) request.getSession().getAttribute("UserCache");
		String userId = "";
		if (user != null) {
			userId = user.getId().toString();
		} else {
			return AppResultUtil.createErrorDto("100", "用户没有登录");
		}
		try {
			return placeAnOrderService.updateAppUserByUserIds(orgId, integral, userIds, userId);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return AppResultUtil.createErrorDto("100", "批量充值失败");
		}

	}

	/**
	 * 根据项目获取该项目的单日积分设置
	 *
	 * @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
	 * @param dateType
	 * @param date
	 * @param endDate
	 * @return
	 */
	@RequestMapping(value = "getDinnerStatistics")
	@ResponseBody
	public WebPageResultDto getDinnerStatistics(String orgId, Integer dateType, String date, String endDate, int pageSize, int pageNo) {
		if (dateType == 2 && StringUtil.isNotEmpty(date)) {
			//当类型为季度时
			Map<String, String> map = getQuarter(date);
			if (map != null) {
				date = map.get("year");
				endDate = map.get("quarter");
			}
		}
		return placeAnOrderService.getDinnerDateByInfo(orgId, dateType, date, endDate, pageSize, pageNo);
	}

	/**
	 * 导出订餐统计报表
	 *
	 * @param orgId
	 * @param dateType
	 * @param date
	 * @param endDate
	 * @return
	 */
	@RequestMapping(value = "exportDinnerStatistics")
	@ResponseBody
	public AppResultDto exportDinnerStatistics(String orgId, Integer dateType, String date, String endDate) {
		if (dateType == 2 && StringUtil.isNotEmpty(date)) {
			//当类型为季度时
			Map<String, String> map = getQuarter(date);
			if (map != null) {
				date = map.get("year");
				endDate = map.get("quarter");
			}
		}
		return placeAnOrderService.exportDinnerStatistics(orgId, dateType, date, endDate);
	}

	/**
	 * 通行统计
	 *
	 * @param orgId
	 * @param dateType
	 * @param date
	 * @param endDate
	 * @return
	 */
	@RequestMapping(value = "getVisitorStatistics")
	@ResponseBody
	public WebPageResultDto getVisitorStatistics(String orgId, Integer dateType, String date, String endDate, int pageSize, int pageNo) {
		if (dateType == 2 && StringUtil.isNotEmpty(date)) {
			//当类型为季度时
			Map<String, String> map = getQuarter(date);
			if (map != null) {
				date = map.get("year");
				endDate = map.get("quarter");
			}
		}
		return placeAnOrderService.getVisitorDateByInfo(orgId, dateType, date, endDate, pageSize, pageNo);
	}

	/**
	 * 导出通行统计报表
	 *
	 * @param orgId
	 * @param dateType
	 * @param date
	 * @param endDate
	 * @return
	 */
	@RequestMapping(value = "exportVisitorStatistics")
	@ResponseBody
	public AppResultDto exportVisitorStatistics(String orgId, Integer dateType, String date, String endDate) {
		if (dateType == 2 && StringUtil.isNotEmpty(date)) {
			//当类型为季度时
			Map<String, String> map = getQuarter(date);
			if (map != null) {
				date = map.get("year");
				endDate = map.get("quarter");
			}
		}
		return placeAnOrderService.exportVisitorStatistics(orgId, dateType, date, endDate);
	}

	/**
	 * 平台活跃度统计
	 *
	 * @param orgId
	 * @param dateType
	 * @param date
	 * @param endDate
	 * @return
	 */
	@RequestMapping(value = "getPlatformStatistics")
	@ResponseBody
	public WebPageResultDto getPlatformStatistics(String orgId, Integer dateType, String date, String endDate, int pageSize, int pageNo) {
		if (dateType == 2 && StringUtil.isNotEmpty(date)) {
			//当类型为季度时
			Map<String, String> map = getQuarter(date);
			if (map != null) {
				date = map.get("year");
				endDate = map.get("quarter");
			}
		}

		if (dateType == 3 && StringUtil.isNotEmpty(date)) {
			//当类型为月份时
			Map<String, String> map = getQuarter(date);
			if (map != null) {
				endDate = map.get("quarter");
			}
		}

		return placeAnOrderService.getPlatformDataByInfo(orgId, dateType, date, endDate, pageSize, pageNo);
	}

	/**
	 * 会议室统计
	 *
	 * @param orgId
	 * @param dateType
	 * @param date
	 * @param endDate
	 * @return
	 */
	@RequestMapping(value = "getMeetStatistics")
	@ResponseBody
	public WebPageResultDto getMeetStatistics(String orgId, Integer dateType, String date, String endDate, int pageSize, int pageNo) {
		return placeAnOrderService.getMeetStatistics(orgId, dateType, date, endDate, pageSize, pageNo);
	}

	/**
	 * 导出会议室报表
	 *
	 * @param orgId
	 * @param dateType
	 * @param date
	 * @param endDate
	 * @return
	 */
	@RequestMapping(value = "exportMeetStatistics")
	@ResponseBody
	public AppResultDto exportMeetStatistics(String orgId, Integer dateType, String date, String endDate) {
		return placeAnOrderService.exportMeetStatistics(orgId, dateType, date, endDate);
	}

	/**
	 * 导出平台活跃度报表
	 *
	 * @param orgId
	 * @param dateType
	 * @param date
	 * @param endDate
	 * @return
	 */
	@RequestMapping(value = "exportPlatformStatistics")
	@ResponseBody
	public AppResultDto exportPlatformStatistics(String orgId, Integer dateType, String date, String endDate) {
		if (dateType == 2 && StringUtil.isNotEmpty(date)) {
			//当类型为季度时
			Map<String, String> map = getQuarter(date);
			if (map != null) {
				date = map.get("year");
				endDate = map.get("quarter");
			}
		}
		return placeAnOrderService.exportPlatformStatistics(orgId, dateType, date, endDate);
	}


	/**
	 * 跳转到平台管理界面
	 *
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "platformStatistic")
	public ModelAndView platformStatistic(HttpServletRequest request,
										  HttpServletResponse response) {
		Model model  = new ExtendedModelMap();
		User  user   = (User) request.getSession().getAttribute("UserCache");
		Long  userId = 1L;
		if (null != user) {
			userId = user.getId();
		}
		List<OrgDto> dtos = appUserService.getUserOrgs(userId);
		model.addAttribute("orgs", dtos);
		return new ModelAndView("admin/dataStatistic/platformStatistic", model.asMap());
	}

	/**
	 * 跳转到餐品管理界面
	 *
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "mealStatistic")
	public ModelAndView mealStatistic(HttpServletRequest request,
									  HttpServletResponse response) {
		Model        model = new ExtendedModelMap();
		User         user  = (User) request.getSession().getAttribute("UserCache");
		List<OrgDto> dtos  = appUserService.getUserOrgs(user.getId());
		model.addAttribute("orgs", dtos);
		return new ModelAndView("admin/dataStatistic/mealStatistic", model.asMap());
	}

	/**
	 * 跳转到会议室管理界面
	 *
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "meetingStatistic")
	public ModelAndView meetingStatistic(HttpServletRequest request,
										 HttpServletResponse response) {
		Model        model = new ExtendedModelMap();
		User         user  = (User) request.getSession().getAttribute("UserCache");
		List<OrgDto> dtos  = appUserService.getUserOrgs(user.getId());
		model.addAttribute("orgs", dtos);
		return new ModelAndView("admin/dataStatistic/meetingStatistic", model.asMap());
	}

	/**
	 * 跳转到通行管理界面
	 *
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "visitorStatistic")
	public ModelAndView visitorStatistic(HttpServletRequest request,
										 HttpServletResponse response) {
		Model        model = new ExtendedModelMap();
		User         user  = (User) request.getSession().getAttribute("UserCache");
		List<OrgDto> dtos  = appUserService.getUserOrgs(user.getId());
		model.addAttribute("orgs", dtos);
		return new ModelAndView("admin/dataStatistic/visitorStatistic", model.asMap());
	}

	/**
	 * 设置项目是否开启短信提醒
	 *
	 * @param orgId
	 * @param isSetMessage
	 * @return
	 */
	@RequestMapping(value = "setOrgByMessage")
	@ResponseBody
	public AppResultDto setOrgByMessage(String orgId, Integer isSetMessage,Integer isSetWechatMessage) {
		if (StringUtils.isAnyBlank(orgId)) {
			return AppResultUtil.createErrorDto("100", "项目不能为空");
		}
		return placeAnOrderService.setOrgByMessage(orgId, isSetMessage,isSetWechatMessage);
	}

	/**
	 * 获取是否开启短信提醒功能
	 *
	 * @param orgId
	 * @return
	 */
	@RequestMapping(value = "getOrgByMessage")
	@ResponseBody
	public AppResultDto getOrgByMessage(String orgId) {
		if (StringUtils.isAnyBlank(orgId)) {
			return AppResultUtil.createErrorDto("100", "项目不能为空");
		}
		Map map=new HashMap();
		int isSetMessage = 0;
		int isSetWechatMessage=1;
		try {
			TbOrganization org = propertyManager.getTbOrganizationById(Integer.parseInt(orgId));
			if (org != null) {
				isSetMessage = org.getIsSetMessage();
				isSetWechatMessage=org.getIsSetWechatMessage();
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		map.put("isSetMessage", isSetMessage);
		map.put("isSetWechatMessage", isSetWechatMessage);
		return AppResultUtil.createSuccesDto(map);
	}
}
