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

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.convert.MallOrderLogConvert;
import com.jeesharp.business.modules.mall.dto.MallOrderLogSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallOrderLog;
import com.jeesharp.business.modules.mall.service.MallOrderLogService;
import com.jeesharp.business.modules.mall.vo.MallOrderLogInfoVO;
import com.jeesharp.business.modules.mall.vo.MallOrderLogListVO;
import com.jeesharp.business.modules.mall.vo.MallOrderLogQueryVO;
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;

/**
 * 订单状态日志 Application Service
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallOrderLogApplicationService {

    private final MallOrderLogService mallOrderLogService;

    public MallOrderLogInfoVO get(String id) {
        return MallOrderLogConvert.INSTANCE.toInfoVO(mallOrderLogService.get(id));
    }

    public PageRespVO<MallOrderLogListVO> findPage(Page<MallOrderLog> page, MallOrderLogQueryVO query) {
        Page<MallOrderLog> result = mallOrderLogService.findPage(page, query);
        return MallOrderLogConvert.INSTANCE.toPageRespVO(result);
    }

    @Transactional(readOnly = false)
    public void save(MallOrderLogSaveDTO dto) {
        MallOrderLog entity = MallOrderLogConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            mallOrderLogService.createOrderLog(entity);
            return;
        }
        mallOrderLogService.updateOrderLog(entity);
    }

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

    /**
     * 记录订单状态变更日志
     */
    @Transactional(readOnly = false)
    public void recordOrderStatusChange(String orderId, String orderNo, Integer beforeStatus,
                                       Integer afterStatus, String operateType, String operatorId,
                                       String operatorName, String remark) {
        mallOrderLogService.recordOrderStatusChange(orderId, orderNo, beforeStatus, afterStatus,
                operateType, operatorId, operatorName, remark);
    }

    /**
     * 获取订单的操作日志列表
     */
    public List<MallOrderLog> getOrderLogs(String orderId) {
        return mallOrderLogService.getOrderLogs(orderId);
    }
}
