package com.ruoyi.web.stock.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.stock.domain.WarehouseInventory;
import com.ruoyi.web.stock.domain.vo.StockOptionVo;
import com.ruoyi.web.stock.mapper.WarehouseBaseMapper;
import com.ruoyi.web.stock.mapper.WarehouseInventoryMapper;
import com.ruoyi.web.stock.service.IWarehouseInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 仓库库存 Service 实现。
 */
@Service
public class WarehouseInventoryServiceImpl implements IWarehouseInventoryService {

    @Autowired
    private WarehouseInventoryMapper warehouseInventoryMapper;

    @Autowired
    private WarehouseBaseMapper warehouseBaseMapper;

    @Override
    public WarehouseInventory selectById(Long id) {
        return warehouseInventoryMapper.selectWarehouseInventoryById(id);
    }

    @Override
    public List<WarehouseInventory> selectList(WarehouseInventory query) {
        return warehouseInventoryMapper.selectWarehouseInventoryList(query);
    }

    @Override
    public List<WarehouseInventory> listByWarehouseNo(String orgId, String warehouseNo) {
        return warehouseInventoryMapper.selectWarehouseInventoryListByWarehouseNo(orgId, warehouseNo);
    }

    @Override
    public int insert(WarehouseInventory entity) {
        fillReferenceName(entity);
        normalizeNumber(entity);
        entity.setDelFlag("0");
        return warehouseInventoryMapper.insertWarehouseInventory(entity);
    }

    @Override
    public int update(WarehouseInventory entity) {
        fillReferenceName(entity);
        normalizeNumber(entity);
        return warehouseInventoryMapper.updateWarehouseInventory(entity);
    }

    @Override
    public int removeById(Long id) {
        return warehouseInventoryMapper.deleteWarehouseInventoryById(id);
    }

    @Override
    public List<StockOptionVo> listMaterialOptions(String orgId) {
        return warehouseInventoryMapper.selectMaterialOptions(orgId);
    }

    @Override
    public boolean checkBatchNoUnique(WarehouseInventory entity, String orgId) {
        if (entity == null || StringUtils.isEmpty(entity.getBatchNo())) {
            return false;
        }
        WarehouseInventory old = warehouseInventoryMapper.selectWarehouseInventoryByBatchNo(entity.getBatchNo().trim(), orgId);
        if (old == null) {
            return true;
        }
        Long currentId = entity.getId() == null ? -1L : entity.getId();
        return old.getId().longValue() == currentId.longValue();
    }

    private void fillReferenceName(WarehouseInventory entity) {
        if (StringUtils.isNotEmpty(entity.getWarehouseNo()) && StringUtils.isEmpty(entity.getWarehouseName())) {
            List<StockOptionVo> warehouseOptions = warehouseBaseMapper.selectWarehouseOptions(entity.getOrgId());
            for (StockOptionVo item : warehouseOptions) {
                if (entity.getWarehouseNo().equals(item.getCode())) {
                    entity.setWarehouseName(item.getName());
                    break;
                }
            }
        }
        if (StringUtils.isNotEmpty(entity.getGoodsNum()) && StringUtils.isEmpty(entity.getGoodsName())) {
            StockOptionVo material = warehouseInventoryMapper.selectMaterialByGoodsNum(entity.getGoodsNum());
            if (material != null) {
                entity.setGoodsName(material.getName());
            }
        }
    }

    private void normalizeNumber(WarehouseInventory entity) {
        if (entity.getStockQty() == null) {
            entity.setStockQty(BigDecimal.ZERO);
        }
        if (entity.getAssistStockQty() == null) {
            entity.setAssistStockQty(BigDecimal.ZERO);
        }
        if (entity.getCostAmount() == null) {
            entity.setCostAmount(BigDecimal.ZERO);
        }
    }
}
