package com.mosty.hczx.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.dto.hczx.TbHcDtgzDto;
import com.mosty.base.model.entity.hczx.TbHcDtgz;
import com.mosty.hczx.mapper.TbHcDtgzMapper;
import com.mosty.hczx.service.TbHcDtgzService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class TbHcDtgzServiceImpl extends ServiceImpl<TbHcDtgzMapper, TbHcDtgz> implements TbHcDtgzService {


    @Override
    public List<TbHcDtgz> getList(TbHcDtgzDto dtgz) {

        LambdaQueryWrapper<TbHcDtgz> eq = new LambdaQueryWrapper<TbHcDtgz>();
        eq.eq(StringUtils.isNotBlank(dtgz.getGlid()), TbHcDtgz::getGlid, dtgz.getGlid())
                .eq(StringUtils.isNotBlank(dtgz.getJrlx()), TbHcDtgz::getJrlx, dtgz.getJrlx())
                .eq(StringUtils.isNotBlank(dtgz.getSbly()), TbHcDtgz::getSbly, dtgz.getSbly())
                .ge(StringUtils.isNotBlank(dtgz.getXsd()), TbHcDtgz::getXsd, dtgz.getXsd())
                .like(StringUtils.isNotBlank(dtgz.getBqmc()), TbHcDtgz::getBqmc, dtgz.getBqmc())
                .eq(StringUtils.isNotBlank(dtgz.getSbbm()), TbHcDtgz::getSbbm, dtgz.getSbbm())
                .eq(StringUtils.isNotBlank(dtgz.getSbmc()), TbHcDtgz::getSbmc, dtgz.getSbmc())
                .orderByDesc(TbHcDtgz::getXtCjsj)

        ;
        if (!CollectionUtils.isEmpty(dtgz.getBqList())) {
            for (String bq : dtgz.getBqList()) {
                eq.like(TbHcDtgz::getBqmc, bq);
            }
        }

        List<TbHcDtgz> tbHcDtgzs = this.baseMapper.selectList(eq);
        if (!CollectionUtils.isEmpty(tbHcDtgzs)) {
            tbHcDtgzs = tbHcDtgzs.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(TbHcDtgz::getSfzh))), ArrayList::new));
        }
        return tbHcDtgzs;
    }
}
