package org.wcs.mapper.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.wcs.mapper.AppStackerLocationMapper;
import org.wcs.mapper.intf.AppStackerLocationService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.stackerLocation.QueryStackerLocationReq;
import org.wcs.model.po.app.AppStackerLocation;
import org.wcs.utils.AppStringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AppStackerLocationDao extends ServiceImpl<AppStackerLocationMapper, AppStackerLocation> implements AppStackerLocationService {


    /**
     * 分页查询库位信息
     * @param request 查询参数
     * @param pageSize 分页大小
     * @param pageIndex 页面序号
     * @return 查询结果
     */
    @Override
    public Tuple2<Long, List<AppStackerLocation>> queryStackerLocation(QueryStackerLocationReq request, Integer pageSize, Integer pageIndex) {
        if(pageSize == null || pageSize <= 0 || pageIndex == null || pageIndex <= 0) {
            return null;
        }
        LambdaQueryWrapper<AppStackerLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getLocationId()), AppStackerLocation::getLocationId, request.getLocationId());
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getBusinessLocation()), AppStackerLocation::getBusinessLocation, request.getBusinessLocation());
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getVehicleNo()), AppStackerLocation::getVehicleNo, request.getVehicleNo());
        queryWrapper.like(request.getLocationStatus() != null, AppStackerLocation::getLocationStatus, request.getLocationStatus());
        queryWrapper.orderByAsc(AppStackerLocation::getLocationId);
        Page<AppStackerLocation> page = new Page<>(pageIndex, pageSize);
        try {
            Page<AppStackerLocation> pageResult = super.baseMapper.selectPage(page, queryWrapper);
            return new Tuple2<>(pageResult.getTotal(), pageResult.getRecords());
        } catch (Exception ignored) {
            return null;
        }
    }

    /**
     * 获取所有堆垛机货位
     */
    @Override
    public List<AppStackerLocation> queryStackerLocation(QueryStackerLocationReq request) {
        LambdaQueryWrapper<AppStackerLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getLocationId()), AppStackerLocation::getLocationId, request.getLocationId());
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getBusinessLocation()), AppStackerLocation::getBusinessLocation, request.getBusinessLocation());
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getVehicleNo()), AppStackerLocation::getVehicleNo, request.getVehicleNo());
        queryWrapper.like(request.getLocationStatus() != null, AppStackerLocation::getLocationStatus, request.getLocationStatus());
        queryWrapper.orderByAsc(AppStackerLocation::getLocationId);
        try {
            return super.list(queryWrapper);
        } catch (Exception ignored) {
            return null;
        }
    }

    /**
     * 添加库位信息
     * @param appStackerLocation 库位信息
     * @return 添加结果
     */
    @Override
    public int insert(AppStackerLocation appStackerLocation) {
        appStackerLocation.setCreateTime(LocalDateTime.now());
        appStackerLocation.setUpdateTime(LocalDateTime.now());
        try {
            return super.save(appStackerLocation) ? 1 : 0;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 根据库位ID查询库位信息
     * @param locationId 库位ID
     * @return 查询结果
     */
    @Override
    public List<AppStackerLocation> queryStackerLocationById(String locationId) {
        if(AppStringUtils.isEmpty(locationId)) {
            return null;
        }
        LambdaQueryWrapper<AppStackerLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppStackerLocation::getLocationId, locationId);
        try {
            return super.baseMapper.selectList(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 查询所有
     * @return 查询结果
     */
    @Override
    public List<AppStackerLocation> queryStackerLocationAll() {
        LambdaQueryWrapper<AppStackerLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(AppStackerLocation::getLocationId);
        try {
            return super.list(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据设备ID和车道ID删除库位信息
     * @param machineId 设备ID
     * @param laneId 车道ID
     * @param rowId 行ID
     * @return 删除结果
     */
    @Override
    public boolean deleteWithMachineIdAndLaneIdNoCatchException(Integer machineId, Integer laneId, Integer rowId) {
        LambdaQueryWrapper<AppStackerLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppStackerLocation::getMachineId, machineId);
        queryWrapper.eq(AppStackerLocation::getLaneId, laneId);
        queryWrapper.eq(AppStackerLocation::getLRow, rowId);
        return super.remove(queryWrapper);
    }

    /**
     * 批量添加库位信息
     * @param appStackerLocations 库位信息
     * @return 添加结果
     */
    @Override
    public boolean insertNoCatchException(List<AppStackerLocation> appStackerLocations) {
        if(appStackerLocations == null || appStackerLocations.isEmpty()) {
            return false;
        }
        for(AppStackerLocation appStackerLocation : appStackerLocations){
            appStackerLocation.setCreateTime(LocalDateTime.now());
            appStackerLocation.setUpdateTime(LocalDateTime.now());
        }
        return super.saveBatch(appStackerLocations);
    }

    /**
     * 修改库位信息
     * @param appStackerLocation 库位信息
     * @return 修改结果
     */
    @Override
    public boolean updateById(AppStackerLocation appStackerLocation) {
        LambdaUpdateWrapper<AppStackerLocation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppStackerLocation::getLocationId, appStackerLocation.getLocationId());
        updateWrapper.set(appStackerLocation.getLocationStatus() != null, AppStackerLocation::getLocationStatus, appStackerLocation.getLocationStatus())
                .set(appStackerLocation.getVehicleNo() != null, AppStackerLocation::getVehicleNo, appStackerLocation.getVehicleNo())
                .set(appStackerLocation.getBusinessLocation() != null, AppStackerLocation::getBusinessLocation, appStackerLocation.getBusinessLocation())
                .set(appStackerLocation.getMachineId() != null, AppStackerLocation::getMachineId, appStackerLocation.getMachineId())
                .set(appStackerLocation.getLaneId() != null, AppStackerLocation::getLaneId, appStackerLocation.getLaneId())
                .set(appStackerLocation.getLRow() != null, AppStackerLocation::getLRow, appStackerLocation.getLRow())
                .set(appStackerLocation.getLLine() != null, AppStackerLocation::getLLine, appStackerLocation.getLLine())
                .set(appStackerLocation.getLLayer() != null, AppStackerLocation::getLLayer, appStackerLocation.getLLayer())
                .set(appStackerLocation.getLocationType() != null, AppStackerLocation::getLocationType, appStackerLocation.getLocationType())
                .set(appStackerLocation.getLDepth() != null, AppStackerLocation::getLDepth, appStackerLocation.getLDepth())
                .set(appStackerLocation.getLocationTag() != null, AppStackerLocation::getLocationTag, appStackerLocation.getLocationTag())
                .set(appStackerLocation.getRemark() != null, AppStackerLocation::getRemark, appStackerLocation.getRemark())
                .set(AppStackerLocation::getUpdateTime, LocalDateTime.now());
        try {
            return super.update(updateWrapper);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 修改库位状态
     * @param locationId 库位ID
     * @param status 库位状态
     * @param vehicleNo 载具号
     * @return 修改结果
     */
    @Override
    public boolean updateStatusAndVehicleNoById(String locationId, Integer status, String vehicleNo) {
        try{
            LambdaUpdateWrapper<AppStackerLocation> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(AppStackerLocation::getLocationId, locationId)
                    .set(AppStackerLocation::getLocationStatus, status)
                    .set(AppStackerLocation::getVehicleNo, vehicleNo)
                    .set(AppStackerLocation::getUpdateTime, LocalDateTime.now());
            return super.update(updateWrapper);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 根据巷道ID查询库位信息
     * @param stackerId 堆垛机ID
     * @param laneId 巷道ID
     * @return 查询结果
     */
    @Override
    public List<AppStackerLocation> queryStackerLocationByStackerIdAndLaneId(Integer stackerId, Integer laneId) {
        LambdaQueryWrapper<AppStackerLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppStackerLocation::getMachineId, stackerId);
        queryWrapper.eq(AppStackerLocation::getLaneId, laneId);
        queryWrapper.orderByAsc(AppStackerLocation::getLocationId);
        try {
            return super.list(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }
}
