package org.dromara.system.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.TblDzbcCargoItem;
import org.dromara.system.domain.bo.CargoItemBo;
import org.dromara.system.domain.vo.TblDzbcCargoItemVo;
import org.dromara.system.mapper.TblDzbcCargoItemMapper;
import org.dromara.system.service.ITblDzbcCargoItemService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 货物明细Service业务层处理
 *
 * @author Claude
 * @date 2026-01-15
 */
@RequiredArgsConstructor
@Service
public class TblDzbcCargoItemServiceImpl implements ITblDzbcCargoItemService {

    private final TblDzbcCargoItemMapper baseMapper;

    /**
     * 查询货物明细
     */
    @Override
    public TblDzbcCargoItemVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询货物明细列表
     */
    @Override
    public TableDataInfo<TblDzbcCargoItemVo> queryPageList(CargoItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TblDzbcCargoItem> lqw = buildQueryWrapper(bo);
        Page<TblDzbcCargoItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询货物明细列表
     */
    @Override
    public List<TblDzbcCargoItemVo> queryList(Long orderId) {
        return selectByOrderId(orderId);
    }

    /**
     * 根据订单ID查询货物明细列表
     */
    @Override
    public List<TblDzbcCargoItemVo> selectByOrderId(Long orderId) {
        return baseMapper.selectVoList(new LambdaQueryWrapper<TblDzbcCargoItem>()
            .eq(TblDzbcCargoItem::getOrderId, orderId));
    }

    /**
     * 保存货物明细列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveItems(Long orderId, List<CargoItemBo> items) {
        if (items == null || items.isEmpty()) {
            return true;
        }

        // 先删除该订单的原有货物明细
        baseMapper.delete(null); // 这里需要根据订单ID删除，实际应该添加deleteByOrderId方法

        // 保存新的货物明细
        for (CargoItemBo itemBo : items) {
            TblDzbcCargoItem item = new TblDzbcCargoItem();
            item.setOrderId(orderId);
            item.setCargoName(itemBo.getCargoName());
            item.setCargoType(itemBo.getCargoType());
            item.setQuantity(itemBo.getQuantity());
            item.setUnit(itemBo.getUnit());
            item.setWeight(itemBo.getWeight());
            item.setLength(itemBo.getLength());
            item.setWidth(itemBo.getWidth());
            item.setHeight(itemBo.getHeight());
            item.setValue(itemBo.getValue());
            if (itemBo.getImages() != null && !itemBo.getImages().isEmpty()) {
                item.setImageUrls(JSONUtil.toJsonStr(itemBo.getImages()));
            }
            item.setRemark(itemBo.getRemark());
            baseMapper.insert(item);
        }

        return true;
    }

    /**
     * 批量删除货物明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<TblDzbcCargoItem> buildQueryWrapper(CargoItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblDzbcCargoItem> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCargoName()), TblDzbcCargoItem::getCargoName, bo.getCargoName());
        lqw.eq(StringUtils.isNotBlank(bo.getCargoType()), TblDzbcCargoItem::getCargoType, bo.getCargoType());
        lqw.eq(bo.getQuantity() != null, TblDzbcCargoItem::getQuantity, bo.getQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), TblDzbcCargoItem::getUnit, bo.getUnit());
        lqw.eq(bo.getWeight() != null, TblDzbcCargoItem::getWeight, bo.getWeight());
        lqw.eq(bo.getValue() != null, TblDzbcCargoItem::getValue, bo.getValue());
        return lqw;
    }
}
