package com.rf.psychological.plan.service.impl;



import com.rf.psychological.dao.dto.InstitutionAuthSubjectNameDto;
import com.rf.psychological.dao.dto.ProgressDto;
import com.rf.psychological.user.dao.model.UserEntity;
import com.rf.psychological.user.dao.repository.UserRepository;
import com.rf.psychological.plan.dao.dto.TestPlanAuthConTaskDto;
import com.rf.psychological.plan.dao.dto.TestPlanContendProgressDto;
import com.rf.psychological.plan.dao.dto.TestPlanUserProgressDto;
import com.rf.psychological.plan.dao.model.TestPlanContendEntity;
import com.rf.psychological.plan.dao.model.TestPlanUserEntity;
import com.rf.psychological.plan.dao.repository.TestPlanContendRepository;
import com.rf.psychological.plan.dao.repository.TestPlanRepository;
import com.rf.psychological.plan.dao.repository.TestPlanUserRepository;
import com.rf.psychological.plan.service.TestPlanContendService;
import org.springframework.data.domain.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


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

/**
 * @author lpf
 * @description:
 * @date 2022/1/614:35
 */
@Service
public class TestPlanContendServiceImpl implements TestPlanContendService {

    @Autowired
    private TestPlanRepository testPlanRepository;

    @Autowired
    private TestPlanContendRepository testPlanContendRepository;

    @Autowired
    private TestPlanUserRepository testPlanUserRepository;

    @Autowired
    private UserRepository userRepository;

    @Override
    public void deleteById(String id) {
        this.testPlanContendRepository.deleteById(id);
    }

    @Override
    public void deleteByTestIdAndTestPlanId(String testId, String testPlanId) {
        testPlanContendRepository.deleteByTestIdAndTestPlanId(testId, testPlanId);
    }

    @Override
    public void deleteByTestPlanId(String testPlanId) {
        this.testPlanContendRepository.deleteAllByTestPlanId(testPlanId);
    }

    @Override
    public void save(TestPlanContendEntity testPlanContendEntity) {
        testPlanContendRepository.save(testPlanContendEntity);
    }

    @Override
    public void saveInBatch(List<TestPlanContendEntity> testPlanContendEntities) {
        testPlanContendRepository.saveAll(testPlanContendEntities);
    }

    @Override
    public Page<InstitutionAuthSubjectNameDto> getTestPlanNoSubTask(String institutionNo, String type, String searchKey, String testPlanId, int beginNum, int pageSize, String categoryEname) {
        if ("ALL".equals(categoryEname)) {
            return testPlanContendRepository.getTestPlanNoSubTask(institutionNo, type, searchKey, testPlanId, PageRequest.of(beginNum - 1, pageSize));
        } else {
            return testPlanContendRepository.getTestPlanNoSubTaskByCatagoryEnglinshName(institutionNo, type, searchKey, testPlanId, categoryEname, PageRequest.of(beginNum - 1, pageSize));
        }

    }

    @Override
    public Page<TestPlanAuthConTaskDto> getTestPlanNoConTask(String institutionNo, String type, String searchKey, String testPlanId, int beginNum, int pageSize, String categoryEname) {
        if ("CALL".equals(categoryEname)) {
            return testPlanContendRepository.getTestPlanNoConTask(institutionNo, type, searchKey, testPlanId, PageRequest.of(beginNum - 1, pageSize));
        } else {
            return testPlanContendRepository.getTestPlanNoConTaskByCatagoryEnglinshName(institutionNo, type, searchKey, testPlanId, categoryEname, PageRequest.of(beginNum - 1, pageSize));
        }

    }

    @Override
    public Page<TestPlanContendEntity> findTestPlanContendEntitiesByTestPlanId(String testPlanId, String type, int beginNum, int pageSize) {
        return testPlanContendRepository.findTestPlanContendEntitiesByTestPlanIdAndType(testPlanId, type, PageRequest.of(beginNum - 1, pageSize));
    }

    @Override
    public Page<TestPlanContendEntity> getUserNoDoTestPlanContend(String institutionNo, String phone, String type, String testPlanId, int beginNum, int pageSize) {
        return testPlanContendRepository.getUserNoDoTestPlan(institutionNo, phone, type, testPlanId, PageRequest.of(beginNum - 1, pageSize));
    }

    @Override
    public List<TestPlanContendEntity> getUserNoDoTestPlanContend(String institutionNo, String phone, String testPlanId) {
        return testPlanContendRepository.getUserNoDoTestPlan(institutionNo, phone, testPlanId);
    }

    @Override
    public List<TestPlanContendEntity> getUserDoTestPlanContend(String institutionNo, String phone, String testPlanId) {
        return testPlanContendRepository.getUserDoTestPlan(institutionNo, phone, testPlanId);
    }

    @Override
    public int getUserNoDoTestPlanContendNum(String institutionNo, String phone, String testPlanId) {
        return testPlanContendRepository.getUserNoDoTestPlanNum(institutionNo, phone, testPlanId);
    }

    @Override
    public int getUserDoTestPlanContendNum(String institutionNo, String phone, String testPlanId) {
        return testPlanContendRepository.getUserDoTestPlanNum(institutionNo, phone, testPlanId);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
    public List<TestPlanContendProgressDto> getTestPlanContendProgress(String testPlanId) {
        ArrayList<TestPlanContendProgressDto> result = new ArrayList<>();
        //查找测试计划下的测试内容
        TestPlanUserEntity testPlanUserEntity = new TestPlanUserEntity();
        testPlanUserEntity.setTestPlanId(testPlanId);
        long allUserCount = testPlanUserRepository.count(Example.of(testPlanUserEntity));
        //获取当前测试计划下的量表和认知任务
        TestPlanContendEntity keyTestPlanId = new TestPlanContendEntity();
        keyTestPlanId.setTestPlanId(testPlanId);
        List<TestPlanContendEntity> testPlanContendEntities = testPlanContendRepository.findAll(Example.of(keyTestPlanId));
        for (TestPlanContendEntity testPlanContendEntity : testPlanContendEntities) {
            TestPlanContendProgressDto testPlanContendProgressDto = new TestPlanContendProgressDto();
            //获取每个量表和认知任务下用户做的次数
            int havingDo = testPlanContendRepository.countHavingDoByFlag(testPlanId, testPlanContendEntity.getFlag());
            BeanUtils.copyProperties(testPlanContendEntity, testPlanContendProgressDto);
            testPlanContendProgressDto.setAllUserNum(String.valueOf(allUserCount));
            testPlanContendProgressDto.setDoUserNum(String.valueOf(havingDo));
            result.add(testPlanContendProgressDto);
        }
        return result;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
    public List<TestPlanUserProgressDto> getTestPlanUsersProgress(String testPlanId) {
        ArrayList<TestPlanUserProgressDto> result = new ArrayList<>();
        //查看测试计划下的用户
        TestPlanUserEntity keyTestPlanId = new TestPlanUserEntity();
        keyTestPlanId.setTestPlanId(testPlanId);
        List<String> usersId = testPlanUserRepository.findAll(Example.of(keyTestPlanId))
                .stream().map(TestPlanUserEntity::getUId)
                .collect(Collectors.toList());
        List<UserEntity> userEntities = userRepository.findAllById(usersId);
        //获取量表测试总数
        TestPlanContendEntity keyTestPlanIdAndType = new TestPlanContendEntity();
        keyTestPlanIdAndType.setTestPlanId(testPlanId);
        keyTestPlanIdAndType.setType("0");
        long allSubCounts = testPlanContendRepository.count(Example.of(keyTestPlanIdAndType));
        //获取认知任务测试总数
        keyTestPlanIdAndType.setType("1");
        long allCogCounts = testPlanContendRepository.count(Example.of(keyTestPlanIdAndType));

        for (UserEntity userEntity : userEntities) {
            TestPlanUserProgressDto testPlanUserProgressDto = new TestPlanUserProgressDto();
            BeanUtils.copyProperties(userEntity, testPlanUserProgressDto);
            //获取用户已经做的量表测试数
            int havingDoSubCounts = testPlanUserRepository.countHavingDoByTypeAndUserId(testPlanId, "0", userEntity.getId());
            //获取用户已经做的认知任务测试数
            int havingDoCogCounts = testPlanUserRepository.countHavingDoByTypeAndUserId(testPlanId, "1", userEntity.getId());
            ProgressDto subProgress = new ProgressDto(allSubCounts, havingDoSubCounts);
            ProgressDto cogProgress = new ProgressDto(allCogCounts, havingDoCogCounts);
            testPlanUserProgressDto.setCogProgress(cogProgress);
            testPlanUserProgressDto.setSubjectProgress(subProgress);
            result.add(testPlanUserProgressDto);
        }

        return result;
    }

    @Override
    public void deleteTestPlanByFlag(String flag) {
        this.testPlanContendRepository.deleteTestPlanByFlag(flag);
    }

    /**
     * 测试计划任务总数
     *
     * @param testPlanId
     * @return
     */
    @Override
    public int countContent(String testPlanId) {
        return this.testPlanContendRepository.countContent(testPlanId);
    }
}
