危化品全生命周期管理后端
“djh”
2025-04-21 437f8e2b89a18363a1073fdbb3ab99bcd840a757
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.gkhy.hazmat.system.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.hazmat.common.api.CommonPage;
import com.gkhy.hazmat.common.config.IdTableNameHandler;
import com.gkhy.hazmat.common.domain.entity.SysUser;
import com.gkhy.hazmat.common.enums.CodePrexEnum;
import com.gkhy.hazmat.common.enums.UserTypeEnum;
import com.gkhy.hazmat.common.exception.ApiException;
import com.gkhy.hazmat.common.utils.PageUtils;
import com.gkhy.hazmat.common.utils.SecurityUtils;
import com.gkhy.hazmat.system.domain.HzProduct;
import com.gkhy.hazmat.system.domain.HzProductFlow;
import com.gkhy.hazmat.system.mapper.HzProductFlowMapper;
import com.gkhy.hazmat.system.mapper.HzProductMapper;
import com.gkhy.hazmat.system.service.HzProductFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.List;
import java.util.Objects;
 
/**
 * <p>
 * 成品流向表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2024-08-06 16:03:53
 */
@Service
public class HzProductFlowServiceImpl extends ServiceImpl<HzProductFlowMapper, HzProductFlow> implements HzProductFlowService {
    @Autowired
    private HzProductMapper productMapper;
 
    @Override
    public CommonPage selectProductFlowList(HzProductFlow productFlow) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
            IdTableNameHandler.setCurrentId(productFlow.getCompanyId());
        }else {
            //设置分表id
            //todo
            IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
            productFlow.setCompanyId(currentUser.getCompanyId());
        }
        PageUtils.startPage();
        List<HzProductFlow> flowList = baseMapper.selectProductFlowList(productFlow);
        IdTableNameHandler.removeCurrentId();
        return CommonPage.restPage(flowList);
    }
 
    @Override
    public HzProductFlow selectProductFlowById(Long productFlowId) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        //设置分表id
        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        HzProductFlow productFlow = baseMapper.selectById(productFlowId);
        IdTableNameHandler.removeCurrentId();
        checkUserAllowed(productFlow,currentUser);
        return productFlow;
    }
 
 
 
    @Override
    public int deleteProductFlowById(Long productFlowId) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        //设置分表id
        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        HzProductFlow productFlow=baseMapper.selectById(productFlowId);
        if(productFlow==null){
            throw new ApiException("流向信息不存在");
        }
        checkUserAllowed(productFlow,currentUser);
        baseMapper.deleteById(productFlow);
        IdTableNameHandler.removeCurrentId();
        return 0;
    }
 
    @Override
    public List<HzProductFlow> selectAllProductFlowById(Long productFlowId) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        //设置分表id
        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        HzProductFlow productFlow=getById(productFlowId);
        if(productFlow==null){
            throw new ApiException("流向不存在");
        }
        List<HzProductFlow> hzProductFlows = baseMapper.selectAllProductFlowByProductId(productFlow.getProductId());
        IdTableNameHandler.removeCurrentId();
        return hzProductFlows;
    }
 
    @Override
    public List<HzProductFlow> selectAllProductFlowByCode(String code,Long companyId) {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        if(!code.startsWith(CodePrexEnum.GOOD.getCode())){
            throw new ApiException("条码格式不正确");
        }
        if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
            IdTableNameHandler.setCurrentId(companyId);
        }else {
            //设置分表id
            IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        }
 
        HzProduct product=productMapper.selectProductByCode(code,currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())?companyId:currentUser.getCompanyId());
        if(product==null){
            throw new ApiException("成品条码数据不存在");
        }
        List<HzProductFlow> hzProductFlows = baseMapper.selectAllProductFlowByProductId(product.getId());
        IdTableNameHandler.removeCurrentId();
        return hzProductFlows;
    }
 
    @Override
    public CommonPage<HzProductFlow> selectAllProductFlowByUser() {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        //设置分表id
        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        HzProductFlow productFlow=new HzProductFlow()
                .setCreateId(currentUser.getId())
                .setCompanyId(currentUser.getCompanyId());
        PageUtils.startPage();
        List<HzProductFlow> productFlowList = baseMapper.selectAllProductFlowByUser(productFlow);
        IdTableNameHandler.removeCurrentId();
        return CommonPage.restPage(productFlowList);
    }
 
    @Override
    public List<HzProductFlow> getAllProductFlowByProductId(Long productId,Long companyId) {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
            IdTableNameHandler.setCurrentId(companyId);
        }else {
            //设置分表id
            IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        }
        HzProduct product=productMapper.selectById(productId);
        if(product==null){
            throw new ApiException("成品数据不存在");
        }
        checkUserAllowed(new HzProductFlow().setCompanyId(product.getCompanyId()),currentUser);
        List<HzProductFlow> hzProductFlows = baseMapper.selectAllProductFlowByProductId(productId);
        IdTableNameHandler.removeCurrentId();
        return hzProductFlows;
    }
 
    public void checkUserAllowed(HzProductFlow productFlow,SysUser user) {
        if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
            throw new ApiException("管理员不能操作");
        }
        if(productFlow!=null){
            if (!user.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())) {
                if (!Objects.equals(user.getCompanyId(), productFlow.getCompanyId())) {
                    throw new ApiException("无权限操作其他企业数据");
                }
            }
        }
    }
}