package com.palmnest.service.estateserve.impl;

import cn.jiguang.common.utils.StringUtils;
import com.palmnest.application.core.util.DateHelper;
import com.palmnest.application.dto.output.BillOperationDto;
import com.palmnest.dao.ElementGroupDao;
import com.palmnest.dao.estateserve.BillOperationRecodesDao;
import com.palmnest.model.Element;
import com.palmnest.model.emig.BillOperationRecodes;
import com.palmnest.service.estateserve.BillOperationRecodesManager;
import com.palmnest.service.impl.GenericManagerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service("billOperationRecodesManager")
public class BillOperationRecodesManagerImpl extends GenericManagerImpl<BillOperationRecodes, Integer> implements BillOperationRecodesManager {

	private BillOperationRecodesDao billOperationRecodesDao;


	@Autowired
	public void setBillOperationRecodesDao(
			BillOperationRecodesDao billOperationRecodesDao) {
		this.billOperationRecodesDao = billOperationRecodesDao;
		this.dao = billOperationRecodesDao;
	}

	@Autowired
	private ElementGroupDao elementGroupDao;

	@Override
	public BillOperationRecodes getRecodeBySourId(Long sourId, Integer type,
												  Integer operationType) {
		return billOperationRecodesDao.getRecodeBySourId(sourId, type, operationType);
	}


	@Transactional
	public void saveRecode(Long sourId, Integer type, Integer opType, Long creator, String operation, String describe) {
		BillOperationRecodes bill = new BillOperationRecodes();
		bill.setSourId(sourId);
		bill.setType(type);
		bill.setOperationType(opType);
		bill.setCreator(creator);
		bill.setRepresent(describe);
		bill.setOperation(operation);
		bill.setCreateTime(new Date());
		try {
			billOperationRecodesDao.saveObj(bill);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Transactional
	public void saveRecodeSlip(Long sourId, Integer type, Integer opType, Long creator, String operation, String describe, String slipBill, String remark) {
		BillOperationRecodes bill = new BillOperationRecodes();
		bill.setSourId(sourId);
		bill.setType(type);
		bill.setOperationType(opType);
		bill.setCreator(creator);
		bill.setRepresent(describe);
		bill.setOperation(operation);
		bill.setCreateTime(new Date());
		bill.setSlipBill(slipBill);
		bill.setRemark(remark);
		try {
			billOperationRecodesDao.saveObj(bill);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	@Override
	public List<BillOperationDto> getRecodeBySourId(Long sourId,
													Integer type) {
		List<BillOperationDto>     dtoList = new ArrayList<BillOperationDto>();
		List<BillOperationRecodes> list    = billOperationRecodesDao.getRecodeBySourId(sourId, type);
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				BillOperationRecodes recode = list.get(i);
				BillOperationDto     dto    = new BillOperationDto();
				dto.setCreateTime(DateHelper.formatDate(recode.getCreateTime(), DateHelper.DATE_STRING));
				dto.setCreator(recode.getCreator());
				dto.setId(recode.getId());
				dto.setOperation(recode.getOperation());
				dto.setOperationType(recode.getOperationType());
				dto.setRemark(recode.getRemark());
				dto.setRepresent(recode.getRepresent());
				String slipBill = "";
				if (StringUtils.isNotEmpty(recode.getSlipBill())) {
					String[] str = recode.getSlipBill().split(";");
					if (str != null) {
						for (int k = 0; k < str.length; k++) {
							Element el = elementGroupDao.getElementId(Long.parseLong(str[k]));
							if (el != null) {
								slipBill = slipBill + el.getItemname() + ";";
							}
						}
					}
				}
				dto.setSlipBill(slipBill);
				dtoList.add(dto);
			}
		}
		return dtoList;
	}


	/**
	 * 根据来源单据id来源单据类型以及操作类型(多个类型)
	 *
	 * @param sourId
	 * @param type
	 * @param operationType
	 * @return
	 */
	@Override
	public BillOperationRecodes getRecodeBySourId(Long sourId, Integer type,
												  String operationType) {
		return billOperationRecodesDao.getRecodeBySourId(sourId, type, operationType);
	}

}
