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

import cn.jiguang.common.utils.StringUtils;
import com.palmnest.application.bean.Page;
import com.palmnest.application.dto.input.WashCarOrderDto;
import com.palmnest.dao.estateserve.BillOperationRecodesDao;
import com.palmnest.dao.washcar.ExtraServiceDao;
import com.palmnest.dao.washcar.OrderExtraServiceDao;
import com.palmnest.dao.washcar.WashCarOrderDao;
import com.palmnest.model.washcar.ExtraService;
import com.palmnest.model.washcar.OrderExtraService;
import com.palmnest.model.washcar.WashCarOrder;
import com.palmnest.service.impl.GenericManagerImpl;
import com.palmnest.service.washcar.manager.WashCarOrderManager;
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.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service("washCarOrderManager")
public class WashCarOrderManagerImpl extends GenericManagerImpl<WashCarOrder, Long> implements WashCarOrderManager {


	private WashCarOrderDao washCarOrderDao;

	@Autowired
	public void setWashCarOrderDao(WashCarOrderDao washCarOrderDao) {
		this.washCarOrderDao = washCarOrderDao;
		this.dao = washCarOrderDao;
	}

	@Autowired
	private OrderExtraServiceDao    orderExtraServiceDao;
	@Autowired
	private ExtraServiceDao         extraServiceDao;
	@Autowired
	private BillOperationRecodesDao billOperationRecodesDao;


	@Override
	public Page findSearch(WashCarOrderDto dto, int pageNo, int pageSize)
			throws Exception {
		Page         page = washCarOrderDao.findSearch(dto, pageNo, pageSize);
		List<Object> list = page.getItems();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> obj = (Map<String, Object>) list.get(i);
				String              id  = obj.get("id").toString();//订单id
				Map<String, Object> map = new HashMap<String, Object>();
				if (StringUtils.isNotEmpty(id)) {
					//获取该条订单的订单内容及金额
					map = getExtAmount(Long.parseLong(id));
					obj.put("orderContent", map.get("orderContent"));//订单内容
					obj.put("amount", map.get("amount"));//费用
				}
			}
		}
		return page;
	}

	@Override
	public WashCarOrder get(Long id) {
		return washCarOrderDao.get(id);
	}

	@Override
	public Map<String, Object> getExtAmount(Long id) throws Exception {
		Map<String, Object> map    = new HashMap<String, Object>();
		BigDecimal          amount = new BigDecimal("0");
		StringBuilder       sb     = new StringBuilder("");
		if (id != null && id != 0) {
			List<OrderExtraService> orderList = orderExtraServiceDao.getOrderExtraListByOrderId(id);
			if (orderList != null && orderList.size() > 0) {
				for (int j = 0; j < orderList.size(); j++) {
					OrderExtraService orderExt = orderList.get(j);
					if (orderExt != null) {
						if (extraServiceDao.exists(orderExt.getExtraId())) {
							ExtraService ext = extraServiceDao.get(orderExt.getExtraId());
							sb.append(ext.getServiceName() + ";");
							amount = amount.add(ext.getAmount());
						}
					}
				}
			}
		}
		map.put("amount", amount.setScale(2, BigDecimal.ROUND_HALF_UP));
		map.put("orderContent", sb.toString());
		return map;
	}

	@Transactional
	public Integer deleteOrder(Long id) throws Exception {
		//删除订单，首先需要删除对应的操作记录，然后删除对应的服务
		int count = 0;
		try {
			billOperationRecodesDao.deleteRecode(id, 3);//删除洗车项目的订单
			orderExtraServiceDao.deleteOrderExtByOrderId(id);
			washCarOrderDao.remove(id);
			count = 1;
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			count = 0;
		}
		return count;
	}

	@Override
	public WashCarOrder saveWashCarOrder(WashCarOrder washCarOrder) throws Exception {
		return washCarOrderDao.save(washCarOrder);
	}

	@Override
	public List<Map<String, Object>> getWashCarService(String orgId, String syscode)
			throws Exception {
		List<Map<String, Object>> list = washCarOrderDao.getWashCaeService(orgId, syscode);
		return list;
	}

	@Override
	public List<WashCarOrder> getWashOrderByCarWasherId(String carWashId,
														String status, Integer type) throws Exception {
		return washCarOrderDao.getWashOrderByCarWasherId(carWashId, status, type);
	}

	@Override
	public WashCarOrder updateWashOrder(WashCarOrder washCarOrder)
			throws Exception {
		washCarOrderDao.updateObj(washCarOrder);
		return washCarOrder;
	}

	@Override
	public List<WashCarOrder> getWashListByDas(String userId, String status,
											   int type) throws Exception {
		return washCarOrderDao.getWashListByDas(userId, status, type);
	}

	@Override
	public String isPower(String syscode, String userId) throws Exception {
		return washCarOrderDao.isPower(syscode, userId);
	}

	@Override
	public boolean exists(Long id) {
		return washCarOrderDao.exists(id);
	}

	@Override
	public int getWashByUserId(String carWashId, String orgId) {
		return washCarOrderDao.getWashByUserId(carWashId, orgId);
	}

	@Override
	public int getWashByManager(String orgId) {
		return washCarOrderDao.getWashByManager(orgId);
	}
}
