package com.palmnest.service.estateserve.airconditioner.impl;

import com.palmnest.application.AppConstants;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.core.util.BeanUtils;
import com.palmnest.application.dto.OrgDto;
import com.palmnest.application.dto.input.ProjectConfigInputDto;
import com.palmnest.application.dto.output.ProjectConfigOutDto;
import com.palmnest.model.User;
import com.palmnest.model.airconditioner.ProjectConfig;
import com.palmnest.service.AppUserService;
import com.palmnest.service.estateserve.airconditioner.ProjectConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;


@Service("projectConfigService")
public class ProjectConfigServiceImpl implements ProjectConfigService {


	@Autowired
	private com.palmnest.service.estateserve.ProjectConfigManager projectConfigManager;
	@Autowired
	private AppUserService                                        appUserService;

	@Override
	public AppResultDto saveProjectConfig(ProjectConfigInputDto dto,
										  HttpServletRequest request) {


		try {

			User         user             = (User) request.getSession().getAttribute("UserCache");
			List<OrgDto> organizationList = appUserService.getUserOrgs(user.getId());
			String       organizationId   = "";
			if (organizationList.size() >= 1) {
				organizationId = organizationList.get(0).getId().toString();
			}

			ProjectConfig projectConfig = null;
			projectConfig = projectConfigManager.getProjectConfigByOrganizationId(Long.valueOf(organizationId));
			if (null == projectConfig) {
				projectConfig = new ProjectConfig();
				BeanUtils.copyProperties(projectConfig, dto);
				if (user != null) {
					projectConfig.setCreator(user.getFullName());
					projectConfig.setModifier(user.getFullName());
				}
				projectConfig.setCreateTime(new Date());
			} else {
				BeanUtils.copyProperties(projectConfig, dto);
			}
			projectConfig.setModifyTime(new Date());


			projectConfig.setOrganizationId(Long.decode(organizationId));


			ProjectConfig bill = projectConfigManager.save(projectConfig);
			
			/*JSONObject json=new JSONObject();
			json.put("billId", bill.getId().toString());
			json.put("beforeApplyTime", bill.getId().toString());
			json.put("beforeCancelTime", bill.getId().toString());
			json.put("satDataType", bill.getId().toString());*/


			return AppResultUtil.createSuccesDto(convert(bill));

		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public ProjectConfigOutDto convert(ProjectConfig projectConfig)
			throws Exception {
		DecimalFormat df4 = new DecimalFormat();
		df4.applyPattern("0.00");

		ProjectConfigOutDto dto = new ProjectConfigOutDto();
		if (null != projectConfig && null != projectConfig.getId()) {
			dto.setBillId(projectConfig.getId().toString());
			dto.setOrganizationId(projectConfig.getOrganizationId().toString());
			dto.setBeforeApplyTime(df4.format(projectConfig.getBeforeApplyTime().setScale(2)));
			dto.setBeforeCancelTime(df4.format(projectConfig.getBeforeCancelTime().setScale(2)));
			dto.setSatDataType(projectConfig.getSatDataType().toString());
			dto.setIsSelectRoom(projectConfig.getIsSelectRoom());
		} else {
			dto.setBillId("");
			dto.setOrganizationId("");
			dto.setBeforeApplyTime("");
			dto.setBeforeCancelTime("");
			dto.setSatDataType("");
			dto.setIsSelectRoom(0);
		}


		return dto;
	}

	@Override
	public AppResultDto getProjectConfig(HttpServletRequest request) {

		User         user             = (User) request.getSession().getAttribute("UserCache");
		List<OrgDto> organizationList = appUserService.getUserOrgs(user.getId());
		String       organizationId   = "1";
		if (organizationList.size() >= 1) {
			organizationId = organizationList.get(0).getId().toString();
		}
		try {
			return AppResultUtil.createSuccesDto(convert(projectConfigManager.getProjectConfigByOrganizationId(Long.valueOf(organizationId))));
		} catch (Exception e) {
			e.printStackTrace();
			return AppResultUtil.createErrorDto(AppConstants.REPAIR_BILL_ERROR, "获得规则失败");
		}
	}

}
