package com.palmnest.service.estateserve.impl;

import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.PropertiesKit;
import com.palmnest.application.dto.AppServiceMenuDto;
import com.palmnest.application.dto.output.enterprise.OpenedFunctionOutDto;
import com.palmnest.dao.estateserve.ServiceProjectAllocationDao;
import com.palmnest.model.Element;
import com.palmnest.model.estateserve.ServiceProjectAllocation;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.service.ElementGroupManager;
import com.palmnest.service.estateserve.AppUserManager;
import com.palmnest.service.estateserve.ServiceProjectAllocationManager;
import com.palmnest.service.impl.GenericManagerImpl;
import com.palmnest.service.property.PropertyManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service("serviceProjectAllocationManager")
public class ServiceProjectAllocationManagerImpl extends GenericManagerImpl<ServiceProjectAllocation, Integer> implements ServiceProjectAllocationManager {

	private Logger LOG = LoggerFactory.getLogger(ServiceProjectAllocationManagerImpl.class);

	private ServiceProjectAllocationDao serviceProjectAllocationDao;

	@Autowired
	private PropertyManager propertyManager;

	@Autowired
	private ElementGroupManager elementGroupManager;

	@Autowired
	private AppUserManager appUserManager;

	public static String IMAGE_SERVER = PropertiesKit.readPropertiesValue("image_server", "wechat.properties");

	@Autowired
	public void setServiceProjectAllocationDao(
			ServiceProjectAllocationDao serviceProjectAllocationDao) {
		this.serviceProjectAllocationDao = serviceProjectAllocationDao;
		this.dao = serviceProjectAllocationDao;
	}

	@Override
	public Page serviceProjectAllocationPage(Integer pageNo, Integer pageSize,
											 String whereSql, String orgIds) throws Exception {
		try {
			return serviceProjectAllocationDao.serviceProjectAllocationPage(pageNo, pageSize, whereSql, orgIds);
		} catch (Exception e) {
			LOG.error("service find serviceProjectAllocationPage error{}", e);
		}
		return null;
	}

	@Override
	public Map<String, Object> getCompanyObj(String orgIds) throws Exception {
		try {
			return serviceProjectAllocationDao.getCompanyObj(orgIds);
		} catch (Exception e) {
			LOG.error("service get getCompanyObj error{}", e);
		}
		return null;
	}

	@Override
	public void assignBatchOnOffService(String status, String serviceTypeId, String orderNo) {
		LOG.info("status:", status);
		LOG.info("serviceTypeId:", serviceTypeId);

		List<TbOrganization> list = propertyManager.queryTbOrganizationList();
		for (TbOrganization tbOrganization : list) {
			LOG.info("orgId and orgName:" + tbOrganization.getId() + ";" + tbOrganization.getName());
			ServiceProjectAllocation localtion = serviceProjectAllocationDao.getServiceProjectAllocation(Integer.parseInt(serviceTypeId), tbOrganization.getId(), "0");
			if (localtion != null) {
				localtion.setStatus(status);
				localtion.setIsEnabled("1");
				localtion.setModifyTime(new Date());
				localtion.setModifier("admin");
				localtion.setOrderNo(orderNo);
				serviceProjectAllocationDao.updateObj(localtion);
			} else if ("1".equals(status)) {
				localtion = new ServiceProjectAllocation();
				localtion.setServiceTypeId(Integer.parseInt(serviceTypeId));
				localtion.setProjectId(tbOrganization.getId());
				localtion.setRoleGroup("0,1,");
				localtion.setType("0");
				localtion.setStatus(status);
				localtion.setOrderNo(orderNo);
				localtion.setCreator("admin");
				localtion.setCreateTime(new Date());
				localtion.setIsEnabled("1");
				serviceProjectAllocationDao.saveObj(localtion);
			}
		}
	}

	@Override
	public List<ServiceProjectAllocation> getServiceProjectAllocationList(
			String serviceTypeId) {
		return serviceProjectAllocationDao.getServiceProjectAllocationList(serviceTypeId);
	}

	@Override
	public ServiceProjectAllocation getServiceProjectAllocation(
			Integer serviceTypeId, Integer organizationId) {
		return serviceProjectAllocationDao.getServiceProjectAllocation(serviceTypeId, organizationId, "0");
	}

	@Override
	public void assignBatchOnOffService(String status, String serviceTypeId,
										String orderNo, String organizationIds, String operateUser, String roleGroups) {
		String[] organizationIdArray = organizationIds.split(";");
		for (String organizationId : organizationIdArray) {
			ServiceProjectAllocation localtion = serviceProjectAllocationDao.getServiceProjectAllocation(Integer.parseInt(serviceTypeId), Integer.parseInt(organizationId), "0");
			if (localtion != null) {
				localtion.setStatus(status);
				localtion.setIsEnabled("1");
				localtion.setModifyTime(new Date());
				localtion.setModifier(operateUser);
				localtion.setOrderNo(orderNo);
				localtion.setRoleGroup(roleGroups);
				serviceProjectAllocationDao.updateObj(localtion);
			} else if ("1".equals(status)) {
				localtion = new ServiceProjectAllocation();
				localtion.setServiceTypeId(Integer.parseInt(serviceTypeId));
				localtion.setProjectId(Integer.parseInt(organizationId));
				localtion.setRoleGroup(roleGroups);
				localtion.setType("0");
				localtion.setStatus(status);
				localtion.setOrderNo(orderNo);
				localtion.setCreator(operateUser);
				localtion.setCreateTime(new Date());
				localtion.setIsEnabled("1");
				serviceProjectAllocationDao.saveObj(localtion);
			}

		}

	}

	@Override
	public List<Map<String, Object>> getServiceGroupList(String organizationId) {
		List<Element> list = elementGroupManager.getElementsByGroupName("serviceGroupType");//获取父级分类信息
		for (int i = 0; i < list.size(); i++) {

		}
		return null;
	}

	@Override
	public List<AppServiceMenuDto> getServiceProjectAllocationList(Integer organizationId, Integer type) throws Exception {
		List<AppServiceMenuDto> dtos = new ArrayList<AppServiceMenuDto>();
		List<Map<String, Object>> services = serviceProjectAllocationDao.getServiceProjectAllocationList(organizationId, type);
		for (Map<String, Object> service : services) {
			Integer id = (Integer) service.get("id");
			String serviceName = (String) service.get("serviceName");
			String serviceRemark = (String) service.get("serviceRemark");
			String serviceIcon = (String) service.get("serviceIcon");
			String jumpAddress = (String) service.get("jumpAddress");
			String linkUrlType = (String) service.get("linkUrlType");
			AppServiceMenuDto dto = new AppServiceMenuDto(id, serviceName, serviceRemark);
			dto.setActionUrl(jumpAddress);
			dto.setIconUrl(IMAGE_SERVER + serviceIcon);
			dto.setLinkUrlType(Integer.parseInt(linkUrlType));

			dtos.add(dto);
		}
		return dtos;
	}

	@Override
	public List<ServiceProjectAllocation> listAll() {
		return serviceProjectAllocationDao.listAll();
	}

	@Override
	public String getServiceProjectByServiceCode(String serviceCode) {
		return serviceProjectAllocationDao.getServiceProjectByServiceCode(serviceCode);
	}

	@Override
	public List<OpenedFunctionOutDto> getOpenedFunctionServiceList(int orgId) {

		// 返回表现层(或者试图view的实体集合),用于封装原始数据
		List<OpenedFunctionOutDto> dtos = new ArrayList<>();
		// 查询到的原始数据集合
		List<Map<String, Object>> list = serviceProjectAllocationDao.getOpenedFunctionServiceList(orgId);

		if (list != null && list.size() > 0) {
			for (Map<String, Object> service : list) {
				OpenedFunctionOutDto dto = new OpenedFunctionOutDto();
				dto.setId((Integer) service.get("id"));
				dto.setServiceId((Integer) service.get("serviceId"));
				dto.setName((String) service.get("name"));
				dtos.add(dto);
			}
		}
		return dtos;
	}

	@Override
	public List<OpenedFunctionOutDto> getOpenedFunctionServiceChildList(List<Integer> serviceIdList) {

		// 返回表现层(或者试图view的实体集合),用于封装原始数据
		List<OpenedFunctionOutDto> dtos = new ArrayList<>();
		// 查询到的原始数据集合
		List<Map<String, Object>> list = serviceProjectAllocationDao.getOpenedFunctionServiceChildList(serviceIdList);

		if (list != null && list.size() > 0) {
			for (Map<String, Object> service : list) {
				OpenedFunctionOutDto dto = new OpenedFunctionOutDto();
				dto.setId((Integer) service.get("id"));
				dto.setServiceId((Integer) service.get("serviceId"));
				dto.setName((String) service.get("name"));
				dto.setCode((String) service.get("code"));
				dtos.add(dto);
			}
		}
		return dtos;
	}

}
