package org.dromara.talk.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 org.springframework.stereotype.Service;
import org.dromara.talk.domain.bo.PhoneUserBo;
import org.dromara.talk.domain.vo.PhoneUserVo;
import org.dromara.talk.domain.PhoneUser;
import org.dromara.talk.mapper.PhoneUserMapper;
import org.dromara.talk.service.IPhoneUserService;

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

/**
 * ZoomPhone用户Service业务层处理
 *
 * @author Lion Li
 *  2026-02-06
 */
@RequiredArgsConstructor
@Service
public class PhoneUserServiceImpl implements IPhoneUserService {

    private final PhoneUserMapper baseMapper;

    /**
     * 查询ZoomPhone用户
     */
    @Override
    public PhoneUserVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询ZoomPhone用户列表
     */
    @Override
    public TableDataInfo<PhoneUserVo> queryPageList(PhoneUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PhoneUser> lqw = buildQueryWrapper(bo);
        Page<PhoneUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询ZoomPhone用户列表
     */
    @Override
    public List<PhoneUserVo> queryList(PhoneUserBo bo) {
        LambdaQueryWrapper<PhoneUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PhoneUser> buildQueryWrapper(PhoneUserBo bo) {
        LambdaQueryWrapper<PhoneUser> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCustomerPhone()), PhoneUser::getCustomerPhone, bo.getCustomerPhone());
        return lqw;
    }

    /**
     * 新增ZoomPhone用户
     */
    @Override
    public Boolean insertByBo(PhoneUserBo bo) {
        PhoneUser add = MapstructUtils.convert(bo, PhoneUser.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改ZoomPhone用户
     */
    @Override
    public Boolean updateByBo(PhoneUserBo bo) {
        PhoneUser update = MapstructUtils.convert(bo, PhoneUser.class);
        return baseMapper.updateById(update) > 0;
    }


    /**
     * 批量删除ZoomPhone用户
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        return baseMapper.deleteByIds(ids) > 0;
    }
}
