package com.ruoyi.asset.service.impl;

import java.util.List;
import com.ruoyi.asset.domain.AmsWarehouse;
import com.ruoyi.asset.service.IAmsWarehouseService;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.asset.mapper.AmsAssetLocationMapper;
import com.ruoyi.asset.domain.AmsAssetLocation;
import com.ruoyi.asset.service.IAmsAssetLocationService;
import com.ruoyi.common.core.text.Convert;

/**
 * 资产位置Service业务层处理
 * 
 * @author Yangk
 * @date 2026-06-04
 */
@Service
public class AmsAssetLocationServiceImpl implements IAmsAssetLocationService 
{
    private static final String ENABLED_YES = "Y";

    private static final String DEL_FLAG_NORMAL = "0";

    @Autowired
    private AmsAssetLocationMapper amsAssetLocationMapper;

    @Autowired
    private IAmsWarehouseService amsWarehouseService;

    /**
     * 查询资产位置
     * 
     * @param locationId 资产位置主键
     * @return 资产位置
     */
    @Override
    public AmsAssetLocation selectAmsAssetLocationByLocationId(Long locationId)
    {
        return amsAssetLocationMapper.selectAmsAssetLocationByLocationId(locationId);
    }

    /**
     * 查询资产位置列表
     * 
     * @param amsAssetLocation 资产位置
     * @return 资产位置
     */
    @Override
    public List<AmsAssetLocation> selectAmsAssetLocationList(AmsAssetLocation amsAssetLocation)
    {
        return amsAssetLocationMapper.selectAmsAssetLocationList(amsAssetLocation);
    }

    /**
     * 校验位置编码是否唯一
     *
     * @param amsAssetLocation 资产位置
     * @return 结果
     */
    @Override
    public boolean checkLocationCodeUnique(AmsAssetLocation amsAssetLocation)
    {
        if (StringUtils.isEmpty(amsAssetLocation.getLocationCode()))
        {
            return UserConstants.UNIQUE;
        }
        Long locationId = StringUtils.isNull(amsAssetLocation.getLocationId()) ? -1L : amsAssetLocation.getLocationId();
        AmsAssetLocation info = amsAssetLocationMapper.checkLocationCodeUnique(amsAssetLocation.getLocationCode());
        if (StringUtils.isNotNull(info) && info.getLocationId().longValue() != locationId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增资产位置
     * 
     * @param amsAssetLocation 资产位置
     * @return 结果
     */
    @Override
    public int insertAmsAssetLocation(AmsAssetLocation amsAssetLocation)
    {
        fillLocationDefaults(amsAssetLocation);
        checkEnabledWarehouse(amsAssetLocation.getWarehouseId());
        amsAssetLocation.setDelFlag(DEL_FLAG_NORMAL);
        amsAssetLocation.setCreateTime(DateUtils.getNowDate());
        return amsAssetLocationMapper.insertAmsAssetLocation(amsAssetLocation);
    }

    /**
     * 修改资产位置
     * 
     * @param amsAssetLocation 资产位置
     * @return 结果
     */
    @Override
    public int updateAmsAssetLocation(AmsAssetLocation amsAssetLocation)
    {
        fillLocationDefaults(amsAssetLocation);
        checkEnabledWarehouse(amsAssetLocation.getWarehouseId());
        amsAssetLocation.setUpdateTime(DateUtils.getNowDate());
        return amsAssetLocationMapper.updateAmsAssetLocation(amsAssetLocation);
    }

    /**
     * 批量删除资产位置
     * 
     * @param locationIds 需要删除的资产位置主键
     * @return 结果
     */
    @Override
    public int deleteAmsAssetLocationByLocationIds(String locationIds)
    {
        String[] ids = Convert.toStrArray(locationIds);
        for (String id : ids)
        {
            checkLocationReferenced(Long.valueOf(id));
        }
        return amsAssetLocationMapper.deleteAmsAssetLocationByLocationIds(ids);
    }

    /**
     * 删除资产位置信息
     * 
     * @param locationId 资产位置主键
     * @return 结果
     */
    @Override
    public int deleteAmsAssetLocationByLocationId(Long locationId)
    {
        checkLocationReferenced(locationId);
        return amsAssetLocationMapper.deleteAmsAssetLocationByLocationId(locationId);
    }

    /**
     * 位置归属仓库是后续资产建档和入库选择的主约束，必须指向仍启用的仓库主数据。
     */
    private void checkEnabledWarehouse(Long warehouseId)
    {
        if (StringUtils.isNull(warehouseId))
        {
            throw new ServiceException("所属仓库不能为空");
        }

        AmsWarehouse warehouse = amsWarehouseService.selectAmsWarehouseByWarehouseId(warehouseId);
        if (StringUtils.isNull(warehouse) || !StringUtils.equals(ENABLED_YES, warehouse.getEnabled()))
        {
            throw new ServiceException("所属仓库不存在或已停用");
        }
    }

    /**
     * 位置被资产、单据或履历引用后禁止删除，避免历史业务记录里的位置引用悬挂。
     */
    private void checkLocationReferenced(Long locationId)
    {
        AmsAssetLocation location = selectAmsAssetLocationByLocationId(locationId);
        if (StringUtils.isNotNull(location) && amsAssetLocationMapper.countLocationReferences(locationId) > 0)
        {
            throw new ServiceException(String.format("资产位置【%1$s】已被引用，不允许删除", location.getLocationName()));
        }
    }

    /**
     * 启用状态为空时按启用保存，逻辑删除标志只由服务层维护。
     */
    private void fillLocationDefaults(AmsAssetLocation amsAssetLocation)
    {
        if (StringUtils.isEmpty(amsAssetLocation.getEnabled()))
        {
            amsAssetLocation.setEnabled(ENABLED_YES);
        }
    }
}
