package com.ylx.massage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylx.massage.domain.OrderAllocationLog;
import com.ylx.massage.mapper.OrderAllocationLogMapper;
import com.ylx.massage.service.OrderAllocationLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 订单分配操作记录服务实现类
 *
 * @author ylx
 * @version 1.0
 * @since 2025-01-10
 */
@Service
@Slf4j
public class OrderAllocationLogServiceImpl extends ServiceImpl<OrderAllocationLogMapper, OrderAllocationLog>
        implements OrderAllocationLogService {

    @Override
    public boolean saveAllocationLog(OrderAllocationLog allocationLog) {
        if (allocationLog == null) {
            log.error("分配记录实体为空，无法保存");
            return false;
        }
        return this.save(allocationLog);
    }

    @Override
    public boolean recordTransferOrder(
            String orderId,
            String orderNo,
            String oldTechnicianId,
            String oldTechnicianName,
            Integer oldTechnicianStatusBefore,
            Integer oldTechnicianStatusAfter,
            String newTechnicianId,
            String newTechnicianName,
            Integer newTechnicianStatusBefore,
            Integer newTechnicianStatusAfter,
            Integer orderStatusBefore,
            Integer orderStatusAfter,
            String operatorId,
            String operatorName,
            String operationReason,
            Integer operationResult
    ) {
        try {
            // 创建分配记录实体
            OrderAllocationLog allocationLog = new OrderAllocationLog();

            // 基础信息
            allocationLog.setOrderId(orderId);
            allocationLog.setOrderNo(orderNo);

            // 操作信息（转单操作类型为1）
            allocationLog.setOperationType(1);
            allocationLog.setOperationReason(operationReason);

            // 原技师信息
            allocationLog.setOldTechnicianId(oldTechnicianId);
            allocationLog.setOldTechnicianName(oldTechnicianName);
            allocationLog.setOldTechnicianStatusBefore(oldTechnicianStatusBefore);
            allocationLog.setOldTechnicianStatusAfter(oldTechnicianStatusAfter);

            // 新技师信息
            allocationLog.setNewTechnicianId(newTechnicianId);
            allocationLog.setNewTechnicianName(newTechnicianName);
            allocationLog.setNewTechnicianStatusBefore(newTechnicianStatusBefore);
            allocationLog.setNewTechnicianStatusAfter(newTechnicianStatusAfter);

            // 订单状态信息
            allocationLog.setOrderStatusBefore(orderStatusBefore);
            allocationLog.setOrderStatusAfter(orderStatusAfter);

            // 操作人信息
            allocationLog.setOperatorId(operatorId);
            allocationLog.setOperatorName(operatorName);

            // 操作结果（0-成功，1-失败）
            allocationLog.setOperationResult(operationResult);

            // 保存记录
            boolean result = this.save(allocationLog);
            if (result) {
                String resultDesc = operationResult == 0 ? "成功" : "失败";
                log.info("成功记录转单操作 - 订单号：{}, 原技师：{}, 新技师：{}, 操作人：{}, 操作结果：{}",
                        orderNo, oldTechnicianName, newTechnicianName, operatorName, resultDesc);
            } else {
                log.error("记录转单操作失败 - 订单号：{}", orderNo);
            }
            return result;
        } catch (Exception e) {
            log.error("记录转单操作异常 - 订单号：{}, 异常信息：{}", orderNo, e.getMessage(), e);
            return false;
        }
    }
}
