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.CouponTemplateMapper;
import com.zsElectric.boot.business.service.CouponTemplateService;
import com.zsElectric.boot.business.model.entity.CouponTemplate;
import com.zsElectric.boot.business.model.form.CouponTemplateForm;
import com.zsElectric.boot.business.model.query.CouponTemplateQuery;
import com.zsElectric.boot.business.model.vo.CouponTemplateVO;
import com.zsElectric.boot.business.converter.CouponTemplateConverter;

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-15 11:14
 */
@Service
@RequiredArgsConstructor
public class CouponTemplateServiceImpl extends ServiceImpl<CouponTemplateMapper, CouponTemplate> implements CouponTemplateService {

    private final CouponTemplateConverter couponTemplateConverter;

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

}
