package com.ruoyi.info.operating.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.info.operating.domain.OperatingPhone;
import com.ruoyi.info.operating.domain.bo.OperatingPhoneBo;
import com.ruoyi.info.operating.domain.vo.OperatingPhoneVo;
import com.ruoyi.info.operating.exception.OperatingPhoneExceptionEnum;
import com.ruoyi.info.operating.mapper.OperatingPhoneMapper;
import com.ruoyi.info.operating.service.IOperatingPhoneService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;

/**
 * 服务电话Service业务层处理
 *
 * @author baifc
 * @date 2025-09-28
 */
@RequiredArgsConstructor
@Service
public class OperatingPhoneServiceImpl implements IOperatingPhoneService {

    private final OperatingPhoneMapper baseMapper;

    /**
     * 查询服务电话分页
     *
     * @param bo 服务电话
     * @return 服务电话
     */
    @Override
    public TableDataInfo<OperatingPhoneVo> queryPageList(OperatingPhoneBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OperatingPhone> lqw = buildQueryWrapper(bo);
        Page<OperatingPhoneVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询服务电话列表
     *
     * @param bo 服务电话
     * @return 服务电话
     */
    @Override
    public List<OperatingPhoneVo> queryList(OperatingPhoneBo bo) {
        LambdaQueryWrapper<OperatingPhone> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OperatingPhone> buildQueryWrapper(OperatingPhoneBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OperatingPhone> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getPhoneNumber()), OperatingPhone::getPhoneNumber, bo.getPhoneNumber());
        return lqw;
    }

    /**
     * 查询服务电话
     *
     * @param id 服务电话主键
     * @return 服务电话
     */
    @Override
    public OperatingPhoneVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 详情服务电话
     *
     * @param id 服务电话主键
     * @return 服务电话
     */
    @Override
    public OperatingPhone loadById(Long id, Boolean tw){
        OperatingPhone info = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(info) && tw){
            throw new ServiceException(OperatingPhoneExceptionEnum.OperatingPhone_IS_NOT_EXISTS);
        }
        return info;
    }

    /**
     * 新增服务电话
     *
     * @param bo 服务电话
     * @return 结果
     */
    @Override
    public Boolean insertByBo(OperatingPhoneBo bo) {
        OperatingPhone add = BeanUtil.toBean(bo, OperatingPhone.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改服务电话
     *
     * @param bo 服务电话
     * @return 结果
     */
    @Override
    public Boolean updateByBo(OperatingPhoneBo bo) {
        OperatingPhone operatingPhone = baseMapper.selectById(bo.getId());
        OperatingPhone update = BeanCopyUtils.copy(bo, operatingPhone);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除服务电话
     *
     * @param ids 需要删除的服务电话主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 批量编辑服务电话
     *
     * @param boList
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchEdit(List<OperatingPhoneBo> boList) {

        // 删除
        List<OperatingPhone> operatingPhoneList = baseMapper.selectList();
        Set<Long> ids = operatingPhoneList.stream().map(OperatingPhone::getId).collect(Collectors.toSet());
        baseMapper.deleteBatchIds(ids);

        // 新增
        List<OperatingPhone> saveList = BeanCopyUtils.copyList(boList, OperatingPhone.class);
        baseMapper.insertBatch(saveList);
    }

}
