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.UserInfoIntegralLogMapper;
import com.zsElectric.boot.business.service.UserInfoIntegralLogService;
import com.zsElectric.boot.business.model.entity.UserInfoIntegralLog;
import com.zsElectric.boot.business.model.form.UserInfoIntegralLogForm;
import com.zsElectric.boot.business.model.query.UserInfoIntegralLogQuery;
import com.zsElectric.boot.business.model.vo.UserInfoIntegralLogVO;
import com.zsElectric.boot.business.converter.UserInfoIntegralLogConverter;

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 10:25
 */
@Service
@RequiredArgsConstructor
public class UserInfoIntegralLogServiceImpl extends ServiceImpl<UserInfoIntegralLogMapper, UserInfoIntegralLog> implements UserInfoIntegralLogService {

    private final UserInfoIntegralLogConverter userInfoIntegralLogConverter;

    /**
    * 获取用户积分日志记录分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<UserInfoIntegralLogVO>} 用户积分日志记录分页列表
    */
    @Override
    public IPage<UserInfoIntegralLogVO> getUserInfoIntegralLogPage(UserInfoIntegralLogQuery queryParams) {
        Page<UserInfoIntegralLogVO> pageVO = this.baseMapper.getUserInfoIntegralLogPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取用户积分日志记录表单数据
     *
     * @param id 用户积分日志记录ID
     * @return 用户积分日志记录表单数据
     */
    @Override
    public UserInfoIntegralLogForm getUserInfoIntegralLogFormData(Long id) {
        UserInfoIntegralLog entity = this.getById(id);
        return userInfoIntegralLogConverter.toForm(entity);
    }
    
    /**
     * 新增用户积分日志记录
     *
     * @param formData 用户积分日志记录表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveUserInfoIntegralLog(UserInfoIntegralLogForm formData) {
        UserInfoIntegralLog entity = userInfoIntegralLogConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新用户积分日志记录
     *
     * @param id   用户积分日志记录ID
     * @param formData 用户积分日志记录表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateUserInfoIntegralLog(Long id,UserInfoIntegralLogForm formData) {
        UserInfoIntegralLog entity = userInfoIntegralLogConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除用户积分日志记录
     *
     * @param ids 用户积分日志记录ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteUserInfoIntegralLogs(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的用户积分日志记录数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
