package com.ruoyi.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.demo.entity.DlUser;
import com.ruoyi.demo.entity.bo.PlanBo;
import com.ruoyi.demo.entity.vo.DlUserListVo;
import com.ruoyi.demo.mapper.DlUserMapper;
import com.ruoyi.demo.service.DlUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class DlUserServiceImpl implements DlUserService {
    @Autowired
    DlUserMapper dlUserMapper;

    @Override
    public PageInfo<List<DlUserListVo>> getList(PlanBo bo) {
        PageHelper.startPage(bo.getPageNum(),bo.getPageSize(),true);
        QueryWrapper<DlUser> queryWrapper = new QueryWrapper<>();
        if (bo.getText() != null)
            queryWrapper.and(dlUserQueryWrapper -> {
                dlUserQueryWrapper.like("user_id",bo.getText())
                    .or()
                    .like("user_name",bo.getText())
                    .or()
                    .like("user_telephone",bo.getText());
            });
        if (bo.getStartTime() != null)
            queryWrapper.apply("end_time >= STR_TO_DATE({0},'%Y-%m-%d %H:%i:%s')",bo.getStartTime());
        if (bo.getEndTime() != null)
            queryWrapper.apply("end_time <= STR_TO_DATE({0},'%Y-%m-%d %H:%i:%s')",bo.getEndTime());
        queryWrapper.eq("deleted","0");
        List<DlUser> dlUsers = dlUserMapper.selectList(queryWrapper);
        List<DlUserListVo> vos = new ArrayList<>();
        for (DlUser user : dlUsers){
            vos.add(new DlUserListVo(user));
        }
        return new PageInfo(vos);
    }
}
