package com.wms_main.service.controller.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wms_main.dao.ITAppLocationService;
import com.wms_main.model.dto.query.LocationQuery;
import com.wms_main.model.dto.response.wms.BaseWmsApiResponse;
import com.wms_main.model.dto.response.wms.WmsApiResponse;
import com.wms_main.model.po.TAppLocation;
import com.wms_main.repository.utils.StringUtils;
import com.wms_main.service.controller.ILocationControllerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 库位控制类服务实现
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class LocationControllerServiceImpl implements ILocationControllerService {
    private final ITAppLocationService appLocationService;// 库位服务

    /**
     * 实现
     * @param locationQuery 查询参数
     * @return 处理结果
     */
    @Override
    @Transactional
    public BaseWmsApiResponse genLocations(LocationQuery locationQuery) {
        List<TAppLocation> newLocationList = new ArrayList<>();
        for (int row = 1; row <= locationQuery.getLRow(); row++) {
            for (int col = 1; col <= locationQuery.getLCol(); col++) {
                for (int layer = 1; layer <= locationQuery.getLLayer(); layer++) {
                    for (int depth = 1; depth <= locationQuery.getLDepth(); depth++) {
                        String locationId = locationQuery.getSubArea() + StringUtils.padLeft(String.valueOf(row), "0", 2)
                                + "-" + StringUtils.padLeft(String.valueOf(col), "0", 2)
                                + "-" + StringUtils.padLeft(String.valueOf(layer), "0", 2)
                                + "-" + StringUtils.padLeft(String.valueOf(depth), "0", 2);
                        TAppLocation newLocation = new TAppLocation(
                                locationId,
                                "",
                                1,
                                0,
                                0,
                                0,
                                (row + 1)/2,
                                (row + 1)/2,
                                row,
                                col,
                                layer,
                                depth,
                                "",
                                locationQuery.getAreaId() != null ? locationQuery.getAreaId() : 1,
                                ""
                        );
                        newLocationList.add(newLocation);
                    }
                }
            }
        }
        appLocationService.saveBatch(newLocationList);
        return BaseWmsApiResponse.success("成功创建了" + newLocationList.size() + "个库位。");
    }

    @Override
    public WmsApiResponse<Long> getUsedLocations(Integer equipmentId, Integer locationType) {
        Long res = appLocationService.count(
                new LambdaQueryWrapper<TAppLocation>()
                        .eq(TAppLocation::getEquipmentId, equipmentId)
                        .eq(TAppLocation::getLocationType, locationType)
                        .eq(TAppLocation::getIsOccupy, 1)
        );
        return WmsApiResponse.success(res);
    }
}
