package com.ruoyi.warehouse.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.warehouse.domain.WarehouseArea;
import com.ruoyi.warehouse.mapper.WarehouseAreaMapper;
import com.ruoyi.warehouse.service.IWarehouseAreaService;

@Service
public class WarehouseAreaServiceImpl implements IWarehouseAreaService
{
    @Autowired
    private WarehouseAreaMapper warehouseAreaMapper;

    @Override
    public List<WarehouseArea> selectWarehouseAreaList(WarehouseArea warehouseArea)
    {
        return warehouseAreaMapper.selectWarehouseAreaList(warehouseArea);
    }

    @Override
    public List<WarehouseArea> selectWarehouseAreaAll()
    {
        return warehouseAreaMapper.selectWarehouseAreaAll();
    }

    @Override
    public WarehouseArea selectWarehouseAreaById(Long id)
    {
        return warehouseAreaMapper.selectWarehouseAreaById(id);
    }

    @Override
    public boolean checkAreaCodeUnique(WarehouseArea warehouseArea)
    {
        if (StringUtils.isBlank(warehouseArea.getAreaCode()))
        {
            return UserConstants.UNIQUE;
        }
        Long id = StringUtils.isNull(warehouseArea.getId()) ? -1L : warehouseArea.getId();
        WarehouseArea info = warehouseAreaMapper.checkAreaCodeUnique(warehouseArea.getAreaCode());
        return StringUtils.isNull(info) || info.getId().longValue() == id.longValue() ? UserConstants.UNIQUE : UserConstants.NOT_UNIQUE;
    }

    @Override
    public int insertWarehouseArea(WarehouseArea warehouseArea)
    {
        normalizeAreaCode(warehouseArea);
        return warehouseAreaMapper.insertWarehouseArea(warehouseArea);
    }

    @Override
    public int updateWarehouseArea(WarehouseArea warehouseArea)
    {
        normalizeAreaCode(warehouseArea);
        return warehouseAreaMapper.updateWarehouseArea(warehouseArea);
    }

    @Override
    public int deleteWarehouseAreaByIds(Long[] ids)
    {
        return warehouseAreaMapper.deleteWarehouseAreaByIds(ids);
    }

    private void normalizeAreaCode(WarehouseArea warehouseArea)
    {
        if (StringUtils.isBlank(warehouseArea.getAreaCode()))
        {
            warehouseArea.setAreaCode(null);
        }
    }
}
