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.ChargeOrderInfoMapper;
import com.zsElectric.boot.business.service.ChargeOrderInfoService;
import com.zsElectric.boot.business.model.entity.ChargeOrderInfo;
import com.zsElectric.boot.business.model.form.ChargeOrderInfoForm;
import com.zsElectric.boot.business.model.query.ChargeOrderInfoQuery;
import com.zsElectric.boot.business.model.vo.ChargeOrderInfoVO;
import com.zsElectric.boot.business.converter.ChargeOrderInfoConverter;

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 19:13
 */
@Service
@RequiredArgsConstructor
public class ChargeOrderInfoServiceImpl extends ServiceImpl<ChargeOrderInfoMapper, ChargeOrderInfo> implements ChargeOrderInfoService {

    private final ChargeOrderInfoConverter chargeOrderInfoConverter;

    /**
    * 获取充电订单信息分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<ChargeOrderInfoVO>} 充电订单信息分页列表
    */
    @Override
    public IPage<ChargeOrderInfoVO> getChargeOrderInfoPage(ChargeOrderInfoQuery queryParams) {
        Page<ChargeOrderInfoVO> pageVO = this.baseMapper.getChargeOrderInfoPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取充电订单信息表单数据
     *
     * @param id 充电订单信息ID
     * @return 充电订单信息表单数据
     */
    @Override
    public ChargeOrderInfoForm getChargeOrderInfoFormData(Long id) {
        ChargeOrderInfo entity = this.getById(id);
        return chargeOrderInfoConverter.toForm(entity);
    }
    
    /**
     * 新增充电订单信息
     *
     * @param formData 充电订单信息表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveChargeOrderInfo(ChargeOrderInfoForm formData) {
        ChargeOrderInfo entity = chargeOrderInfoConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新充电订单信息
     *
     * @param id   充电订单信息ID
     * @param formData 充电订单信息表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateChargeOrderInfo(Long id,ChargeOrderInfoForm formData) {
        ChargeOrderInfo entity = chargeOrderInfoConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除充电订单信息
     *
     * @param ids 充电订单信息ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteChargeOrderInfos(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的充电订单信息数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
