package com.huiju.estateagents.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.entity.TaDocumentImg;
import com.huiju.estateagents.entity.TaDocumentVerify;
import com.huiju.estateagents.entity.TaDocumentVerifyModel;
import com.huiju.estateagents.mapper.TaDocumentImgMapper;
import com.huiju.estateagents.mapper.TaDocumentVerifyMapper;
import com.huiju.estateagents.service.ITaDocumentVerifyService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 资料审核  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-10-23
 */
@Service
public class TaDocumentVerifyServiceImpl extends ServiceImpl<TaDocumentVerifyMapper, TaDocumentVerify> implements ITaDocumentVerifyService {

    @Autowired
    TaDocumentVerifyMapper documentVerifyMapper;
    @Autowired
    TaDocumentImgMapper taDocumentImgMapper;

    @Override
    public IPage<TaDocumentVerify> getDocumentVerifyList (Integer pageNum,Integer pageSize,String personId,Integer orgId){
        IPage<TaDocumentVerify>page = new Page<>(pageNum,pageSize);
        IPage<TaDocumentVerify> data = documentVerifyMapper.getDocumentVerifyList(page,personId,orgId);
        List<Integer> idList = new ArrayList<>();
        for (int i = 0; i<data.getRecords().size();i++){
            idList.add(data.getRecords().get(i).getDocumentVerifyId());
        }

        if (CollectionUtils.isNotEmpty(idList)) {
            QueryWrapper<TaDocumentImg>imgQueryWrapper = new QueryWrapper<>();
            imgQueryWrapper.in("document_verify_id",idList);
            imgQueryWrapper.eq("org_id",orgId);
            List<TaDocumentImg>allImgList = taDocumentImgMapper.selectList(imgQueryWrapper);
            for (int i = 0;i<data.getRecords().size();i++){
                List<TaDocumentImg>imgList = new ArrayList<>();
                for(int j = 0; j<allImgList.size();j++){
                    if(data.getRecords().get(i).getDocumentVerifyId().equals(allImgList.get(j).getDocumentVerifyId())){
                        imgList.add(allImgList.get(j));
                    }
                }
                data.getRecords().get(i).setImgList(imgList);
            }
        }

        return data;
    }

    @Override
    public ResponseBean getAdminDocumentList(Integer pageNum, Integer pageSize, String name, String tel, Integer orgId, LocalDateTime startDate, LocalDateTime endDate){
        Page page = new Page<>(pageNum,pageSize);
        List<TaDocumentVerifyModel> taDocumentVerifyModelList = documentVerifyMapper.getAdminDocumentList(page, name, tel, orgId, startDate, endDate);
        page.setRecords(taDocumentVerifyModelList);
        return ResponseBean.success(page);
    }

}
