package com.uas.platform.b2b.service.impl;

import com.uas.platform.b2b.dao.PagingReleaseCountDao;
import com.uas.platform.b2b.dao.PurchaseMRBDao;
import com.uas.platform.b2b.model.PurchaseMRB;
import com.uas.platform.b2b.model.SearchFilter;
import com.uas.platform.b2b.search.SearchService;
import com.uas.platform.b2b.service.PurchaseMRBService;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.core.model.PageInfo;
import com.uas.platform.core.model.Status;
import com.uas.platform.core.persistence.criteria.PredicateUtils;
import com.uas.search.b2b.model.PageParams;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PurchaseMRBServiceImpl implements PurchaseMRBService {

	@Autowired
	private PurchaseMRBDao purchaseMRBDao;

	@Autowired
	private SearchService searchService;

	@Autowired
	private PagingReleaseCountDao pagingReleaseCountDao;

	@Override
	public void save(List<PurchaseMRB> purchaseMRBs) {
		purchaseMRBDao.save(purchaseMRBs);
	}

	@Override
	public List<PurchaseMRB> findNotUpload() {
		return purchaseMRBDao.findPurchaseMRBByVendUUAndSendStatus(SystemSession.getUser().getEnterprise().getUu(),
				(short) Status.NOT_UPLOAD.value());
	}

	@Override
	public void onUploadSuccess(String[] idArray) {
		for (String id : idArray) {
			PurchaseMRB mrb = purchaseMRBDao.findOne(Long.parseLong(id));
			if (mrb != null) {
				mrb.setSendStatus((short) Status.DOWNLOADED.value());
				purchaseMRBDao.save(mrb);
			}
		}
	}

	@Override
	public Page<PurchaseMRB> findAllByPageInfo(final PageInfo pageInfo, final String keyword,
			final SearchFilter filter) {
		return purchaseMRBDao.findAll(new Specification<PurchaseMRB>() {

			@Override
            public Predicate toPredicate(Root<PurchaseMRB> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
				if (filter != null) {
					if (!CollectionUtils.isEmpty(filter.getDistribute())) {
                        pageInfo.expression(PredicateUtils.in("enUU", filter.getDistribute(), false));
                    }
                    if (filter.getFromDate() != null) {
                        pageInfo.expression(PredicateUtils.gte("date", new Date(filter.getFromDate()), false));
                    }
                    if (filter.getEndDate() != null) {
                        pageInfo.expression(PredicateUtils.lte("date", new Date(filter.getEndDate()), false));
                    }
				}
                if (StringUtils.hasText(keyword)) {
					List<Object> lists = new ArrayList<>();
					PageParams pageParams = new PageParams();
					ModelMap map = new ModelMap();
					map.put("pm_venduu", SystemSession.getUser().getEnterprise().getUu());
					pageParams.setFilters(map);
					for (PurchaseMRB list : searchService.searchPurchaseMRBIds(keyword, pageParams).getContent()) {
						lists.add(list.getId());
					}
					pageInfo.expression(PredicateUtils.in("id", lists, false));
				}

				return query.where(pageInfo.getPredicates(root, query, builder)).getRestriction();
			}
		}, pageInfo);
	}

	@Override
	public PurchaseMRB findById(Long id) {
		return purchaseMRBDao.findOne(id);
	}

}
