package com.zhongzheng.modules.top.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.core.domain.entity.TopSysUser;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.course.domain.MajorCategory;
import com.zhongzheng.modules.top.bo.TopSysUserAddBo;
import com.zhongzheng.modules.top.bo.TopSysUserEditBo;
import com.zhongzheng.modules.top.bo.TopSysUserQueryBo;
import com.zhongzheng.modules.top.mapper.TopSysUserMapper;
import com.zhongzheng.modules.top.service.ITopSysUserService;
import com.zhongzheng.modules.top.vo.TopSysUserVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * SAAS用户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-04
 */
@Service
public class TopSysUserServiceImpl extends ServiceImpl<TopSysUserMapper, TopSysUser> implements ITopSysUserService {

    @Override
    public TopSysUserVo queryById(Long userId){
        TopSysUser db = this.baseMapper.selectById(userId);
        return BeanUtil.toBean(db, TopSysUserVo.class);
    }

    @Override
    public List<TopSysUserVo> queryList(TopSysUserQueryBo bo) {
        LambdaQueryWrapper<TopSysUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, TopSysUser::getDeptId, bo.getDeptId());
        lqw.like(StrUtil.isNotBlank(bo.getUserName()), TopSysUser::getUserName, bo.getUserName());
        lqw.like(StrUtil.isNotBlank(bo.getNickName()), TopSysUser::getNickName, bo.getNickName());
        lqw.eq(StrUtil.isNotBlank(bo.getEmail()), TopSysUser::getEmail, bo.getEmail());
        lqw.eq(StrUtil.isNotBlank(bo.getPhonenumber()), TopSysUser::getPhonenumber, bo.getPhonenumber());
        lqw.eq(StrUtil.isNotBlank(bo.getSex()), TopSysUser::getSex, bo.getSex());
        lqw.eq(StrUtil.isNotBlank(bo.getAvatar()), TopSysUser::getAvatar, bo.getAvatar());
        lqw.eq(StrUtil.isNotBlank(bo.getPassword()), TopSysUser::getPassword, bo.getPassword());
        lqw.eq(StrUtil.isNotBlank(bo.getStatus()), TopSysUser::getStatus, bo.getStatus());
        lqw.eq(StrUtil.isNotBlank(bo.getLoginIp()), TopSysUser::getLoginIp, bo.getLoginIp());
        lqw.eq(bo.getLoginDate() != null, TopSysUser::getLoginDate, bo.getLoginDate());
        return entity2Vo(this.list(lqw));
    }

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<TopSysUserVo> entity2Vo(Collection<TopSysUser> collection) {
        List<TopSysUserVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, TopSysUserVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<TopSysUser> page = (Page<TopSysUser>)collection;
            Page<TopSysUserVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(TopSysUserAddBo bo) {
        TopSysUser add = BeanUtil.toBean(bo, TopSysUser.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public TopSysUser selectUserByUserName(String userName) {
        return baseMapper.selectUserByUserName(userName);
    }

    @Override
    public Boolean updateByEditBo(TopSysUserEditBo bo) {
        TopSysUser update = BeanUtil.toBean(bo, TopSysUser.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TopSysUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }


}
