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.AppConveyLocationMapper;
import org.wcs.mapper.intf.AppConveyLocationService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.convey.QueryConveyLocationReq;
import org.wcs.model.po.app.AppConveyLocation;
import org.wcs.utils.AppStringUtils;

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

@Service
public class AppConveyLocationDao extends ServiceImpl<AppConveyLocationMapper, AppConveyLocation> implements AppConveyLocationService {

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

    /**
     * 根据locationId查询
     * @param locationId locationId
     * @return AppConveyLocation
     */
    @Override
    public List<AppConveyLocation> queryByLocationId(String locationId) {
        LambdaQueryWrapper<AppConveyLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppConveyLocation::getLocationId, locationId);
        queryWrapper.orderByAsc(AppConveyLocation::getLocationId);
        try{
            return super.list(queryWrapper);
        } catch (Exception e){
            return null;
        }
    }

    /**
     * 插入一条数据
     * @param appConveyLocation 要插入的数据
     * @return 插入结果
     */
    @Override
    public boolean insertConveyLocation(AppConveyLocation appConveyLocation) {
        if(appConveyLocation == null) {
            return false;
        }
        appConveyLocation.setCreateTime(LocalDateTime.now());
        appConveyLocation.setUpdateTime(LocalDateTime.now());
        try {
            return super.save(appConveyLocation);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 以位置号更新数据
     * @param appConveyLocation 更新的数据
     * @return 更新结果
     */
    @Override
    public boolean updateConveyLocationByLocationId(AppConveyLocation appConveyLocation) {
        if(appConveyLocation == null || AppStringUtils.isEmpty(appConveyLocation.getLocationId())) {
            return false;
        }
        appConveyLocation.setUpdateTime(LocalDateTime.now());
        LambdaUpdateWrapper<AppConveyLocation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppConveyLocation::getLocationId, appConveyLocation.getLocationId())
                .set(appConveyLocation.getBusinessLocationId() != null, AppConveyLocation::getBusinessLocationId, appConveyLocation.getBusinessLocationId())
                .set(appConveyLocation.getLocationName() != null, AppConveyLocation::getLocationName, appConveyLocation.getLocationName())
                .set(appConveyLocation.getLocationStatus() != null, AppConveyLocation::getLocationStatus, appConveyLocation.getLocationStatus())
                .set(appConveyLocation.getLocationType() != null, AppConveyLocation::getLocationType, appConveyLocation.getLocationType())
                .set(appConveyLocation.getPlcId() != null, AppConveyLocation::getPlcId, appConveyLocation.getPlcId())
                .set(appConveyLocation.getLocationRouter() != null, AppConveyLocation::getLocationRouter, appConveyLocation.getLocationRouter())
                .set(appConveyLocation.getReadStatusAddress() != null, AppConveyLocation::getReadStatusAddress, appConveyLocation.getReadStatusAddress())
                .set(appConveyLocation.getWriteTaskAddress() != null, AppConveyLocation::getWriteTaskAddress, appConveyLocation.getWriteTaskAddress())
                .set(appConveyLocation.getRemark() != null, AppConveyLocation::getRemark, appConveyLocation.getRemark());
        try {
            return super.update(updateWrapper);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 查询所有的 数据
     * @return 查询结果
     */
    @Override
    public List<AppConveyLocation> queryAll() {
        LambdaQueryWrapper<AppConveyLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(AppConveyLocation::getLocationId);
        try{
            return super.list(queryWrapper);
        } catch (Exception ex) {
            return null;
        }
    }
}
