package com.palmnest.webapp.controller.backstage;

import com.palmnest.application.Constants;
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.ExcelUtils;
import com.palmnest.application.dto.WebPageResultDto;
import com.palmnest.application.dto.input.lingling.CurrentsetDto;
import com.palmnest.model.User;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.service.estateserve.VisitorInFoManager;
import com.palmnest.service.lingling.service.LingLingService;
import com.palmnest.webapp.controller.BaseFormController;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.*;
import org.springframework.web.servlet.ModelAndView;

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

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 访客预约记录 控制器
 *
 * @author HJY
 */
@Slf4j
@Controller
@RequestMapping("/visitor/order/*")
public class VisitorOrderInfoController extends BaseFormController {


	@Autowired
	private VisitorInFoManager visitorInFoManager;
	@Autowired
	private LingLingService lingLingService;

	/**
	 * 查询预约记录单跳转
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getVisitorOrderInfoList")
	public ModelAndView getVisitorOrderInfoList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Model model = new ExtendedModelMap();
		return new ModelAndView("/admin/visitor/visitorOrderInfoList", model.asMap());
	}
	
	

	
	@RequestMapping(value = "/exportVisitorOrder")
	@ResponseBody
	public AppResultDto exportVisitorOrder(HttpServletRequest request) {
		
			List<TbOrganization> organizationList = getUserRelationOrgList(request);
			String visitorInfo = request.getParameter("visitorInfo");//--访客信息 姓名、手机、企业
			String respondentsInfo = request.getParameter("respondentsInfo");//--被访人信息 姓名、手机、企业
			String visitorOrderStatus = request.getParameter("visitorOrderStatus");//状态			
			//申请开始时间
			String createBeginTime = request.getParameter("createBeginTime");
			//申请结束时间
			String createEndTime = request.getParameter("createEndTime");
			

			String paramValue = request.getParameter("paramValue");
			//访客姓名
			String name = request.getParameter("name");
			//访客手机号
			String mobile = request.getParameter("mobile");
			//访客开始时间
			String beginTime = request.getParameter("beginTime");
			//访客结束时间
			String endTime = request.getParameter("endTime");	
			
			//被访人
			String respondents = request.getParameter("respondents");
			//被访人公司
			String respondentsVirt = request.getParameter("respondentsVirt");
			//目的
			String objective = request.getParameter("objective");//--到访目的

			//项目
			String orgId     = request.getParameter("orgId");
			String visitorId = request.getParameter("visitorId");//--访客编码
			Map<String, String> map = new HashMap<String, String>();
			map.put("paramValue", paramValue);
			map.put("name", name);
			map.put("mobile", mobile);
			map.put("beginTime", beginTime);
			map.put("endTime", endTime);
			map.put("respondents", respondents);
			map.put("respondentsVirt", respondentsVirt);
			map.put("objective", objective);
			map.put("visitorId", visitorId);
			map.put("visitorInfo", visitorInfo);
			map.put("respondentsInfo", respondentsInfo);
			map.put("visitorOrderStatus", visitorOrderStatus);
			map.put("createBeginTime", createBeginTime);
			map.put("createEndTime", createEndTime);
			
			//权限集合
			String orgidStr = getUserRelationOrganizationStr(organizationList);
			if (orgidStr.split(",").length > 20) {
				orgidStr = "";
			}
			if (StringUtils.isNotEmpty(orgId)) {
				orgidStr = orgId;
			}
			
		
			return	visitorInFoManager.exportVisitorOrder(map, orgidStr);
			
		
			
		
	}
	/**
	 * 查询预约记录单
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getVisitorOrderList", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	
	public WebPageResultDto getVisitorOrderList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			List<TbOrganization> organizationList = getUserRelationOrgList(request);
			String               pageNo           = request.getParameter("page");
			String               pageSize           = request.getParameter("limit");
			if (StringUtils.isEmpty(pageNo)) {
				pageNo = "1";
			}
			if (StringUtils.isBlank(pageSize)) {
				pageSize = "10";
			}

//			String pageSize = request.getParameter("limit");
//			if(StringUtils.isEmpty(pageSize)){
//				pageSize="10";
//			}
			
			
			String visitorInfo = request.getParameter("visitorInfo");//--访客信息 姓名、手机、企业
			String respondentsInfo = request.getParameter("respondentsInfo");//--被访人信息 姓名、手机、企业
			String visitorOrderStatus = request.getParameter("visitorOrderStatus");//状态			
			//申请开始时间
			String createBeginTime = request.getParameter("createBeginTime");
			//申请结束时间
			String createEndTime = request.getParameter("createEndTime");
			

			String paramValue = request.getParameter("paramValue");
			//访客姓名
			String name = request.getParameter("name");
			//访客手机号
			String mobile = request.getParameter("mobile");
			//访客开始时间
			String beginTime = request.getParameter("beginTime");
			//访客结束时间
			String endTime = request.getParameter("endTime");	
			
			//被访人
			String respondents = request.getParameter("respondents");
			//被访人公司
			String respondentsVirt = request.getParameter("respondentsVirt");
			//目的
			String objective = request.getParameter("objective");//--到访目的

			//项目
			String orgId     = request.getParameter("orgId");
			String visitorId = request.getParameter("visitorId");//--访客编码

			Map<String, String> map = new HashMap<String, String>();
			map.put("pageSize", pageSize);
			
			
			map.put("paramValue", paramValue);
			map.put("name", name);
			map.put("mobile", mobile);
			map.put("beginTime", beginTime);
			map.put("endTime", endTime);
			map.put("respondents", respondents);
			map.put("respondentsVirt", respondentsVirt);
			map.put("objective", objective);
			map.put("visitorId", visitorId);
			
			
			map.put("visitorInfo", visitorInfo);
			map.put("respondentsInfo", respondentsInfo);
			map.put("visitorOrderStatus", visitorOrderStatus);
			map.put("createBeginTime", createBeginTime);
			map.put("createEndTime", createEndTime);
			
			//权限集合
			String orgidStr = getUserRelationOrganizationStr(organizationList);
			if (orgidStr.split(",").length > 20) {
				orgidStr = "";
			}
			if (StringUtils.isNotEmpty(orgId)) {
				orgidStr = orgId;
			}
			Page page = visitorInFoManager.getVisitorOrderInfoList(map, orgidStr, Integer.parseInt(pageNo));
			if (null == page) {
				return new WebPageResultDto("500", "获取列表失败", null);
			}
			WebPageResultDto dto = new WebPageResultDto();
			if (null != page.getItems()) {
				dto.setCount(page.getTotalCount());
			}

			dto.setCode("200");
			dto.setData(page.getItems());
			dto.setMsg("成功");
			return dto;
		} catch (Exception e) {
			log.error("错误信息异常！", e.getMessage());
			e.printStackTrace();
			return new WebPageResultDto("500", "获取列表失败", null);
		}
	}

	/**
	 * 作废访客二维码
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/delVisitorQrCode", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public AppResultDto updateVisitorOrder(HttpServletRequest request) throws Exception {

		try {
			String vistorId = request.getParameter("visitorId");
			String flag     = visitorInFoManager.delVisitorQrCode(Integer.parseInt(vistorId), "3");

			if (!flag.equals(Constants.SUCCESS_FLAG)) {
				return AppResultUtil.createErrorDto("100", flag);
			} else {
				return AppResultUtil.createSuccesDto();
			}
		} catch (Exception e) {
			log.error("VisitorOrderInfoController delVisitorQrCode:" + e.getMessage());
			return AppResultUtil.createErrorDto("100", "删除访客二维码异常");
		}
	}

	/**
	 * 获取通行基础设置信息
	 *
	 * @param orgId
	 * @return
	 */
	@RequestMapping(value = "getCurrentsetInfo")
	@ResponseBody
	public AppResultDto getCurrentsetInfo(@RequestParam("orgId") Integer orgId) {
		if (null == orgId) {
			return AppResultUtil.createErrorDto("100", "项目不允许为空");
		}
		return lingLingService.getCurrentsetInfo(orgId);
	}

	/**
	 * 保存通行基础设置信息
	 *
	 * @param currentsetDto
	 * @return
	 */
	@RequestMapping(value = "saveCurrentset")
	@ResponseBody
	public AppResultDto saveCurrentset(@RequestBody CurrentsetDto currentsetDto,HttpServletRequest request) {
		if(null == currentsetDto){
			return AppResultUtil.createErrorDto("500","参数不能为空");
		}
		if(null == currentsetDto.getOrgId()){
			return AppResultUtil.createErrorDto("500","项目不能为空");
		}
		if(null == currentsetDto.getIsLinglingAudit()){
			currentsetDto.setIsLinglingAudit(0);
		}
		if(null == currentsetDto.getVisitCompType()){
			currentsetDto.setVisitCompType(1);
		}
		if(null == currentsetDto.getVisitNameType()){
			currentsetDto.setVisitNameType(1);
		}
		if(null == currentsetDto.getVisitedCompType()){
			currentsetDto.setVisitedCompType(1);
		}
		if(null == currentsetDto.getVisitedMobileType()){
			currentsetDto.setVisitedMobileType(1);
		}

		if(null == currentsetDto.getOrderAffirm()){
			currentsetDto.setOrderAffirm(0);
		}

		if(0 == currentsetDto.getIsLinglingAudit().intValue() && (1 != currentsetDto.getVisitedMobileType().intValue() || 1 != currentsetDto.getVisitedCompType().intValue())){
			return AppResultUtil.createErrorDto("500","访客无需审核未开启时，预约输入字段被访人相关信息必须为必填项");
		}
		try{
			User user = (User) request.getSession().getAttribute("UserCache");// 获取当前用户
			if(null != user){
				currentsetDto.setOperator(Integer.parseInt(user.getId().toString()));
			}
			return  lingLingService.saveCurrentset(currentsetDto);
		}catch (Exception e){
			e.printStackTrace();
			return AppResultUtil.createErrorDto("500","保存失败");
		}
	}
}
