package com.uas.platform.b2b.mobile.controller;

import com.uas.platform.b2b.model.PurchaseInquiryItem;
import com.uas.platform.b2b.model.PurchaseInquiryReply;
import com.uas.platform.b2b.model.SearchFilter;
import com.uas.platform.b2b.search.SearchService;
import com.uas.platform.b2b.service.PurchaseInquiryService;
import com.uas.platform.b2b.service.UserService;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.b2b.support.UsageBufferedLogger;
import com.uas.platform.core.logging.BufferedLoggerManager;
import com.uas.platform.core.model.Constant;
import com.uas.platform.core.model.PageInfo;
import com.uas.platform.core.model.Status;
import com.uas.platform.core.util.serializer.FlexJsonUtils;
import com.uas.platform.core.web.bind.RequestState;
import com.uas.search.b2b.model.MultiValue;
import com.uas.search.b2b.model.SPage;
import com.uas.search.b2b.model.Sort;
import com.uas.search.b2b.util.SearchConstants;
import com.uas.sso.support.Page;
import org.apache.axis.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * 移动端询价单相关操作
 * Created by dongbw
 * 18/01/16 20:02.
 */
@RestController
@RequestMapping("/mobile/sale/inquiry")
public class AppInquiryController {

    @Autowired
    private PurchaseInquiryService purchaseInquiryService;

    @Autowired
    private UserService userService;

    @Autowired
    private SearchService searchService;

    /**
     * 询价种类
     */
    private final String INQUIRYKIND = "publicInquiry";

    private final static UsageBufferedLogger logger = BufferedLoggerManager.getLogger(UsageBufferedLogger.class);

    /**
     * 作为卖家，收到的采购询价（全部）- 搜索
     *
     * @param page  页码
     * @param size 每页条数
     * @param keyword 关键词
     * @return
     */
    @RequestMapping(value = "/info/search", method = RequestMethod.GET)
    public Page<PurchaseInquiryItem> getReceivedPurchaseInquiries(int page, int size, String keyword) {
        // 关键词不为空，使用索引
        if (!StringUtils.isEmpty(keyword)) {
            com.uas.search.b2b.model.PageParams pageParams = new com.uas.search.b2b.model.PageParams(page, size, new HashMap<String, Object>());
            // 当前登录企业作为供应商
            pageParams.getFilters().put("id_venduu", SystemSession.getUser().getEnterprise().getUu());
            SearchFilter filter = userService.distribute();
            if (filter != null && filter.getDistribute() == null) {
                return null;
            }
            if (filter != null && !CollectionUtils.isEmpty(filter.getDistribute())) {
                List<Object> list = new ArrayList<>();
                list.addAll(filter.getDistribute());
                pageParams.getFilters().put("id_inid", new MultiValue(list, true));
            }
            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("id_id", false, Sort.Type.LONG, new Long(1)));
            pageParams.getFilters().put(SearchConstants.SORT_KEY, sortList);
            Page<PurchaseInquiryItem> purchaseInquiryItemPage = searchService.searchPurchaseInquiryItemIdsForApp(keyword, pageParams);
            logger.log("客户询价单", "app搜索查看收到的客户询价单列表（全部）");
            return purchaseInquiryItemPage;
        } else {
            PageInfo pageInfo = new PageInfo(page, size);
            pageInfo.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
            org.springframework.data.domain.Sort.Order statusOrder = new org.springframework.data.domain.Sort.Order(org.springframework.data.domain.Sort.Direction.ASC, "status");
            org.springframework.data.domain.Sort.Order endDateOrder = new org.springframework.data.domain.Sort.Order(org.springframework.data.domain.Sort.Direction.DESC, "date");
            org.springframework.data.domain.Sort.Order idOrder = new org.springframework.data.domain.Sort.Order(org.springframework.data.domain.Sort.Direction.DESC, "id");
            List<org.springframework.data.domain.Sort.Order> orders = new ArrayList<>();
            orders.add(statusOrder);
            orders.add(endDateOrder);
            orders.add(idOrder);
            pageInfo.setSort(new org.springframework.data.domain.Sort(orders));
            SearchFilter filter = new SearchFilter();
            SearchFilter distribute = userService.distribute();
            if (distribute != null && distribute.getDistribute() == null) {
                return null;
            }
            if (distribute != null && !CollectionUtils.isEmpty(distribute.getDistribute())) {
                filter.setDistribute(distribute.getDistribute());
            }
            return toPage(purchaseInquiryService.findAllByPageInfo(pageInfo, null, filter));
        }
    }

    /**
     * 作为卖家，收到的采购询价（待报价）- 搜索
     *
     * @param page  页码
     * @param size 每页条数
     * @param keyword 关键词
     * @return
     */
    @RequestMapping(value = "/info/search", params = RequestState.TODO, method = RequestMethod.GET)
    public Page<PurchaseInquiryItem> getTodoInquiries(int page, int size, String keyword) {
        if (!StringUtils.isEmpty(keyword)) {
            logger.log("客户询价单", "搜索查看收到的客户询价单（待报价）");
            com.uas.search.b2b.model.PageParams pageParams = new com.uas.search.b2b.model.PageParams(page, size, new HashMap<String, Object>());
            // 当前登录企业作为供应商
            pageParams.getFilters().put("id_venduu", SystemSession.getUser().getEnterprise().getUu());
            SearchFilter filter = userService.distribute();
            if (filter != null && filter.getDistribute() == null) {
                return null;
            }
            if (filter != null && !CollectionUtils.isEmpty(filter.getDistribute())) {
                List<Object> list = new ArrayList<>();
                list.addAll(filter.getDistribute());
                pageParams.getFilters().put("id_inid", new MultiValue(list, true));
            }
            pageParams.getFilters().put("id_status", Status.NOT_REPLY.value());
            pageParams.getFilters().put("id_overdue", Constant.NO);
            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("id_id", false, Sort.Type.LONG, new Long(1)));
            pageParams.getFilters().put(SearchConstants.SORT_KEY, sortList);
            return searchService.searchPurchaseInquiryItemIdsForApp(keyword, pageParams);
        } else {
            logger.log("客户询价单", "查看收到的客户询价单列表（待报价）");
            PageInfo pageInfo = new PageInfo(page, size);
            pageInfo.filter("status", Status.NOT_REPLY.value());
            pageInfo.filter("overdue", Constant.NO);
            pageInfo.filter("invalid", Constant.NO);
            pageInfo.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
            SearchFilter filter = new SearchFilter();
            SearchFilter distribute = userService.distribute();
            if (distribute != null && distribute.getDistribute() == null) {
                return null;
            }
            if (distribute != null && !CollectionUtils.isEmpty(distribute.getDistribute())) {
                filter.setDistribute(distribute.getDistribute());
            }
            return toPage(purchaseInquiryService.findTodoByPageInfo(pageInfo, null, filter));
        }
    }

    /**
     * 作为卖家，收到的采购询价（已过期）- 搜索
     *
     * @param page  页码
     * @param size 每页条数
     * @param keyword 关键词
     * @return
     */
    @RequestMapping(value = "/info/search", params = RequestState.END, method = RequestMethod.GET)
    public Page<PurchaseInquiryItem> getEndInquiries(int page, int size, String keyword) {
        if (!StringUtils.isEmpty(keyword)) {
            logger.log("客户询价单", "搜索查看收到的客户询价单（已过期）");
            com.uas.search.b2b.model.PageParams pageParams = new com.uas.search.b2b.model.PageParams(page, size, new HashMap<String, Object>());
            // 当前登录企业作为供应商
            pageParams.getFilters().put("id_venduu", SystemSession.getUser().getEnterprise().getUu());
            SearchFilter filter = userService.distribute();
            if (filter != null && filter.getDistribute() == null) {
                return null;
            }
            if (filter != null && !CollectionUtils.isEmpty(filter.getDistribute())) {
                List<Object> list = new ArrayList<>();
                list.addAll(filter.getDistribute());
                pageParams.getFilters().put("id_inid", new MultiValue(list, true));
            }
            pageParams.getFilters().put("id_status", Status.NOT_REPLY.value());
            pageParams.getFilters().put("id_overdue", Constant.YES);
            HashMap<String, Object> map = new HashMap<>();
            map.put("id_invalid", Constant.NO);
            pageParams.setNotEqualFilters(map);
            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("id_id", false, Sort.Type.LONG, new Long(1)));
            pageParams.getFilters().put(SearchConstants.SORT_KEY, sortList);
            return searchService.searchPurchaseInquiryItemIdsForApp(keyword, pageParams);
        } else {
            logger.log("客户询价单", "查看收到的客户询价单列表（已过期）");
            PageInfo pageInfo = new PageInfo(page, size);
            pageInfo.filter("overdue", Constant.YES);
            pageInfo.filter("invalid", Constant.NO);
            pageInfo.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
            SearchFilter filter = new SearchFilter();
            SearchFilter distribute = userService.distribute();
            if (distribute != null && distribute.getDistribute() == null) {
                return null;
            }
            if (distribute != null && !CollectionUtils.isEmpty(distribute.getDistribute())) {
                filter.setDistribute(distribute.getDistribute());
            }
            return toPage(purchaseInquiryService.findAllByPageInfo(pageInfo, null, filter));
        }
    }

    /**
     * 作为卖家，收到的采购询价（已报价）- 搜索
     *
     * @param page  页码
     * @param size 每页条数
     * @param keyword 关键词
     * @return
     */
    @RequestMapping(value = "/info/search", params = RequestState.DONE, method = RequestMethod.GET)
    public Page<PurchaseInquiryItem> getDoneInquiries(int page, int size, String keyword) {
        if (!StringUtils.isEmpty(keyword)) {
            logger.log("客户询价单", "搜索查看收到的客户询价单（已报价）");
            com.uas.search.b2b.model.PageParams pageParams = new com.uas.search.b2b.model.PageParams(page, size, new HashMap<String, Object>());
            // 当前登录企业作为供应商
            pageParams.getFilters().put("id_venduu", SystemSession.getUser().getEnterprise().getUu());
            SearchFilter filter = userService.distribute();
            if (filter != null && filter.getDistribute() == null) {
                return null;
            }
            if (filter != null && !CollectionUtils.isEmpty(filter.getDistribute())) {
                List<Object> list = new ArrayList<>();
                list.addAll(filter.getDistribute());
                pageParams.getFilters().put("id_inid", new MultiValue(list, true));
            }
            pageParams.getFilters().put("id_status", Status.REPLIED.value());
            pageParams.getFilters().put("id_decidestatus", Status.UNAUDIT.value());
            HashMap<String, Object> map = new HashMap<>();
            map.put("id_invalid", Constant.YES);
            pageParams.setNotEqualFilters(map);
            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("id_id", false, Sort.Type.LONG, new Long(1)));
            pageParams.getFilters().put(SearchConstants.SORT_KEY, sortList);
            return searchService.searchPurchaseInquiryItemIdsForApp(keyword, pageParams);
        } else {
            logger.log("客户询价单", "查看收到的客户询价单列表（已报价）");
            PageInfo pageInfo = new PageInfo(page, size);
//            pageInfo.filter("overdue", Constant.NO);
            pageInfo.filter("status", Status.REPLIED.value());
            pageInfo.filter("invalid", Constant.NO);
            pageInfo.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
            pageInfo.filter("decideStatus", Status.UNAUDIT.value());
            SearchFilter filter = new SearchFilter();
            SearchFilter distribute = userService.distribute();
            if (distribute != null && distribute.getDistribute() == null) {
                return null;
            }
            if (distribute != null && !CollectionUtils.isEmpty(distribute.getDistribute())) {
                filter.setDistribute(distribute.getDistribute());
            }
            return toPage(purchaseInquiryService.findAllByPageInfo(pageInfo, null, filter));
        }
    }

    /**
     * 作为卖家，收到的采购询价（已失效）- 搜索
     *
     * @param page  页码
     * @param size 每页条数
     * @param keyword 关键词
     * @return
     */
    @RequestMapping(value = "/info/search", params = RequestState.INVALID, method = RequestMethod.GET)
    public Page<PurchaseInquiryItem> getInvalidInquiries(int page, int size, String keyword) {
        if (!StringUtils.isEmpty(keyword)) {
            logger.log("客户询价单", "搜索查看收到的客户询价单（已失效）");
            com.uas.search.b2b.model.PageParams pageParams = new com.uas.search.b2b.model.PageParams(page, size, new HashMap<String, Object>());
            // 当前登录企业作为供应商
            pageParams.getFilters().put("id_venduu", SystemSession.getUser().getEnterprise().getUu());
            SearchFilter filter = userService.distribute();
            if (filter != null && filter.getDistribute() == null) {
                return null;
            }
            if (filter != null && !CollectionUtils.isEmpty(filter.getDistribute())) {
                List<Object> list = new ArrayList<>();
                list.addAll(filter.getDistribute());
                pageParams.getFilters().put("id_inid", new MultiValue(list, true));
            }
            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("id_id", false, Sort.Type.LONG, 1L));
            pageParams.getFilters().put(SearchConstants.SORT_KEY, sortList);
            pageParams.getFilters().put("id_overdue", Constant.YES); // 已失效
            return searchService.searchPurchaseInquiryItemIdsForApp(keyword, pageParams);
        } else {
            logger.log("客户询价单", "查看收到的客户询价单列表（已失效）");
            PageInfo pageInfo = new PageInfo(page, size);
            pageInfo.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
            SearchFilter filter = new SearchFilter();
            SearchFilter distribute = userService.distribute();
            if (distribute != null && distribute.getDistribute() == null) {
                return null;
            }
            if (distribute != null && !CollectionUtils.isEmpty(distribute.getDistribute())) {
                filter.setDistribute(distribute.getDistribute());
            }
            return toPage(purchaseInquiryService.findInvalidByPageInfo(pageInfo, keyword, filter));
        }
    }

    /**
     * 作为卖家，收到的采购询价（已采纳）- 搜索
     *
     * @param page  页码
     * @param size 每页条数
     * @param keyword 关键词
     * @return
     */
    @RequestMapping(value = "/info/search", params = RequestState.AGREED, method = RequestMethod.GET)
    public Page<PurchaseInquiryItem> getAgreedInquiries(int page, int size, String keyword) {
        if (!StringUtils.isEmpty(keyword)) {
            logger.log("客户询价单", "搜索查看收到的客户询价单（已采纳）");
            com.uas.search.b2b.model.PageParams pageParams = new com.uas.search.b2b.model.PageParams(page, size, new HashMap<String, Object>());
            // 当前登录企业作为供应商
            pageParams.getFilters().put("id_venduu", SystemSession.getUser().getEnterprise().getUu());
            SearchFilter filter = userService.distribute();
            if (filter != null && filter.getDistribute() == null) {
                return null;
            }
            if (filter != null && !CollectionUtils.isEmpty(filter.getDistribute())) {
                List<Object> list = new ArrayList<>();
                for (Object object : filter.getDistribute()) {
                    list.add(object);
                }
                pageParams.getFilters().put("id_inid", new MultiValue(list, true));
            }
            pageParams.getFilters().put("id_agreed", Constant.YES);
            pageParams.getFilters().put("id_status", Status.REPLIED.value());
            HashMap<String, Object> map = new HashMap<>();
            map.put("id_invalid", Constant.YES);
            pageParams.setNotEqualFilters(map);
            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("id_id", false, Sort.Type.LONG, new Long(1)));
            pageParams.getFilters().put(SearchConstants.SORT_KEY, sortList);
            return searchService.searchPurchaseInquiryItemIdsForApp(keyword, pageParams);
        } else {
            logger.log("客户询价单", "查看收到的客户询价单列表（已采纳）");
            PageInfo pageInfo = new PageInfo(page, size);
            pageInfo.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
            pageInfo.filter("agreed", Constant.YES);
            pageInfo.filter("status", Status.REPLIED.value());
            pageInfo.filter("invalid", Constant.NO);
            SearchFilter filter = new SearchFilter();
            SearchFilter distribute = userService.distribute();
            if (distribute != null && distribute.getDistribute() == null) {
                return null;
            }
            if (distribute != null && !CollectionUtils.isEmpty(distribute.getDistribute())) {
                filter.setDistribute(distribute.getDistribute());
            }
            return toPage(purchaseInquiryService.findAllByPageInfo(pageInfo, keyword, filter));
        }
    }

    /**
     * 作为卖家，收到的采购询价（未采纳）- 搜索
     *
     * @param page  页码
     * @param size 每页条数
     * @param keyword 关键词
     * @return
     */
    @RequestMapping(value = "/info/search", params = RequestState.REFUSED, method = RequestMethod.GET)
    public Page<PurchaseInquiryItem> getRefusedInquiries(int page, int size, String keyword) {
        if (!StringUtils.isEmpty(keyword)) {
            logger.log("客户询价单", "搜索查看收到的客户询价单（未采纳）");
            com.uas.search.b2b.model.PageParams pageParams = new com.uas.search.b2b.model.PageParams(page, size, new HashMap<String, Object>());
            // 当前登录企业作为供应商
            pageParams.getFilters().put("id_venduu", SystemSession.getUser().getEnterprise().getUu());
            SearchFilter filter = userService.distribute();
            if (filter != null && filter.getDistribute() == null) {
                return null;
            }
            if (filter != null && !CollectionUtils.isEmpty(filter.getDistribute())) {
                List<Object> list = new ArrayList<>();
                for (Object object : filter.getDistribute()) {
                    list.add(object);
                }
                pageParams.getFilters().put("id_inid", new MultiValue(list, true));
            }
            pageParams.getFilters().put("id_agreed", Constant.NO);
            pageParams.getFilters().put("id_status", Status.REPLIED.value());
            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("id_id", false, Sort.Type.LONG, new Long(1)));
            pageParams.getFilters().put(SearchConstants.SORT_KEY, sortList);
            return searchService.searchPurchaseInquiryItemIdsForApp(keyword, pageParams);
        } else {
            logger.log("客户询价单", "查看收到的客户询价单列表（未采纳）");
            PageInfo pageInfo = new PageInfo(page, size);
            pageInfo.filter("agreed", Constant.NO);
            pageInfo.filter("status", Status.REPLIED.value());
            pageInfo.filter("invalid", Constant.NO);
            pageInfo.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
            SearchFilter filter = new SearchFilter();
            SearchFilter distribute = userService.distribute();
            if (distribute != null && distribute.getDistribute() == null) {
                return null;
            }
            if (distribute != null && !CollectionUtils.isEmpty(distribute.getDistribute())) {
                filter.setDistribute(distribute.getDistribute());
            }
            return toPage(purchaseInquiryService.findAllByPageInfo(pageInfo, keyword, filter));
        }
    }

    /**
     * 已转报价的公共询价单--搜索
     *
     * @param page  页码
     * @param size 每页条数
     * @param keyword 关键词
     * @return
     */
    @RequestMapping(value = "/info/search", params = RequestState.TEAMS, method = RequestMethod.GET)
    public Page<PurchaseInquiryItem> getPublicInquiry(int page, int size, String keyword) {
        if (!StringUtils.isEmpty(keyword)) {
            logger.log("客户询价单", "搜索已转报价的公共询价单");
            com.uas.search.b2b.model.PageParams pageParams = new com.uas.search.b2b.model.PageParams(page, size, new HashMap<String, Object>());
            // 当前登录企业作为供应商
            pageParams.getFilters().put("id_venduu", SystemSession.getUser().getEnterprise().getUu());
            pageParams.getFilters().put("id_kind", INQUIRYKIND);
            SearchFilter filter = userService.distribute();
            if (filter != null && filter.getDistribute() == null) {
                return null;
            }
            if (filter != null && !CollectionUtils.isEmpty(filter.getDistribute())) {
                List<Object> list = new ArrayList<>();
                for (Object object : filter.getDistribute()) {
                    list.add(object);
                }
                pageParams.getFilters().put("id_inid", new MultiValue(list, true));
            }
            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("id_id", false, Sort.Type.LONG, new Long(1)));
            pageParams.getFilters().put(SearchConstants.SORT_KEY, sortList);
            return searchService.searchPurchaseInquiryItemIdsForApp(keyword, pageParams);
        } else {
            logger.log("客户询价单", "查看收到已报价的公共询价单");
            PageInfo pageInfo = new PageInfo(page, size);
            pageInfo.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
            pageInfo.filter("status", Status.REPLIED.value());
            pageInfo.filter("invalid", Constant.NO);
            pageInfo.filter("kind", INQUIRYKIND);
            SearchFilter filter = new SearchFilter();
            SearchFilter distribute = userService.distribute();
            if (distribute != null && distribute.getDistribute() == null) {
                return null;
            }
            if (distribute != null && !CollectionUtils.isEmpty(distribute.getDistribute())) {
                filter.setDistribute(distribute.getDistribute());
            }
            return toPage(purchaseInquiryService.findAllByPageInfo(pageInfo, null, filter));
        }
    }

    /**
     * 作为卖家，根据询价单明细ID查找询价单(含明细)
     *
     * @param itemId 询价单明细id
     * @return
     */
    @RequestMapping(value = "/{itemId}/info", method = RequestMethod.GET)
    public PurchaseInquiryItem getReceivedPurchaseOrderItemById(@PathVariable("itemId") Long itemId) {
        return purchaseInquiryService.getInquiryItemByItemId(itemId);
    }

    /**
     * 作为卖家，给询价单报价
     *
     * @param replies 询价回复实体json
     * @param inquiryItemId 询价单明细id
     * @param leadtime 交货周期（天）
     * @param minPackQty 最小包装辆
     * @param minOrderQty 最小起订量
     * @return
     */
    @RequestMapping(value = "/items/{inquiryItemId}/reply", method = RequestMethod.POST)
    public ResponseEntity<String> replyInquiryItem(@PathVariable("inquiryItemId") Long inquiryItemId, String replies, Long leadtime, Double minPackQty, Double minOrderQty) {
        PurchaseInquiryItem item = toItem(inquiryItemId, replies, leadtime, minOrderQty, minPackQty);
        item = purchaseInquiryService.reply(item);
        logger.log("客户询价单", "给单个客户询价明细填写价格并确认报价", item.replyDescription(), item.getInquiry().getCode(), inquiryItemId);
        return new ResponseEntity<String>(HttpStatus.OK);
    }

    /**
     * 转成询价单明细记录
     * @param replies 询价回复实体json
     * @param inquiryItemId 询价单明细id
     * @param leadtime 交货周期（天）
     * @param minPackQty 最小包装辆
     * @param minOrderQty 最小起订量
     * @return
     */
    private PurchaseInquiryItem toItem(Long inquiryItemId, String replies, Long leadtime, Double minOrderQty, Double minPackQty) {
        List<PurchaseInquiryReply> replyList = FlexJsonUtils.fromJsonArray(replies, PurchaseInquiryReply.class);
        PurchaseInquiryItem item = purchaseInquiryService.findItemByItemId(inquiryItemId);
        if (null != leadtime) {
            item.setLeadtime(leadtime);
        }
        if (null != minPackQty) {
            item.setMinPackQty(minPackQty);
        }
        if (null != minOrderQty) {
            item.setMinOrderQty(minOrderQty);
        }
        HashSet<PurchaseInquiryReply> replyHashSet = new HashSet<>(replyList);
        item.setReplies(replyHashSet);
        return item;
    }

    /**
     * page信息转换
     *
     * @param page
     *            直接从数据库获取的page数据
     * @return 合并后的结果
     */
    private <T> Page<T> toPage(org.springframework.data.domain.Page<T> page) {
        if (page.getContent() == null) {
            return null;
        }
        Page<T> result = new Page<>();
        result.setContent(page.getContent());
        result.setFirst(page.isFirst());
        result.setLast(page.isLast());
        result.setTotalElements(page.getTotalElements());
        result.setNumber(page.getNumber());
        result.setSize(page.getSize());
        return result;
    }

    /**
     * page信息转换
     *
     * @param page
     *            直接从数据库获取的page数据
     * @return 合并后的结果
    */
    private <T> Page<T> toPage(SPage<T> page) {
        if (page.getContent() == null) {
            return null;
        }
        Page<T> result = new Page<>();
        result.setContent(page.getContent());
        result.setFirst(page.isFirst());
        result.setLast(page.isLast());
        result.setTotalElements(page.getTotalElement());
        result.setNumber(page.getPage());
        result.setSize(page.getSize());
        return result;
    }
}
