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.UserRefundsOrderInfoMapper;
import com.zsElectric.boot.business.service.UserRefundsOrderInfoService;
import com.zsElectric.boot.business.UserRefundsOrderInfo;
import com.zsElectric.boot.business.model.form.UserRefundsOrderInfoForm;
import com.zsElectric.boot.business.model.query.UserRefundsOrderInfoQuery;
import com.zsElectric.boot.business.model.vo.UserRefundsOrderInfoVO;
import com.zsElectric.boot.business.converter.UserRefundsOrderInfoConverter;

import java.util.Arrays;
import java.util.List;

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

/**
 * 退款订单服务实现类
 *
 * @author zsElectric
 * @since 2025-12-17 16:48
 */
@Service
@RequiredArgsConstructor
public class UserRefundsOrderInfoServiceImpl extends ServiceImpl<UserRefundsOrderInfoMapper, UserRefundsOrderInfo> implements UserRefundsOrderInfoService {

    private final UserRefundsOrderInfoConverter userRefundsOrderInfoConverter;

    /**
    * 获取退款订单分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<UserRefundsOrderInfoVO>} 退款订单分页列表
    */
    @Override
    public IPage<UserRefundsOrderInfoVO> getUserRefundsOrderInfoPage(UserRefundsOrderInfoQuery queryParams) {
        Page<UserRefundsOrderInfoVO> pageVO = this.baseMapper.getUserRefundsOrderInfoPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取退款订单表单数据
     *
     * @param id 退款订单ID
     * @return 退款订单表单数据
     */
    @Override
    public UserRefundsOrderInfoForm getUserRefundsOrderInfoFormData(Long id) {
        UserRefundsOrderInfo entity = this.getById(id);
        return userRefundsOrderInfoConverter.toForm(entity);
    }
    
    /**
     * 新增退款订单
     *
     * @param formData 退款订单表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveUserRefundsOrderInfo(UserRefundsOrderInfoForm formData) {
        UserRefundsOrderInfo entity = userRefundsOrderInfoConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新退款订单
     *
     * @param id   退款订单ID
     * @param formData 退款订单表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateUserRefundsOrderInfo(Long id,UserRefundsOrderInfoForm formData) {
        UserRefundsOrderInfo entity = userRefundsOrderInfoConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除退款订单
     *
     * @param ids 退款订单ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteUserRefundsOrderInfos(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的退款订单数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
