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.UserAccountLogMapper;
import com.zsElectric.boot.business.service.UserAccountLogService;
import com.zsElectric.boot.business.model.entity.UserAccountLog;
import com.zsElectric.boot.business.model.form.UserAccountLogForm;
import com.zsElectric.boot.business.model.query.UserAccountLogQuery;
import com.zsElectric.boot.business.model.vo.UserAccountLogVO;
import com.zsElectric.boot.business.converter.UserAccountLogConverter;

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:18
 */
@Service
@RequiredArgsConstructor
public class UserAccountLogServiceImpl extends ServiceImpl<UserAccountLogMapper, UserAccountLog> implements UserAccountLogService {

    private final UserAccountLogConverter userAccountLogConverter;

    /**
    * 获取个人账户变更记录分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<UserAccountLogVO>} 个人账户变更记录分页列表
    */
    @Override
    public IPage<UserAccountLogVO> getUserAccountLogPage(UserAccountLogQuery queryParams) {
        Page<UserAccountLogVO> pageVO = this.baseMapper.getUserAccountLogPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取个人账户变更记录表单数据
     *
     * @param id 个人账户变更记录ID
     * @return 个人账户变更记录表单数据
     */
    @Override
    public UserAccountLogForm getUserAccountLogFormData(Long id) {
        UserAccountLog entity = this.getById(id);
        return userAccountLogConverter.toForm(entity);
    }
    
    /**
     * 新增个人账户变更记录
     *
     * @param formData 个人账户变更记录表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveUserAccountLog(UserAccountLogForm formData) {
        UserAccountLog entity = userAccountLogConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新个人账户变更记录
     *
     * @param id   个人账户变更记录ID
     * @param formData 个人账户变更记录表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateUserAccountLog(Long id,UserAccountLogForm formData) {
        UserAccountLog entity = userAccountLogConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除个人账户变更记录
     *
     * @param ids 个人账户变更记录ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteUserAccountLogs(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的个人账户变更记录数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
