yangys
2024-04-04 2cf2921440e7473ae50796c2cb688f609b3a7995
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.qianwen.core.mp.base;
 
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.validation.constraints.NotEmpty;
import com.qianwen.core.mp.base.BaseEntity;
import com.qianwen.core.secure.BladeUser;
import com.qianwen.core.secure.utils.AuthUtil;
import com.qianwen.core.tool.utils.BeanUtil;
import com.qianwen.core.tool.utils.ClassUtil;
import com.qianwen.core.tool.utils.DateUtil;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.core.tool.utils.ObjectUtil;
import com.qianwen.core.tool.utils.ReflectUtil;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
@Validated
/* loaded from: blade-starter-mybatis-9.3.0.0-SNAPSHOT.jar:org/springblade/core/mp/base/BaseServiceImpl.class */
public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> implements BaseService<T> {
    public boolean save(T entity) {
        resolveEntity(entity);
        return super.save(entity);
      }
      
      public boolean saveBatch(Collection<T> entityList, int batchSize) {
        entityList.forEach(this::resolveEntity);
        return super.saveBatch(entityList, batchSize);
      }
      
      public boolean updateById(T entity) {
        resolveEntity(entity);
        return super.updateById(entity);
      }
      
      public boolean updateBatchById(Collection<T> entityList, int batchSize) {
        entityList.forEach(this::resolveEntity);
        return super.updateBatchById(entityList, batchSize);
      }
      
      public boolean saveOrUpdate(T entity) {
        if (entity.getId() == null)
          return save(entity); 
        return updateById(entity);
      }
      
      public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
        entityList.forEach(this::resolveEntity);
        return super.saveOrUpdateBatch(entityList, batchSize);
      }
 
    @Override // org.springblade.core.mp.base.BaseService
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteLogic(@NotEmpty List<Long> ids) {
        BladeUser user = AuthUtil.getUser();
        List<T> list = new ArrayList<>();
        ids.forEach(id -> {
              //BaseEntity baseEntity = (BaseEntity)BeanUtil.newInstance(currentModelClass());
              T baseEntity = (T) BeanUtil.newInstance(currentModelClass());
              if (user != null)
                baseEntity.setUpdateUser(user.getUserId()); 
              baseEntity.setUpdateTime(DateUtil.now());
              baseEntity.setId(id);
              list.add(baseEntity);
            });
        return (updateBatchById(list) && removeByIds(ids));
    }
 
    @Override // org.springblade.core.mp.base.BaseService
    public boolean changeStatus(@NotEmpty List<Long> ids, Integer status) {
        BladeUser user = AuthUtil.getUser();
        List<T> list = new ArrayList<>();
        ids.forEach(id -> {
            //BaseEntity baseEntity = (BaseEntity) BeanUtil.newInstance(currentModelClass());
            T baseEntity = (T) BeanUtil.newInstance(currentModelClass());
            if (user != null) {
                baseEntity.setUpdateUser(user.getUserId());
            }
            baseEntity.setUpdateTime(DateUtil.now());
            baseEntity.setId(id);
            baseEntity.setStatus(status);
            list.add(baseEntity);
        });
        return super.updateBatchById(list);
    }
 
    private void resolveEntity(T entity) {
        try {
            BladeUser user = AuthUtil.getUser();
            Date now = DateUtil.now();
            if (entity.getId() == null) {
                if (user != null) {
                    entity.setCreateUser(user.getUserId());
                    if (entity.getCreateDept() == null) {
                        entity.setCreateDept(Func.firstLong(user.getDeptId()));
                    }
                    entity.setUpdateUser(user.getUserId());
                }
                if (entity.getStatus() == null) {
                    entity.setStatus(1);
                }
                entity.setCreateTime(now);
            } else if (user != null) {
                entity.setUpdateUser(user.getUserId());
            }
            entity.setUpdateTime(now);
            entity.setIsDeleted(0);
            Field field = ReflectUtil.getField(entity.getClass(), "tenantId");
            if (ObjectUtil.isNotEmpty(field)) {
                Method getTenantId = ClassUtil.getMethod(entity.getClass(), "getTenantId", new Class[0]);
                String tenantId = String.valueOf(getTenantId.invoke(entity, new Object[0]));
                // String tenantId = String.valueOf(getTenantId.invoke(entity, new Object[0]));
                if (ObjectUtil.isEmpty(tenantId)) {
                    Method setTenantId = ClassUtil.getMethod(entity.getClass(), "setTenantId",
                            new Class[] { String.class });
 
                    setTenantId.invoke(entity, new Object[] { null });
                }
            }
        } catch (Throwable ex) {
            throw new RuntimeException(ex);
        }
    }
}