package com.ylx.massage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylx.common.exception.ServiceException;
import com.ylx.common.utils.DateUtils;
import com.ylx.common.utils.SecurityUtils;
import com.ylx.massage.domain.AfterSaleOrder;
import com.ylx.massage.domain.AfterSaleOrderFee;
import com.ylx.massage.domain.dto.AfterSaleOrderFeeBatchDTO;
import com.ylx.massage.domain.dto.AfterSaleOrderFeeDTO;
import com.ylx.massage.mapper.AfterSaleOrderFeeMapper;
import com.ylx.massage.service.IAfterSaleOrderFeeService;
import com.ylx.massage.service.IAfterSaleOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AfterSaleOrderFeeServiceImpl extends ServiceImpl<AfterSaleOrderFeeMapper, AfterSaleOrderFee> implements IAfterSaleOrderFeeService {

    @Resource
    private AfterSaleOrderFeeMapper afterSaleOrderFeeMapper;
    @Resource
    private IAfterSaleOrderService afterSaleOrderService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSaveFees(AfterSaleOrderFeeBatchDTO batchDTO) {
        Long afterSaleOrderId = batchDTO.getAfterSaleOrderId();
        AfterSaleOrder afterSaleOrder = this.afterSaleOrderService.getById(afterSaleOrderId);

        if (ObjectUtil.isNull(afterSaleOrder)) {
            throw new ServiceException("售后订单不存在");
        }

        List<AfterSaleOrderFeeDTO> feeList = batchDTO.getFeeList();

        if (CollUtil.isEmpty(feeList)) {
            return;
        }

        // 拆分：新增列表、更新列表
        List<AfterSaleOrderFeeDTO> insertList = new ArrayList<>();
        List<AfterSaleOrderFeeDTO> updateList = new ArrayList<>();

        for (AfterSaleOrderFeeDTO dto : feeList) {
            if (dto.getId() == null) {
                insertList.add(dto);
            } else {
                updateList.add(dto);
            }
        }

        // 1. 批量新增：无 ID 的
        if (CollUtil.isNotEmpty(insertList)) {
            List<AfterSaleOrderFee> insertEntities = insertList.stream()
                    .map(dto -> {
                        AfterSaleOrderFee entity = BeanUtil.copyProperties(dto, AfterSaleOrderFee.class);
                        entity.setAfterSaleOrderId(afterSaleOrderId);
                        entity.setOrderNo(afterSaleOrder.getOrderNo());
                        entity.setCreateBy(SecurityUtils.getUsername());
                        entity.setCreateTime(DateUtils.getNowDate());
                        return entity;
                    })
                    .collect(Collectors.toList());
            super.saveBatch(insertEntities);
        }

        // 2. 批量更新：有 ID 的
        if (CollUtil.isNotEmpty(updateList)) {
            List<AfterSaleOrderFee> updateEntities = updateList.stream()
                    .map(dto -> {
                        AfterSaleOrderFee entity = BeanUtil.copyProperties(dto, AfterSaleOrderFee.class);
                        entity.setAfterSaleOrderId(afterSaleOrderId);
                        entity.setUpdateBy(SecurityUtils.getUsername());
                        entity.setUpdateTime(DateUtils.getNowDate());
                        return entity;
                    })
                    .collect(Collectors.toList());
            super.updateBatchById(updateEntities);
        }
    }
}
