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.util.UserContext;
import com.kcim.dao.mapper.ShareParamTypeMapMapper;
import com.kcim.dao.model.ShareParamTypeMap;
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-01-15 10:08
 **/
@Repository
public class ShareParamTypeMapRepository extends ServiceImpl<ShareParamTypeMapMapper, ShareParamTypeMap> {
    public ShareParamTypeMap getByShareParamCode(String shareParamCode) {
        LambdaQueryWrapper<ShareParamTypeMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShareParamTypeMap::getHospId, UserContext.getHospId());
        queryWrapper.eq(ShareParamTypeMap::getShareParamCode,shareParamCode);
        return this.getOne(queryWrapper);
    }

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

    }

    public List<ShareParamTypeMap> getList(String filter) {
        LambdaQueryWrapper<ShareParamTypeMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShareParamTypeMap::getHospId, UserContext.getHospId());
        if(!StringUtils.isEmpty(filter)){
            queryWrapper.and(q->q.like(ShareParamTypeMap::getShareParamCode,filter).or().like(ShareParamTypeMap::getShareParamName,filter));
        }
        return this.list(queryWrapper);

    }

    public List<ShareParamTypeMap>  getList() {
        LambdaQueryWrapper<ShareParamTypeMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShareParamTypeMap::getHospId, UserContext.getHospId());
        return this.list(queryWrapper);
    }
}
