package com.kcim.dao.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcim.common.constants.NumberConstant;
import com.kcim.common.util.UserContext;
import com.kcim.dao.mapper.SqlMapper;
import com.kcim.dao.model.Sql;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @program: CostAccount
 * @description:
 * @author: Wang.YS
 * @create: 2024-03-18 16:15
 **/
@Repository
public class SqlRepository extends ServiceImpl<SqlMapper,Sql> {
    public List<Sql> getByFilter(String sqlType, String sqlDefinition) {
        LambdaQueryWrapper<Sql> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Sql::getSqlType,sqlType);
        queryWrapper.eq(Sql::getHospId, UserContext.getCurrentLoginHospId());
        if(!StringUtils.isEmpty(sqlDefinition)){
            queryWrapper.like(Sql::getSqlDefinition,sqlDefinition);
        }
        return this.list(queryWrapper);
    }

    public void deleteSql(Integer id) {
        Sql byId = this.getById(id);
        if(Objects.nonNull(byId)){
            byId.setDeleteTime(new Date());
            byId.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
            this.updateById(byId);
            this.removeById(id);
        }
    }

    public List<Sql> getSqlBySqlType(String sqlType) {
        LambdaQueryWrapper<Sql> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Sql::getSqlType,sqlType);
        queryWrapper.eq(Sql::getHospId, UserContext.getCurrentLoginHospId());
        queryWrapper.eq(Sql::getStatus, NumberConstant.ONE);
        return this.list(queryWrapper);
    }
}
