package com.xinyilu.system.service.checkReportFailInfo.impl;

import com.xinyilu.common.core.domain.AjaxResult;
import com.xinyilu.common.utils.DateUtils;
import com.xinyilu.system.domain.checkReportFailInfo.CheckReportFailInfo;
import com.xinyilu.system.domain.dto.CheckReportFailInfoDTO;
import com.xinyilu.system.domain.vo.checkReportFailInfo.CheckReportFailInfoVO;
import com.xinyilu.system.mapper.checkReportFailInfo.CheckReportFailInfoMapper;
import com.xinyilu.system.service.checkReportFailInfo.ICheckReportFailInfoService;
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.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 个人报告和幼儿园报告审核失败信息记录Service业务层处理
 *
 * @author xinyilu
 * @date 2023-07-31
 */
@Service
public class CheckReportFailInfoServiceImpl implements ICheckReportFailInfoService {
    @Resource
    private CheckReportFailInfoMapper checkReportFailInfoMapper;

    /**
     * 查询个人报告和幼儿园报告审核失败信息记录
     *
     * @param id 个人报告和幼儿园报告审核失败信息记录主键
     * @return 个人报告和幼儿园报告审核失败信息记录
     */
    @Override
    public CheckReportFailInfo selectCheckReportFailInfoById(Long id) {
        return checkReportFailInfoMapper.selectCheckReportFailInfoById(id);
    }

    /**
     * 查询个人报告和幼儿园报告审核失败信息记录列表
     *
     * @param checkReportFailInfo 个人报告和幼儿园报告审核失败信息记录
     * @return 个人报告和幼儿园报告审核失败信息记录
     */
    @Override
    public List<CheckReportFailInfoDTO> selectCheckReportFailInfoList(CheckReportFailInfo checkReportFailInfo) {
        return checkReportFailInfoMapper.selectCheckReportFailInfoList(checkReportFailInfo);
    }

    /**
     * 新增个人报告和幼儿园报告审核失败信息记录
     *
     * @param checkReportFailInfoVO 个人报告和幼儿园报告审核失败信息记录
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult insertCheckReportFailInfo(CheckReportFailInfoVO checkReportFailInfoVO) {
        //点击一键审核功能,根据班级id/幼儿园id/批次,查找失败信息进行处理
        CheckReportFailInfo checkReportFailInfo = new CheckReportFailInfo();
        if (StringUtils.isNotBlank(checkReportFailInfoVO.getBatchCode())) {
            checkReportFailInfo.setBatchCode(checkReportFailInfoVO.getBatchCode());
        }
        if (Objects.nonNull(checkReportFailInfoVO.getClassId())) {
            checkReportFailInfo.setClassId(checkReportFailInfoVO.getClassId());
        }
        if (Objects.nonNull(checkReportFailInfoVO.getKindergartenId())) {
            checkReportFailInfo.setKindergartenId(checkReportFailInfoVO.getKindergartenId());
        }
        checkReportFailInfo.setCheckReportType(checkReportFailInfoVO.getCheckReportType());
        //数据库中失败的数据
        List<CheckReportFailInfoDTO> checkReportFailInfos = checkReportFailInfoMapper.selectCheckReportFailInfoList(checkReportFailInfo);
        //传过来的失败数据
        List<CheckReportFailInfoDTO> checkReportFail = checkReportFailInfoVO.getData();
        //新增报错信息
        Collection<CheckReportFailInfoDTO> collectInsert = CollectionUtils.subtract(checkReportFail, checkReportFailInfos);
        List<CheckReportFailInfoDTO> collect = collectInsert.stream().filter(Objects::nonNull).peek(n -> n.setCreateTime(DateUtils.getNowDate())).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(collect)) {
            checkReportFailInfoMapper.insertCheckReportFailInfo(collect);
        }
        //需要修改状态数据集合
        Collection<CheckReportFailInfoDTO> collectUpdate = CollectionUtils.subtract(checkReportFailInfos, checkReportFail);
        //进行批量修改
        if (CollectionUtils.isNotEmpty(collectUpdate)) {
            checkReportFailInfoMapper.updateCheckReportFailInfoStatus(collectUpdate.stream().map(CheckReportFailInfo::getId).toArray(Long[]::new));
        }
        return AjaxResult.success();
    }

    /**
     * 修改个人报告和幼儿园报告审核失败信息记录
     *
     * @param checkReportFailInfo 个人报告和幼儿园报告审核失败信息记录
     * @return 结果
     */
    @Override
    public int updateCheckReportFailInfo(CheckReportFailInfo checkReportFailInfo) {
        checkReportFailInfo.setUpdateTime(DateUtils.getNowDate());
        return checkReportFailInfoMapper.updateCheckReportFailInfo(checkReportFailInfo);
    }

    /**
     * 批量删除个人报告和幼儿园报告审核失败信息记录
     *
     * @param ids 需要删除的个人报告和幼儿园报告审核失败信息记录主键
     * @return 结果
     */
    @Override
    public int deleteCheckReportFailInfoByIds(Long[] ids) {
        return checkReportFailInfoMapper.deleteCheckReportFailInfoByIds(ids);
    }

    /**
     * 删除个人报告和幼儿园报告审核失败信息记录信息
     *
     * @param id 个人报告和幼儿园报告审核失败信息记录主键
     * @return 结果
     */
    @Override
    public int deleteCheckReportFailInfoById(Long id) {
        return checkReportFailInfoMapper.deleteCheckReportFailInfoById(id);
    }
}
