package com.palmnest.service.dorm.service.impl;

import cn.jiguang.common.utils.StringUtils;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.dto.DormImportDto;
import com.palmnest.application.dto.input.DormRoomerListDto;
import com.palmnest.application.dto.output.DormRoomerInfoDto;
import com.palmnest.model.dorm.DormRoomerInfo;
import com.palmnest.service.dorm.manager.DormRoomerManager;
import com.palmnest.service.dorm.service.DormRoomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 宿舍管理信息service实现类
 *
 * @author longmeizhen
 */
@Slf4j
@Service("dormRoomerService")
public class DormRoomerServiceImpl implements DormRoomerService {


	@Autowired
	private DormRoomerManager dormRoomerManager;

	/**
	 * 获取宿舍管理信息列表
	 *
	 * @param dto界面上的参数
	 * @param pageNo
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	@Override
	public Page findList(DormRoomerListDto dto, int pageNo, int pageSize)
			throws Exception {
		return dormRoomerManager.findList(dto, pageNo, pageSize);
	}

	/**
	 * 导入宿舍管理数据
	 *
	 * @param ins
	 * @param userId 创建人
	 * @return
	 * @throws Exception
	 */
	@Override
	public AppResultDto importDormRoomer(InputStream ins, String userId, String orgId)
			throws Exception {
		//数据导入
		Map<String, Object> map      = dormRoomerManager.importDormRoomer(ins, orgId);
		int                 bugCount = Integer.parseInt(map.get("bugCount").toString());
		if (bugCount > 0) {//如果bug数量大于0，说明有异常，直接返回异常即可
			return AppResultUtil.createErrorDto("100", map.get("bugStr").toString());
		} else {//否则的话就将用户信息保存到数据库表中
			if (map.containsKey("dormList")) {
				try {
					List<DormImportDto> dtoList = (List<DormImportDto>) map.get("dormList");
					if (dtoList != null && dtoList.size() > 0) {
						Map<String, Object> resMap = dormRoomerManager.operateDormRoomerList(dtoList, userId, orgId);
						String              bugStr = resMap.get("bugStr").toString();
						if (StringUtils.isNotEmpty(bugStr)) {
							return AppResultUtil.createErrorDto("100", "导入员工入宿信息失败(操作数据):" + bugStr);
						}
						return AppResultUtil.createSuccesDto(dtoList);
					} else {
						return AppResultUtil.createErrorDto("100", "导入员工入宿信息数据为空");
					}
				} catch (Exception e) {
					log.error(e.getMessage());
					e.printStackTrace();
					return AppResultUtil.createErrorDto("100", "导入员工入宿信息失败:" + e.getMessage());
				}
			} else {
				return AppResultUtil.createErrorDto("100", "导入员工入宿信息失败：获取不到dormList");
			}
		}
	}

	/**
	 * 根据openId获取登录的员工入宿信息
	 *
	 * @param openId
	 * @return
	 */
	@Override
	public DormRoomerInfo getDormInfoByOpenId(String openId, String orgId) {
		return dormRoomerManager.getDormInfoByOpenId(openId, orgId);
	}

	/**
	 * 根据公寓、楼宇、楼层、房间号以及项目获取全部舍友（排除自己）
	 * (如果维护了id之后可直接根据房间号id获取对应的数据，现无法满足2018-09-26)
	 * 备注：避免不同楼宇楼层房间名称一样的情况，因此需要这些参数一起做校验
	 *
	 * @param orgId 项目
	 * @return
	 */
	@Override
	public List<DormRoomerInfoDto> getRoomerByOpenId(String openId, String orgId) {
		DormRoomerInfo          dormInfo = dormRoomerManager.getDormInfoByOpenId(openId, orgId);
		List<DormRoomerInfoDto> dtoList  = new ArrayList<DormRoomerInfoDto>();
		if (dormInfo != null) {
			List<DormRoomerInfo> dormList = dormRoomerManager.getRoomerByRoomInfo(dormInfo.getId(), orgId, dormInfo.getApartment()
					, dormInfo.getBuild(), dormInfo.getFloor(), dormInfo.getRoom());
			if (dormList != null && dormList.size() > 0) {
				for (int i = 0; i < dormList.size(); i++) {
					DormRoomerInfoDto dto  = new DormRoomerInfoDto();
					DormRoomerInfo    dorm = dormList.get(i);
					dto.setId(dorm.getId().toString());
					dto.setName(dorm.getUserName());
					dto.setWorkNo(dorm.getWorkNo());
					dtoList.add(dto);
				}
				return dtoList;
			}
		}
		return null;
	}
}
