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

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.convert.MallFreightTemplateConvert;
import com.jeesharp.business.modules.mall.dto.MallFreightTemplateSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallFreightTemplate;
import com.jeesharp.business.modules.mall.service.MallFreightTemplateService;
import com.jeesharp.business.modules.mall.vo.MallFreightTemplateInfoVO;
import com.jeesharp.business.modules.mall.vo.MallFreightTemplateListVO;
import com.jeesharp.business.modules.mall.vo.MallFreightTemplateQueryVO;
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;

import java.math.BigDecimal;
import java.util.List;

/**
 * 运费模板 Application Service
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallFreightTemplateApplicationService {

    private final MallFreightTemplateService mallFreightTemplateService;

    public MallFreightTemplateInfoVO get(String id) {
        return MallFreightTemplateConvert.INSTANCE.toInfoVO(mallFreightTemplateService.get(id));
    }

    public PageRespVO<MallFreightTemplateListVO> findPage(Page<MallFreightTemplate> page, MallFreightTemplateQueryVO query) {
        Page<MallFreightTemplate> result = mallFreightTemplateService.findPage(page, query);
        return MallFreightTemplateConvert.INSTANCE.toPageRespVO(result);
    }

    @Transactional(readOnly = false)
    public void save(MallFreightTemplateSaveDTO dto) {
        MallFreightTemplate entity = MallFreightTemplateConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            mallFreightTemplateService.createTemplate(entity);
            return;
        }
        mallFreightTemplateService.updateTemplate(entity);
    }

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

    /**
     * 获取所有启用的运费模板
     */
    public List<MallFreightTemplate> getEnabledTemplates() {
        return mallFreightTemplateService.getEnabledTemplates();
    }

    /**
     * 计算运费
     * @param templateId 运费模板ID
     * @param province 省份
     * @param city 城市
     * @param totalWeight 总重量(kg)
     * @param totalQuantity 总数量
     * @param orderAmount 订单金额
     * @return 运费金额
     */
    public BigDecimal calculateFreight(String templateId, String province, String city,
                                      BigDecimal totalWeight, Integer totalQuantity, BigDecimal orderAmount) {
        return mallFreightTemplateService.calculateFreight(templateId, province, city, totalWeight, totalQuantity, orderAmount);
    }

    /**
     * 计算运费（含行政区划编码，用于与地区运费行精确匹配）。
     */
    public BigDecimal calculateFreight(String templateId,
                                       String provinceCode, String cityCode, String districtCode,
                                       String province, String city, String district,
                                       BigDecimal totalWeight, Integer totalQuantity, BigDecimal orderAmount) {
        return mallFreightTemplateService.calculateFreight(templateId, provinceCode, cityCode, districtCode,
            province, city, district, totalWeight, totalQuantity, orderAmount);
    }

    /**
     * 获取默认运费模板
     */
    public MallFreightTemplate getDefaultTemplate() {
        return mallFreightTemplateService.getDefaultTemplate();
    }
}
