package com.willalp.yiqing.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.common.exception.base.BaseException;
import com.willalp.common.utils.StringUtils;
import com.willalp.common.utils.bean.BeanUtils;
import com.willalp.yiqing.domain.HsPositiveContiguityUser;
import com.willalp.yiqing.domain.HsYiqingRnsResult;
import com.willalp.yiqing.domain.vo.RnsResultVo;
import com.willalp.yiqing.mapper.HsYiqingRnsResultMapper;
import com.willalp.yiqing.service.IHsPositiveContiguityUserService;
import com.willalp.yiqing.service.IHsYiqingRnsResultService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 核酸检测结果Service业务层处理
 *
 * @author willalp
 * @date 2022-05-18
 */
@Service
public class HsYiqingRnsResultServiceImpl extends ServiceImpl<HsYiqingRnsResultMapper, HsYiqingRnsResult> implements IHsYiqingRnsResultService {
    @Resource
    private HsYiqingRnsResultMapper hsYiqingRnsResultMapper;

    @Resource
    private IHsPositiveContiguityUserService pcuService;

    /**
     * 查询核酸检测结果列表
     *
     * @param rnsResultVo 核酸检测结果
     * @return 核酸检测结果
     */
    @Override
    public List<RnsResultVo> selectHsYiqingRnsResultList(RnsResultVo rnsResultVo) {
        return hsYiqingRnsResultMapper.selectHsYiqingRnsResultList(rnsResultVo);
    }

    @Override
    public String importData(List<RnsResultVo> list) {
        if (0 != list.size()) {
            List<RnsResultVo> rnasDb = hsYiqingRnsResultMapper.selectHsYiqingRnsResultList(null);
            List<HsPositiveContiguityUser> usersDb = pcuService.getBaseMapper().selectList(null);
            List<HsYiqingRnsResult> saveRnas = new ArrayList<>();
            int errorNum = 0;
            try {
                for (RnsResultVo rnsVo : list) {
                    int num = 0;
                    HsYiqingRnsResult rnsResult = new HsYiqingRnsResult();
                    for (HsPositiveContiguityUser userDb : usersDb) {
                        if (StringUtils.equals(rnsVo.getUserName(), userDb.getUserName()) &&
                                StringUtils.equals(rnsVo.getAttestNo(), userDb.getAttestNo())) {
                            BeanUtils.copyBeanProp(rnsResult, rnsVo);
                            rnsResult.setUserDataId(userDb.getDataId());
                            num++;
                            break;
                        }
                    }
                    if (0 != rnasDb.size()) {
                        for (RnsResultVo resultDb : rnasDb) {
                            if (resultDb.getUserDataId().equals(rnsVo.getUserDataId())) {
                                if (StringUtils.equals(rnsResult.getUserDataId(), resultDb.getUserDataId())) {
                                    if (resultDb.getSamplingTime().getTime() == rnsVo.getSamplingTime().getTime()) {
                                        errorNum++;
                                        throw new Exception();
                                    }
                                }
                            }
                        }
                    }
                    if (0 < num) {
                        saveRnas.add(rnsResult);
                    }
                }
            } catch (Exception e) {
                errorNum++;
            }
            if (0 != saveRnas.size()) {
                if (!this.saveBatch(saveRnas)) {
                    throw new BaseException("批量导入失败");
                } else {
                    return "共导入 " + saveRnas.size() + " 条有效数据，无效数据 " + errorNum + " 条";
                }
            } else {
                return "共导入" + saveRnas.size() + "条数据，无效数据" + errorNum + "条";
            }
        }
        return "没有有效数据";
    }
}
