package com.template.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.template.common.utils.TimeExchange;
import com.template.mapper.SmartVisitorMapper;
import com.template.model.pojo.SmartVisitor;
import com.template.model.pojo.SmartVisitor;
import com.template.mapper.SmartVisitorMapper;
import com.template.model.result.PageUtils;
import com.template.model.vo.*;
import com.template.services.SmartVisitorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2023-12-04
 */
@Service
public class SmartVisitorServiceImpl extends ServiceImpl<SmartVisitorMapper, SmartVisitor> implements SmartVisitorService {
    @Autowired
    private SmartVisitorMapper smartVisitorMapper;

    @Override
    public int insertSmartVisitor(SmartVisitor sa) {
        int result = smartVisitorMapper.insert(sa);
        return result;
    }

    @Override
    public int updateSmartVisitor(SmartVisitor sa) {
        int result = smartVisitorMapper.updateById(sa);
        return result;
    }

    @Override
    public PageUtils<SmartVisitor> queryPageSmartVisitors(int currentPage, int pageCount, String name) {
        Page<SmartVisitor> page = new Page<>(currentPage, pageCount);
        QueryWrapper<SmartVisitor> queryWrapper = new QueryWrapper<>();
        //queryWrapper.like(StringUtils.hasText(name), "name", name);
        IPage<SmartVisitor> result = smartVisitorMapper.selectPage(page, queryWrapper);
        return new PageUtils<>(result);
    }

    @Override
    public int deleteSmartVisitorById(int id) {
        int result = smartVisitorMapper.deleteById(id);
        return result;
    }

    @Override
    public SmartVisitor getSmartById(int id) {
        SmartVisitor result = smartVisitorMapper.selectById(id);
        return result;
    }

    @Override
    public boolean insertVisitorBatch(List<SmartVisitor> visitors) {
        boolean result = this.saveBatch(visitors);
        return result;
    }

    @Override
    public PageUtils<VisitorPageListVo> smartVisitorPageByUserId(int currentPage, int pageCount, Integer userId, Integer statu, String createStartTime, String createEndTime, String keyWord, String visitorStartTime, String visitorEndTime) {
        Page<VisitorPageVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<VisitorPageVo> datas = smartVisitorMapper.queryVisitorPage(page, userId, statu, createStartTime, createEndTime, keyWord, visitorStartTime, visitorEndTime);

        List<VisitorPageVo> list = datas.getRecords();
        ArrayList<VisitorPageListVo> smartVisitorVos = new ArrayList<>();
        for (VisitorPageVo smartVisitor : list) {
            VisitorPageListVo smartVisitorVo = new VisitorPageListVo();
            BeanUtils.copyProperties(smartVisitor,smartVisitorVo);
            String image = smartVisitor.getImage();
            if (ObjectUtils.isNotEmpty(image)) {
                String[] split = image.split(",");
                ArrayList<String> strings = new ArrayList<>();
                for (int i = 0; i < split.length; i++) {
                    String s = split[i];
                    strings.add(s);
                }
                smartVisitorVo.setImage(strings);
            }
            smartVisitorVos.add(smartVisitorVo);
        }

        IPage<VisitorPageListVo> voIPage=new Page<>();
        BeanUtils.copyProperties(datas ,voIPage);
        voIPage.setRecords(smartVisitorVos);


        return new PageUtils(voIPage);
    }

    @Override
    public Integer queryVisitorCount(Integer userId, String startTime, String endTime) {
        Integer result = smartVisitorMapper.queryVisitorCount(userId, startTime, endTime);
        return result;
    }

    @Override
    public PageUtils<SmartVisitor> queryVisitorPageDatas(int currentPage, int pageCount, Integer type, List<Integer> studentIds, String isAdmin) {
        Page<SmartVisitor> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<SmartVisitor> datas = smartVisitorMapper.queryVisitorPageDatas(page, type, studentIds, isAdmin);
        return new PageUtils(datas);
    }


    @Override
    public SmartVisitor querySmartVisitorByCode(String visitorCode) {
        QueryWrapper<SmartVisitor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(visitorCode), "visitor_code", visitorCode);
        SmartVisitor result = smartVisitorMapper.selectOne(queryWrapper);
        return result;
    }

    @Override
    public SmartVisitor queryParentVisitor(Integer userId) {
        QueryWrapper<SmartVisitor> queryWrapper = new QueryWrapper<>();
        String nowTime = TimeExchange.getTime();
        queryWrapper.le("visitor_time", nowTime);//<=
        queryWrapper.ge("visitor_deadline", nowTime);//>=
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("statu",3);//必须是已推送的
        queryWrapper.orderByAsc("visitor_time");
        List<SmartVisitor> resultDatas = smartVisitorMapper.selectList(queryWrapper);

        return resultDatas != null && resultDatas.size() > 0 ? resultDatas.get(0) : null;
    }

    @Override
    public PageUtils<SmartVisitorVo> getPage(int currentPage, int pageCount, Integer type, String keyWord, Integer status, String startTime, String endTime, String visitorStartTime, String visitorEndTime) {
        Page<SmartVisitorPageVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<SmartVisitorPageVo> datas = smartVisitorMapper.getPage(page,type,keyWord,status,startTime,endTime,visitorStartTime,visitorEndTime);

        List<SmartVisitorPageVo> list = datas.getRecords();
        ArrayList<SmartVisitorVo> smartVisitorVos = new ArrayList<>();
        for (SmartVisitorPageVo smartVisitor : list) {
            SmartVisitorVo smartVisitorVo = new SmartVisitorVo();
            BeanUtils.copyProperties(smartVisitor,smartVisitorVo);
            String image = smartVisitor.getImage();
            if (ObjectUtils.isNotEmpty(image)) {
                String[] split = image.split(",");
                ArrayList<String> strings = new ArrayList<>();
                for (int i = 0; i < split.length; i++) {
                    String s = split[i];
                    strings.add(s);
                }
                smartVisitorVo.setImage(strings);
            }
            smartVisitorVos.add(smartVisitorVo);
        }

        IPage<SmartVisitorVo> voIPage=new Page<>();
        BeanUtils.copyProperties(datas ,voIPage);
        voIPage.setRecords(smartVisitorVos);

        return new PageUtils(voIPage);
    }

    @Override
    public PageUtils<SmartVisitorScreenListVo> getScreenPage(int currentPage, int pageCount, String phone, String status) {
        Page<SmartVisitorScreenVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<SmartVisitorScreenVo> datas = smartVisitorMapper.getScreenPage(page,phone,status);

        List<SmartVisitorScreenVo> list = datas.getRecords();
        ArrayList<SmartVisitorScreenListVo> smartVisitorVos = new ArrayList<>();
        for (SmartVisitorScreenVo smartVisitor : list) {
            SmartVisitorScreenListVo smartVisitorVo = new SmartVisitorScreenListVo();
            BeanUtils.copyProperties(smartVisitor,smartVisitorVo);
            String image = smartVisitor.getImage();
            if (ObjectUtils.isNotEmpty(image)) {
                String[] split = image.split(",");
                ArrayList<String> strings = new ArrayList<>();
                for (int i = 0; i < split.length; i++) {
                    String s = split[i];
                    strings.add(s);
                }
                smartVisitorVo.setImage(strings);
            }
            smartVisitorVos.add(smartVisitorVo);
        }

        IPage<SmartVisitorScreenListVo> voIPage=new Page<>();
        BeanUtils.copyProperties(datas ,voIPage);
        voIPage.setRecords(smartVisitorVos);


        return new PageUtils(voIPage);
    }

    @Override
    public PageUtils<SmartVisitorScreenListVo> parentsScreenPage(int currentPage, int pageCount, int userId, String status) {
        Page<SmartVisitorScreenVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<SmartVisitorScreenVo> datas = smartVisitorMapper.parentsScreenPage(page,userId,status);

        List<SmartVisitorScreenVo> list = datas.getRecords();
        ArrayList<SmartVisitorScreenListVo> smartVisitorVos = new ArrayList<>();
        for (SmartVisitorScreenVo smartVisitor : list) {
            SmartVisitorScreenListVo smartVisitorVo = new SmartVisitorScreenListVo();
            BeanUtils.copyProperties(smartVisitor,smartVisitorVo);
            String image = smartVisitor.getImage();
            if (ObjectUtils.isNotEmpty(image)) {
                String[] split = image.split(",");
                ArrayList<String> strings = new ArrayList<>();
                for (int i = 0; i < split.length; i++) {
                    String s = split[i];
                    strings.add(s);
                }
                smartVisitorVo.setImage(strings);
            }
            smartVisitorVos.add(smartVisitorVo);
        }

        IPage<SmartVisitorScreenListVo> voIPage=new Page<>();
        BeanUtils.copyProperties(datas ,voIPage);
        voIPage.setRecords(smartVisitorVos);


        return new PageUtils(voIPage);
    }
}
