package com.chinaweal.aiccs.aiccs.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaweal.aiccs.aiccs.abnormal.entity.EBaseinfo;
import com.chinaweal.aiccs.aiccs.abnormal.service.EBaseinfoService;
import com.chinaweal.aiccs.aiccs.business.entity.XrTaskAnnex;
import com.chinaweal.aiccs.aiccs.business.mapper.XrTaskAnnexMapper;
import com.chinaweal.aiccs.aiccs.business.service.IXrTaskAnnexService;
import com.chinaweal.aiccs.aiccs.seriousillegalPerson.entity.dto.PersonSpeSaveIvdDto;
import com.chinaweal.youfool.framework.springboot.base.query.BaseQuery;
import com.chinaweal.youfool.framework.springboot.exception.custom.BusinessException;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 待办附录表 服务实现类
 * </p>
 *
 * @author qbh
 * @since 2024-03-08
 */
@Service
public class XrTaskAnnexServiceImpl extends ServiceImpl<XrTaskAnnexMapper, XrTaskAnnex> implements IXrTaskAnnexService {

    @Resource
    private EBaseinfoService ieBaseinfoService;
    @Override
    public XrTaskAnnex saveXrTaskAnnex(XrTaskAnnex xrTaskAnnex) {
        save(xrTaskAnnex);
        return xrTaskAnnex;
    }

    @Override
    public IPage<XrTaskAnnex> listXrTaskAnnex(BaseQuery<XrTaskAnnex> query) {
        XrTaskAnnex entity = query.getEntity(XrTaskAnnex.class);
        Page<XrTaskAnnex> page = query.getPage();

        LambdaQueryWrapper<XrTaskAnnex> lambdaQueryWrapper = Wrappers.lambdaQuery();

        return page(page, lambdaQueryWrapper);
    }

    @Override
    public void saveTaskAnnexByPripIdListOnTaskId(List<String> pripIdList, String taskId) {
        if (pripIdList == null || pripIdList.size() == 0){
            throw new RuntimeException("主体id为空！");
        }
//        List<EBaseinfo> list = ieBaseinfoService.list(new LambdaQueryWrapper<EBaseinfo>().in(EBaseinfo::getPripId, pripIdList));
        List<List<String>> newPripIdList = Lists.partition(pripIdList,400);
        List<EBaseinfo> list = new ArrayList<>();
        for (List pripids : newPripIdList) {
            list.addAll(ieBaseinfoService.listByIds(pripids));
        }
//        if (list.size() != pripIdList.size()){
        if (list.size() == 0){
            throw new BusinessException("未查询到数据！");
        }
        list.forEach(item -> {
            XrTaskAnnex entity = new XrTaskAnnex();
            entity.setPripId(item.getPripid());
            entity.setTaskLikeId(taskId);
            entity.setEntName(item.getEntname());
            entity.setRegNo(item.getRegno());
            entity.setUniscid(item.getUniscid());
            saveXrTaskAnnex(entity);
        });
    }

    @Override
    public void saveTaskAnnexByeBaseInfoOnTaskId(EBaseinfo eBaseinfo, String taskId) {
        XrTaskAnnex entity = new XrTaskAnnex();
        entity.setPripId(eBaseinfo.getPripid());
        entity.setTaskLikeId(taskId);
        entity.setEntName(eBaseinfo.getEntname());
        entity.setRegNo(eBaseinfo.getRegno());
        entity.setUniscid(eBaseinfo.getUniscid());
        saveXrTaskAnnex(entity);
    }

    @Override
    public void saveTaskAnnexByPripIdOnTaskId(String pripId, String taskId) {
        EBaseinfo baseinfo = ieBaseinfoService.getById(pripId);
        if (baseinfo == null){
            throw new RuntimeException("未查询到数据！");
        }
        saveTaskAnnexByeBaseInfoOnTaskId(baseinfo, taskId);
    }

    @Override
    public void saveTaskAnnexByPersonListOnTaskId(List<PersonSpeSaveIvdDto> personList, String taskId) {
        if (personList == null || personList.size() == 0){
            throw new RuntimeException("自然人信息为空！");
        }
        personList.forEach(item -> {
            XrTaskAnnex entity = new XrTaskAnnex();
            entity.setTaskLikeId(taskId);
            entity.setInv(item.getInv());
            entity.setCerType(item.getCerType());
            entity.setCerNo(item.getCerNo());
            saveXrTaskAnnex(entity);
        });
    }
}
