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

import com.uas.platform.b2b.manage.dao.NoticeRecordDao;
import com.uas.platform.b2b.manage.dao.NoticeRecordInfoDao;
import com.uas.platform.b2b.manage.model.NoticeRecordInfo;
import com.uas.platform.b2b.manage.page.PageInfo;
import com.uas.platform.b2b.manage.page.criteria.PredicateUtils;
import com.uas.platform.b2b.manage.service.NoticeRecordService;
import com.uas.platform.b2b.manage.utils.SearchUtils;
import com.uas.platform.b2b.manage.core.util.StringUtils;
import com.uas.platform.b2b.manage.model.NoticeRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 * ERP提醒校验记录
 *
 * @author hejq
 * @date 2018-08-24 14:31
 */
@Service
public class NoticeRecordServiceImpl implements NoticeRecordService {

    @Autowired
    private NoticeRecordInfoDao recordInfoDao;

    @Autowired
    private NoticeRecordDao recordDao;

    /**
     * 通过分页信息及搜索条件查询发货提醒记录
     *
     * @param pageInfo 分页信息
     * @param keyword  关键字
     * @param method   搜索分类
     * @return Page<NoticeRecordInfo>
     */
    @Override
    public Page<NoticeRecordInfo> findRecordByPageInfo(final PageInfo pageInfo, final String keyword, final String method) {
        if (!StringUtils.isEmpty(keyword)) {
            pageInfo.expression(PredicateUtils.like("enName", keyword, true));
        }
        if (!StringUtils.isEmpty(method)) {
            if (SearchUtils.SEARCH_ERROR.equals(method)) {
                pageInfo.expression(PredicateUtils.lt("errorCount", 0, true));
            }
        }
        return recordInfoDao.findAll((Root<NoticeRecordInfo> root, CriteriaQuery<?> query, CriteriaBuilder builder)
                -> query.where(pageInfo.getPredicates(root, query, builder)).getRestriction(), pageInfo);
    }

    /**
     * 通过id查询详情
     *
     * @param id id
     * @return NoticeRecord
     */
    @Override
    public NoticeRecord findById(Long id) {
        return recordDao.findOne(id);
    }
}
