
package com.palmnest.webapp.controller.wechat;

import cn.superdesk.libs.common.json.JsonUtils;
import com.palmnest.application.AppConstants;
import com.palmnest.application.CodeConstants;
import com.palmnest.application.bean.AppCommonResultDto;
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.LogExceptionStackUtil;
import com.palmnest.application.dto.WebPageResultDto;
import com.palmnest.application.dto.input.AirAddTimeBusinessInputDto;
import com.palmnest.application.dto.input.AirConditionerAddTimeInputDto;
import com.palmnest.application.dto.input.AirConditionerAddTimeMeetingInputDto;
import com.palmnest.application.dto.input.AirSelectCompanyInputDto;
import com.palmnest.application.dto.output.AirConditionerAddTimeOutDto;
import com.palmnest.model.estateserve.AppUser;
import com.palmnest.model.estateserve.WxUser;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.model.property.VirtualArchitecture;
import com.palmnest.service.AppUserService;
import com.palmnest.service.OrganizationManager;
import com.palmnest.service.estateserve.AppUserManager;
import com.palmnest.service.estateserve.ScWxuserManager;
import com.palmnest.service.estateserve.VirtualArchManager;
import com.palmnest.service.estateserve.airconditioner.ConditionerAddTimeService;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

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


//微信端
@Slf4j
@Controller
@RequestMapping(value = "/wechat/air")
public class WxAirConditionerAddTimeController {


	@Autowired
	private ConditionerAddTimeService conditionerAddTimeService;
	@Autowired
	private VirtualArchManager        virtualArchManager;
	@Autowired
	private AppUserService            appUserService;
	@Autowired
	private OrganizationManager       organizationManager;
	@Autowired
	private ScWxuserManager           scWxuserManager;
	@Autowired
	private AppUserManager            appUserManager;


	/**
	 * 空调加时申请
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/apply")
	@ResponseBody
	public AppResultDto apply(@ModelAttribute("airConditionerAddTimeInputDto") AirConditionerAddTimeInputDto dto, HttpServletRequest request) {

		log.info("WxAirConditionerAddTimeController apply======");
		dto.setBillSource("1");//空调加时
		dto.setBillFrom(CodeConstants.WECHAT);
		dto.setIsAppManageBook(0);
		AppResultDto rdto = conditionerAddTimeService.apply(dto, request);		
		return rdto;
	}


	/**
	 * 空调加时申请
	 *
	 * @param request
	 * @return
	 *//*
	@RequestMapping(value = "/meetingApply")
	@ResponseBody
	public AppResultDto meetingApply(@ModelAttribute("airConditionerAddTimeMeetingInputDto") AirConditionerAddTimeMeetingInputDto dto, HttpServletRequest request) {

		log.info("WxAirConditionerAddTimeController meetingApply======");
		log.info("WxAirConditionerAddTimeController meetingApply dto ======" + dto.toString());
		dto.setBillSource("2");//会议室空调加时
		dto.setIsAppManageBook(0);
		return conditionerAddTimeService.meetingApply(dto, request);
	}*/

	/**
	 * 会议室申请成功后 确认加时订单
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/meetingApplyConfirm")
	@ResponseBody
	public AppResultDto meetingApplyConfirm(HttpServletRequest request) {

		log.info("WxAirConditionerAddTimeController meetingApplyConfirm======");
		return conditionerAddTimeService.meetingApplyConfirm(request);
	}

	/**
	 * 会议室加时申请-加时选框是否显示
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getIsShowSelectBox")
	@ResponseBody
	public AppResultDto getIsShowSelectBox(@ModelAttribute("airConditionerAddTimeMeetingInputDto") AirConditionerAddTimeMeetingInputDto dto, HttpServletRequest request) {

		log.info("WxAirConditionerAddTimeController getIsShowSelectBox======");
		log.info("WxAirConditionerAddTimeController getIsShowSelectBox dto ======" + dto.toString());
		return conditionerAddTimeService.getIsShowSelectBox(dto, request);
	}


	/**
	 * 物品放行
	 * 选择公司
	 *
	 * @throws Exception
	 */
	@RequestMapping(value = "/selectCompany")
	@ResponseBody
	public AppResultDto selectCompany(@ModelAttribute("airSelectCompanyInputDto") AirSelectCompanyInputDto dto, HttpServletRequest request) throws Exception {

		//String openId=request.getParameter("openId");
		dto.setAir(false);
		return conditionerAddTimeService.selectCompany(dto, request);
	}


	/**
	 * 空调加时申请确认
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/applyConfirm")
	@ResponseBody
	public AppResultDto applyConfirm(HttpServletRequest request) {

		log.info("WxAirConditionerAddTimeController applyConfirm======");
		AppResultDto rdto = conditionerAddTimeService.applyConfirm(request);
		
		
		try {
			log.info("===>推送WxAirConditionerAddTimeController push====== start");
			AirConditionerAddTimeOutDto gDto = (AirConditionerAddTimeOutDto) ((AppCommonResultDto) rdto).getData();
			conditionerAddTimeService.push(gDto.getBillId().toString());
			log.info("===>推送WxAirConditionerAddTimeController push====== end");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return rdto;
	}

	/**
	 * @param request
	 * @return 空调加时列表
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public WebPageResultDto list(HttpServletRequest request) {

		log.info("WxAirConditionerAddTimeController list======");

		try {
			int                 pageSize = Integer.parseInt(request.getParameter("pageSize"));
			int                 pageNo   = Integer.parseInt(request.getParameter("pageNo"));
			Map<String, Object> params   = new HashMap<String, Object>();
			//String openId = request.getParameter("openId");
			String status    = request.getParameter("status");
			String virtualId = request.getParameter("virtualId");//企业Id
			String openId    = request.getParameter("openId");
			String isAuditPage    = request.getParameter("isAuditPage");//是否审核页面


			//微信全部传:21,22,23,31,32,33,34,35
			//微信全部传-改后:24,31,32,33,34,35
			params.put("status", status);
			params.put("virtualId", virtualId);

			if (StringUtils.isNotEmpty(virtualId) && !"null".equals(virtualId)) {
				VirtualArchitecture virtual = virtualArchManager.get(Integer.valueOf(virtualId));
				if (null != virtual.getOrganizationId()) {
					params.put("organizationId", virtual.getOrganizationId());

					
					if(StringUtils.isNotEmpty(isAuditPage)  && "1".equals(isAuditPage)){
						
					}else{//非审核页面 过滤个人订单
						
						//项目为个人类型 过滤个人订单
						TbOrganization org = organizationManager.getOrganizationById(virtual.getOrganizationId());
						if (null != org && org.getAccountType() == 1) {
							if (StringUtils.isNotEmpty(openId)) {

								params.put("creatorBillId", openId);
								WxUser wxUser = scWxuserManager.getScWxuserByOpenId(openId);
								if (null != wxUser) {
									AppUser appUer = appUserManager.getAppUserByWxUserAndOrg(wxUser.getId(), virtual.getOrganizationId());
									params.put("userMobile", appUer.getUserMobile());
								}
							}
						}
					}
					
	
				}
			}

			Page             page = conditionerAddTimeService.findPageBy(params, pageSize, pageNo);
			WebPageResultDto dto  = new WebPageResultDto(AppConstants.SUCCESS_CODE, "成功", page.getItems());
			dto.setCount(page.getTotalCount());
			return dto;
		} catch (Exception e) {
			e.printStackTrace();
			return new WebPageResultDto(AppConstants.REPAIR_BILL_LIST_ERROR, "查询列表失败", LogExceptionStackUtil.LogExceptionStack(e));
		}
	}

	
	
	/**
	 * 同意放行
	 */
	@RequestMapping(value = "/audit")
	@ResponseBody
	public AppResultDto audit(@ModelAttribute("airConditionerAddTimeInputDto") AirConditionerAddTimeInputDto dto, HttpServletRequest request) {
		log.info("ApiAirConditionerAddTimeController agree======");

		dto.setBillFrom(CodeConstants.WECHAT);
		return conditionerAddTimeService.audit(dto, request);
	}

	
	/**
	 * 取消空调加时
	 * 待审批、待使用
	 */
	@RequestMapping(value = "/cancel")
	@ResponseBody
	public AppResultDto cancel(@ModelAttribute("airConditionerAddTimeInputDto") AirConditionerAddTimeInputDto dto, HttpServletRequest request) {

		log.info("WxAirConditionerAddTimeController refuse======");
		dto.setBillFrom(CodeConstants.WECHAT);
		return conditionerAddTimeService.cancel(dto, request);
	}

	/**
	 * 取消空调加时
	 * 待审批、待使用
	 */
	@RequestMapping(value = "/meetingCancel")
	@ResponseBody
	public AppResultDto meetingCancel(HttpServletRequest request) {

		log.info("WxAirConditionerAddTimeController meetingCancel======");
		return conditionerAddTimeService.meetingCancel(request);
	}

	/**
	 * @return 查找维修单详情
	 */
	@RequestMapping(value = "/getDetail")
	@ResponseBody
	public AppResultDto getDetail(@RequestParam("id") Long id) {

		log.info("WxAirConditionerAddTimeController getDetail======");

		return AppResultUtil.createSuccesDto(conditionerAddTimeService.findById(id));
	}
	
	
	
	/**
	 * @return 查找维修单详情
	 */
	@RequestMapping(value = "/getRuleByOrgId")
	@ResponseBody
	public AppResultDto getRuleByOrgId(@RequestParam("orgId") Long orgId) {

		log.info("WxAirConditionerAddTimeController getRuleByOrgId======");

		return AppResultUtil.createSuccesDto(conditionerAddTimeService.getRuleByOrgId(orgId));
	}
	

	/**
	 * 空调加时申请确认
	 *
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/getRoomsAndAreaByVirtualId")
	@ResponseBody
	public AppResultDto getRoomsAndAreaByVirtualId(@ModelAttribute("airConditionerAddTimeInputDto") AirConditionerAddTimeInputDto dto, HttpServletRequest request) throws Exception {

		log.info("WxAirConditionerAddTimeController getRoomsAndAreaByVirtualId======");
		return conditionerAddTimeService.getRoomsAndAreaByVirtualId(dto, request);
	}
	
	
	
	
	
	
	
	
	/**
	 * 返回空调加时业务数据
	 */
	@RequestMapping(value = "/getBusinessData")
	@ResponseBody
	public AppResultDto getBusinessData(@RequestBody AirAddTimeBusinessInputDto dto) throws Exception {
		log.info("getBusinessData-dto:{}", JsonUtils.toJson(dto));
		return conditionerAddTimeService.getBusinessData(dto);
	}

}
