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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.base.model.BaseEntity;
import com.rf.psychological.user.dao.repository.UserRepository;
import com.rf.psychological.plan.dao.repository.TestPlanUserRepository;
import com.rf.psychological.plan.service.TestPlanUserService;
import com.rf.psychological.utils.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.rf.psychological.plan.dao.model.TestPlanUserEntity;
import com.rf.psychological.utils.Result;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

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

    @Autowired
    private TestPlanUserRepository testPlanUserRepository;

    @Autowired
    private UserRepository userRepository;

    @Override
    public void deleteByUIdAndTestPlanId(String uId, String testPlanId) {
        testPlanUserRepository.deleteByuIdAndTestPlanId(uId, testPlanId);
    }

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

    @Override
    public int countByTestId(String testId) {
        return this.testPlanUserRepository.countByTestPlanId(testId);
    }

    @Override
    public Page<TestPlanUserEntity> findTestPlanUserByTestId(String testId, String searchKey, int beginNum, int pageSize) {
        return this.testPlanUserRepository.findAllByTestPlanId(testId, searchKey, PageRequest.of(beginNum - 1, pageSize));
    }

    @Override
    public Optional<TestPlanUserEntity> findTestPlanUserByTestIdAndUserId(String testPlanId, String userId) {
        TestPlanUserEntity testPlanUserEntity = new TestPlanUserEntity();
        testPlanUserEntity.setTestPlanId(testPlanId);
        testPlanUserEntity.setUId(userId);
        return testPlanUserRepository.findOne(Example.of(testPlanUserEntity));
    }

    @Override
    public List<TestPlanUserEntity> findTestPlanByUId(String uId, int beginNum, int pageSize, String status, String isComplete) {
        return testPlanUserRepository.findTestPlanByUId(uId,status,isComplete, PageRequest.of(beginNum - 1, pageSize));
    }

    @Override
    public void deleteAllByTestPlanId(String testPlanId) {
        testPlanUserRepository.deleteAllByTestPlanId(testPlanId);
    }

    @Override
    public void save(TestPlanUserEntity testPlanUserEntity) {
        testPlanUserRepository.save(testPlanUserEntity);
    }

    @Override
    public void saveInBatch(List<TestPlanUserEntity> testPlanUserEntities) {
        testPlanUserRepository.saveAll(testPlanUserEntities);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result saveByGroup(JSONArray jsonArray) {
        HashSet<TestPlanUserEntity> testPlanUserEntities = new HashSet<>();
        for (Object o : jsonArray) {
            String groupId = ((JSONObject) o).getString("groupId");
            String testPlanId = ((JSONObject) o).getString("testPlanId");
            //查找当前组下的用户id
            List<String> nowUserId = userRepository.findUsersInGroup(groupId)
                    .stream().map(BaseEntity::getId)
                    .collect(Collectors.toList());
            //查看当前测试计划下原有的用户id
            TestPlanUserEntity queryParam = new TestPlanUserEntity();
            queryParam.setTestPlanId(testPlanId);
            List<String> userIdExits = testPlanUserRepository.findAll(Example.of(queryParam))
                    .stream().map(TestPlanUserEntity::getUId)
                    .collect(Collectors.toList());
            //去除当前组下已经存在于测试计划中的用户id
            nowUserId.removeAll(userIdExits);

            for (String userId : nowUserId) {
                TestPlanUserEntity testPlanUserEntity = new TestPlanUserEntity(testPlanId, userId, "0", LocalDateTime.now(), LocalDateTime.now());
                testPlanUserEntities.add(testPlanUserEntity);
            }
        }
        testPlanUserRepository.saveAll(testPlanUserEntities);
        return new Result<>(HttpStatus.SUCCESS, "成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateIsComplete(String testPlanId, String userId, String isComplete) {
        testPlanUserRepository.updateIsComplete(testPlanId, userId, isComplete);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateIsComplete(String testPlanId, String isComplete) {
        testPlanUserRepository.updateIsComplete(testPlanId, isComplete);
    }

    @Override
    public void deletePlanIdAndGroupId(String planId, String groupId) {
        this.testPlanUserRepository.deletePlanIdAndGroupId( planId,  groupId);
    }

    /**
     * planId---->List<UserId>
     *
     * @param planId
     * @return
     */
    @Override
    public List<String> findUserIdByPlanId(String planId) {
        return this.testPlanUserRepository.findUserIdByPlanId(planId);
    }

    /**
     * userId&planId ----> entry
     *
     * @param userId
     * @param planId
     */
    @Override
    public TestPlanUserEntity findByUserIdAndPlanId(String userId, String planId) {
        return this.testPlanUserRepository.findByUserIdAndPlanId( userId,  planId);
    }

    @Override
    public List<TestPlanUserEntity> findUncompletedUser(String id) {
        return this.testPlanUserRepository.findUncompleteUser(id);
    }

}
