package com.jeesharp.business.modules.pay.paymerchant.application;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.pay.paymerchant.convert.PayMerchantConvert;
import com.jeesharp.business.modules.pay.paymerchant.enums.PayMerchantStatusEnum;
import com.jeesharp.business.modules.pay.paymerchant.dto.PayMerchantSaveDTO;
import com.jeesharp.business.modules.pay.paymerchant.entity.PayMerchant;
import com.jeesharp.business.modules.pay.paymerchant.service.PayMerchantService;
import com.jeesharp.business.modules.pay.paymerchant.vo.PayMerchantInfoVO;
import com.jeesharp.business.modules.pay.paymerchant.vo.PayMerchantListVO;
import com.jeesharp.business.modules.pay.paymerchant.vo.PayMerchantQueryVO;
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.Arrays;
import java.util.List;

/**
 * 支付商户应用服务：管理端查询与保存编排。
 *
 * @author jeesharp
 * @since 2026-05-25
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class PayMerchantApplicationService {

    private final PayMerchantService payMerchantService;

    public PayMerchantInfoVO get(String id) {
        return PayMerchantConvert.INSTANCE.toInfoVO(payMerchantService.get(id));
    }

    public PageRespVO<PayMerchantListVO> findPage(Page<PayMerchant> page, PayMerchantQueryVO query) {
        Page<PayMerchant> result = payMerchantService.findPage(page, query);
        return PayMerchantConvert.INSTANCE.toPageRespVO(result);
    }

    @Transactional(readOnly = false)
    public void save(PayMerchantSaveDTO dto) {
        PayMerchant entity = PayMerchantConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId()) && dto.getStatus() == null) {
            entity.setStatus(PayMerchantStatusEnum.ENABLED.getValue());
        }
        payMerchantService.save(entity);
    }

    @Transactional(readOnly = false)
    public void delete(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        payMerchantService.batchDelete(idList);
    }
}
