package com.hw.wms.service.impl;

import java.util.List;

import com.hw.common.core.constant.WmsConstants;
import com.hw.common.core.enums.WmsLocationStatus;
import com.hw.common.core.exception.ServiceException;
import com.hw.common.core.utils.DateUtils;
import com.hw.common.security.utils.SecurityUtils;
import com.hw.wms.domain.WmsBaseWarehouse;
import com.hw.wms.mapper.WmsBaseWarehouseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hw.wms.mapper.WmsBaseLocationMapper;
import com.hw.wms.domain.WmsBaseLocation;
import com.hw.wms.service.IWmsBaseLocationService;

/**
 * 库位Service业务层处理
 *
 * @author xs
 * @date 2023-12-20
 */
@Service
public class WmsBaseLocationServiceImpl implements IWmsBaseLocationService {
    @Autowired
    private WmsBaseLocationMapper wmsBaseLocationMapper;

    @Autowired
    private WmsBaseWarehouseMapper wmsBaseWarehouseMapper;

    /**
     * 查询库位
     *
     * @param locationId 库位主键
     * @return 库位
     */
    @Override
    public WmsBaseLocation selectWmsBaseLocationByLocationId(Long locationId) {
        return wmsBaseLocationMapper.selectWmsBaseLocationByLocationId(locationId);
    }

    /**
     * 查询库位列表
     *
     * @param wmsBaseLocation 库位
     * @return 库位
     */
    @Override
    public List<WmsBaseLocation> selectWmsBaseLocationList(WmsBaseLocation wmsBaseLocation) {
        return wmsBaseLocationMapper.selectWmsBaseLocationList(wmsBaseLocation);
    }

    /**
     * 新增库位
     *
     * @param wmsBaseLocation 库位
     * @return 结果
     */
    @Override
    public int insertWmsBaseLocation(WmsBaseLocation wmsBaseLocation) {
        wmsBaseLocation.setCreateTime(DateUtils.getNowDate());
        wmsBaseLocation.setCreateBy(SecurityUtils.getUsername());
        WmsBaseWarehouse warehouse = wmsBaseWarehouseMapper.selectWmsBaseWarehouseByWarehouseId(wmsBaseLocation.getWarehouseId());
        wmsBaseLocation.setWarehouseFloor(warehouse.getWarehouseFloor());
        return wmsBaseLocationMapper.insertWmsBaseLocation(wmsBaseLocation);
    }

    /**
     * 修改库位
     *
     * @param wmsBaseLocation 库位
     * @return 结果
     */
    @Override
    public int updateWmsBaseLocation(WmsBaseLocation wmsBaseLocation) {
        wmsBaseLocation.setUpdateTime(DateUtils.getNowDate());
        return wmsBaseLocationMapper.updateWmsBaseLocation(wmsBaseLocation);
    }

    /**
     * 批量删除库位
     *
     * @param locationIds 需要删除的库位主键
     * @return 结果
     */
    @Override
    public int deleteWmsBaseLocationByLocationIds(Long[] locationIds) {
        return wmsBaseLocationMapper.deleteWmsBaseLocationByLocationIds(locationIds);
    }

    /**
     * 删除库位信息
     *
     * @param locationId 库位主键
     * @return 结果
     */
    @Override
    public int deleteWmsBaseLocationByLocationId(Long locationId) {
        return wmsBaseLocationMapper.deleteWmsBaseLocationByLocationId(locationId);
    }


    /**
     * 查询库位列表Join warehouse
     *
     * @param wmsBaseLocation 库位
     * @return 库位
     */
    @Override
    public List<WmsBaseLocation> selectWmsBaseLocationJoinList(WmsBaseLocation wmsBaseLocation) {
        return wmsBaseLocationMapper.selectWmsBaseLocationJoinList(wmsBaseLocation);
    }


    /**
     * 锁定库位
     *
     * @param locationId 库位ID
     * @return 结果
     */
    @Override
    public int lockWmsBaseLocation(Long locationId) {
//        wmsBaseLocation.setCreateTime(DateUtils.getNowDate());
//        return wmsBaseLocationMapper.insertWmsBaseLocation(wmsBaseLocation);
        WmsBaseLocation baseLocation = wmsBaseLocationMapper.selectWmsBaseLocationByLocationId(locationId);
        String locationStatus = baseLocation.getLocationStatus();
        if(locationStatus.equals(WmsLocationStatus.MANUALLOCK.getCode())){
            return 1;
        }
        if(!locationStatus.equals(WmsLocationStatus.NORMAL.getCode())){
            throw new ServiceException("库位状态为"+WmsLocationStatus.locationStatusMap.get(locationStatus)+"，不能锁定");
        }

        baseLocation.setLocationStatus(WmsLocationStatus.MANUALLOCK.getCode());
        return wmsBaseLocationMapper.updateWmsBaseLocation(baseLocation);
    }

    /**
     * 解锁库位
     *
     * @param locationId 库位ID
     * @return 结果
     */
    @Override
    public int unlockWmsBaseLocation(Long locationId) {
        WmsBaseLocation baseLocation = wmsBaseLocationMapper.selectWmsBaseLocationByLocationId(locationId);
        String locationStatus = baseLocation.getLocationStatus();
        if(locationStatus.equals(WmsLocationStatus.NORMAL.getCode())){
            return 1;
        }
        if(!locationStatus.equals(WmsLocationStatus.MANUALLOCK.getCode())){
            throw new ServiceException("库位状态为"+WmsLocationStatus.locationStatusMap.get(locationStatus)+"，不能解锁");
        }

        baseLocation.setLocationStatus(WmsLocationStatus.NORMAL.getCode());
        return wmsBaseLocationMapper.updateWmsBaseLocation(baseLocation);
    }


}
