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

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.biz.domain.ParamsConfig;
import com.cn.fdapfe.biz.mapper.FormulasMapper;
import com.cn.fdapfe.biz.mapper.ParamsConfigMapper;
import com.cn.fdapfe.common.utils.DateUtils;
import com.cn.fdapfe.common.utils.SecurityUtils;
import com.cn.fdapfe.common.utils.uuid.UUID;
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;
    @Resource
    private ParamsConfigMapper paramsConfigMapper;


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

        FaultPhysicalModel dataInfo = faultPhysicalModelMapper.selectById(id);
        QueryWrapper<ParamsConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(StringUtils.isNotEmpty(id),ParamsConfig::getModelId,id)
                        .orderByAsc(ParamsConfig::getParamSort);
        dataInfo.setConfigData(paramsConfigMapper.selectList(queryWrapper));
        return dataInfo;
    }

    /**
     * 查询故障物理模型管理列表
     * 
     * @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)
    {
        String uuid = UUID.randomUUID().toString();
        po.setCreateTime(DateUtils.getNowDate());
        po.setCreateBy(SecurityUtils.getUsername());
        po.setModelId(uuid);
        QueryWrapper<ParamsConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(StringUtils.isNotEmpty(uuid),ParamsConfig::getModelId,uuid);
        ParamsConfig config = paramsConfigMapper.selectOne(queryWrapper);
        if(config!=null){
            paramsConfigMapper.delete(queryWrapper);
        }
        List<ParamsConfig> configData = po.getConfigData();
        if (configData != null && !configData.isEmpty()) {
            for (ParamsConfig paramsConfig : configData) {
                paramsConfig.setModelId(uuid);
                paramsConfigMapper.insert(paramsConfig);
            }
        }
        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());
        if(byId!=null){
            QueryWrapper<ParamsConfig> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(StringUtils.isNotEmpty(po.getModelId()),ParamsConfig::getModelId,po.getModelId());
            paramsConfigMapper.delete(queryWrapper);
            for (ParamsConfig configDatum : po.getConfigData()) {
                configDatum.setModelId(po.getModelId());
                paramsConfigMapper.insert(configDatum);
            }
        }else{
            insertFaultPhysicalModel(po);
        }
        return faultPhysicalModelMapper.updateById(po);
    }

    /**
     * 批量删除故障物理模型管理
     * 
     * @param ids 需要删除的故障物理模型管理主键
     * @return 结果
     */
    @Override
    public void deleteFaultPhysicalModelByIds(String[] ids)
    {

            List<String> list = Arrays.asList(ids);
            for (String id : list){
                faultPhysicalModelMapper.deleteById(id);
                QueryWrapper<ParamsConfig> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(StringUtils.isNotEmpty(id),ParamsConfig::getModelId,id);
                paramsConfigMapper.delete(queryWrapper);
                faultPhysicalModelMapper.deleteById(id);
            }
    }

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