package com.palmnest.webapp.controller.backstage;

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.LogExceptionStackUtil;
import com.palmnest.application.dto.OrgDto;
import com.palmnest.application.dto.WebPageResultDto;
import com.palmnest.application.dto.input.AirRuleInputDto;
import com.palmnest.model.User;
import com.palmnest.service.AppUserService;
import com.palmnest.service.estateserve.airconditioner.AirRuleService;
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.web.bind.annotation.ModelAttribute;
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.util.HashMap;
import java.util.List;
import java.util.Map;


//PC端
@Slf4j
@Controller
@RequestMapping(value = "/rule")
public class AirRuleController {


	@Autowired
	private AirRuleService airRuleService;

	@Autowired
	private AppUserService appUserService;

	/**
	 * 同意放行
	 */
	@RequestMapping(value = "/addOrSaveRole")
	@ResponseBody
	public AppResultDto addOrSaveRole(@ModelAttribute("AirRuleInputDto") AirRuleInputDto dto, HttpServletRequest request) {
		log.info("AirRuleController agree======");

		return airRuleService.addRole(dto, request);
	}


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

		return AppResultUtil.createSuccesDto(airRuleService.findById(id));
	}


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

		log.info("AirRuleController 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              isEnabled = request.getParameter("isEnabled");
			String              dataType  = request.getParameter("dataType");
			String              orgId  = request.getParameter("orgId");

			params.put("isEnabled", isEnabled);
			params.put("dataType", dataType);//日期类型


			User         user             = (User) request.getSession().getAttribute("UserCache");
			List<OrgDto> organizationList = appUserService.getUserOrgs(user.getId());

			String       organizationId   = "";
			/*if(StringUtils.isEmpty(orgId)){
				if (organizationList.size() >= 1) {
					organizationId = organizationList.get(0).getId().toString();
				}
			}else{
				organizationId = orgId;
			}*/
			if(StringUtils.isNotEmpty(orgId)){
				organizationId = orgId;
			}
			params.put("organizationId", organizationId);

			Page             page = airRuleService.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 = "/enableOrDisable")
	@ResponseBody
	public AppResultDto enableOrDisable(HttpServletRequest request) {
		log.info("AirRuleController agree======");

		return airRuleService.enableOrDisable(request);
	}


	/**
	 * 同意放行
	 */
	@RequestMapping(value = "/del")
	@ResponseBody
	public AppResultDto del(HttpServletRequest request) {
		log.info("AirRuleController agree======");

		return airRuleService.del(request);
	}

}
