package com.jeesharp.business.modules.mall.application;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.convert.MallFreightTemplateRegionConvert;
import com.jeesharp.business.modules.mall.dto.MallFreightTemplateRegionSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallFreightTemplateRegion;
import com.jeesharp.business.modules.mall.service.MallFreightTemplateRegionService;
import com.jeesharp.business.modules.mall.vo.MallFreightTemplateRegionInfoVO;
import com.jeesharp.business.modules.mall.vo.MallFreightTemplateRegionListVO;
import com.jeesharp.business.modules.mall.vo.MallFreightTemplateRegionQueryVO;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 运费模板地区配置应用服务。
 *
 * @author jeesharp
 * @since 2026-05-07
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallFreightTemplateRegionApplicationService {

    private final MallFreightTemplateRegionService mallFreightTemplateRegionService;

    public MallFreightTemplateRegionInfoVO get(String id) {
        return MallFreightTemplateRegionConvert.INSTANCE.toInfoVO(mallFreightTemplateRegionService.get(id));
    }

    public PageRespVO<MallFreightTemplateRegionListVO> findPage(Page<MallFreightTemplateRegion> page,
                                                                MallFreightTemplateRegionQueryVO query) {
        Page<MallFreightTemplateRegion> result = mallFreightTemplateRegionService.findPage(page, query);
        return MallFreightTemplateRegionConvert.INSTANCE.toPageRespVO(result);
    }

    @Transactional(readOnly = false)
    public void save(MallFreightTemplateRegionSaveDTO dto) {
        MallFreightTemplateRegion entity = MallFreightTemplateRegionConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            mallFreightTemplateRegionService.createRegion(entity);
            return;
        }
        mallFreightTemplateRegionService.updateRegion(entity);
    }

    @Transactional(readOnly = false)
    public void delete(String ids) {
        mallFreightTemplateRegionService.batchDelete(StrUtil.splitTrim(ids, ','));
    }
}
