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.PromotionCouponTemplateMapper;
import com.zsElectric.boot.business.service.PromotionCouponTemplateService;
import com.zsElectric.boot.business.model.entity.PromotionCouponTemplate;
import com.zsElectric.boot.business.model.form.PromotionCouponTemplateForm;
import com.zsElectric.boot.business.model.query.PromotionCouponTemplateQuery;
import com.zsElectric.boot.business.model.vo.PromotionCouponTemplateVO;
import com.zsElectric.boot.business.converter.PromotionCouponTemplateConverter;

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 15:11
 */
@Service
@RequiredArgsConstructor
public class PromotionCouponTemplateServiceImpl extends ServiceImpl<PromotionCouponTemplateMapper, PromotionCouponTemplate> implements PromotionCouponTemplateService {

    private final PromotionCouponTemplateConverter promotionCouponTemplateConverter;

    /**
    * 获取优惠劵模板分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<PromotionCouponTemplateVO>} 优惠劵模板分页列表
    */
    @Override
    public IPage<PromotionCouponTemplateVO> getPromotionCouponTemplatePage(PromotionCouponTemplateQuery queryParams) {
        Page<PromotionCouponTemplateVO> pageVO = this.baseMapper.getPromotionCouponTemplatePage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取优惠劵模板表单数据
     *
     * @param id 优惠劵模板ID
     * @return 优惠劵模板表单数据
     */
    @Override
    public PromotionCouponTemplateForm getPromotionCouponTemplateFormData(Long id) {
        PromotionCouponTemplate entity = this.getById(id);
        return promotionCouponTemplateConverter.toForm(entity);
    }
    
    /**
     * 新增优惠劵模板
     *
     * @param formData 优惠劵模板表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean savePromotionCouponTemplate(PromotionCouponTemplateForm formData) {
        PromotionCouponTemplate entity = promotionCouponTemplateConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新优惠劵模板
     *
     * @param id   优惠劵模板ID
     * @param formData 优惠劵模板表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updatePromotionCouponTemplate(Long id,PromotionCouponTemplateForm formData) {
        PromotionCouponTemplate entity = promotionCouponTemplateConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除优惠劵模板
     *
     * @param ids 优惠劵模板ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deletePromotionCouponTemplates(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的优惠劵模板数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
