package com.palmnest.service.washcar.service.impl;

import cn.jiguang.common.utils.StringUtils;
import com.palmnest.application.dto.ExtraServiceDto;
import com.palmnest.application.dto.output.ExtraServiceOutputDto;
import com.palmnest.model.washcar.ExtraService;
import com.palmnest.model.washcar.ProjectSet;
import com.palmnest.service.washcar.manager.ExtraServiceManager;
import com.palmnest.service.washcar.manager.ProjectSetManager;
import com.palmnest.service.washcar.service.ExtraServiceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 额外服务
 *
 * @author longmeizhen
 */
@Slf4j
@SuppressWarnings("unused")
@Service("extraServiceService")
public class ExtraServiceServiceImpl implements ExtraServiceService {


	@Autowired
	private ExtraServiceManager extraServiceManager;
	@Autowired
	private ProjectSetManager   projectSetManager;

	@Override
	public ExtraService saveExtraService(ExtraServiceDto dto, String userId)
			throws Exception {
		return extraServiceManager.saveExtraService(dto, userId);
	}

	@Override
	public ExtraServiceDto findByIdAndProId(String orgId, Long id, String proSetId, String userId) throws Exception {
		ExtraServiceDto dto = new ExtraServiceDto();
		if (id != null && id != 0) {
			if (extraServiceManager.exists(id)) {//如果存在，就是修改的界面
				ExtraService extra = extraServiceManager.get(id);
				dto.setAmount(extra.getAmount());
				dto.setContent(extra.getContent());
				dto.setId(extra.getId().toString());
				dto.setProSetId(extra.getProSetId().toString());
				dto.setServiceName(extra.getServiceName());
				dto.setIsOther(extra.getIsOther());
			} else {
				if (StringUtils.isEmpty(proSetId)) {//如果没有设置名额的时候，新增一条名额数据
					ProjectSet proSet = projectSetManager.saveQuota(orgId, Long.parseLong("0"), 0, userId);
					dto.setProSetId(proSet.getId().toString());
				}
			}
		} else {
			if (StringUtils.isEmpty(proSetId)) {//如果没有设置名额的时候，新增一条名额数据
				ProjectSet proSet = projectSetManager.saveQuota(orgId, Long.parseLong("0"), 0, userId);
				dto.setProSetId(proSet.getId().toString());
			}
		}
		return dto;
	}

	@Override
	public ExtraServiceOutputDto getExtraList(String projectId)
			throws Exception {
		ExtraServiceOutputDto dto = new ExtraServiceOutputDto();
		try {
			ProjectSet proSet = projectSetManager.fingByProjectId(projectId);
			if (proSet != null && proSet.getId() != null) {
				dto.setId(proSet.getId());
				dto.setProjectId(proSet.getProjectId());
				dto.setQuota(proSet.getQuota());
				dto.setTotal(proSet.getQuota());
				List<ExtraService> list = extraServiceManager.findByProSetIdandNotIsOther(proSet.getId());
				if (list != null && list.size() > 0) {
					ExtraService es = list.get(0);
					dto.setContent(es.getContent());
					dto.setExtraName(es.getServiceName());
					dto.setExtraPrice(es.getAmount().setScale(2, BigDecimal.ROUND_HALF_UP));
					dto.setExtraServiceId(es.getId().toString());
					dto.setIsOther(es.getIsOther());
				}
				List<ExtraServiceDto> extraList = extraServiceManager.findList(proSet.getId());
				dto.setExList(extraList);
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return dto;
	}

	@Override
	public Integer deleteExtra(Long id, String userId) throws Exception {
		return extraServiceManager.deleteExtra(id, userId);
	}

	@Override
	public List<ExtraService> findByProSetId(Long proSetId) throws Exception {
		return extraServiceManager.findByProSetId(proSetId);
	}
}
