package com.palmnest.service.dinner.service.impl;

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.DeptAddressDto;
import com.palmnest.application.dto.input.DeptDto;
import com.palmnest.model.dinner.Department;
import com.palmnest.model.property.VirtualArchitecture;
import com.palmnest.service.dinner.manager.DeptManager;
import com.palmnest.service.dinner.service.DeptService;
import com.palmnest.service.estateserve.VirtualArchManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Author hxt
 * @Description TODO
 * @Date 2019/1/3 17:45
 **/
@Slf4j
@Service("deptService")
public class DeptServiceImpl implements DeptService {


	@Autowired
	private DeptManager        deptManager;
	@Autowired
	private VirtualArchManager virtualArchManager;

	@Override
	public List<DeptAddressDto> getAllDept(String orgId, String virId, String name) {
		List<DeptAddressDto> dtoList  = new ArrayList<DeptAddressDto>();
		List<Department>     deptList = deptManager.getAllDept(orgId, virId, name);
		String               address  = "";
		if (StringUtil.isNotEmpty(virId) && virtualArchManager.exists(Integer.parseInt(virId))) {
			VirtualArchitecture virt = virtualArchManager.get(Integer.parseInt(virId));
			if (virt != null && StringUtil.isNotEmpty(virt.getAddress())) {
				address = virt.getAddress();
			}
		}
		if (deptList != null && deptList.size() > 0) {
			for (int i = 0; i < deptList.size(); i++) {
				Department     dept = deptList.get(i);
				DeptAddressDto dto  = new DeptAddressDto();
				dto.setId(Integer.parseInt(dept.getId().toString()));
				dto.setName(dept.getName());
				dto.setAddress(address);
				if (StringUtil.isNotEmpty(dept.getAddress())) {
					dto.setAddress(dept.getAddress());
				}
				dtoList.add(dto);
			}
		}
		return dtoList;
	}

	/**
	 * 批量保存部门
	 */
	@Transactional
	public String saveDeptList(String orgId, String virId, String userId, List<DeptDto> deptList) {
		String res = "";
		try {
			if (deptList != null && deptList.size() > 0) {
				for (int i = 0; i < deptList.size(); i++) {
					DeptDto    dto        = deptList.get(i);
					Department department = new Department();
					if (dto != null) {
						department.setCreator(Long.parseLong(userId));
						department.setCreateTime(new Date());
						department.setName(dto.getName());
						if (StringUtil.isNotEmpty(dto.getOrderNum())) {
							department.setOrderNum(Integer.parseInt(dto.getOrderNum()));
						}
						department.setOrgId(Integer.parseInt(orgId));
						department.setShortName(dto.getShortName());
						department.setVirId(Integer.parseInt(virId));
						department.setIsEnabled(1);
						deptManager.saveUpdateDept(department);
					}
				}
			}
			res = "SUCCESS";
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			res = e.getMessage();
		}
		return res;
	}

	@Override
	public AppResultDto importDept(String orgId, String virId, String userId,
								   InputStream ins) {
		Map<String, Object> map      = deptManager.importDept(ins, orgId, virId);
		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("deptList")) {
				List<DeptDto> deptList = (List<DeptDto>) map.get("deptList");
				if (deptList != null && deptList.size() > 0) {
					String res = saveDeptList(orgId, virId, userId, deptList);
					if ("SUCCESS".equals(res)) {
						return AppResultUtil.createSuccesDto();
					} else {
						log.error("保存部门失败:" + res);
						return AppResultUtil.createErrorDto("100", "保存部门失败");
					}
				}
				return AppResultUtil.createErrorDto("100", "导入的部门信息为空");
			} else {
				log.error("保存部门失败:deptList为空");
				return AppResultUtil.createErrorDto("100", "导入部门失败");
			}
		}
	}

	/**
	 * 部门名称是否重复了
	 *
	 * @param orgId
	 * @param virId
	 * @param name
	 * @param id
	 * @return
	 */
	@Override
	public int isRepeatByDept(String orgId, String virId, String name, String id) {
		List<DeptAddressDto> deptList = getAllDept(orgId, virId, name);
		//是否重复 0未重复1重复
		int isRepeat = 0;
		//修改部门时判断
		if (StringUtil.isNotEmpty(id) && deptManager.exists(Long.parseLong(id))) {
			Department dept = deptManager.get(Long.parseLong(id));
			//传过来的部门名称和本身的部门名称不一样的时候需要进行判断是否重复
			if (dept != null && !name.equals(dept.getName())) {
				if (deptList != null && deptList.size() > 0) {
					isRepeat = 1;
				}
			}
		} else {
			//新增部门时判断
			if (deptList != null && deptList.size() > 0) {
				isRepeat = 1;
			}
		}
		return isRepeat;
	}

}
