package org.dromara.erp.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.erp.domain.ErpOrderDetail;
import org.dromara.erp.domain.query.ErpOrderDetailQuery;
import org.dromara.erp.domain.vo.ErpOrderDetailVo;
import org.dromara.erp.mapper.ErpOrderDetailMapper;
import org.dromara.erp.service.IErpOrderDetailService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 订单明细 Service 业务层处理
 *
 * @Author: Antigravity
 */
@RequiredArgsConstructor
@Service
public class ErpOrderDetailServiceImpl implements IErpOrderDetailService {

    private final ErpOrderDetailMapper baseMapper;

    @Override
    public List<ErpOrderDetailVo> queryList(ErpOrderDetailQuery query) {
        LambdaQueryWrapper<ErpOrderDetail> lqw = Wrappers.lambdaQuery();
        if (ObjectUtil.isNotEmpty(query.getOrderId())) {
            lqw.eq(ErpOrderDetail::getOrderId, query.getOrderId());
        } else {
            lqw.isNull(ErpOrderDetail::getOrderId);
        }
        if (ObjectUtil.isNotEmpty(query.getPlacer())) {
            lqw.eq(ErpOrderDetail::getPlacer, query.getPlacer());
        }
        if (ObjectUtil.isNotEmpty(query.getClientId())) {
            lqw.eq(ErpOrderDetail::getClientId, query.getClientId());
        }
        lqw.orderByDesc(ErpOrderDetail::getCreateTime);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public ErpOrderDetailVo queryById(String rowId) {
        return baseMapper.selectVoById(rowId);
    }

    @Override
    public Boolean insert(org.dromara.erp.domain.bo.ErpOrderDetailBo bo) {
        ErpOrderDetail entity = MapstructUtils.convert(bo, ErpOrderDetail.class);
        if (ObjectUtil.isEmpty(entity.getRowId())) {
            entity.setRowId(IdUtil.fastUUID());
        }
        // 处理其他可能为空字符串的 RowID 字段，防止 SQL Server 报错
        if (ObjectUtil.isEmpty(entity.getOrderId())) entity.setOrderId(null);
        if (ObjectUtil.isEmpty(entity.getModelId())) entity.setModelId(null);
        if (ObjectUtil.isEmpty(entity.getSurfaceId())) entity.setSurfaceId(null);
        if (ObjectUtil.isEmpty(entity.getPackId())) entity.setPackId(null);

        // 审计字段回填
        Long userId = null;
        try {
            userId = org.dromara.common.satoken.utils.LoginHelper.getUserId();
        } catch (Exception e) {}
        Date now = new Date();
        entity.setCreateBy(userId);
        entity.setCreateTime(now);
        entity.setUpdateBy(userId);
        entity.setUpdateTime(now);

        return baseMapper.insert(entity) > 0;
    }

    @Override
    public Boolean update(org.dromara.erp.domain.bo.ErpOrderDetailBo bo) {
        ErpOrderDetail entity = MapstructUtils.convert(bo, ErpOrderDetail.class);

        // 审计字段回填
        Long userId = null;
        try {
            userId = org.dromara.common.satoken.utils.LoginHelper.getUserId();
        } catch (Exception e) {}
        entity.setUpdateBy(userId);
        entity.setUpdateTime(new Date());

        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public Boolean deleteById(String rowId) {
        return baseMapper.deleteById(rowId) > 0;
    }

    /**
     * 批量修改订单明细（表面处理 / 包装方式）
     *
     * @Author: Trae
     */
    @Override
    public Boolean batchUpdate(org.dromara.erp.domain.bo.ErpOrderDetailBatchUpdateBo bo) {
        if (bo.getRowIds() == null || bo.getRowIds().isEmpty()) {
            return false;
        }

        LambdaUpdateWrapper<ErpOrderDetail> luw = Wrappers.lambdaUpdate();
        boolean hasUpdate = false;

        if (ObjectUtil.isNotEmpty(bo.getSurfaceId()) || ObjectUtil.isNotEmpty(bo.getSurfaceName())) {
            luw.set(ErpOrderDetail::getSurfaceId, bo.getSurfaceId());
            luw.set(ErpOrderDetail::getSurfaceName, bo.getSurfaceName());
            hasUpdate = true;
        }

        if (ObjectUtil.isNotEmpty(bo.getPackId()) || ObjectUtil.isNotEmpty(bo.getPackName())) {
            luw.set(ErpOrderDetail::getPackId, bo.getPackId());
            luw.set(ErpOrderDetail::getPackName, bo.getPackName());
            hasUpdate = true;
        }

        if (!hasUpdate) {
            return false;
        }

        Long userId = null;
        try {
            userId = org.dromara.common.satoken.utils.LoginHelper.getUserId();
        } catch (Exception e) {}
        luw.set(ErpOrderDetail::getUpdateBy, userId);
        luw.set(ErpOrderDetail::getUpdateTime, new Date());

        luw.in(ErpOrderDetail::getRowId, bo.getRowIds());
        return baseMapper.update(null, luw) > 0;
    }

}
