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.AppStackerStandMapper;
import org.wcs.mapper.intf.AppStackerStandService;
import org.wcs.model.po.app.AppStackerStand;
import org.wcs.utils.AppStringUtils;

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

@Service
public class AppStackerStandDao extends ServiceImpl<AppStackerStandMapper, AppStackerStand> implements AppStackerStandService {

    /**
     * 根据堆垛机ID查询站台信息
     * @param stackerId 堆垛机编号
     * @return 查询结果
     */
    @Override
    public List<AppStackerStand> queryStackerStand(Integer stackerId) {
         LambdaQueryWrapper <AppStackerStand> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.eq(AppStackerStand::getStackerId, stackerId);
         queryWrapper.orderByAsc(AppStackerStand::getStandId);
         try {
             return super.list(queryWrapper);
         } catch ( Exception e) {
             return null;
         }
    }

    /**
     * 根据站台号查询站台信息
     * @param standId 站台信息
     * @return  查询结果
     */
    @Override
    public List<AppStackerStand> queryStandInfo(String standId) {
         LambdaQueryWrapper <AppStackerStand> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.eq(AppStackerStand::getStandId, standId);
         queryWrapper.orderByAsc( AppStackerStand::getStandId);
          try {
             return super.list(queryWrapper);
         } catch ( Exception e) {
             return null;
         }
    }

    /**
     * 查询所有站台信息
     * @return 查询结果
     */
    @Override
    public List<AppStackerStand> queryAll() {
        try {
            return super.list();
        } catch ( Exception e) {
            return null;
        }
    }

    /**
     * 新增站台信息
     * @param appStackerStand 站台信息
     * @return  新增结果
     */
    @Override
    public int insert(AppStackerStand appStackerStand) {
        appStackerStand.setCreateTime(LocalDateTime.now());
        appStackerStand.setUpdateTime(LocalDateTime.now());
        try {
            return super.save(appStackerStand) ? 1 : 0;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 修改站台信息
     * @param appStackerStand 修改站台信息
     * @return 修改结果
     */
    @Override
    public int update(AppStackerStand appStackerStand) {
        LambdaUpdateWrapper<AppStackerStand>  updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppStackerStand::getStackerId, appStackerStand.getStackerId())
                .eq(AppStackerStand::getStandId, appStackerStand.getStandId());
        updateWrapper.set(AppStringUtils.isNotEmpty(appStackerStand.getStandName()), AppStackerStand::getStandName, appStackerStand.getStandName())
                .set(appStackerStand.getPlcId() != null, AppStackerStand::getPlcId, appStackerStand.getPlcId())
                .set(appStackerStand.getStandStatus() != null, AppStackerStand::getStandStatus, appStackerStand.getStandStatus())
                .set(appStackerStand.getAllowIn() != null, AppStackerStand::getAllowIn, appStackerStand.getAllowIn())
                .set( appStackerStand.getAllowOut() != null, AppStackerStand::getAllowOut, appStackerStand.getAllowOut())
                .set(appStackerStand.getLaneId() != null, AppStackerStand::getLaneId, appStackerStand.getLaneId())
                .set(AppStringUtils.isNotEmpty(appStackerStand.getStandLocation()), AppStackerStand::getStandLocation, appStackerStand.getStandLocation())
                .set(AppStringUtils.isNotEmpty(appStackerStand.getStandXyz()), AppStackerStand::getStandXyz, appStackerStand.getStandXyz())
                .set(AppStringUtils.isNotEmpty(appStackerStand.getAreaId()),  AppStackerStand::getAreaId, appStackerStand.getAreaId())
                .set(appStackerStand.getInType() != null, AppStackerStand::getInType, appStackerStand.getInType())
                .set(appStackerStand.getOutType() != null, AppStackerStand::getOutType, appStackerStand.getOutType())
                .set(AppStringUtils.isNotEmpty(appStackerStand.getReadStatusAddress()),  AppStackerStand::getReadStatusAddress, appStackerStand.getReadStatusAddress())
                .set(AppStringUtils.isNotEmpty(appStackerStand.getWriteTaskAddress()),  AppStackerStand::getWriteTaskAddress, appStackerStand.getWriteTaskAddress())
                .set(AppStringUtils.isNotEmpty(appStackerStand.getRemark()),  AppStackerStand::getRemark, appStackerStand.getRemark())
                .set(AppStackerStand::getUpdateTime, LocalDateTime.now());
        try {
            return super.update(updateWrapper) ? 1 : 0;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 删除站台信息
     * @param standId 站台编号
     * @return 删除结果
     */
    @Override
    public int delete(String standId) {
        try {
            return super.removeById(standId) ? 1 : 0;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 根据站台编号查询站台信息
     * @param standIdList 站台编号
     * @return 查询结果
     */
    @Override
    public List<AppStackerStand> queryStackerStand(List<String> standIdList) {
        if(standIdList == null || standIdList.isEmpty()) {
            return null;
        }
        LambdaQueryWrapper<AppStackerStand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AppStackerStand::getStandId, standIdList);
        queryWrapper.orderByAsc(AppStackerStand::getStandId);
        try {
            return super.list(queryWrapper);
        } catch ( Exception e) {
            return null;
        }
    }
}
