package com.xinyilu.system.service.parentactionlogrecord.Impl;

import com.xinyilu.common.core.domain.AjaxResult;
import com.xinyilu.common.enums.LogTypeEnum;
import com.xinyilu.common.exception.ServiceException;
import com.xinyilu.system.domain.applet.AppletParentAndStudentInfo;
import com.xinyilu.system.domain.applet.AppletStudentInfo;
import com.xinyilu.system.domain.apploginfo.AppLogInfo;
import com.xinyilu.system.domain.applogkindergarteninfo.AppLogKindergartenInfo;
import com.xinyilu.system.mapper.apploginfo.AppLogInfoMapper;
import com.xinyilu.system.mapper.applogkindergarteninfo.AppLogKindergartenInfoMapper;
import com.xinyilu.system.mapper.organize.OrganizeInfoMapper;
import com.xinyilu.system.service.parentactionlogrecord.IParentActionLogRecordService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 家长操作日志Service业务层处理
 *
 * @author zh
 * @date 2022-07-28
 */
@Service
public class IParentActionLogRecordServiceImpl implements IParentActionLogRecordService {
    @Resource
    private AppLogInfoMapper appLogInfoMapper;
    @Resource
    private OrganizeInfoMapper organizeInfoMapper;
    @Resource
    private AppLogKindergartenInfoMapper appLogKindergartenInfoMapper;

    /**
     * 登录日志记录
     *
     * @param appletParentAndStudentInfo 信息日志
     * @return AjaxResult
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult loginLogging(AppletParentAndStudentInfo appletParentAndStudentInfo) {
        String logType = LogTypeEnum.LOGIN.getLogType();
        recordLogging(appletParentAndStudentInfo, logType);
        return AjaxResult.success();
    }

    /**
     * 处方记录信息日志
     *
     * @param appletParentAndStudentInfo 信息日志
     * @return AjaxResult
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult prescriptionLogging(AppletParentAndStudentInfo appletParentAndStudentInfo) {
        String logType = LogTypeEnum.EXERCISE_PRESCRIPTION.getLogType();
        recordLogging(appletParentAndStudentInfo, logType);
        return AjaxResult.success();
    }

    /**
     * 处方视频记录信息日志
     *
     * @param appletParentAndStudentInfo 信息日志
     * @return AjaxResult
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult prescriptionVideoLogging(AppletParentAndStudentInfo appletParentAndStudentInfo) {
        if (Objects.isNull(appletParentAndStudentInfo.getPrescriptionVideoId())) {
            return AjaxResult.error("视频信息为空！");
        }
        String logType = LogTypeEnum.EXERCISE_PRESCRIPTION_VIDEO.getLogType();
        recordLogging(appletParentAndStudentInfo, logType);
        return AjaxResult.success();
    }

    /**
     * 记录信息日志
     *
     * @param appletParentAndStudentInfo 信息日志
     * @param logType                    类型
     */
    public void recordLogging(AppletParentAndStudentInfo appletParentAndStudentInfo, String logType) {
        //新增AppLogInfo表
        AppLogInfo appLogInfo = new AppLogInfo();
        appLogInfo.setParentInfoId(Objects.isNull(appletParentAndStudentInfo.getParentInfoId()) ? null : appletParentAndStudentInfo.getParentInfoId());
        appLogInfo.setPrescriptionVideoId(Objects.isNull(appletParentAndStudentInfo.getPrescriptionVideoId()) ? null : appletParentAndStudentInfo.getPrescriptionVideoId());
        appLogInfo.setLogIp(StringUtils.isBlank(appletParentAndStudentInfo.getLogIp()) ? null : appletParentAndStudentInfo.getLogIp());
        appLogInfo.setLogType(logType);
        appLogInfo.setLogTime(LocalDateTime.now());
        appLogInfo.setLogDate(LocalDate.now());
        appLogInfo.setCreateTime(LocalDateTime.now());
        int insertAppLogCount = appLogInfoMapper.insertAppLogInfo(appLogInfo);
        if (insertAppLogCount <= 0) {
            throw new ServiceException("新增幼儿体测登录记录信息表失败！");
        }
        appLogInfo.setId(appLogInfo.getId());
        //批量新增AppLogKindergartenInfo表
        if (CollectionUtils.isEmpty(appletParentAndStudentInfo.getStudentInfoList())) {
            return;
        }
        List<AppLogKindergartenInfo> appLogKindergartenInfos = new ArrayList<>();
        for (AppletStudentInfo appletStudentInfo : appletParentAndStudentInfo.getStudentInfoList()) {
            AppLogKindergartenInfo appLogKindergartenInfo = new AppLogKindergartenInfo();
            appLogKindergartenInfo.setAppletLogInfoId(appLogInfo.getId());
            appLogKindergartenInfo.setClassId(Objects.isNull(appletStudentInfo.getClassId()) ? 0L : appletStudentInfo.getClassId());
            appLogKindergartenInfo.setStudentId(Objects.isNull(appletStudentInfo.getStudentId()) ? 0L : appletStudentInfo.getStudentId());
            appLogKindergartenInfo.setKindergartenId(Objects.isNull(appletStudentInfo.getKindergartenId()) ? 0L : appletStudentInfo.getKindergartenId());
            appLogKindergartenInfos.add(appLogKindergartenInfo);
        }
        List<Long> kindergartenIds = appletParentAndStudentInfo.getStudentInfoList().stream().filter(Objects::nonNull).map(AppletStudentInfo::getKindergartenId).distinct().collect(Collectors.toList());
        //查询组织及区域信息
        List<AppLogKindergartenInfo> areaAndOrganizes = organizeInfoMapper.selectOrganizeByKindergartenId(kindergartenIds);
        for (AppLogKindergartenInfo logKindergartenInfo : appLogKindergartenInfos) {
            AppLogKindergartenInfo areaAndOrganize = areaAndOrganizes.stream().filter(Objects::nonNull).filter(item -> Objects.nonNull(item.getKindergartenId()) && Objects.nonNull(logKindergartenInfo.getKindergartenId()) && logKindergartenInfo.getKindergartenId().equals(item.getKindergartenId()))
                    .findFirst().orElse(new AppLogKindergartenInfo());
            logKindergartenInfo.setOrganizeId(Objects.isNull(areaAndOrganize.getOrganizeId()) ? 0L : areaAndOrganize.getOrganizeId());
            logKindergartenInfo.setAreaId(Objects.isNull(areaAndOrganize.getAreaId()) ? 0L : areaAndOrganize.getAreaId());
            logKindergartenInfo.setAreaCode(StringUtils.isBlank(areaAndOrganize.getAreaCode()) ? "" : areaAndOrganize.getAreaCode());
            logKindergartenInfo.setCreateTime(LocalDateTime.now());
            logKindergartenInfo.setLogTime(LocalDateTime.now());
            logKindergartenInfo.setLogDate(LocalDate.now());
            logKindergartenInfo.setLogType(logType);
        }
        int insertAppLogKindergartenCount = appLogKindergartenInfoMapper.insertAppLogKindergartenInfoList(appLogKindergartenInfos);
        if (insertAppLogKindergartenCount <= 0) {
            throw new ServiceException("新增登录幼儿园信息记录表失败！");
        }
    }
}
