package com.njcn.gather.system.cfg.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.gather.system.cfg.mapper.SysTestConfigMapper;
import com.njcn.gather.system.cfg.pojo.param.SysTestConfigParam;
import com.njcn.gather.system.cfg.pojo.po.SysTestConfig;
import com.njcn.gather.system.cfg.service.ISysTestConfigService;
import com.njcn.gather.system.dictionary.pojo.po.DictData;
import com.njcn.gather.system.dictionary.service.IDictDataService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author caozehui
 * @date 2024-11-16
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SysTestConfigServiceImpl extends ServiceImpl<SysTestConfigMapper, SysTestConfig> implements ISysTestConfigService {

    private final IDictDataService dictDataService;


    @Override
    @Transactional
    public boolean addTestConfig(String scene) {
        SysTestConfig sysTestConfig = new SysTestConfig();
        sysTestConfig.setAutoGenerate(1);
        // 最大被检次数默认为3次
        sysTestConfig.setMaxTime(3);
        //sysTestConfig.setDataRule("46cf964bd76fb12a19cfb1700442eeeb"); // 任意值
        sysTestConfig.setScene(scene);
        sysTestConfig.setState(DataStateEnum.ENABLE.getCode());
        return this.save(sysTestConfig);
    }

    @Override
    @Transactional
    public boolean updateTestConfig(SysTestConfigParam.UpdateParam param) {
        SysTestConfig oneConfig = this.getOneConfig();
        oneConfig.setAutoGenerate(ObjectUtil.isNotNull(param.getAutoGenerate()) ? param.getAutoGenerate() : oneConfig.getAutoGenerate());
        oneConfig.setScale(ObjectUtil.isNotNull(param.getScale()) ? param.getScale() : oneConfig.getScale());
        oneConfig.setMaxTime(ObjectUtil.isNotNull(param.getMaxTime()) ? param.getMaxTime() : oneConfig.getMaxTime());
        oneConfig.setScene(StringUtils.isNotBlank(param.getScene()) ? param.getScene() : oneConfig.getScene());
        oneConfig.setCoefficient(param.getCoefficient());
        return this.updateById(oneConfig);
    }

    @Override
    public SysTestConfig getOneConfig() {
        QueryWrapper<SysTestConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", DataStateEnum.ENABLE.getCode());
        queryWrapper.last("LIMIT 1");
        return this.getOne(queryWrapper);
    }

    @Override
    public String getCurrrentScene() {
        String scene = getOneConfig().getScene();
        DictData dictData = dictDataService.getDictDataById(scene);
        if (ObjectUtil.isNotNull(dictData)) {
            return dictData.getValue();
        }
        return null;
    }

    @Override
    public Integer getAutoGenerate() {
        return getOneConfig().getAutoGenerate();
    }
}
