package com.uas.platform.b2b.search;

import com.alibaba.fastjson.JSONObject;
import com.uas.platform.b2b.core.util.SplitArray;
import com.uas.platform.b2b.dao.*;
import com.uas.platform.b2b.erp.model.B2BCustomer;
import com.uas.platform.b2b.erp.model.B2BVendor;
import com.uas.platform.b2b.erp.model.SaleTenderErp;
import com.uas.platform.b2b.erp.model.SaleTenderItemErp;
import com.uas.platform.b2b.model.*;
import com.uas.platform.core.persistence.criteria.PredicateFactory;
import com.uas.platform.core.persistence.criteria.PredicateUtils;
import com.uas.search.b2b.exception.SearchException;
import com.uas.search.b2b.model.PageParams;
import com.uas.search.b2b.model.SPage;
import com.uas.search.b2b.service.SearchService.Table_name;
import com.uas.search.b2b.util.SearchConstants;
import com.uas.sso.support.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 搜索实现
 *
 * @author sunyj
 * @since 2016年11月11日 上午10:43:25
 */
/**
 * @author hejq
 * @time 创建时间：2017年4月28日
 */
@Service
public class SearchServiceImpl implements com.uas.platform.b2b.search.SearchService {

	@Autowired
	private com.uas.search.b2b.service.SearchService searchService;

	@Autowired
	private PurchaseOrderAllDao purchaseOrderAllDao;

	@Autowired
	private PurchaseOrderTodoDao purchaseOrderTodoDao;

	@Autowired
	private PurchaseAcceptDao purchaseAcceptDao;

	@Autowired
	private PurchaseApBillDao purchaseApBillDao;

	@Autowired
	private PurchaseBadInDao purchaseBadInDao;

	@Autowired
	private PurchaseBadOutDao purchaseBadOutDao;

	@Autowired
	private PurchaseInquiryItemDao purchaseInquiryItemDao;

	@Autowired
    private PurchaseInquiryDetailDao inquiryDetailDao;

	@Autowired
	private PurchaseMRBDao purchaseMRBDao;

	@Autowired
	private PurchaseOrderChangeDao purchaseOrderChangeDao;

	@Autowired
	private PurchaseProofingItemDao purchaseProofingItemDao;

	@Autowired
	private PurchaseReturnDao purchaseReturnDao;

	@Autowired
	private MakeReturnDao makeReturnDao;

	@Autowired
	private PurchaseNoticeDao purchaseNoticeDao;

	@Autowired
	private SaleARCheckDao saleARCheckDao;

	@Autowired
	private PurchaseOrderDoneDao purchaseOrderDoneDao;

	@Autowired
	private PurchaseOrderEndDao purchaseOrderEndDao;

	@Autowired
	private PurchaseOrderWaitingDao purchaseOrderWaitingDao;

	@Autowired
	private PurchaseOrderReceivedDao purchaseOrderReceivedDao;

	@Autowired
	private PurchaseInquiryMouldInfoDao purchaseInquiryMouldInfoDao;

	@Autowired
	private PurchaseTenderDao purchaseTenderDao;

	@Autowired
	private SaleTenderErpDao saleTenderErpDao;

	@Autowired
	private VendorDao vendorDao;

	@Autowired
	private InvitationRecordDao invitationRecordDao;

	@Autowired
	private SaleTenderDao saleTenderDao;

	@Autowired
	private SaleTenderQuestionDao saleTenderQuestionDao;

	private ConcurrentHashMap<String, Field> sortFields = new ConcurrentHashMap<String, Field>();

	private Field getPropertyField(Class<?> targetCls, String properyName) {
		Field field = sortFields.get(targetCls.toString());
		if (null == field) {
			Field[] fields = targetCls.getDeclaredFields();
			for (Field f : fields) {
				if (f.getName().equals(properyName)) {
					field = f;
					field.setAccessible(true);
					sortFields.put(targetCls.toString(), f);
					break;
				}
			}
		}
		return field;
	}

	private <T> void sortByProperty(List<T> content, Class<T> targetCls, String properyName,
									final List<Long> propertyList) {
		final Field field = getPropertyField(targetCls, properyName);
		if (null != field) {
			Collections.sort(content, new Comparator<T>() {

				@Override
				public int compare(T param1, T param2) {
					try {
						return Integer.compare(propertyList.indexOf((long) field.get(param1)),
								propertyList.indexOf((long) field.get(param2)));
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					}
					return 0;
				}

			});
		}
	}

	/**
	 * 将id的SPage信息、数据合并
	 *
	 * @param idsPage
	 *            id的SPage信息
	 * @param content
	 *            数据
	 * @return 合并后的结果
	 */
	private <T> SPage<T> toSPage(SPage<Long> idsPage, List<T> content) {
		if (idsPage == null) {
			return null;
		}
		SPage<T> result = new SPage<>();
		result.setContent(content);
		result.setFirst(idsPage.isFirst());
		result.setLast(idsPage.isLast());
		result.setPage(idsPage.getPage());
		result.setSize(idsPage.getSize());
		result.setTotalElement(idsPage.getTotalElement());
		result.setTotalPage(idsPage.getTotalPage());
		return result;
	}

	/**
	 * 将id的Page信息、数据合并
	 *
	 * @param idsPage
	 *            id的Page信息
	 * @param content
	 *            数据
	 * @return 合并后的结果
	 */
	private <T> Page<T> toPage(SPage<Long> idsPage, List<T> content) {
		if (idsPage == null) {
			return null;
		}
		Page<T> result = new Page<>();
		result.setContent(content);
		result.setFirst(idsPage.isFirst());
		result.setLast(idsPage.isLast());
		result.setTotalElements(idsPage.getTotalElement());
		result.setNumber(idsPage.getPage());
		result.setSize(idsPage.getSize());
		return result;
	}

	/**
	 * 买家验收单
	 */
	@Override
	public SPage<PurchaseAccept> searchPurchaseAcceptIds(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$ACCEPT, pageParams);
		List<PurchaseAccept> content = purchaseAcceptDao.findAll(idsPage.getContent());
		sortByProperty(content, PurchaseAccept.class, "id", idsPage.getContent());
		return toSPage(idsPage, content);
	}

	/**
	 * 买家应付发票表
	 */
	@Override
	public SPage<PurchaseApBill> searchPurchaseApBillIds(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$APBILL, pageParams);
		List<PurchaseApBill> content = purchaseApBillDao.findAll(idsPage.getContent());
		sortByProperty(content, PurchaseApBill.class, "id", idsPage.getContent());
		return toSPage(idsPage, content);
	}

	/**
	 * 不良品入库单
	 *
	 * @param keyword
	 * @param pageParams
	 * @return
	 */
	@Override
	public SPage<PurchaseBadIn> searchPurchaseBadInIds(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$BADIN, pageParams);
		List<PurchaseBadIn> content = purchaseBadInDao.findAll(idsPage.getContent());
		sortByProperty(content, PurchaseBadIn.class, "id", idsPage.getContent());
		return toSPage(idsPage, content);
	}

	/**
	 * 不良品出库单
	 */
	@Override
	public SPage<PurchaseBadOut> searchPurchaseBadOutIds(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$BADOUT, pageParams);
		List<PurchaseBadOut> content = purchaseBadOutDao.findAll(idsPage.getContent());
		sortByProperty(content, PurchaseBadOut.class, "id", idsPage.getContent());
		return toSPage(idsPage, content);
	}

	/**
	 * 采购询价单
	 */
	@Override
	public SPage<PurchaseInquiryItem> searchPurchaseInquiryItemIds(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$INQUIRYITEMS, pageParams);
		final PredicateFactory<PurchaseInquiryItem> factory = new PredicateFactory<PurchaseInquiryItem>();
		factory.add(PredicateUtils.in("id", idsPage.getContent(), false));
		List<PurchaseInquiryItem> content = purchaseInquiryItemDao.findAll(new Specification<PurchaseInquiryItem>() {
			@Override
            public Predicate toPredicate(Root<PurchaseInquiryItem> root, CriteriaQuery<?> query,
                                         CriteriaBuilder builder) {
				query.where(factory.toPredicate(root, query, builder));
				return null;
			}
		});
		sortByProperty(content, PurchaseInquiryItem.class, "id", idsPage.getContent());
		return toSPage(idsPage, content);
	}

	/**
	 * 客户MRB
	 */
	@Override
	public SPage<PurchaseMRB> searchPurchaseMRBIds(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$MRB, pageParams);
		List<PurchaseMRB> content = purchaseMRBDao.findAll(idsPage.getContent());
		sortByProperty(content, PurchaseMRB.class, "id", idsPage.getContent());
		return toSPage(idsPage, content);
	}

	/**
	 * 采购变更单
	 */
	@Override
	public SPage<PurchaseOrderChange> searchPurchaseOrderChangeIds(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$CHANGES, pageParams);
		List<PurchaseOrderChange> content = purchaseOrderChangeDao.findAll(idsPage.getContent());
		sortByProperty(content, PurchaseOrderChange.class, "id", idsPage.getContent());
		return toSPage(idsPage, content);
	}

	/**
	 * 打样申请单
	 */
	@Override
	public SPage<PurchaseProofingItem> searchPurchaseProofingItemIds(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$PROOFINGITEMS, pageParams);
		List<PurchaseProofingItem> content = purchaseProofingItemDao.findAll(idsPage.getContent());
		sortByProperty(content, PurchaseProofingItem.class, "id", idsPage.getContent());
		return toSPage(idsPage, content);
	}

	/**
	 * 客户验退单
	 */
	@Override
	public SPage<PurchaseReturn> searchPurchaseReturnIds(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$RETURN, pageParams);
		List<PurchaseReturn> content = purchaseReturnDao.findAll(idsPage.getContent());
		sortByProperty(content, PurchaseReturn.class, "id", idsPage.getContent());
		return toSPage(idsPage, content);
	}

	/**
	 * 委外验退单
	 */
	@Override
	public SPage<MakeReturn> searchMakeReturnIds(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.MAKE$RETURN, pageParams);
		List<MakeReturn> content = makeReturnDao.findAll(idsPage.getContent());
		sortByProperty(content, MakeReturn.class, "id", idsPage.getContent());
		return toSPage(idsPage, content);
	}

	/**
	 * 平台发货提醒单
	 */
	@Override
	public SPage<PurchaseNotice> searchPurchaseNoticeIds(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$NOTICE, pageParams);
		List<PurchaseNotice> content = new ArrayList<PurchaseNotice>();
		if(idsPage.getContent().size()<=1000){
			content = purchaseNoticeDao.findAll(idsPage.getContent());
		}else{
			List<List<Long>> subAry = SplitArray.splitAry(idsPage.getContent(), SplitArray.QUERY_MAX_NUMBER);
			for (List<Long> obj : subAry) {
				List<PurchaseNotice> notices = purchaseNoticeDao.findAll(obj);
				content.addAll(notices);
			}
		}
		sortByProperty(content, PurchaseNotice.class, "id", idsPage.getContent());
		return toSPage(idsPage, content);
	}

	/**
	 * 应付对账单： sale$archeck
	 */
	@Override
	public SPage<SaleARCheck> searchSaleARCheckIds(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.SALE$ARCHECK, pageParams);
		List<SaleARCheck> content = saleARCheckDao.findAll(idsPage.getContent());
		sortByProperty(content, SaleARCheck.class, "id", idsPage.getContent());
		return toSPage(idsPage, content);
	}

	/**
	 * 模具询价单： purc$inquirymould
	 */
	@Override
	public SPage<PurchaseInquiryMouldInfo> searchPurcInquiryMouldIds(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$INQUIRYMOULD, pageParams);
		List<PurchaseInquiryMouldInfo> content = purchaseInquiryMouldInfoDao.findAll(idsPage.getContent());
		sortByProperty(content, PurchaseInquiryMouldInfo.class, "id", idsPage.getContent());
		return toSPage(idsPage, content);
	}

	@Override
	public PageParams convertPageParams(com.uas.platform.core.model.PageParams params, String searchFilter) {
		if (params == null) {
			return null;
		}
		PageParams pageParams = new PageParams();
		pageParams.setPage(params.getPage());
		pageParams.setSize(params.getCount());
		Map<String, Object> filters = new HashMap<>();

		if (!StringUtils.isEmpty(searchFilter)) {
			JSONObject jsonObject = JSONObject.parseObject(searchFilter);

			// TODO 确定类型后再删除（value应该是Long）
			// 只处理开始日期、截止日期两个过滤条件，其他的如状态等过滤条件需要转为数据库中相应字段的名称，需单独进行处理
			Object fromDate = jsonObject.get(SearchConstants.FROM_DATE_KEY);
			if (fromDate != null && !fromDate.equals("null")) {
				filters.put(SearchConstants.FROM_DATE_KEY, fromDate);
			}
			Object endDate = jsonObject.get(SearchConstants.END_DATE_KEY);
			if (endDate != null && !endDate.equals("null")) {
				filters.put(SearchConstants.END_DATE_KEY, endDate);
			}
		}
		pageParams.setFilters(filters);
		return pageParams;
	}

	/**
	 *  erp查看招标单
	 * @param keyword
	 * @param pageParams
	 * @return
	 */
	@Override
	public SPage<PurchaseTender> erpSearchPurcTenderIds(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$TENDER, pageParams);
		List<PurchaseTender> content = purchaseTenderDao.findAll(idsPage.getContent());
		Set<PurchaseTenderProd> prods = new HashSet<>();
		List<PurchaseTender> tenderList = new ArrayList<>();
		for (PurchaseTender tender : content) {
			tender.setPurchaseTenderProds(prods); // erp查看列表只返回主表信息
			// 已投标企业统计
			Integer saleTenders = saleTenderDao.findNumOfBIdEn(tender.getEnUU(), tender.getCode(), "待投标");
			tender.setBidEnNum(Short.valueOf(String.valueOf(saleTenders)));
			tenderList.add(tender);
		}
		sortByProperty(tenderList, PurchaseTender.class, "id", idsPage.getContent());
		return toSPage(idsPage, tenderList);
	}

	/**
	 *  erp查看投标单
	 * @param keyword
	 * @param pageParams
	 * @return
	 */
	@Override
	public SPage<SaleTenderErp> erpSearchSaleTenderIds(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.SALE$TENDER, pageParams);
		List<SaleTenderErp> content = saleTenderErpDao.findAll(idsPage.getContent());
		Set<SaleTenderItemErp> itemErps = new HashSet<>();
		List<SaleTenderErp> tenderList = new ArrayList<>();
		for (SaleTenderErp saleTenderErp : content) {
			saleTenderErp.setSaleTenderItems(itemErps); // erp查看列表只返回主表信息
			tenderList.add(saleTenderErp);
		}
		sortByProperty(tenderList, SaleTenderErp.class, "id", idsPage.getContent());
		return toSPage(idsPage, tenderList);
	}

	/**
	 * 供应商(ERP)
	 */
	@Override
	public Page<B2BVendor> searchVendorForErp(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$VENDORS, pageParams);
		Page<B2BVendor> vendorPage = new Page<>();
		List<Vendor> content = vendorDao.findAll(idsPage.getContent());
		sortByProperty(content, Vendor.class, "id", idsPage.getContent());
		List<B2BVendor> b2bVendors = new ArrayList<>();
		for (Vendor vendor : content) {
			B2BVendor b2BVendor = new B2BVendor(vendor);
			b2bVendors.add(b2BVendor);
		}
		vendorPage.setContent(b2bVendors);
		vendorPage.setTotalElements(idsPage.getTotalElement());
		vendorPage.setNumber(idsPage.getPage());
		vendorPage.setSize(idsPage.getSize());
		return vendorPage;
	}

	/**
	 * 供应商(客户)
	 */
	@Override
	public Page<B2BCustomer> searchCustomerForErp(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$VENDORS_CUST, pageParams);
		Page<B2BCustomer> customerPage = new Page<>();
		List<Vendor> content = vendorDao.findAll(idsPage.getContent());
		sortByProperty(content, Vendor.class, "id", idsPage.getContent());
		List<B2BCustomer> b2BCustomers = new ArrayList<>();
		for (Vendor vendor : content) {
			B2BCustomer b2BCustomer = new B2BCustomer(vendor);
			b2BCustomers.add(b2BCustomer);
		}
		customerPage.setContent(b2BCustomers);
		customerPage.setTotalElements(idsPage.getTotalElement());
		customerPage.setNumber(idsPage.getPage());
		customerPage.setSize(idsPage.getSize());
		return customerPage;
	}

	/**
	 * 供应商企业信息
	 * @param keyword
	 * @param pageParams
	 * @return
	 */
	@Override
	public SPage<Enterprise> searchVendorIds(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$VENDORS, pageParams);
		List<Vendor> content = vendorDao.findAll(idsPage.getContent());
		List<Enterprise> vendors = new ArrayList<>();
		for (Vendor vendor : content) {
			vendors.add(vendor.getVendorEnterprise());
		}
		sortByProperty(vendors, Enterprise.class, "uu", idsPage.getContent());
		return toSPage(idsPage, vendors);
	}

	@Override
	public SPage<InvitationRecord> getInvitationByKeyword(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.INVITATIONRECORDS, pageParams);
		List<InvitationRecord> content = invitationRecordDao.findAll(idsPage.getContent());
		sortByProperty(content, InvitationRecord.class, "id", idsPage.getContent());
		return toSPage(idsPage, content);
	}

	@Override
	public SPage<SaleTenderQuestion> searchSaleTenderQuestion(String keyword, PageParams pageParams) {
		SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.SALE$TENDERQUESTION, pageParams);
		List<SaleTenderQuestion> content = saleTenderQuestionDao.findAll(idsPage.getContent());
		sortByProperty(content, SaleTenderQuestion.class, "id", idsPage.getContent());
		return toSPage(idsPage, content);
	}

	/**
	 * app查找客户采购询价单
	 *
	 * @param keyword    关键词
	 * @param pageParams 分页参数
	 * @return
	 */
	@Override
	public Page<PurchaseInquiryItem> searchPurchaseInquiryItemIdsForApp(String keyword, PageParams pageParams) {
		try {
			SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$INQUIRYITEMS, pageParams);
			List<PurchaseInquiryItem> content = purchaseInquiryItemDao.findAll(idsPage.getContent());
			sortByProperty(content, PurchaseInquiryItem.class, "id", idsPage.getContent());
			return toPage(idsPage, content);
		} catch (SearchException e) {
			return new Page<PurchaseInquiryItem>(pageParams.getPage(), pageParams.getSize(), new ArrayList<PurchaseInquiryItem>(), 0);
		}
	}

	/**
	 * app查找采购订单(所有)
	 *
	 * @param keyword 关键词
	 * @param pageParams 分页参数
	 * @return
	 */
	@Override
	public Page<PurchaseOrderAll> searchPurchaseOrderIdsForApp(String keyword, PageParams pageParams) {
		try {
			SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$ORDERS, pageParams);
			List<PurchaseOrderAll> content = purchaseOrderAllDao.findAll(idsPage.getContent());
			sortByProperty(content, PurchaseOrderAll.class, "id", idsPage.getContent());
			return toPage(idsPage, content);
		} catch (SearchException e) {
			return new Page<PurchaseOrderAll>(pageParams.getPage(), pageParams.getSize(), new ArrayList<PurchaseOrderAll>(), 0);
		}
	}

	/**
	 * app查找采购订单（待回复）
	 *
	 * @param keyword    关键词
	 * @param pageParams 分页参数
	 * @return
	 */
	@Override
	public Page<PurchaseOrderTodo> searchPurchaseTodoOrderIdsForApp(String keyword, PageParams pageParams) {
		try {
			SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$ORDERS, pageParams);
			List<PurchaseOrderTodo> content = purchaseOrderTodoDao.findAll(idsPage.getContent());
			sortByProperty(content, PurchaseOrderTodo.class, "id", idsPage.getContent());
			return toPage(idsPage, content);
		} catch (SearchException e) {
			return new Page<PurchaseOrderTodo>(pageParams.getPage(), pageParams.getSize(), new ArrayList<PurchaseOrderTodo>(), 0);
		}
	}

	/**
	 * app查找采购订单（已回复）
	 *
	 * @param keyword    关键词
	 * @param pageParams 分页参数
	 * @return
	 */
	@Override
	public Page<PurchaseOrderDone> searchPurchaseDoneOrderIdsForApp(String keyword, PageParams pageParams) {
		try {
			SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$ORDERS, pageParams);
			List<PurchaseOrderDone> content = purchaseOrderDoneDao.findAll(idsPage.getContent());
			sortByProperty(content, PurchaseOrderDone.class, "id", idsPage.getContent());
			return toPage(idsPage, content);
		} catch (SearchException e) {
			return new Page<PurchaseOrderDone>(pageParams.getPage(), pageParams.getSize(), new ArrayList<PurchaseOrderDone>(), 0);
		}
	}

	/**
	 * app查找采购订单（待发货）
	 *
	 * @param keyword    关键词
	 * @param pageParams 分页参数
	 * @return
	 */
	@Override
	public Page<PurchaseOrderWaiting> searchPurchaseWaitingOrderIdsForApp(String keyword, PageParams pageParams) {
		try {
			SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$ORDERS, pageParams);
			List<PurchaseOrderWaiting> content = purchaseOrderWaitingDao.findAll(idsPage.getContent());
			sortByProperty(content, PurchaseOrderWaiting.class, "id", idsPage.getContent());
			return toPage(idsPage, content);
		} catch (SearchException e) {
			return new Page<PurchaseOrderWaiting>(pageParams.getPage(), pageParams.getSize(), new ArrayList<PurchaseOrderWaiting>(), 0);
		}
	}

	/**
	 * app查找采购订单（已收货）
	 *
	 * @param keyword    关键词
	 * @param pageParams 分页参数
	 * @return
	 */
	@Override
	public Page<PurchaseOrderReceived> searchPurchaseReceivedOrderIdsForApp(String keyword, PageParams pageParams) {
		try {
			SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$ORDERS, pageParams);
			List<PurchaseOrderReceived> content = purchaseOrderReceivedDao.findAll(idsPage.getContent());
			sortByProperty(content, PurchaseOrderReceived.class, "id", idsPage.getContent());
			return toPage(idsPage, content);
		} catch (SearchException e) {
			return new Page<PurchaseOrderReceived>(pageParams.getPage(), pageParams.getSize(), new ArrayList<PurchaseOrderReceived>(), 0);
		}
	}

	/**
	 * app查找采购订单（已结案）
	 *
	 * @param keyword    关键词
	 * @param pageParams 分页参数
	 * @return
	 */
	@Override
	public Page<PurchaseOrderEnd> searchPurchaseEndOrderIdsForApp(String keyword, PageParams pageParams) {
		try {
			SPage<Long> idsPage = searchService.searchIds(keyword, Table_name.PURC$ORDERS, pageParams);
			List<PurchaseOrderEnd> content = purchaseOrderEndDao.findAll(idsPage.getContent());
			sortByProperty(content, PurchaseOrderEnd.class, "id", idsPage.getContent());
			return toPage(idsPage, content);
		} catch (SearchException e) {
			return new Page<PurchaseOrderEnd>(pageParams.getPage(), pageParams.getSize(), new ArrayList<PurchaseOrderEnd>(), 0);
		}
	}
}
