package org.dromara.client.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.client.domain.bo.TUserBo;
import org.dromara.client.domain.vo.TUserVo;
import org.dromara.client.domain.TUser;
import org.dromara.client.mapper.TUserMapper;
import org.dromara.client.service.ITUserService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 客户用户基础信息Service业务层处理
 *
 * @author ZhouWenTao
 * @date 2026-03-22
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TUserServiceImpl implements ITUserService {

    private final TUserMapper baseMapper;

    /**
     * 查询客户用户基础信息
     *
     * @param id 主键
     * @return 客户用户基础信息
     */
    @Override
    public TUserVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询客户用户基础信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 客户用户基础信息分页列表
     */
    @Override
    public TableDataInfo<TUserVo> queryPageList(TUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TUser> lqw = buildQueryWrapper(bo);
        Page<TUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的客户用户基础信息列表
     *
     * @param bo 查询条件
     * @return 客户用户基础信息列表
     */
    @Override
    public List<TUserVo> queryList(TUserBo bo) {
        LambdaQueryWrapper<TUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TUser> buildQueryWrapper(TUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TUser> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(TUser::getId);
        lqw.like(StringUtils.isNotBlank(bo.getUsername()), TUser::getUsername, bo.getUsername());
        lqw.like(StringUtils.isNotBlank(bo.getNickname()), TUser::getNickname, bo.getNickname());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatarUrl()), TUser::getAvatarUrl, bo.getAvatarUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), TUser::getPhone, bo.getPhone());
        lqw.eq(bo.getGender() != null, TUser::getGender, bo.getGender());
        lqw.eq(bo.getStatus() != null, TUser::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getPassword()), TUser::getPassword, bo.getPassword());
        lqw.eq(StringUtils.isNotBlank(bo.getSalt()), TUser::getSalt, bo.getSalt());
        return lqw;
    }

    /**
     * 新增客户用户基础信息
     *
     * @param bo 客户用户基础信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TUserBo bo) {
        TUser add = MapstructUtils.convert(bo, TUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改客户用户基础信息
     *
     * @param bo 客户用户基础信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TUserBo bo) {
        TUser update = MapstructUtils.convert(bo, TUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除客户用户基础信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
