package cn.com.countrygarden.bi.main.service.financial.Impl;

import cn.com.countrygarden.bi.main.model.financial.Subbalance;
import cn.com.countrygarden.bi.main.service.financial.SubbalanceService;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;

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

@Service
public class SubbalanceServiceImpl implements SubbalanceService {
	@Autowired
	private cn.com.countrygarden.bi.main.dao.financial.SubbalanceMapper SubbalanceMapper;

	@Override
	public void save(Subbalance model) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void save(List<Subbalance> models) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteById(Integer id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteByIds(String ids) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(Subbalance model) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Subbalance findById(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Subbalance findBy(String fieldName, Object value)
			throws TooManyResultsException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Subbalance> findByIds(String ids) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Subbalance> findByCondition(Condition condition) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Subbalance> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void insertOrUpdate(List<Subbalance> sub) {
		// TODO Auto-generated method stub
		this.SubbalanceMapper.insertOrUpdate(sub);
	}

	@Override
	public void insertSubbalance(Subbalance sb) {
		this.SubbalanceMapper.insertSubbalance(sb);
	}

	@Override
	public void codoSubbanlance() {
		String assValue;
		int index;
		List<String> codoList = SubbalanceMapper.selectCodo();
		//System.out.println(codoList.size());
		for (String codo : codoList) {
			codo = codo + "-0001";
			//System.out.println("codo : " +codo);
			List<Subbalance> subbalanceList = SubbalanceMapper.selectSubblanceList(codo);
			//System.out.println(subbalanceList.size());
			//该账簿一对多项目时，对assValue字段进行截取分类，保存进assValueList中
			List<String> assValueList = new ArrayList<>();
			for (Subbalance subbalance : subbalanceList) {
				assValue = subbalance.getAssValue();
				index = assValue.indexOf("【P");
				if(index != -1){
					assValue = assValue.substring(index);
					assValue = assValue.substring(assValue.indexOf("/") + 1, assValue.indexOf("】"));
//					System.out.println(assValue);
				}else {
					break;
				}

				if (!assValueList.contains(assValue)) {
//					System.out.println("assvalue : "+assValue);
					assValueList.add(assValue);
				}
			}
			for (String assvalue : assValueList) {
				SubbalanceMapper.insertTempComm(assvalue,codo);
				//System.out.println("插入数据" + assvalue + "成功");
			}
		}

	}

	@Override
	public void deleteByMonth(String year, String month) {
		SubbalanceMapper.deleteSubbalance(year,month);
	}

}
