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.DiscountsActivityMapper;
import com.zsElectric.boot.business.service.DiscountsActivityService;
import com.zsElectric.boot.business.model.entity.DiscountsActivity;
import com.zsElectric.boot.business.model.form.DiscountsActivityForm;
import com.zsElectric.boot.business.model.query.DiscountsActivityQuery;
import com.zsElectric.boot.business.model.vo.DiscountsActivityVO;
import com.zsElectric.boot.business.converter.DiscountsActivityConverter;

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-20 16:16
 */
@Service
@RequiredArgsConstructor
public class DiscountsActivityServiceImpl extends ServiceImpl<DiscountsActivityMapper, DiscountsActivity> implements DiscountsActivityService {

    private final DiscountsActivityConverter discountsActivityConverter;

    /**
     * 获取优惠活动分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<DiscountsActivityVO>} 优惠活动分页列表
     */
    @Override
    public IPage<DiscountsActivityVO> getDiscountsActivityPage(DiscountsActivityQuery queryParams) {
        Page<DiscountsActivityVO> pageVO = this.baseMapper.getDiscountsActivityPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }

    /**
     * 获取优惠活动表单数据
     *
     * @param id 优惠活动ID
     * @return 优惠活动表单数据
     */
    @Override
    public DiscountsActivityForm getDiscountsActivityFormData(Long id) {
        DiscountsActivity entity = this.getById(id);
        return discountsActivityConverter.toForm(entity);
    }

    /**
     * 新增优惠活动
     *
     * @param formData 优惠活动表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveDiscountsActivity(DiscountsActivityForm formData) {
        DiscountsActivity entity = discountsActivityConverter.toEntity(formData);
        return this.save(entity);
    }

    /**
     * 更新优惠活动
     *
     * @param id       优惠活动ID
     * @param formData 优惠活动表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateDiscountsActivity(Long id, DiscountsActivityForm formData) {
        DiscountsActivity entity = discountsActivityConverter.toEntity(formData);
        return this.updateById(entity);
    }

    /**
     * 删除优惠活动
     *
     * @param ids 优惠活动ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteDiscountsActivitys(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的优惠活动数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
