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.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.wcs.mapper.AppQuartzConfigMapper;
import org.wcs.mapper.intf.AppQuartzConfigService;
import org.wcs.model.po.app.AppQuartzConfig;
import org.wcs.utils.AppStringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AppQuartzConfigDao extends ServiceImpl<AppQuartzConfigMapper, AppQuartzConfig> implements AppQuartzConfigService {

    /**
     * 查询所有
     * @return 查询结果
     */
    @Override
    public List<AppQuartzConfig> queryAll() {
        try {
            return super.list();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据类名查询
     * @param className 类名称
     * @return 数据
     */
    @Override
    public AppQuartzConfig queryByClassName(String className) {
        if(AppStringUtils.isEmpty(className)) {
            return null;
        }
        try {
            return super.getById(className);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 更具条件模糊查询
     * @param className 类名
     * @param jobDesc 工作描述
     * @return 查询结果
     */
    @Override
    public List<AppQuartzConfig> queryIndistinct(String className, String jobDesc) {
        LambdaQueryWrapper<AppQuartzConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AppStringUtils.isNotEmpty(className), AppQuartzConfig::getClassName, className);
        queryWrapper.like(AppStringUtils.isNotEmpty(jobDesc), AppQuartzConfig::getJobDesc, jobDesc);
        queryWrapper.orderByAsc(AppQuartzConfig::getCreateTime);
        try {
            return super.list(queryWrapper);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 判断是否存在
     * @param className 类名
     * @return 存在结果
     */
    @Override
    public Boolean exist(String className) {
        try {
            return super.exists(new LambdaQueryWrapper<AppQuartzConfig>().eq(AppQuartzConfig::getClassName, className));
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 插入一条数据
     * @param appQuartzConfig 数据
     * @return 插入结果
     */
    @Override
    public int insert(AppQuartzConfig appQuartzConfig) {
        appQuartzConfig.setUpdateTime(LocalDateTime.now());
        appQuartzConfig.setCreateTime(LocalDateTime.now());
        try{
            return super.save(appQuartzConfig) ? 1 : 0;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 删除数据
     * @param className 删除的名称
     * @return 删除结果
     */
    @Override
    public int delete(String className) {
        try{
            return super.removeById(className) ? 1 : 0;
        } catch (Exception e){
            return 0;
        }
    }

    /**
     * 更新数据
     * @param appQuartzConfig 数据
     * @return 更新结果
     */
    @Override
    public int update(AppQuartzConfig appQuartzConfig) {
        appQuartzConfig.setUpdateTime(LocalDateTime.now());
        LambdaUpdateWrapper<AppQuartzConfig> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppQuartzConfig::getClassName, appQuartzConfig.getClassName());
        updateWrapper.set(AppStringUtils.isNotEmpty(appQuartzConfig.getJobDesc()), AppQuartzConfig::getJobDesc, appQuartzConfig.getJobDesc())
                .set(AppStringUtils.isNotEmpty(appQuartzConfig.getCron()), AppQuartzConfig::getCron, appQuartzConfig.getCron())
                .set(appQuartzConfig.getStatus() != null, AppQuartzConfig::getStatus, appQuartzConfig.getStatus())
                .set(AppStringUtils.isNotEmpty(appQuartzConfig.getRemark()), AppQuartzConfig::getRemark, appQuartzConfig.getRemark())
                .set(AppQuartzConfig::getUpdateTime, appQuartzConfig.getUpdateTime());
        try {
            return super.update(appQuartzConfig, updateWrapper) ? 1 : 0;
        } catch (Exception e) {
            return 0;
        }
    }
}
