package top.continew.admin.business.service.Impl;

import cn.dev33.satoken.stp.SaLoginConfig;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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 org.springframework.stereotype.Service;
import top.continew.admin.business.mapper.TgUserMapper;
import top.continew.admin.business.model.entity.TgUser;
import top.continew.admin.business.model.req.TgUserReq;
import top.continew.admin.business.service.ITgUserService;
import top.continew.admin.common.context.UserContext;
import top.continew.admin.common.context.UserContextHolder;
import top.continew.admin.common.context.UserExtraContext;
import top.continew.starter.data.mp.base.BaseMapper;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.web.util.SpringWebUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * tg用户 服务实现类
 * </p>
 *
 * @author xudm
 * @since 2024-12-14
 */
@Service
public class TgUserServiceImpl extends ServiceImpl<TgUserMapper, TgUser> implements ITgUserService {
    @Override
    public PageResp<TgUser> pageQuery(TgUserReq queryVo) {
        Page<TgUser> page = new Page<>();
        page.setCurrent(queryVo.getPage() != 0 ? queryVo.getPage() : 1);
        page.setSize(queryVo.getSize() != 0 ? queryVo.getSize() : 10);

        LambdaQueryWrapper<TgUser> queryWrapper = new LambdaQueryWrapper<>();
        String userName = queryVo.getUserName();
        if (StrUtil.isNotBlank(userName)) {
            queryWrapper.and(w -> {
                w.like(StrUtil.isNotBlank(userName), TgUser::getFirstName, userName)
                        .or()
                        .like(StrUtil.isNotBlank(userName), TgUser::getLastName, userName)
                        .or()
                        .like(StrUtil.isNotBlank(userName), TgUser::getTgAccount, userName);
            });
        }
        // 处理时间范围查询
        try {
            List<Date> times = queryVo.getTime();
            if (CollUtil.isNotEmpty(times) && times.size() == 2) {
                Date startime = times.get(0);
                Date endtime = times.get(1);

                LocalDateTime startDateTime = LocalDateTime.ofInstant(startime.toInstant(), ZoneId.systemDefault());
                queryWrapper.ge(TgUser::getCreatedTime, startDateTime);

                LocalDateTime endDateTime = LocalDateTime.ofInstant(endtime.toInstant(), ZoneId.systemDefault());
                queryWrapper.le(TgUser::getCreatedTime, endDateTime);
            }

        } catch (DateTimeParseException e) {
            throw new RuntimeException("日期格式错误，正确格式为：yyyy-MM-dd HH:mm:ss");
        }

        queryWrapper.orderByDesc(TgUser::getCreatedTime);
        page = page(page, queryWrapper);
        PageResp<TgUser> pageResp = PageResp.build(page, TgUser.class);
        return pageResp;
    }
}
