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.UserAccountMapper;
import com.zsElectric.boot.business.service.UserAccountService;
import com.zsElectric.boot.business.model.entity.UserAccount;
import com.zsElectric.boot.business.model.form.UserAccountForm;
import com.zsElectric.boot.business.model.query.UserAccountQuery;
import com.zsElectric.boot.business.model.vo.UserAccountVO;
import com.zsElectric.boot.business.converter.UserAccountConverter;

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:20
 */
@Service
@RequiredArgsConstructor
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

    private final UserAccountConverter userAccountConverter;

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

}
