package com.younike.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.younike.common.core.page.PageParam;
import com.younike.common.id.IDUtils;
import com.younike.common.utils.string.StringUtils;
import com.younike.service.mapper.TAppUserMapper;
import com.younike.service.pojo.TAppUser;
import com.younike.service.service.ITAppUserService;
import org.springframework.stereotype.Service;

/**
 * app用户Service业务层处理
 *
 * @author jiay
 * @date 2022-02-25
 */
@Service
public class TAppUserServiceImpl extends ServiceImpl<TAppUserMapper, TAppUser> implements ITAppUserService {

    /**
     * 自定义分页逻辑 这里就省略掉
     * @param pageParam
     * @return
     */
    @Override
    public PageParam<TAppUser> page(PageParam<TAppUser> pageParam) {

        PageParam<TAppUser> page = new PageParam<>();
        if (StringUtils.isNotEmpty(pageParam.getParam().getPhone())
                && StringUtils.isNotEmpty(pageParam.getParam().getNikeName())) {// 都不为空
            page = this.page(pageParam, new LambdaQueryWrapper<TAppUser>()
                    .like(TAppUser::getPhone, pageParam.getParam().getPhone())
                    .like(TAppUser::getNikeName, pageParam.getParam().getNikeName()));
        }else if (StringUtils.isNotEmpty(pageParam.getParam().getPhone())) {//手机号不为空
            page = this.page(pageParam, new LambdaQueryWrapper<TAppUser>()
                    .like(TAppUser::getPhone, pageParam.getParam().getPhone()));
        } else if (StringUtils.isNotEmpty(pageParam.getParam().getNikeName())) {//昵称不为空
            page = this.page(pageParam, new LambdaQueryWrapper<TAppUser>()
                    .like(TAppUser::getNikeName, pageParam.getParam().getNikeName()));
        } else {// 都为空
            page = this.page(pageParam, new LambdaQueryWrapper<TAppUser>());
        }

        return page;
    }

    @Override
    public boolean addUser(TAppUser user) {
        // 新增用户
        user.setId(IDUtils.nextId());
        if(!this.save(user)){
            return false;
        }
        return true;
    }

}
