package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.entity.RunningMonitoring;
import com.lightinit.hsdataplatform.mapper.RunningMonitoringMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.ResultStateCode;
import com.lightinit.hsdataplatform.model.admin.ApiRuningMonitoringModel;
import com.lightinit.hsdataplatform.model.admin.MicroappRuningModel;
import com.lightinit.hsdataplatform.model.admin.UserLogRuningModel;
import com.lightinit.hsdataplatform.service.admin.IRuningMonitoringService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RuningMonitoringServiceImpl implements IRuningMonitoringService {
    @Autowired
    private RunningMonitoringMapper  runningMonitoringMapper;
    @Override
    public ApiRuningMonitoringModel queryAPIType() {

        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return runningMonitoringMapper.queryAPIType();
    }

    @Override
    public MicroappRuningModel queryMicroappType() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return runningMonitoringMapper.queryMicroappType();
    }

    @Override
    public UserLogRuningModel queryUserLogType() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return runningMonitoringMapper.queryUserLogType();
    }

    @Override
    public int del(Long id) {
        return runningMonitoringMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ResultState<Long> add(ApiRuningMonitoringModel inputModel) {
        ResultState<Long> resultState = new ResultState<Long>();
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        inputModel.setCreatedAt(new Date());
        RunningMonitoring entity = new RunningMonitoring();
        BeanUtils.copyProperties(inputModel, entity);
        long sqlResult = runningMonitoringMapper.insert(entity);
        if (!(sqlResult > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据插入失败");
        }
        resultState.setData(entity.getId());
        return resultState;
    }
    @Override
    public ResultState<Long> add(MicroappRuningModel inputModel) {
        ResultState<Long> resultState = new ResultState<Long>();
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        inputModel.setCreatedAt(new Date());
        RunningMonitoring entity = new RunningMonitoring();
        BeanUtils.copyProperties(inputModel, entity);
        long sqlResult = runningMonitoringMapper.insert(entity);
        if (!(sqlResult > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据插入失败");
        }
        resultState.setData(entity.getId());
        return resultState;
    }
    @Override
    public ResultState<Long> add(UserLogRuningModel inputModel) {
        ResultState<Long> resultState = new ResultState<Long>();
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        inputModel.setCreatedAt(new Date());
        RunningMonitoring entity = new RunningMonitoring();
        BeanUtils.copyProperties(inputModel, entity);
        long sqlResult = runningMonitoringMapper.insert(entity);
        if (!(sqlResult > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据插入失败");
        }
        resultState.setData(entity.getId());
        return resultState;
    }

}
