package com.izouma.wenlvju.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.izouma.wenlvju.domain.Record;
import com.izouma.wenlvju.dto.PageQuery;
import com.izouma.wenlvju.dto.RecordExcelDTO;
import com.izouma.wenlvju.exception.BusinessException;
import com.izouma.wenlvju.repo.RecordRepo;
import com.izouma.wenlvju.utils.JpaUtils;
import jodd.util.StringUtil;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class RecordService {

    private RecordRepo recordRepo;

    public Page<Record> all(PageQuery pageQuery) {
        return recordRepo.findAll(JpaUtils.toSpecification(pageQuery, Record.class), JpaUtils.toPageRequest(pageQuery));
    }

    public Page<Record> all2(PageQuery pageQuery) {
        Map<String, Object> query = pageQuery.getQuery();
        Object agency = query.get("examinationAgency");
        String examinationAgency = null;
        if (agency != null) {
            examinationAgency = String.valueOf(agency);
            query.remove("examinationAgency");
        }

        Object organizer1 = query.get("organizer");
        String organizer = null;
        if (organizer1 != null) {
            organizer = String.valueOf(organizer1);
            query.remove("organizer");
        }

        Object examinationTime = query.get("examinationTime");
        LocalDate date = null;
        if (examinationTime != null) {
            date = Convert.toLocalDateTime(examinationTime).toLocalDate();
            query.remove("examinationTime");
        }


        String finalAgency = examinationAgency;
        String finalOrganizer = organizer;
        LocalDate finalDate = date;
        return recordRepo.findAll((root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> and = JpaUtils.toPredicates(pageQuery, Record.class, root, criteriaQuery, criteriaBuilder);
            if (StringUtil.isNotBlank(finalAgency)) {
                and.add(criteriaBuilder.like(root.get("examinationAgency"), "%" + finalAgency + "%"));
            }
            if (StringUtil.isNotBlank(finalOrganizer)) {
                and.add(criteriaBuilder.like(root.get("organizer"), "%" + finalOrganizer + "%"));
            }
            if (ObjectUtil.isNotEmpty(finalDate)) {
                and.add(criteriaBuilder.lessThanOrEqualTo(root.get("examinationStartTime"), finalDate));
                and.add(criteriaBuilder.greaterThanOrEqualTo(root.get("examinationEndTime"), finalDate));
            }
            return criteriaBuilder.and(and.toArray(new Predicate[0]));
        }, JpaUtils.toPageRequest(pageQuery));
    }

    public Record addSupervisor(Long id, Long userId) {
        Record record = recordRepo.findById(id).orElseThrow(new BusinessException("无记录"));
        record.setSupervisorUserId(userId);
        return recordRepo.save(record);
    }
}
