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

import cn.jiguang.common.utils.StringUtils;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.application.dto.ExtraServiceDto;
import com.palmnest.dao.washcar.ExtraServiceDao;
import com.palmnest.model.washcar.ExtraService;
import com.palmnest.service.impl.GenericManagerImpl;
import com.palmnest.service.washcar.manager.ExtraServiceManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 额外服务
 *
 * @author longmeizhen
 */
@Slf4j
@SuppressWarnings("unused")
@Service("extraServiceManager")
public class ExtraServiceManagerImpl extends GenericManagerImpl<ExtraService, Long> implements ExtraServiceManager {


	private ExtraServiceDao extraServiceDao;

	@Autowired
	public void setExtraServiceDao(ExtraServiceDao extraServiceDao) {
		this.extraServiceDao = extraServiceDao;
		this.dao = extraServiceDao;
	}

	@Override
	public ExtraService saveExtraService(ExtraServiceDto dto,
										 String userId) throws Exception {
		if (StringUtil.isNotEmpty(dto.getProSetId())) {
			if (StringUtils.isEmpty(dto.getId())) {
				ExtraService extraService = new ExtraService();
				extraService.setAmount(dto.getAmount());
				extraService.setContent(dto.getContent());
				extraService.setIsEnabled("1");
				extraService.setIsOther(dto.getIsOther());
				if (StringUtils.isNotEmpty(dto.getProSetId())) {
					extraService.setProSetId(Long.parseLong(dto.getProSetId()));
				}
				extraService.setServiceName(dto.getServiceName());
				extraService.setCreateTime(new Date());
				extraService.setCreator(userId);
				extraService = extraServiceDao.save(extraService);
				return extraService;
			} else {
				ExtraService exSer = extraServiceDao.get(Long.parseLong(dto.getId()));
				exSer.setAmount(dto.getAmount());
				exSer.setContent(dto.getContent());
				exSer.setServiceName(dto.getServiceName());
				exSer.setIsOther(dto.getIsOther());
				exSer.setModifier(userId);
				exSer.setModifyTime(new Date());
				extraServiceDao.updateObj(exSer);
				return exSer;
			}
		}
		return null;
	}

	@Override
	public ExtraService get(Long id) {
		return extraServiceDao.get(id);
	}

	@Override
	public List<ExtraService> getAll() {
		return extraServiceDao.getAll();
	}

	@Override
	public List<ExtraServiceDto> findList(Long setId) throws Exception {
		List<ExtraServiceDto> extraList = new ArrayList<ExtraServiceDto>();
		List<ExtraService>    exList    = extraServiceDao.findByProSetId(setId);
		if (exList != null && exList.size() > 0) {
			for (int i = 0; i < exList.size(); i++) {
				ExtraServiceDto dto   = new ExtraServiceDto();
				ExtraService    extra = exList.get(i);
				dto.setAmount(extra.getAmount());
				dto.setContent(extra.getContent());
				dto.setId(extra.getId().toString());
				dto.setProSetId(extra.getProSetId().toString());
				dto.setServiceName(extra.getServiceName());
				extraList.add(dto);
			}
		}
		return extraList;
	}

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

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

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


}
