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

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.convert.MallLogisticsCompanyConvert;
import com.jeesharp.business.modules.mall.dto.MallLogisticsCompanySaveDTO;
import com.jeesharp.business.modules.mall.entity.MallLogisticsCompany;
import com.jeesharp.business.modules.mall.service.MallLogisticsCompanyService;
import com.jeesharp.business.modules.mall.vo.MallLogisticsCompanyInfoVO;
import com.jeesharp.business.modules.mall.vo.MallLogisticsCompanyListVO;
import com.jeesharp.business.modules.mall.vo.MallLogisticsCompanyQueryVO;
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.util.List;

@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallLogisticsCompanyApplicationService {

    private final MallLogisticsCompanyService mallLogisticsCompanyService;

    public MallLogisticsCompanyInfoVO get(String id) {
        return MallLogisticsCompanyConvert.INSTANCE.toInfoVO(mallLogisticsCompanyService.get(id));
    }

    public PageRespVO<MallLogisticsCompanyListVO> findPage(Page<MallLogisticsCompany> page, MallLogisticsCompanyQueryVO query) {
        Page<MallLogisticsCompany> result = mallLogisticsCompanyService.findPage(page, query);
        return MallLogisticsCompanyConvert.INSTANCE.toPageRespVO(result);
    }

    @Transactional(readOnly = false)
    public void save(MallLogisticsCompanySaveDTO dto) {
        MallLogisticsCompany entity = MallLogisticsCompanyConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            mallLogisticsCompanyService.createCompany(entity);
            return;
        }
        mallLogisticsCompanyService.updateCompany(entity);
    }

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

    /**
     * 获取所有启用的物流公司
     */
    public List<MallLogisticsCompany> listEnabledCompanies() {
        return mallLogisticsCompanyService.listEnabledCompanies();
    }

    /**
     * 根据编码获取物流公司
     */
    public MallLogisticsCompany getByCode(String companyCode) {
        // 这里需要自定义查询逻辑，暂时返回null
        return null;
    }
}
