package com.zsElectric.boot.business.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsElectric.boot.business.mapper.RechargeLevelMapper;
import com.zsElectric.boot.business.service.RechargeLevelService;
import com.zsElectric.boot.business.model.entity.RechargeLevel;
import com.zsElectric.boot.business.model.form.RechargeLevelForm;
import com.zsElectric.boot.business.model.query.RechargeLevelQuery;
import com.zsElectric.boot.business.model.vo.RechargeLevelVO;
import com.zsElectric.boot.business.converter.RechargeLevelConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 充值档位信息服务实现类
 *
 * @author zsElectric
 * @since 2025-12-12 14:47
 */
@Service
@RequiredArgsConstructor
public class RechargeLevelServiceImpl extends ServiceImpl<RechargeLevelMapper, RechargeLevel> implements RechargeLevelService {

    private final RechargeLevelConverter rechargeLevelConverter;

    /**
    * 获取充值档位信息分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<RechargeLevelVO>} 充值档位信息分页列表
    */
    @Override
    public IPage<RechargeLevelVO> getRechargeLevelPage(RechargeLevelQuery queryParams) {
        Page<RechargeLevelVO> pageVO = this.baseMapper.getRechargeLevelPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取充值档位信息表单数据
     *
     * @param id 充值档位信息ID
     * @return 充值档位信息表单数据
     */
    @Override
    public RechargeLevelForm getRechargeLevelFormData(Long id) {
        RechargeLevel entity = this.getById(id);
        return rechargeLevelConverter.toForm(entity);
    }
    
    /**
     * 新增充值档位信息
     *
     * @param formData 充值档位信息表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveRechargeLevel(RechargeLevelForm formData) {
        RechargeLevel entity = rechargeLevelConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新充值档位信息
     *
     * @param id   充值档位信息ID
     * @param formData 充值档位信息表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateRechargeLevel(Long id,RechargeLevelForm formData) {
        RechargeLevel entity = rechargeLevelConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除充值档位信息
     *
     * @param ids 充值档位信息ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteRechargeLevels(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的充值档位信息数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
