/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.warehouse.ledger.service;

import java.math.BigDecimal;
import java.util.*;

import com.jeeplus.modules.warehouse.ledger.dto.CountLedgerInfo;
import com.jeeplus.modules.warehouse.qrcode.entity.QrCode;
import com.jeeplus.modules.warehouse.qrcode.mapper.QrCodeMapper;
import com.jeeplus.modules.warehouse.shelves.utils.ShelvesUtil;
import com.jeeplus.modules.warehouse.storage.dto.CountStorageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.warehouse.ledger.entity.Ledger;
import com.jeeplus.modules.warehouse.ledger.mapper.LedgerMapper;
import com.jeeplus.modules.warehouse.ledger.entity.LedgerInfo;
import com.jeeplus.modules.warehouse.ledger.mapper.LedgerInfoMapper;

/**
 * 台账信息Service
 * @author huyi
 * @version 2023-02-13
 */
@Service
@Transactional(readOnly = true)
public class LedgerService extends CrudService<LedgerMapper, Ledger> {

	@Autowired
	private LedgerInfoMapper ledgerInfoMapper;
	@Autowired
	private QrCodeMapper qrCodeMapper;
	
	public Ledger get(String id) {
		Ledger ledger = super.get(id);
		ledger.setLedgerInfoList(ledgerInfoMapper.findList(new LedgerInfo(ledger)));
		return ledger;
	}

	public Ledger countLedger(String id) {
		Ledger ledger = super.get(id);
		List<CountLedgerInfo> countLedgerInfoList = ledgerInfoMapper.findCliList(new LedgerInfo(ledger));

		Map<String, List<CountLedgerInfo>> map = new HashMap<>();
		for (CountLedgerInfo countLedgerInfo : countLedgerInfoList) {
			String batchNum = countLedgerInfo.getBatchNum();
			List<CountLedgerInfo> list = map.get(batchNum);
			if (null == list) {
				list = new ArrayList<>();

				list.add(countLedgerInfo);
				map.put(countLedgerInfo.getBatchNum(), list);
			} else {
				list.add(countLedgerInfo);
			}
		}

		List<CountLedgerInfo> cliList = new ArrayList<>();
		for (Map.Entry<String, List<CountLedgerInfo>> entry : map.entrySet()) {
			List<CountLedgerInfo> list = entry.getValue();
			CountLedgerInfo cli = new CountLedgerInfo();
			Set<String> wNameSet = new HashSet<>();
			String shelvesNames = "";
			int num = 0;
			BigDecimal sum = new BigDecimal(0);
			for (int i = 0; i < list.size(); i ++) {
				CountLedgerInfo c = list.get(0);
				if (i == 0) {
					cli.setBatchNum(c.getBatchNum());
					cli.setModel(c.getModel());
					cli.setName(c.getName());
					cli.setType(c.getType());
				}
				if (i != list.size() - 1) {
					shelvesNames = shelvesNames + c.getShelves().getName() + ",";
				} else {
					shelvesNames = shelvesNames + c.getShelves().getName();
				}
				wNameSet.add(c.getShelves().getWarehouse().getName());
				num += c.getNum();
				sum = sum.add(c.getSum());
			}
			String wNames = ShelvesUtil.mergerWarehouseName(wNameSet);
			cli.setShelvesNames(shelvesNames);
			cli.setWarehouseNames(wNames);
			cli.setNum(num);
			cli.setSum(sum);
			cliList.add(cli);
		}

		ledger.setCountLedgerInfoList(cliList);
		return ledger;
	}
	
	public List<Ledger> findList(Ledger ledger) {
		return super.findList(ledger);
	}
	
	public Page<Ledger> findPage(Page<Ledger> page, Ledger ledger) {
		return super.findPage(page, ledger);
	}

	public Page<LedgerInfo> findInfoPage(Page<LedgerInfo> page, LedgerInfo ledgerInfo) {
		dataRuleFilter(ledgerInfo);
		ledgerInfo.setPage(page);
		page.setList(ledgerInfoMapper.findList(ledgerInfo));
		return page;
	}

	@Transactional(readOnly = false)
	public void save(Ledger ledger) {
		Ledger oldLeger = mapper.findByTypeAndModel(ledger);
		// 判断是否存在同名称、同类型、同型号的台账，如果不存在，做新增操作，如果存在，则做修改操作
		if (null == oldLeger || StringUtils.isBlank(oldLeger.getId())) {
			super.save(ledger);
		} else {
			String shelvesIds = ledger.getShelvesIds();
			int num = ledger.getNum();
			BigDecimal sum = ledger.getSum();

			Set<String> set = new HashSet<>();
			String [] sIdArr = shelvesIds.split(",");
			String [] oldSIdArr = oldLeger.getShelvesIds().split(",");
			for (String sId : sIdArr) {
				set.add(sId);
			}
			for (String oSId : oldSIdArr) {
				set.add(oSId);
			}
			List<String> list = new ArrayList<>();
			for (String sid : set) {
				list.add(sid);
			}
			String newSIds = "";
			for (int i = 0; i < list.size(); i ++) {
				if (i != list.size() - 1) {
					newSIds = newSIds + list.get(i) +",";
				} else {
					newSIds = newSIds + list.get(i);
				}
			}
			oldLeger.setShelvesIds(newSIds);
			oldLeger.setNum(oldLeger.getNum() + num);
			oldLeger.setSum(oldLeger.getSum().add(sum));
			oldLeger.preUpdate();
			mapper.update(oldLeger);
			ledger = oldLeger;
		}
		for (LedgerInfo ledgerInfo : ledger.getLedgerInfoList()){
			if (ledgerInfo.getId() == null){
				continue;
			}
			if (LedgerInfo.DEL_FLAG_NORMAL.equals(ledgerInfo.getDelFlag())){
				if (StringUtils.isBlank(ledgerInfo.getId())){
					QrCode qrCode = ledgerInfo.getQr();
					QrCode qc = qrCodeMapper.get(qrCode);
					qc.setState("2");
					qrCodeMapper.update(qc);
					ledgerInfo.setLedger(ledger);
					ledgerInfo.preInsert();
					ledgerInfoMapper.insert(ledgerInfo);
				}else{
					ledgerInfo.preUpdate();
					ledgerInfoMapper.update(ledgerInfo);
				}
			}else{
				ledgerInfoMapper.delete(ledgerInfo);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Ledger ledger) {
		super.delete(ledger);
		ledgerInfoMapper.delete(new LedgerInfo(ledger));
	}

	@Transactional(readOnly = false)
	public LedgerInfo findByQrOrCode(LedgerInfo ledgerInfo) {
		List<LedgerInfo> ledgerInfoList = ledgerInfoMapper.findList(ledgerInfo);
		if (null != ledgerInfoList && ledgerInfoList.size() > 0) {
			return ledgerInfoList.get(0);
		} else {
			return null;
		}
	}
	
}