package abacus.springboot.example.esb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import abacus.springboot.example.api.view.ApiBillHg;
import abacus.springboot.example.api.view.ApiBillHgItem;
import abacus.springboot.example.api.view.ApiBillHgPayment;
import abacus.springboot.example.api.view.ApiJcBill;
import abacus.springboot.example.api.view.ApiJcBillHgSearchItem;
import abacus.springboot.example.api.view.ApiJcBillItem;
import abacus.springboot.example.api.view.ApiJcBillSearch;
import abacus.springboot.example.api.view.ApiReqJcBill;
import abacus.springboot.example.api.view.ApiReqJcBillGoodsPhoto;
import abacus.springboot.example.api.view.ApiReqJcBillItem;
import abacus.springboot.example.api.view.JcBillCount;
import abacus.springboot.example.api.view.JcCodeFlow;
import abacus.springboot.example.api.view.JcProduct;
import abacus.springboot.example.api.view.JcProductBillSouce;
import abacus.springboot.example.dao.JcBill;
import abacus.springboot.example.dao.JcBillHg;
import abacus.springboot.example.dao.JcBillHgPay;
import abacus.springboot.example.dao.JcBillInItem;
import abacus.springboot.example.dao.JcBillPhoto;
import abacus.springboot.example.dao.JcBillSourceItem;
import abacus.springboot.example.dao.JcSourceItem;
import abacus.springboot.example.pi.ApiJcBillAccessIF;
import abacus.springboot.example.pi.JcBillAccessIF;
import abacus.springboot.example.repository.JcBillHgPayRepository;
import abacus.springboot.example.repository.JcBillHgRepository;
import abacus.springboot.example.repository.JcBillInItemRepository;
import abacus.springboot.example.repository.JcBillPhotoRepository;
import abacus.springboot.example.repository.JcBillRepository;
import abacus.springboot.example.repository.JcBillSourceItemRepository;
import abacus.springboot.example.repository.JcSourceItemRepository;
import abacus.springboot.example.wsi.ApiJcBillServiceIF;

@Service
public class ApiJcBillService implements ApiJcBillServiceIF {

	private static final Logger LOG = LoggerFactory.getLogger(ApiJcBillService.class);
	
	@Autowired
	private ApiJcBillAccessIF apiJcBillAccess;
	
	@Autowired
	private JcBillAccessIF jcBillAccess;
	
	@Autowired
	private JcBillRepository jcBillRepository;
	
	@Autowired
	private JcBillSourceItemRepository jcBillSourceItemRepository;
	
	@Autowired
	private JcBillPhotoRepository jcBillPhotoRepository;
	
	@Autowired
	private JcBillHgRepository jcBillHgRepository;
	
	@Autowired
	private JcBillHgPayRepository jcBillHgPayRepository;
	
	@Autowired
	private JcSourceItemRepository jcSourceItemRepository;
	
	@Autowired
	private JcBillInItemRepository jcBillInItemRepository;
	
	@Override
	public Page<ApiJcBillSearch> queryApiJcBillSearch(String keyword, String employee, String status, Pageable pageable)
			throws RuntimeException {
		try {
			return this.apiJcBillAccess.queryApiJcBillSearch(keyword, employee, status, pageable);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public ApiJcBill queryApiJcBillById(String id) throws RuntimeException {
		try {
			return this.apiJcBillAccess.queryApiJcBillById(id);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public List<ApiJcBillItem> queryApiJcBillItemByBIllId(String billId) throws RuntimeException {
		try {
			return this.apiJcBillAccess.queryApiJcBillItemByBIllId(billId);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public Map<String, List<JcBillPhoto>> queryJcBillPhoto(String srcid) throws RuntimeException {
		try {
			List<JcBillPhoto> billPhotos = jcBillAccess.queryJcBillPhoto(srcid, null, null);
			
			if(billPhotos == null) return new HashMap<String, List<JcBillPhoto>>();
			
			Map<String, List<JcBillPhoto>> map = billPhotos.stream()
					.collect(Collectors.groupingBy(e -> {
						return e.getTypephoto() + "#" + e.getSrcitemid();
					}, 
					Collectors.collectingAndThen(Collectors.toList(), value -> value)
				));
			
			return map;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}
	
	@Override
	public Map<String, List<JcBillPhoto>> queryJcBillPhotoIn(List<String> srcids) throws RuntimeException {
		try {
			List<JcBillPhoto> billPhotos = jcBillAccess.queryJcBillPhotoIn(srcids);
			
			if(billPhotos == null) return new HashMap<String, List<JcBillPhoto>>();
			
			Map<String, List<JcBillPhoto>> map = billPhotos.stream()
					.collect(Collectors.groupingBy(e -> {
						return e.getTypephoto() + "#" + e.getSrcitemid();
					}, 
					Collectors.collectingAndThen(Collectors.toList(), value -> value)
				));
			
			return map;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public ApiReqJcBill saveApiReqJcBill(ApiReqJcBill reqJcBill, JcBill jcBill, List<JcBillSourceItem> createItems)
			throws RuntimeException {
		try {
			
			List<JcBillSourceItem> tempItems = jcBillSourceItemRepository.saveAll(createItems);
			
//			Map<String, JcBillSourceItem> map = tempItems.stream()
//					.collect(Collectors.groupingBy(e -> {
//						return String.valueOf(e.getIndexno()) + e.getGoodsid();
//					}, 
//					Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0))
//				));
			
			// 新增
			List<JcBillPhoto> createPhoto = new ArrayList<JcBillPhoto>();
			if(reqJcBill.getItems() != null && reqJcBill.getItems().size() >= 1) {
				JcBillPhoto fkPhoto = null;
				JcBillPhoto hpPhoto = null;
				for(ApiReqJcBillItem item : reqJcBill.getItems()) {
					
					// 付款图
					if(item.getPaymentPhoto() != null && item.getPaymentPhoto().size() >= 1) {
						for(String urlphoto : item.getPaymentPhoto()) {
							fkPhoto = new JcBillPhoto();
							fkPhoto.setSrcid(jcBill.getId());
							fkPhoto.setSrcitemid(String.valueOf(item.getIndexno()));
							fkPhoto.setTypephoto(JcBillPhoto.JC_FK);
							fkPhoto.setUrlphoto(urlphoto);
							
							createPhoto.add(fkPhoto);
						}
					}
					
					for(ApiReqJcBillGoodsPhoto photo : item.getGoodsPhotos()) {
						// 货品图
						if(item.getGoodsPhotos() != null && item.getGoodsPhotos().size() >= 1) {
							for(String urlphoto : photo.getGoodsPhotos()) {
								hpPhoto = new JcBillPhoto();
								hpPhoto.setSrcid(jcBill.getId());
								hpPhoto.setSrcitemid(String.valueOf(item.getIndexno()) + "#" + photo.getGoodsid());
								hpPhoto.setTypephoto(JcBillPhoto.JC_HP);
								hpPhoto.setUrlphoto(urlphoto);
								
								createPhoto.add(hpPhoto);
							}
						}
					}
				}
			}
			
			
			// 删除图片
			jcBillAccess.deleteJcBillPhoto(jcBill.getId(), null, JcBillPhoto.JC_FK);
			jcBillAccess.deleteJcBillPhoto(jcBill.getId(), null, JcBillPhoto.JC_HP);
			
			jcBillPhotoRepository.saveAll(createPhoto);
			
			jcBillRepository.save(jcBill);
			
			return reqJcBill;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}
	
	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public ApiReqJcBill updateApiReqJcBill(ApiReqJcBill reqJcBill, JcBill jcBill, List<JcBillSourceItem> createItems,
			List<JcBillSourceItem> updateItems) throws RuntimeException {
		try {
			
//			Map<String, JcBillSourceItem> map = tempItems.stream()
//					.collect(Collectors.groupingBy(e -> {
//						return String.valueOf(e.getIndexno()) + e.getGoodsid();
//					}, 
//					Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0))
//				));
			
			// 新增
			List<JcBillPhoto> createPhoto = new ArrayList<JcBillPhoto>();
			if(reqJcBill.getItems() != null && reqJcBill.getItems().size() >= 1) {
				JcBillPhoto fkPhoto = null;
				JcBillPhoto hpPhoto = null;
				for(ApiReqJcBillItem item : reqJcBill.getItems()) {
					
					// 付款图
					if(item.getPaymentPhoto() != null && item.getPaymentPhoto().size() >= 1) {
						for(String urlphoto : item.getPaymentPhoto()) {
							fkPhoto = new JcBillPhoto();
							fkPhoto.setSrcid(jcBill.getId());
							fkPhoto.setSrcitemid(String.valueOf(item.getIndexno()));
							fkPhoto.setTypephoto(JcBillPhoto.JC_FK);
							fkPhoto.setUrlphoto(urlphoto);
							
							createPhoto.add(fkPhoto);
						}
					}
					
					for(ApiReqJcBillGoodsPhoto photo : item.getGoodsPhotos()) {
						// 货品图
						if(item.getGoodsPhotos() != null && item.getGoodsPhotos().size() >= 1) {
							for(String urlphoto : photo.getGoodsPhotos()) {
								hpPhoto = new JcBillPhoto();
								hpPhoto.setSrcid(jcBill.getId());
								hpPhoto.setSrcitemid(String.valueOf(item.getIndexno()) + "#" + photo.getGoodsid());
								hpPhoto.setTypephoto(JcBillPhoto.JC_HP);
								hpPhoto.setUrlphoto(urlphoto);
								
								createPhoto.add(hpPhoto);
							}
						}
					}
				}
			}
			
			
			// 删除图片
			jcBillAccess.deleteJcBillPhoto(jcBill.getId(), null, JcBillPhoto.JC_FK);
			jcBillAccess.deleteJcBillPhoto(jcBill.getId(), null, JcBillPhoto.JC_HP);
			
			jcBillSourceItemRepository.deleteByBillid(jcBill.getId());
			
			jcBillRepository.deleteById(jcBill.getId());
			
			List<JcBillSourceItem> tempItems = jcBillSourceItemRepository.saveAll(createItems);
			
			jcBillPhotoRepository.saveAll(createPhoto);
			
			jcBillRepository.save(jcBill);
			
			return reqJcBill;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}
	
	/*@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public ApiReqJcBill updateApiReqJcBill(ApiReqJcBill reqJcBill, JcBill jcBill, List<JcBillSourceItem> createItems,
			List<JcBillSourceItem> updateItems) throws RuntimeException {
		try {
			
			List<JcBillSourceItem> tempItems = jcBillSourceItemRepository.saveAll(createItems);
			
//			Map<String, JcBillSourceItem> map = tempItems.stream()
//					.collect(Collectors.groupingBy(e -> {
//						return String.valueOf(e.getIndexno());
//					}, 
//					Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0))
//				));
			
			// 新增
			List<JcBillPhoto> createPhoto = new ArrayList<JcBillPhoto>();
			// 修改
			List<JcBillPhoto> updatePhoto = new ArrayList<JcBillPhoto>();
			
			if(reqJcBill.getItems() != null && reqJcBill.getItems().size() >= 1) {
				JcBillPhoto fkPhoto = null;
				JcBillPhoto hpPhoto = null;
				for(ApiReqJcBillItem item : reqJcBill.getItems()) {
					
					// 付款图
					if(item.getPaymentPhoto() != null && item.getPaymentPhoto().size() >= 1) {
						for(String urlphoto : item.getPaymentPhoto()) {
							fkPhoto = new JcBillPhoto();
							fkPhoto.setSrcid(jcBill.getId());
							fkPhoto.setSrcitemid(String.valueOf(item.getIndexno()));
							fkPhoto.setTypephoto(JcBillPhoto.JC_FK);
							fkPhoto.setUrlphoto(urlphoto);
							
							createPhoto.add(fkPhoto);
						}
					}
					
					for(ApiReqJcBillGoodsPhoto photo : item.getGoodsPhotos()) {
						// 货品图
						if(item.getGoodsPhotos() != null && item.getGoodsPhotos().size() >= 1) {
							for(String urlphoto : photo.getGoodsPhotos()) {
								hpPhoto = new JcBillPhoto();
								hpPhoto.setSrcid(jcBill.getId());
								hpPhoto.setSrcitemid(String.valueOf(item.getIndexno()) + "#" + photo.getGoodsid());
								hpPhoto.setTypephoto(JcBillPhoto.JC_HP);
								hpPhoto.setUrlphoto(urlphoto);
								
								createPhoto.add(hpPhoto);
							}
						}
					}
				}
			}
			
			
			// 删除图片
//			for(JcBillPhoto photo : createPhoto) {
//				jcBillAccess.deleteJcBillPhoto(photo.getSrcid(), photo.getSrcitemid(), photo.getTypephoto());
//			}
			jcBillAccess.deleteJcBillPhoto(jcBill.getId(), null, JcBillPhoto.JC_FK);
			jcBillAccess.deleteJcBillPhoto(jcBill.getId(), null, JcBillPhoto.JC_HP);
			
			jcBillPhotoRepository.saveAll(createPhoto);
			
			jcBillAccess.updateJcBill(jcBill);
			
			jcBillAccess.updateJcBillSourceItem(updateItems);
			
			List<JcBillSourceItem> itemsLogs = new ArrayList<JcBillSourceItem>();
			itemsLogs.addAll(tempItems);
			itemsLogs.addAll(updateItems);
			
			List<JcBillPhoto> photoLogs = new ArrayList<JcBillPhoto>();
			photoLogs.addAll(createPhoto);
			photoLogs.addAll(updatePhoto);
			
			操作日志
			OperationLog log = new OperationLog("api_jc_bill", jcBill.getId(), "更新", "", "", 
					JSONObject.toJSONString(jcBill) + JSONArray.toJSONString(itemsLogs) + JSONArray.toJSONString(photoLogs));
			log.setOperator(jcBill.getApplicant());
    		log.setOperatorName(jcBill.getApplicantname());
			BusinessESUtil.checkService(this.operationLogService).saveOperationLog(log);
			
			return reqJcBill;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}*/

	@Override
	public int countJcBillItemIndexno(String billId) throws RuntimeException {
		try {
			return jcBillAccess.countJcBillItemIndexno(billId);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public JcBill queryJcBillById(String billId) throws RuntimeException {
		try {
			Optional<JcBill> optional = jcBillRepository.findById(billId);
			
			return optional.isPresent() ? optional.get() : null;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public List<JcBillSourceItem> queryJcBillSourceItemByBillId(String billId) throws RuntimeException {
		try {
			List<JcBillSourceItem> list = jcBillSourceItemRepository.findByBillid(billId);
			
			return (list != null && list.size() >= 1) ? list : null;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}
	
	@Override
	public JcBillSourceItem queryJcBillSourceItembyId(Long id) throws RuntimeException {
		try {
			
			Optional<JcBillSourceItem> optional = jcBillSourceItemRepository.findById(id);
			
			return optional.isPresent() ? optional.get() : new JcBillSourceItem();
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void updateStatusJcBill(JcBill jcBill, int oldStatus) throws RuntimeException {
		try {
			jcBillRepository.save(jcBill);
			
//			jcBillSourceItemRepository.saveAll(sourceItems);
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public Page<ApiJcBillHgSearchItem> queryApiJcBillHgSearchItem(String keyword, String employee, String status, Pageable pageable)
			throws RuntimeException {
		try {
			return this.apiJcBillAccess.queryApiJcBillHgSearchItem(keyword, employee, status, pageable);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public Map<String, JcBillCount> queryJcBillCount(List<String> ids) throws RuntimeException {
		try {
			
			if(ids != null && ids.size() >= 1) {
				List<JcBillCount> counts = apiJcBillAccess.queryJcBillCount(ids);
				
				if(counts == null) return new HashMap<String, JcBillCount>();
				
				Map<String, JcBillCount> map = counts.stream()
						.collect(Collectors.groupingBy(e -> {
							return e.getId();
						}, 
						Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0))
					));
				
				return map;
			} else {
				return new HashMap<String, JcBillCount>();
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public ApiBillHg queryApiBillHgById(String hgId) throws RuntimeException {
		try {
			return this.apiJcBillAccess.queryApiBillHgById(hgId);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public List<ApiBillHgItem> queryApiBillHgItemByHgId(String hgId) throws RuntimeException {
		try {
			return this.apiJcBillAccess.queryApiBillHgItemByHgId(hgId);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public List<ApiBillHgPayment> queryApiBillHgPaymentByHgId(String hgId) throws RuntimeException {
		try {
			return this.apiJcBillAccess.queryApiBillHgPaymentByHgId(hgId);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public JcBillHg queryJcBillHg(String billHg) throws RuntimeException {
		try {
			Optional<JcBillHg> optional = jcBillHgRepository.findById(billHg);
			
			return optional.isPresent() ? optional.get() : null;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void updateJcBillHg(JcBillHg entity, List<JcBillHgPay> pays) throws RuntimeException {
		try {
			jcBillHgRepository.save(entity);
			
			jcBillHgPayRepository.saveAll(pays);
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public JcBillPhoto queryJcBillPhoto(String srcid, String srcitemid, String typephoto) throws RuntimeException {
		try {
			return jcBillPhotoRepository.findBySrcidAndSrcitemidAndTypephoto(srcid, srcitemid, typephoto);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}
	
	@Override
	public List<JcBillPhoto> queryJcBillPhoto(String srcid, String typephoto) throws RuntimeException {
		try {
			return jcBillAccess.queryJcBillPhoto(srcid, null, typephoto);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void saveAndDeletePhoto(List<JcBillPhoto> photos, JcBillHg billHg) throws RuntimeException {
		try {
			
			for(JcBillPhoto photo : photos) {
				jcBillPhotoRepository.deleteBySrcidAndSrcitemidAndTypephoto(photo.getSrcid(), photo.getSrcitemid(), photo.getTypephoto());
			}
			
			jcBillPhotoRepository.saveAll(photos);
			
			jcBillHgRepository.save(billHg);
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
		
	}
	
	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void saveAndDeletePhoto(List<JcBillPhoto> photos, String billId) throws RuntimeException {
		try {
			
			jcBillPhotoRepository.saveAll(photos);
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
		
	}

	@Override
	public String lastBillHgId() throws RuntimeException {
		try {
			return jcBillAccess.lastBillHgId();
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public List<JcCodeFlow> queryJcCodeFlow(List<String> codes) throws RuntimeException {
		try {
			List<JcCodeFlow> flows = apiJcBillAccess.queryJcCodeFlow(codes);
			
			if(flows == null) return null;
			
//			Map<String, JcCodeFlow> map = flows.stream()
//					.collect(Collectors.groupingBy(e -> {
//						return e.getCode();
//					}, 
//					Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0))
//				));
			
			return flows;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public List<JcSourceItem> queryJcSourceItemByBillId(String billId) throws RuntimeException {
		try {
			return jcBillAccess.queryJcSourceItemByBillId(billId);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void saveJcSourceItem(List<JcBillSourceItem> items, List<JcSourceItem> sourceItems, JcBill jcBill) throws RuntimeException {
		try {
			
			jcBillAccess.deleteJcSourceItemBySource(jcBill.getId());
			
			if(sourceItems != null && sourceItems.size() >= 1) jcSourceItemRepository.saveAll(sourceItems);
			
			jcBillSourceItemRepository.saveAll(items);
			
			boolean flag = true;
			for(JcBillSourceItem item : items) {
				if(item.getStatus() == 0 && item.getStatus() == -2) flag = false;
			}
			if(flag)jcBill.setStatus(JcBill.STATUS_ONE);
			
			jcBillRepository.save(jcBill);
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
		
	}

	@Override
	public JcBillPhoto queryJcBillPhotoById(String id) throws RuntimeException {
		try {
			Optional<JcBillPhoto> optional = jcBillPhotoRepository.findById(Long.parseLong(id));
			
			return optional.isPresent() ? optional.get() : null;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void deleteJcBillPhotoById(JcBillPhoto entity) throws RuntimeException {
		try {
			jcBillPhotoRepository.delete(entity);
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void deleteJcSourceItem(String billid, String itemid) throws RuntimeException {
		try {
			Optional<JcBillSourceItem> optional = jcBillSourceItemRepository.findById(Long.parseLong(itemid));
			
			if(optional.isPresent()) {
				JcBillSourceItem item = optional.get();
				
				jcBillAccess.deleteJcBillPhoto(billid, String.valueOf(item.getIndexno()), JcBillPhoto.JC_FK);
			
				jcBillAccess.deleteJcBillPhoto(billid, item.getIndexno() + "#" + item.getGoodsid(), JcBillPhoto.JC_HP);
				
				jcBillSourceItemRepository.delete(item);
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<ApiBillHgPayment> queryApiBillHgPaymentByHgId(List<String> hgIds) throws RuntimeException {
		try {
			return this.apiJcBillAccess.queryApiBillHgPaymentByHgId(hgIds);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public String getBaiduToken() throws RuntimeException {
		return jcBillAccess.getBaiduToken();
	}

	@Override
	public List<JcProduct> queryJcProduct(String product, String stockInId, String hgId) throws RuntimeException {
		try {
			return this.apiJcBillAccess.queryJcProduct(product, stockInId, hgId);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public Map<String, List<JcProductBillSouce>> queryJcProductBillSouce(List<String> billIds) throws RuntimeException {
		try {
			List<JcProductBillSouce> billPhotos = apiJcBillAccess.queryJcProductBillSouce(billIds);
			
			if(billPhotos == null) return new HashMap<String, List<JcProductBillSouce>>();
			
			Map<String, List<JcProductBillSouce>> map = billPhotos.stream()
					.collect(Collectors.groupingBy(e -> {
						return e.getBillid() + e.getAccount() + e.getProduct();
					}, 
					Collectors.collectingAndThen(Collectors.toList(), value -> value)
				));
			
			return map;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public List<JcProduct> queryJcProduct(String product, String stockInId) throws RuntimeException {
		try {
			return this.apiJcBillAccess.queryJcProduct(product, stockInId);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public void updateStatusByJcBill(JcBill jcBill, int status) throws RuntimeException {
		try {
			
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}
	
	@Override
	public List<JcBillHg> qeryJcBillHgs(List<String> billids) throws RuntimeException {
		try {
			return apiJcBillAccess.qeryJcBillHgs(billids);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public JcBillInItem queryJcBillInItemById(Long id) throws RuntimeException {
		try {
			
			Optional<JcBillInItem> optional = jcBillInItemRepository.findById(id);
			
			return optional.isPresent() ? optional.get() : new JcBillInItem();
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}
}
