package cn.wise.sc.energy.power.plant.business.service.impl;

import cn.wise.sc.energy.power.plant.business.domain.CaseAnalysisInfo;
import cn.wise.sc.energy.power.plant.business.domain.PageQuery;
import cn.wise.sc.energy.power.plant.business.repository.CaseAnalysisInfoRepository;
import cn.wise.sc.energy.power.plant.business.service.ICaseAnalysisInfoService;
import cn.wise.sc.energy.power.plant.common.core.bean.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.Predicate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author neo.shu
 * @since 2020/9/12 15:26
 */
@Service
public class CaseAnalysisInfoServiceImpl implements ICaseAnalysisInfoService {
    @Autowired
    CaseAnalysisInfoRepository caseRepository;

    @Override
    public BaseResponse<Page<CaseAnalysisInfo>> page(PageQuery page, String plantId) {

        Sort sort = Sort.by(Sort.Direction.DESC, "caseId");
        if (page.getOrder() != null && !page.getOrder().trim().equals("")) {
            sort = Sort.by(Sort.Direction.DESC, page.getOrder());
        }
        Pageable pages = PageRequest.of(page.getPageNo(), page.getPageSize(), sort);
        Page<CaseAnalysisInfo> infoPage;

        Specification<CaseAnalysisInfo> specification = (root, query, cb) -> {
            List<Predicate> list = new ArrayList<Predicate>();
            //模糊搜索
            if (!StringUtils.isEmpty(page.getVaguewords())) {
                list.add(cb.like(root.get("title").as(String.class), "%" + page.getVaguewords() + "%"));
            }
            list.add(cb.equal(root.get("valid"), 0));
            //电厂id必传
            if (!StringUtils.isEmpty(plantId)) {
                list.add(cb.equal(root.get("plantId"), plantId));
            }
            //匹配关键字
            if (page.getKeywords().size() > 0) {
                //包含元素
               /* for (String item : page.getKeywords()) {
                    Expression expression = query.
                    list.add(cb.isTrue());
                }*/

            }

            //时间选择
            Predicate timePredicate = cb.conjunction();
            timePredicate.in();
            if (page.getStartTime() != null && !page.getStartTime().trim().equals("")) {
                list.add(cb.greaterThanOrEqualTo(root.get("createTime").as(LocalDateTime.class), LocalDateTime.parse(page.getStartTime())));
            }
            //结束日期
            if (page.getEndTime() != null && !page.getEndTime().trim().equals("")) {
                list.add(cb.lessThanOrEqualTo(root.get("createTime").as(LocalDateTime.class), LocalDateTime.parse(page.getEndTime())));
            }

            return cb.and(list.toArray(new Predicate[list.size()]));
        };
        infoPage = caseRepository.findAll(specification, pages);
        return BaseResponse.okData(infoPage);
    }

}
