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.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.wcs.mapper.AppStackerInfoMapper;
import org.wcs.mapper.intf.AppStackerInfoService;
import org.wcs.model.po.app.AppStackerInfo;
import org.wcs.utils.AppStringUtils;

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

@Service
public class AppStackerInfoDao extends ServiceImpl<AppStackerInfoMapper, AppStackerInfo> implements AppStackerInfoService {

    /**
     * 根据堆垛机名称模糊查询堆垛机信息
     * @param stackerName 堆垛机名称
     * @return 查询结果
     */
    @Override
    public List<AppStackerInfo> queryStackerInfoIndistinct(String stackerName) {
        if(AppStringUtils.isEmpty(stackerName)) {
            try {
                return super.list();
            } catch (Exception ex) {
                return null;
            }
        }
        LambdaQueryWrapper<AppStackerInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AppStackerInfo::getStackerName, stackerName);
        queryWrapper.orderByAsc(AppStackerInfo::getStackerId);
        try {
            return super.list(queryWrapper);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 根据堆垛机ID查询堆垛机信息
     * @param stackerId 堆垛机ID
     * @return 查询结果
     */
    @Override
    public List<AppStackerInfo> queryStackerInfoById(Integer stackerId) {
        if(stackerId == null) {
            return null;
        }
        LambdaQueryWrapper<AppStackerInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppStackerInfo::getStackerId, stackerId);
        try {
            return super.list(queryWrapper);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 根据堆垛机状态查询堆垛机信息
     * @param status 堆垛机状态
     * @return 查询结果
     */
    @Override
    public List<AppStackerInfo> queryStackerInfoByStatus(Integer status) {
        if(status == null) {
            return null;
        }
        LambdaQueryWrapper<AppStackerInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppStackerInfo::getStackerStatus, status);
        try {
            return super.list(queryWrapper);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 查询所有堆垛机信息
     * @return 查询结果
     */
    @Override
    public List<AppStackerInfo> queryAll() {
        try {
            return super.list();
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 插入堆垛机信息
     * @param appStackerInfo 堆垛机信息
     * @return 插入结果
     */
    @Override
    public int insert(AppStackerInfo appStackerInfo) {
        appStackerInfo.setCreateTime(LocalDateTime.now());
        appStackerInfo.setUpdateTime(LocalDateTime.now());
        try {
            return super.save(appStackerInfo) ? 1 : 0;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 更新堆垛机信息
     * @param appStackerInfo 要更新的信息
     * @return 更新结果
     */
    @Override
    public int update(AppStackerInfo appStackerInfo) {
        LambdaUpdateWrapper<AppStackerInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppStackerInfo::getStackerId, appStackerInfo.getStackerId());
        appStackerInfo.setUpdateTime(LocalDateTime.now());
        updateWrapper.set(AppStringUtils.isNotEmpty(appStackerInfo.getStackerName()), AppStackerInfo::getStackerName, appStackerInfo.getStackerName())
                .set(appStackerInfo.getStackerStatus() != null, AppStackerInfo::getStackerStatus, appStackerInfo.getStackerStatus())
                .set(appStackerInfo.getPlcId() != null, AppStackerInfo::getPlcId, appStackerInfo.getPlcId())
                .set(AppStringUtils.isNotEmpty(appStackerInfo.getForkStatus()), AppStackerInfo::getForkStatus, appStackerInfo.getForkStatus())
                .set(appStackerInfo.getAllowIn() != null, AppStackerInfo::getAllowIn, appStackerInfo.getAllowIn())
                .set(appStackerInfo.getAllowOut() != null, AppStackerInfo::getAllowOut, appStackerInfo.getAllowOut())
                .set(appStackerInfo.getAllowMove() != null, AppStackerInfo::getAllowMove, appStackerInfo.getAllowMove())
                .set(AppStringUtils.isNotEmpty(appStackerInfo.getActionBeat()), AppStackerInfo::getActionBeat, appStackerInfo.getActionBeat())
                .set(AppStringUtils.isNotEmpty(appStackerInfo.getReadStatusAddress()), AppStackerInfo::getReadStatusAddress, appStackerInfo.getReadStatusAddress())
                .set(AppStringUtils.isNotEmpty(appStackerInfo.getWriteTaskAddress()), AppStackerInfo::getWriteTaskAddress, appStackerInfo.getWriteTaskAddress())
                .set(AppStringUtils.isNotEmpty(appStackerInfo.getRemark()), AppStackerInfo::getRemark, appStackerInfo.getRemark());
        try {
            return super.update(appStackerInfo, updateWrapper) ? 1 : 0;
        } catch (Exception e) {
            return 0;
        }
    }
}
