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.AppBasePlcMapper;
import org.wcs.mapper.intf.AppBasePlcService;
import org.wcs.model.po.app.AppBasePlc;
import org.wcs.utils.AppStringUtils;

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

/**
 * 箱式线 PLC 基础信息类
 */
@Service
public class AppBasePlcDao extends ServiceImpl<AppBasePlcMapper, AppBasePlc> implements AppBasePlcService {


    /**
     * 条件模糊查询
     * @param plcName plc 名称
     * @param plcType plc 类型
     * @return 查询结果
     */
    @Override
    public List<AppBasePlc> queryIndistinct(String plcName, Integer plcType) {
        if(AppStringUtils.isEmpty(plcName) &&  plcType == null) {
            LambdaQueryWrapper<AppBasePlc> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.orderByAsc(AppBasePlc::getPlcId);
            try{
                return super.list(queryWrapper);
            } catch (Exception ex) {
                return null;
            }
        }
        LambdaQueryWrapper<AppBasePlc> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AppStringUtils.isNotEmpty(plcName), AppBasePlc::getPlcName, plcName);
        queryWrapper.eq(plcType != null, AppBasePlc::getPlcType, plcType);
        queryWrapper.orderByAsc(AppBasePlc::getPlcId);
        try {
            return super.list(queryWrapper);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 根据主键查询
     * @param plcId 主键
     * @return 查询结果
     */
    @Override
    public List<AppBasePlc> queryById(Integer plcId) {
        LambdaQueryWrapper<AppBasePlc> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppBasePlc::getPlcId, plcId);
        try {
            return super.list(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 查询所有数据
     * @return 所有数据
     */
    @Override
    public List<AppBasePlc> queryAll() {
        try {
            return super.list();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 插入一条数据
     * @param appBasePlc 数据
     * @return 插入结果
     */
    @Override
    public int insert(AppBasePlc appBasePlc) {
        appBasePlc.setCreateTime(LocalDateTime.now());
        appBasePlc.setUpdateTime(LocalDateTime.now());
        try {
            return super.save(appBasePlc) ? 1 : 0;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 更新一条数据
     * @param appBasePlc 数据
     * @return 更新结果
     */
    @Override
    public int update(AppBasePlc appBasePlc) {
        LambdaUpdateWrapper<AppBasePlc> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppBasePlc::getPlcId, appBasePlc.getPlcId());
        updateWrapper.set(AppStringUtils.isNotEmpty(appBasePlc.getPlcName()), AppBasePlc::getPlcName, appBasePlc.getPlcName())
                .set(appBasePlc.getPlcType() != null, AppBasePlc::getPlcType, appBasePlc.getPlcType())
                .set(appBasePlc.getPlcStatus() != null, AppBasePlc::getPlcStatus, appBasePlc.getPlcStatus())
                .set(AppStringUtils.isNotEmpty(appBasePlc.getIp()), AppBasePlc::getIp, appBasePlc.getIp())
                .set(appBasePlc.getPort() != null, AppBasePlc::getPort, appBasePlc.getPort())
                .set(appBasePlc.getSlot() != null, AppBasePlc::getSlot, appBasePlc.getSlot())
                .set(appBasePlc.getRack() != null, AppBasePlc::getRack, appBasePlc.getRack())
                .set(AppBasePlc::getUpdateTime, LocalDateTime.now())
                .set(AppStringUtils.isNotEmpty(appBasePlc.getRemark()), AppBasePlc::getRemark, appBasePlc.getRemark());
        try {
            return super.update(updateWrapper) ? 1 : 0;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 删除一个PLC
     * @param plcId PLCID
     * @return 删除结果
     */
    @Override
    public int deletePlc(Integer plcId) {
        try {
            return super.removeById(plcId) ? 1 : 0;
        } catch (Exception e) {
            return 0;
        }
    }
}
