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.AppTrayConveyLocationMapper;
import org.wcs.mapper.intf.AppTrayConveyLocationService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.trayConveyLocation.QueryTrayConveyLocationReq;
import org.wcs.model.po.app.AppTrayConveyLocation;
import org.wcs.utils.AppStringUtils;

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

@Service
public class AppTrayConveyLocationDao extends ServiceImpl<AppTrayConveyLocationMapper, AppTrayConveyLocation> implements AppTrayConveyLocationService {

    /**
     * 分页条件查询
     * @param request 查询条件
     * @param pageSize 每页数量
     * @param pageIndex 页码
     * @return 查询结果
     */
    @Override
    public Tuple2<Long, List<AppTrayConveyLocation>> queryTrayConveyLocation(QueryTrayConveyLocationReq request, Integer pageSize, Integer pageIndex) {
        if(pageSize == null || pageSize <= 0 || pageIndex == null || pageIndex <= 0) {
            return null;
        }
        LambdaQueryWrapper<AppTrayConveyLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getLocationId()), AppTrayConveyLocation::getLocationId, request.getLocationId());
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getBusinessLocationId()), AppTrayConveyLocation::getBusinessLocationId, request.getBusinessLocationId());
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getLocationName()), AppTrayConveyLocation::getLocationName, request.getLocationName());
        queryWrapper.eq(request.getLocationStatus() !=  null, AppTrayConveyLocation::getLocationStatus, request.getLocationStatus());
        queryWrapper.eq(request.getLocationType() != null, AppTrayConveyLocation::getLocationType, request.getLocationType());
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getAreaId()), AppTrayConveyLocation::getAreaId, request.getAreaId());
        queryWrapper.orderByAsc(AppTrayConveyLocation::getLocationId);
        Page<AppTrayConveyLocation> page = new Page<>(pageIndex, pageSize);
        try {
            Page<AppTrayConveyLocation> pageResult = super.baseMapper.selectPage(page, queryWrapper);
            return new Tuple2<>(pageResult.getTotal(), pageResult.getRecords());
        } catch (Exception ignored) {
            return null;
        }
    }

    /**
     * 条件查询 ---- 不分页
     * @param request 查询条件
     * @return 查询结果
     */
    @Override
    public List<AppTrayConveyLocation> queryTrayConveyLocation(QueryTrayConveyLocationReq request) {
        LambdaQueryWrapper<AppTrayConveyLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getLocationId()), AppTrayConveyLocation::getLocationId, request.getLocationId());
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getBusinessLocationId()), AppTrayConveyLocation::getBusinessLocationId, request.getBusinessLocationId());
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getLocationName()), AppTrayConveyLocation::getLocationName, request.getLocationName());
        queryWrapper.eq(request.getLocationStatus() !=  null, AppTrayConveyLocation::getLocationStatus, request.getLocationStatus());
        queryWrapper.eq(request.getLocationType() != null, AppTrayConveyLocation::getLocationType, request.getLocationType());
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getAreaId()), AppTrayConveyLocation::getAreaId, request.getAreaId());
        queryWrapper.orderByAsc(AppTrayConveyLocation::getLocationId);
        try {
            return super.list(queryWrapper);
        } catch (Exception ignored) {
            return null;
        }
    }

    /**
     * 插入数据
     * @param trayConveyLocation 插入数据
     * @return 插入结果
     */
    @Override
    public int insert(AppTrayConveyLocation trayConveyLocation) {
        try {
            return super.baseMapper.insert(trayConveyLocation);
        } catch (Exception ignored) {
            return 0;
        }
    }

    /**
     * 更新数据
     * @param trayConveyLocation 更新数据
     * @return 更新结果
     */
    @Override
    public int update(AppTrayConveyLocation trayConveyLocation) {
        LambdaUpdateWrapper<AppTrayConveyLocation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppTrayConveyLocation::getId, trayConveyLocation.getId());
        updateWrapper.set(AppStringUtils.isNotEmpty(trayConveyLocation.getLocationId()), AppTrayConveyLocation::getLocationId, trayConveyLocation.getLocationId())
                .set(AppStringUtils.isNotEmpty(trayConveyLocation.getBusinessLocationId()), AppTrayConveyLocation::getBusinessLocationId, trayConveyLocation.getBusinessLocationId())
                .set(AppStringUtils.isNotEmpty(trayConveyLocation.getLocationName()), AppTrayConveyLocation::getLocationName, trayConveyLocation.getLocationName())
                .set(trayConveyLocation.getLocationStatus() != null, AppTrayConveyLocation::getLocationStatus, trayConveyLocation.getLocationStatus())
                .set(trayConveyLocation.getLocationType() != null, AppTrayConveyLocation::getLocationType, trayConveyLocation.getLocationType())
                .set(trayConveyLocation.getPlcId() != null, AppTrayConveyLocation::getPlcId, trayConveyLocation.getPlcId())
                .set(AppStringUtils.isNotEmpty(trayConveyLocation.getLocationXyz()), AppTrayConveyLocation::getLocationXyz, trayConveyLocation.getLocationXyz())
                .set(AppStringUtils.isNotEmpty(trayConveyLocation.getReadStatusAddress()), AppTrayConveyLocation::getReadStatusAddress, trayConveyLocation.getReadStatusAddress())
                .set(AppStringUtils.isNotEmpty(trayConveyLocation.getWriteTaskAddress()), AppTrayConveyLocation::getWriteTaskAddress, trayConveyLocation.getWriteTaskAddress())
                .set(AppTrayConveyLocation::getUpdateTime, LocalDateTime.now())
                .set(AppStringUtils.isNotEmpty(trayConveyLocation.getRemark()), AppTrayConveyLocation::getRemark, trayConveyLocation.getRemark());
        try{
            return super.update(updateWrapper) ? 1 : 0;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 查询所有托盘输送机信息
     * @return 查询结果
     */
    @Override
    public List<AppTrayConveyLocation> queryAll() {
        try {
            return super.list();
        } catch (Exception ignored) {
            return null;
        }
    }

    /**
     * 根据locationId查询托盘输送机信息
     * @param locationId locationId
     * @return 查询结果
     */
    @Override
    public List<AppTrayConveyLocation> getDataByLocationId(String locationId) {
        LambdaQueryWrapper<AppTrayConveyLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppTrayConveyLocation::getLocationId, locationId);
        try{
            return super.list(queryWrapper);
        } catch (Exception ignored) {
            return null;
        }
    }
}
