package com.izouma.wenlvju.service.performance;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.izouma.wenlvju.domain.performance.ProgrammeScore;
import com.izouma.wenlvju.dto.PageQuery;
import com.izouma.wenlvju.repo.performance.ProgrammeRepo;
import com.izouma.wenlvju.repo.performance.ProgrammeScoreRepo;
import com.izouma.wenlvju.utils.JpaUtils;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.persistence.Id;
import java.time.LocalDate;
import java.time.LocalDateTime;

@Service
@AllArgsConstructor
public class ProgrammeScoreService {

    private ProgrammeScoreRepo programmeScoreRepo;
    private ProgrammeRepo      programmeRepo;

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

    public void saveScore(Long programmeId, Double score, String remark, Long userId) {
        ProgrammeScore programmeScore = programmeScoreRepo.findByProgrammeIdAndExpertId(programmeId, userId);
        LocalDateTime now = LocalDateTime.now();
        if (ObjectUtil.isNull(programmeScore)) {
            programmeScore = ProgrammeScore.builder()
                    .expertId(userId)
                    .programmeId(programmeId)
                    .score(score)
                    .markAt(now)
                    .remark(remark)
                    .build();
        } else {
            if (StrUtil.isNotEmpty(remark)) {
                programmeScore.setRemark(remark);
            }
            if (ObjectUtil.isNotNull(score)) {
                if (ObjectUtil.isNull(programmeScore.getScore())) {
                    programmeScore.setScore(score);
                    programmeScore.setMarkAt(now);
                } else {
                    programmeScore.setScore2(programmeScore.getScore());
                    programmeScore.setMark2At(programmeScore.getMarkAt());
                    programmeScore.setScore(score);
                    programmeScore.setMarkAt(now);
                }
            }
        }

        programmeScoreRepo.save(programmeScore);

        Object avgObj = programmeScoreRepo.avgScore(programmeId);
        if (ObjectUtil.isNotNull(avgObj)) {
            Double avg = Convert.convert(Double.class, avgObj);
            programmeRepo.score(avg, programmeId);
        }

    }


    public void saveScore(Long programmeId, double score, Long userId) {
        ProgrammeScore programmeScore = programmeScoreRepo.findByProgrammeIdAndExpertId(programmeId, userId);
        LocalDateTime now = LocalDateTime.now();
        if (ObjectUtil.isNull(programmeScore)) {
            programmeScore = ProgrammeScore.builder()
                    .expertId(userId)
                    .programmeId(programmeId)
                    .score(score)
                    .markAt(now)
                    .build();
        } else if (ObjectUtil.isNull(programmeScore.getScore())) {
            programmeScore.setScore(score);
            programmeScore.setMarkAt(now);
        } else {
            programmeScore.setScore2(programmeScore.getScore());
            programmeScore.setMark2At(programmeScore.getMarkAt());
            programmeScore.setScore(score);
            programmeScore.setMarkAt(now);
        }
        programmeScoreRepo.save(programmeScore);

        Object avgObj = programmeScoreRepo.avgScore(programmeId);
        if (ObjectUtil.isNotNull(avgObj)) {
            Double avg = Convert.convert(Double.class, avgObj);
            programmeRepo.score(avg, programmeId);
        }

    }


    public void saveRemark(Long programmeId, String remark, Long userId) {
        ProgrammeScore programmeScore = programmeScoreRepo.findByProgrammeIdAndExpertId(programmeId, userId);
        if (ObjectUtil.isNull(programmeScore)) {
            programmeScore = ProgrammeScore.builder()
                    .expertId(userId)
                    .programmeId(programmeId)
                    .remark(remark)
                    .build();
        } else {
            programmeScore.setRemark(remark);
        }
        programmeScoreRepo.save(programmeScore);
    }
}

