package com.dcsoft.system.visitor.service.impl;

import com.dcsoft.common.core.constant.Constants;
import com.dcsoft.common.core.constant.SecurityConstants;
import com.dcsoft.common.core.domain.R;
import com.dcsoft.common.core.enums.ExamineEnum;
import com.dcsoft.common.core.exception.ServiceException;
import com.dcsoft.common.core.utils.CollUtil;
import com.dcsoft.common.core.web.page.TableDataInfoPage;
import com.dcsoft.common.datascope.annotation.DataScope;
import com.dcsoft.common.security.utils.SecurityUtils;
import com.dcsoft.system.api.RemoteFileService;
import com.dcsoft.system.api.domain.SysFile;
import com.dcsoft.system.api.model.LoginUser;
import com.dcsoft.system.domain.SysBlackList;
import com.dcsoft.system.domain.SysBranch;
import com.dcsoft.system.domain.SysManageRecord;
import com.dcsoft.system.domain.SysPeople;
import com.dcsoft.system.domain.vo.SysFileVo;
import com.dcsoft.system.mapper.SysBlackListMapper;
import com.dcsoft.system.mapper.SysDictDataMapper;
import com.dcsoft.system.utils.PictureUtils;
import com.dcsoft.system.visitor.domain.*;
import com.dcsoft.system.visitor.mapper.VisitorMapper;
import com.dcsoft.system.visitor.service.IVisitorService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;
import java.util.stream.Collectors;

import static com.dcsoft.common.core.utils.PageUtils.startPage;

/**
 * 访客信息Service业务层处理
 *
 * @author 倪春
 * @date 2023-03-09
 */
@Slf4j
@Service
public class VisitorServiceImpl implements IVisitorService {
    @Resource
    private VisitorMapper visitorMapper;

    @Resource
    private SysBlackListMapper sysBlackListMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Autowired
    private RemoteFileService fileService;

    @Value("${system.type}")
    public String systemType;

    @Value("${qd.url:http://}")
    public String qdUrl;
    /**
     * 卡权限创建接口
     */
    public static final String create = "/cardinfo/create";

    /**
     * 查询访客信息
     *
     * @param id 访客信息主键
     * @return 访客信息
     */
    @Override
    public Visitor selectVisitorById(Long id) {
        Visitor visitor = visitorMapper.selectVisitorById(id);
        if (visitor == null) {
            return new Visitor();
        }

        List<Long> collect = Arrays.asList(id);
        List<CheckCodeVo> checkCodeVos = visitorMapper.queryCode(collect);
        if (CollUtil.isNotEmpty(checkCodeVos)) {
            visitor.setQrCode(checkCodeVos.get(0).getCode());
        }

        if(StringUtils.isNotEmpty(visitor.getVisitorPosition())) {
            visitor.setPosition(visitor.getVisitorPosition());
        }
        Visitor item = new Visitor();
        item.setParentId(id);
        List<Visitor> itemList = visitorMapper.selectVisitorItemList(item);
        visitor.setItemList(itemList);

        SysFileVo fileVo = new SysFileVo();
        fileVo.setBusinessId(String.valueOf(id));
        fileVo.setBusinessType("sys_visitor_insurance");
        List<SysFileVo> list = visitorMapper.querySysFile(fileVo);
        if(!CollectionUtils.isEmpty(list)) {
            try {
                for (SysFileVo sysFileVo : list) {
                    sysFileVo.setFileName(URLDecoder.decode(sysFileVo.getFileName(), "utf-8"));
                }
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
            visitor.setFileList(list);
        }
        List<VisitorReviewProcessVo> reviewProcess = visitorMapper.queryVisitorReviewProcessList(id);
        if(!CollectionUtils.isEmpty(reviewProcess)) {
            boolean flag = true;
            for (VisitorReviewProcessVo process : reviewProcess) {
                if(!flag) {
                    process.setState(null);
                }
                if(Constants.ONE.equals(process.getState())) {
                    flag = false;
                }
            }
            visitor.setReviewProcessList(reviewProcess);
        }
        return visitor;
    }

    /**
     * 查询访客信息列表
     *
     * @param visitor 访客信息
     * @return 访客信息
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<Visitor> selectVisitorList(Visitor visitor) {
        return visitorMapper.selectVisitorList(visitor);
    }

    /**
     * 新增访客信息
     *
     * @param visitor 访客信息
     * @return 结果
     */
    @Override
    public int insertVisitor(Visitor visitor) {
        visitor.setExamineState(ExamineEnum.IN_REVIEW.getCode());
        visitor.setCurrentNode(1);
        return visitorMapper.insertVisitor(visitor);
    }

    /**
     * 修改访客信息
     *
     * @param visitor 访客信息
     * @return 结果
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public int updateVisitor(Visitor visitor) {
        visitor.setUpdateTime(new Date());
        return visitorMapper.updateVisitor(visitor);
    }

    /**
     * 批量删除访客信息
     *
     * @param ids 需要删除的访客信息主键
     * @return 结果
     */
    @Override
    public int deleteVisitorByIds(Long[] ids) {
        return visitorMapper.deleteVisitorByIds(ids);
    }

    /**
     * 删除访客信息信息
     *
     * @param id 访客信息主键
     * @return 结果
     */
    @Override
    public int deleteVisitorById(Long id) {
        return visitorMapper.deleteVisitorById(id);
    }

    @Override
    public List<Visitor> selectVisitorItemList(Visitor visitor) {
        return visitorMapper.selectVisitorItemList(visitor);
    }

    @Override
    public int deleteVisitorItemByParentId(Long parentId) {
        return visitorMapper.deleteVisitorItemByParentId(parentId);
    }

    @Override
    public Visitor selectByGuid(String guid) {
        return visitorMapper.selectByGuid(guid);
    }

    @Override
    public Visitor selectVisitorByPhone(String phone) {
        return visitorMapper.selectVisitorByPhone(phone);
    }

    /**
     * 查询访客预约信息
     */
    @Override
    public TableDataInfoPage queryVisitorAppointment(VisitorParamVo paramVo) {
        List<Visitor> visitors = visitorMapper.queryVisitorAppointment(paramVo);
        List<Visitor> result = visitors.stream()
                .skip((long) (paramVo.getPageNum() - 1) * paramVo.getPageSize())
                .limit(paramVo.getPageSize())
                .collect(Collectors.toList());
        return new TableDataInfoPage(result, visitors.size(), paramVo.getPageNum(), result.size());
    }

    /**
     * 查询黑名单
     */
    @Override
    public TableDataInfoPage selectSysBlackListList(VisitorParamVo paramVo) {
        List<SysBlackList> sysBlackLists = sysBlackListMapper.selectSysBlackListList(new SysBlackList());
        List<SysBlackList> result = sysBlackLists.stream()
                .skip((long) (paramVo.getPageNum() - 1) * paramVo.getPageSize())
                .limit(paramVo.getPageSize())
                .collect(Collectors.toList());
        return new TableDataInfoPage(result, sysBlackLists.size(), paramVo.getPageNum(), result.size());
    }

    /**
     * 查询访客人员信息
     */
    @Override
    public PersonnelInfoVo queryPersonnelInfo(List<String> list) {
        PersonnelInfoVo sysEmpowerRecords = sysBlackListMapper.queryPersonnelInfo(list);
        List<PersonnelInfoVo> infoVos = sysBlackListMapper.queryGeneralInfo(sysEmpowerRecords.getId());
        sysEmpowerRecords.setList(infoVos);
        return sysEmpowerRecords;
    }

    /**
     * 查询被访人
     */
    @Override
    public List<PersonnelInfoVo> queryPeople(Visitor paramVo) {
        return sysBlackListMapper.queryPeople(paramVo);
    }

    /**
     * 查询已审核状态信息
     */
    @Override
    public VisitorParamVo queryAuditedVisitor(Visitor paramVo) {
        return sysBlackListMapper.queryAuditedVisitor(paramVo);
    }

    /**
     * 查询详情
     */
    @Override
    public VisitorInfoVo queryVisitorInfo(Visitor paramVo) {
        VisitorParamVo visitorParamVo = sysBlackListMapper.queryAuditedVisitor(paramVo);
        if (visitorParamVo != null) {
            checkVisitor(visitorParamVo);
        }

        VisitorInfoVo visitorInfoVo = sysBlackListMapper.queryVisitorInfo(paramVo);
        if (visitorInfoVo != null) {
            Visitor infoVo = new Visitor();
            infoVo.setId(visitorInfoVo.getId());
            visitorInfoVo.setItemList(sysBlackListMapper.queryFollowUp(infoVo));
            String s = dictDataMapper.selectDictLabel("vis_info_reason", visitorInfoVo.getMatter());
            visitorInfoVo.setMatter(s);
            visitorInfoVo.setDeptName(dictDataMapper.selectDictLabel("sys_company_type", visitorInfoVo.getDeptId()));
        }


        return visitorInfoVo;
    }

    /**
     * 修改状态
     */
    @Override
    public int updateState(Visitor paramVo) {
        VisitorParamVo visitorParamVo = sysBlackListMapper.queryAuditedVisitor(paramVo);
        if (visitorParamVo != null) {
            checkVisitor(visitorParamVo);
        }

        VisitorInfoVo visitorInfo = sysBlackListMapper.queryVisitorType(paramVo);
        if ("2".equals(paramVo.getType()) && StringUtils.isEmpty(visitorInfo.getInTime())) {
            throw new ServiceException("您未签到");
        }
        if ("2".equals(paramVo.getType()) && "2".equals(visitorInfo.getType())) {
            throw new ServiceException("您已经签离");
        }
        if ("1".equals(paramVo.getType()) && "2".equals(visitorInfo.getType())) {
            throw new ServiceException("您已经签离");
        }

        List<VisitorInfoVo> visitorInfoVos = sysBlackListMapper.queryFollowUp(paramVo);
        // 修改同行人
        for (VisitorInfoVo visitorInfoVo : visitorInfoVos) {
            Visitor visitor = new Visitor();
            visitor.setId(visitorInfoVo.getId());
            if (Constants.ONE.equals(paramVo.getType())) {
                visitor.setInTime(new Date());
                visitor.setType("1");
            }
            if (Constants.TWO.equals(paramVo.getType())) {
                visitor.setType("2");
                visitor.setOutTime(new Date());
            }
            visitorMapper.updateVisitor(visitor);
        }
        if ("1".equals(paramVo.getType())) {
            paramVo.setInTime(new Date());
        }
        if ("2".equals(paramVo.getType())) {
            paramVo.setOutTime(new Date());
        }
        return visitorMapper.updateVisitorInfo(paramVo);
    }

    /**
     * 查询访客预约数
     */
    @Override
    public int queryVisitorByCardCount(List<String> idcards) {
        return visitorMapper.queryVisitorByCardCount(idcards);
    }

    /**
     * 访客信息
     */
    @Override
    public PersonnelInfoVo queryVisitorInfoById(String id) {
        return visitorMapper.queryVisitorInfoById(id);
    }

    /**
     * 查询未签离列表
     */
    @Override
    public List<Visitor> queryVisitorUnsigned(VisitorParamVo paramVo) {
        startPage();
        return visitorMapper.queryVisitorUnsigned(paramVo);
    }

    @Override
    public int updateSignOut(Visitor paramVo) {
        List<VisitorInfoVo> visitorInfoVos = sysBlackListMapper.queryFollowUp(paramVo);
        Visitor visitorInfo = new Visitor();
        // 修改同行人
        for (VisitorInfoVo visitorInfoVo : visitorInfoVos) {
            Visitor visitor = new Visitor();
            visitor.setId(visitorInfoVo.getId());
            if (Constants.ONE.equals(paramVo.getType())) {
                visitor.setInTime(new Date());
            }
            if (Constants.TWO.equals(paramVo.getType())) {
                visitor.setOutTime(new Date());
            }
            visitorMapper.updateVisitor(visitor);
        }
        if ("1".equals(paramVo.getType())) {
            visitorInfo.setInTime(new Date());
        }
        if ("2".equals(paramVo.getType())) {
            visitorInfo.setOutTime(new Date());
        }
        visitorInfo.setId(paramVo.getId());
        return visitorMapper.updateVisitor(visitorInfo);
    }

    @Override
    public void saveVisCheckCode(CheckCodeVo codeVo) {
        visitorMapper.saveVisCheckCode(codeVo);
    }

    @Override
    public CheckCodeVo queryVisCheckCodeById(String qrData) {
        return visitorMapper.queryVisCheckCodeById(qrData);
    }

    @Override
    public String queryVisCheckCode(String visitorId) {
        return visitorMapper.queryVisCheckCode(visitorId);
    }

    @Override
    public Visitor selectVisitorInfo(SysPeople people) {
        return visitorMapper.selectVisitorInfo(people);
    }

    /**
     * 查询被访人授权记录
     *
     * @param userId
     * @return
     */
    @Override
    public List<SysManageRecord> querySysManageRecord(Long userId) {
        return visitorMapper.querySysManageRecord(userId);
    }

    /**
     * 保存访客guid
     */
    @Override
    public void updateVisitorInfoGuid(List<Visitor> listVisitorId) {
        visitorMapper.updateVisitorInfoGuid(listVisitorId);
    }

    @Override
    public Visitor queryVisitorGuid(String guid) {
        return visitorMapper.queryVisitorGuid(guid);
    }

    /**
     * 查询是否进入
     *
     * @param id
     */
    @Override
    public int queryVisitorRecord(Long id) {
        return visitorMapper.queryVisitorRecord(id);
    }

    /**
     * 更新离开时间
     *
     * @param record
     */
    @Override
    public void updateVisitorRecord(VisitorRecordVo record) {
        visitorMapper.updateVisitorRecord(record);
    }

    /**
     * 新增访客识别记录
     *
     * @param record
     */
    @Override
    public void saveVisitorRecord(VisitorRecordVo record) {
        visitorMapper.saveVisitorRecord(record);
    }

    /**
     * 查询访客记录列表
     *
     * @param visitor
     * @return
     */
    @Override
    public List<VisitorRecordVo> queryVisitorRecordList(VisitorRecordVo visitor) {
        return visitorMapper.queryVisitorRecordList(visitor);
    }

    /**
     * 删除访客记录
     *
     * @param ids
     */
    @Override
    public void deleteVisitorRecordList(String[] ids) {
        visitorMapper.deleteVisitorRecordList(ids);
    }

    @Override
    public List<SysBranch> querySysBranch(String deptType) {
        return visitorMapper.querySysBranch(deptType);
    }

    /**
     * 查询访客数量（今日，本周，本月）
     *
     * @param recordVo
     * @return
     */
    @Override
    public VisitorCountVo queryVisitorCount(VisitorRecordVo recordVo) {
        VisitorCountVo visitorCountVo = new VisitorCountVo();
        recordVo.setType(systemType);
        int dayCount = visitorMapper.queryVisitorDayCount(recordVo);
        int weekCount = visitorMapper.queryVisitorWeekCount(recordVo);
        int monthCount = visitorMapper.queryVisitorMonthCount(recordVo);
        visitorCountVo.setDayCount(dayCount);
        visitorCountVo.setWeekCount(weekCount);
        visitorCountVo.setMonthCount(monthCount);
        return visitorCountVo;
    }

    /**
     * 首页头像展示
     *
     * @param recordVo
     * @return
     */
    @Override
    public List<VisitorRecordVo> queryVisitorAvatar(VisitorRecordVo recordVo) {
        return visitorMapper.queryVisitorAvatar(recordVo);
    }

    /**
     * 通过手机号查询访客id
     *
     * @param phone
     * @return
     */
    @Override
    public VisitorRecordVo queryVisitorById(String phone) {
        return visitorMapper.queryVisitorById(phone);
    }

    @Override
    public int queryVisitorByIdCount(List<String> phones) {
        return visitorMapper.queryVisitorByIdCount(phones);
    }

    /**
     * 查询近七天的访客数据
     *
     * @param paramVo
     * @return
     */
    @Override
    public List<Visitor> queryExportVisitorInfo(VisitorParamVo paramVo) {
        return visitorMapper.queryExportVisitorInfo(paramVo);
    }


    @Override
    public String queryTest(VisitorRecordVo recordVo) {
        List<String> list = visitorMapper.queryBranch();
        for (String s : list) {
            String branch = visitorMapper.queryBranch1(s);
            System.out.println(branch);
        }
        return null;
    }

    /**
     * 访客校验
     */
    private void checkVisitor(VisitorParamVo visitorParamVo) {
//        if(visitorParamVo == null) {
//            throw new ServiceException("未查询到预约信息");
//        }
        if (Constants.ZERO.equals(visitorParamVo.getReviewStatus())) {
            throw new ServiceException("您未审核");
        }
        if (Constants.TWO.equals(visitorParamVo.getReviewStatus())) {
            throw new ServiceException("您审核未通过");
        }

        if (visitorParamVo.getEndTime().getTime() < new Date().getTime()) {
            throw new ServiceException("您得预约时间已超时");
        }

    }

    /**
     * 保存访客机断线数据
     *
     * @param visitor
     */
    @Override
    @Transactional
    public void saveDisconnect(List<Visitor> visitor) {
        for (Visitor visitor1 : visitor) {
            log.info("保存断线数据：" + visitor1);
            if (visitorMapper.queryVisitorByCardCount(Collections.singletonList(visitor1.getIdcard())) > 0) {
                MultipartFile file = PictureUtils.base64ToMultipartFiles(visitor1.getAvatar());
                R<SysFile> sysFileR = fileService.uploadMinio1(file, "true");
                visitor1.setAvatar(sysFileR.getData().getUrl());
                visitorMapper.updateDisconnect(visitor1);
            } else {
                MultipartFile file = PictureUtils.base64ToMultipartFiles(visitor1.getAvatar());
                R<SysFile> sysFileR = fileService.uploadMinio1(file, "true");
                visitor1.setCreateTime(visitor1.getCreateTime());
                visitor1.setStartTime(visitor1.getCreateTime());
                visitor1.setSource(Constants.ZERO);
                visitor1.setAvatar(sysFileR.getData().getUrl());
                visitor1.setState(Constants.ONE);
                visitor1.setType(visitor1.getOutTime() == null ? Constants.ONE : Constants.TWO);
                visitorMapper.insertVisitor(visitor1);
            }
            List<Visitor> itemList = visitor1.getItemList();
            // 随访人员
            if (!CollectionUtils.isEmpty(itemList)) {
                for (Visitor item : itemList) {
                    log.info("保存断线随访人数据：" + item);
                    if (visitorMapper.queryVisitorByCardCount(Collections.singletonList(item.getIdcard())) > 0) {
                        MultipartFile file = PictureUtils.base64ToMultipartFiles(item.getAvatar());
                        R<SysFile> sysFileR = fileService.uploadMinio1(file, "true");
                        item.setAvatar(sysFileR.getData().getUrl());
                        visitorMapper.updateDisconnect(item);
                    } else {
                        if (StringUtils.isNotEmpty(item.getAvatar())) {
                            MultipartFile multipartFile = PictureUtils.base64ToMultipartFiles(item.getAvatar());
                            R<SysFile> uploadMinio = fileService.uploadMinio1(multipartFile, "true");
                            item.setAvatar(uploadMinio.getData().getUrl());
                        }
                        item.setParentId(visitor1.getId());
                        item.setSource(Constants.ZERO);
                        item.setUserId(item.getUserId());
                        item.setDeptId(item.getDeptId());
                        item.setCreateTime(visitor1.getCreateTime());
                        item.setStartTime(visitor1.getStartTime());
                        item.setEndTime(visitor1.getEndTime());
                        item.setState(Constants.ONE);
                        item.setType(item.getOutTime() == null ? Constants.ONE : Constants.TWO);
                        visitorMapper.insertVisitor(item);
                    }
                }
            }
        }
    }


    /**
     * 审批
     *
     * @param visitor
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void approve(Visitor visitor) {

        //保存审核记录表
        visitorMapper.saveVisitorAuditRecords(visitor);

        // 审核同意
        if (Constants.ZERO.equals(visitor.getExamineState())) {
            // 保存审核二维码地址信息
            CheckCodeVo codeVo = new CheckCodeVo();
            codeVo.setCode(qdUrl + visitor.getId() + "&time=" + new Date().getTime());
            codeVo.setVisitorId(String.valueOf(visitor.getId()));
            String replace = UUID.randomUUID().toString().replace("-", "");
            codeVo.setId(replace);
            if (StringUtils.isNotEmpty(visitor.getCheckCodeId())) {
                codeVo.setId(visitor.getCheckCodeId());
            }

            if (visitor != null) {
                codeVo.setStartTime(visitor.getStartTime());
                codeVo.setEndTime(visitor.getEndTime());
            }
            saveVisCheckCode(codeVo);
        }

        //不修改userId信息
        visitor.setUserId(null);
        visitorMapper.updateVisitor(visitor);

        //修改随访人员信息
        Visitor updateVisitor = new Visitor();
        updateVisitor.setParentId(visitor.getId());
        updateVisitor.setExamineState(visitor.getExamineState());
        visitorMapper.updateVisitorByParentId(updateVisitor);

    }

    /**
     * 保存审核记录
     *
     * @param visitor
     */
    @Override
    public void saveVisitorAuditRecords(Visitor visitor) {
        visitorMapper.saveVisitorAuditRecords(visitor);
    }

    /**
     * 查询物业子部门访客审核人员
     *
     * @param deptId
     * @return
     */
    @Override
    public List<SysPeople> queryPropertyExaminePeople(String deptId) {
        return visitorMapper.queryPropertyExaminePeople(deptId);
    }

    @Override
    public void deleteFile(SysFileVo sysFile) {
        visitorMapper.deleteFile(sysFile);
    }

    @Override
    public void saveFile(List<SysFileVo> sysFiles) {
        visitorMapper.saveFile(sysFiles);
    }

    @Override
    public void saveVisitorReviewProcess(Visitor visitor) {
        visitorMapper.saveVisitorReviewProcess(visitor);
    }

    @Override
    public SysPeople queryVisitorReviewProcess(Long id) {
        return visitorMapper.queryVisitorReviewProcess(id);
    }

    @Override
    public void updateVisitorReviewProcess(Visitor visitor) {
        visitorMapper.updateVisitorReviewProcess(visitor);
    }

    @Override
    public VisitorReviewProcessVo queryReviewProcess(VisitorReviewProcessVo visitor) {
        return visitorMapper.queryReviewProcess(visitor);
    }

    @Override
    public int queryVisitorReviewProcessCount(Long id) {
        return visitorMapper.queryVisitorReviewProcessCount(id);
    }


    /**
     * 查询访客信息列表
     *
     * @param visitor 访客信息
     * @return 访客信息
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<Visitor> selectAppVisitorList(Visitor visitor) {
        //查询我作为随访人员的申请信息主键ids
        List<Long> ids = visitorMapper.selectAppVisitorFollow(visitor);

        //查询我申请的访客信息
        visitor.setIds(ids);
        List<Visitor> visitors = visitorMapper.selectAppVisitorList(visitor);

        List<Long> collect = visitors.stream().map(Visitor::getId).collect(Collectors.toList());
        if(CollUtil.isNotEmpty(collect)) {
            List<CheckCodeVo> checkCodeVos = visitorMapper.queryCode(collect);
            visitors.forEach(visit -> {
                checkCodeVos.forEach(code -> {
                    if (StringUtils.equals(String.valueOf(visit.getId()), code.getVisitorId())) {
                        visit.setQrCode(code.getCode());
                    }
                });
            });
        }
        return visitors;
    }


    @Override
    public Visitor selectVisitorSimpleById(Long id) {
        Visitor visitor = visitorMapper.selectVisitorById(id);
        if (visitor == null) {
            return new Visitor();
        }
        return visitor;
    }

    @Override
    public String selectRecordsByPhone(Long id, String telephone) {
        return visitorMapper.selectRecordsByPhone(id, telephone);
    }

    @Override
    public void saveVisitorReviewProcessList(List<Visitor> list) {
        visitorMapper.saveVisitorReviewProcessList(list);
    }

    /**
     * 查询访客预约数
     */
    @Override
    public int queryVisitor(Visitor visitor) {
        return visitorMapper.queryVisitor(visitor);
    }

    @Override
    public int queryVisitorByIdCountOld(String phone) {
        return visitorMapper.queryVisitorByIdCountOld(phone);
    }

    @Override
    public String queryReviewProcessOld(Visitor visitor) {
        return visitorMapper.queryReviewProcessOld(visitor);
    }

    @Override
    public void cancel(Visitor visitor) {
        Visitor visitor1 = visitorMapper.selectVisitorById(visitor.getId());
        if (visitor1 != null && !StringUtils.equals(visitor1.getExamineState(), ExamineEnum.IN_REVIEW.getCode())) {
            throw new ServiceException("审核中，无法取消");
        }

        //修改访客申请主表信息
        Visitor updateVisitor = new Visitor();
        updateVisitor.setId(visitor.getId());
        updateVisitor.setExamineState("4");
        visitorMapper.updateVisitor(updateVisitor);

        updateVisitor.setParentId(visitor.getId());
        //修改随访人员信息
        visitorMapper.updateVisitorByParentId(updateVisitor);

        //删除流程环节信息
        visitorMapper.deleteVisitorReviewProcess(visitor.getId());
    }

    @Override
    public List<String> addVisitorCheck(List<String> phones, Date startTime) {
        return visitorMapper.addVisitorCheck(phones, startTime);
    }

    @Override
    public List<Visitor> queryReviewList(Long peopleId) {
        return visitorMapper.queryReviewList(peopleId);
    }

}
