package com.cn.fdapfe.biz.service.impl;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.cn.fdapfe.common.utils.DateUtils;
import com.cn.fdapfe.common.utils.SecurityUtils;
import com.cn.fdapfe.common.utils.uuid.UUID;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cn.fdapfe.biz.mapper.FaultPhysicalModelMapper;
import com.cn.fdapfe.biz.domain.FaultPhysicalModel;
import com.cn.fdapfe.biz.service.IFaultPhysicalModelService;

import javax.annotation.Resource;

/**
 * 故障物理模型管理Service业务层处理
 * 
 * @author Allen
 * @date 2025-03-07
 */
@Service
public class FaultPhysicalModelServiceImpl implements IFaultPhysicalModelService 
{
    @Resource
    private FaultPhysicalModelMapper faultPhysicalModelMapper;

    /**
     * 查询故障物理模型管理
     * 
     * @param id 故障物理模型管理主键
     * @return 故障物理模型管理
     */
    @Override
    public FaultPhysicalModel selectFaultPhysicalModelById(String id)
    {
        return faultPhysicalModelMapper.selectById(id);
    }

    /**
     * 查询故障物理模型管理列表
     * 
     * @param po 故障物理模型管理
     * @return 故障物理模型管理
     */
    @Override
    public List<FaultPhysicalModel> selectFaultPhysicalModelList(FaultPhysicalModel po)
    {
        QueryWrapper<FaultPhysicalModel> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(StringUtils.isNotEmpty(po.getModelName()), FaultPhysicalModel::getModelName, po.getModelName())
                .eq(StringUtils.isNotEmpty(po.getModelType()), FaultPhysicalModel::getModelType, po.getModelType())
                .like(StringUtils.isNotEmpty(po.getModelPath()), FaultPhysicalModel::getModelPath, po.getModelPath())
                .eq(StringUtils.isNotEmpty(po.getModelAttribution()),FaultPhysicalModel::getModelAttribution,po.getModelAttribution())
                .orderByDesc(FaultPhysicalModel::getCreateBy);
        return faultPhysicalModelMapper.selectList(wrapper);
    }

    /**
     * 新增故障物理模型管理
     * 
     * @param po 故障物理模型管理
     * @return 结果
     */
    @Override
    public int insertFaultPhysicalModel(FaultPhysicalModel po)
    {
        po.setCreateTime(DateUtils.getNowDate());
        po.setCreateBy(SecurityUtils.getUsername());
        po.setModelId(UUID.randomUUID().toString());
        return faultPhysicalModelMapper.insert(po);
    }

    /**
     * 修改故障物理模型管理
     * 
     * @param po 故障物理模型管理
     * @return 结果
     */
    @Override
    public int updateFaultPhysicalModel(FaultPhysicalModel po)
    {
        po.setUpdateTime(DateUtils.getNowDate());
        po.setUpdateBy(SecurityUtils.getUsername());
        FaultPhysicalModel byId = faultPhysicalModelMapper.selectById(po.getModelId());

        return byId==null ?insertFaultPhysicalModel(po): faultPhysicalModelMapper.updateById(po);
    }

    /**
     * 批量删除故障物理模型管理
     * 
     * @param ids 需要删除的故障物理模型管理主键
     * @return 结果
     */
    @Override
    public int deleteFaultPhysicalModelByIds(String[] ids)
    {
        return ids!=null && ids.length == 1 ? faultPhysicalModelMapper.deleteById(ids[0]) : faultPhysicalModelMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public List<Map<String, Object>> getOptions() {
        return faultPhysicalModelMapper.getOptions();
    }
}
