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.AppBaseErrInfoMapper;
import org.wcs.mapper.intf.AppBaseErrInfoService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.baseError.QueryBaseErrorReq;
import org.wcs.model.po.app.AppBaseErrInfo;

import java.util.List;

@Service
public class AppBaseErrInfoDao extends ServiceImpl<AppBaseErrInfoMapper, AppBaseErrInfo> implements AppBaseErrInfoService {

    /**
     * 查询基础报警列表
     * @param request 查询参数
     * @param pageSize 页大小
     * @param pageIndex 页索引
     * @return 查询结果
     */
    @Override
    public Tuple2<Long, List<AppBaseErrInfo>> queryBaseErrorList(QueryBaseErrorReq request, Integer pageSize, Integer pageIndex) {
        if(request == null || pageSize == null || pageIndex == null || pageSize <= 0 || pageIndex <= 0) {
            return null;
        }
        LambdaQueryWrapper<AppBaseErrInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(request.getEquipmentType() != null, AppBaseErrInfo::getEquipmentType, request.getEquipmentType());
        queryWrapper.like(request.getErrCode() != null, AppBaseErrInfo::getErrCode, request.getErrCode());
        queryWrapper.eq(request.getErrLevel() != null, AppBaseErrInfo::getErrLevel, request.getErrLevel());
        queryWrapper.eq(request.getErrType() != null, AppBaseErrInfo::getErrType, request.getErrType());
        queryWrapper.and(request.getErrMsg() != null,
                q -> q.like(AppBaseErrInfo::getErrMsg, request.getErrMsg())
                        .or()
                        .like(AppBaseErrInfo::getSuggest, request.getErrMsg())
                        .or()
                        .like(AppBaseErrInfo::getRemark, request.getErrMsg()));
        queryWrapper.orderByAsc(AppBaseErrInfo::getEquipmentType);
        Page<AppBaseErrInfo> page = new Page<>(pageIndex, pageSize);
        try {
            Page<AppBaseErrInfo> pageResult = super.baseMapper.selectPage(page, queryWrapper);
            return new Tuple2<>(pageResult.getTotal(), pageResult.getRecords());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据设备状态和错误编号查询基础报警列表
     * @param equipmentType 设备类型
     * @param errCode 错误码
     * @return 列表
     */
    @Override
    public List<AppBaseErrInfo> queryBaseErrorList(Integer equipmentType, String errCode) {
        if(equipmentType == null || errCode == null) {
            return null;
        }
        LambdaQueryWrapper<AppBaseErrInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppBaseErrInfo::getEquipmentType, equipmentType);
        queryWrapper.like(AppBaseErrInfo::getErrCode, errCode);
        try {
            return super.baseMapper.selectList(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 添加基础错误
     * @param baseErrorInfo 错误信息
     * @return 添加结果
     */
    @Override
    public int addBaseError(AppBaseErrInfo baseErrorInfo) {
        try {
            return super.baseMapper.insert(baseErrorInfo);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 批量添加基础错误
     * @param baseErrorInfoList 错误信息列表
     * @return 添加结果
     */
    @Override
    public boolean addBaseErrorList(List<AppBaseErrInfo> baseErrorInfoList) {
        try {
            return super.saveBatch(baseErrorInfoList);
        } catch (Exception e) {
            log.error("批量添加基础错误失败", e);
            return false;
        }
    }

    /**
     * 根据设备类型删除基础错误
     * @param equipmentType 设备类型
     * @return 删除结果
     */
    @Override
    public int deleteBaseErrorByEquipmentType(Integer equipmentType) {
        try {
            LambdaQueryWrapper<AppBaseErrInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AppBaseErrInfo::getEquipmentType, equipmentType);
            return super.baseMapper.delete(queryWrapper);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 根据id查询基础错误
     * @param id id
     * @return 列表
     */
    @Override
    public List<AppBaseErrInfo> queryById(String id) {
        try {
            LambdaQueryWrapper<AppBaseErrInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AppBaseErrInfo::getId, id);
            return super.baseMapper.selectList(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据id删除基础错误
     * @param id id
     * @return 删除结果
     */
    @Override
    public int deleteById(String id) {
        try {
            LambdaQueryWrapper<AppBaseErrInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AppBaseErrInfo::getId, id);
            return super.baseMapper.delete(queryWrapper);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 更新基础错误
     * @param baseErrorInfo 错误信息
     * @return 更新结果
     */
    @Override
    public int updateBaseError(AppBaseErrInfo baseErrorInfo) {
        LambdaUpdateWrapper<AppBaseErrInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppBaseErrInfo::getId, baseErrorInfo.getId());
        updateWrapper.set(baseErrorInfo.getEquipmentType() != null, AppBaseErrInfo::getEquipmentType, baseErrorInfo.getEquipmentType());
        updateWrapper.set(baseErrorInfo.getErrCode() != null, AppBaseErrInfo::getErrCode, baseErrorInfo.getErrCode());
        updateWrapper.set(baseErrorInfo.getErrLevel() != null, AppBaseErrInfo::getErrLevel, baseErrorInfo.getErrLevel());
        updateWrapper.set(baseErrorInfo.getErrType() != null, AppBaseErrInfo::getErrType, baseErrorInfo.getErrType());
        updateWrapper.set(baseErrorInfo.getErrMsg() != null, AppBaseErrInfo::getErrMsg, baseErrorInfo.getErrMsg());
        updateWrapper.set(baseErrorInfo.getSuggest() != null, AppBaseErrInfo::getSuggest, baseErrorInfo.getSuggest());
        updateWrapper.set(baseErrorInfo.getRemark() != null, AppBaseErrInfo::getRemark, baseErrorInfo.getRemark());
        try {
            return super.baseMapper.update(updateWrapper);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 根据设备类型和错误编号查询基础错误
     * @param equipmentType 设备类型
     * @param errCode 错误编号
     * @return 列表
     */
    @Override
    public List<AppBaseErrInfo> queryByEquipmentTypeAndErrorCode(Integer equipmentType, String errCode) {
        LambdaQueryWrapper<AppBaseErrInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppBaseErrInfo::getEquipmentType, equipmentType);
        queryWrapper.eq(AppBaseErrInfo::getErrCode, errCode);
        queryWrapper.orderByAsc(AppBaseErrInfo::getEquipmentType);
        try {
            return super.baseMapper.selectList(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }
}
