jiangping
2023-08-16 fb65ffe3606482e15bcdf1fd5adfb1acb1b8e846
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package doumeemes.service.ext.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import doumeemes.core.constants.ResponseStatus;
import doumeemes.core.exception.BusinessException;
import doumeemes.core.model.LoginUserInfo;
import doumeemes.core.model.PageData;
import doumeemes.core.model.PageWrap;
import doumeemes.core.utils.Constants;
import doumeemes.core.utils.redis.RedisUtil;
import doumeemes.dao.business.BomMapper;
import doumeemes.dao.business.ProceduresMapper;
import doumeemes.dao.business.model.Bom;
import doumeemes.dao.business.model.Procedures;
import doumeemes.dao.ext.CompanyExtMapper;
import doumeemes.dao.ext.DepartmentExtMapper;
import doumeemes.dao.ext.ProceduresExtMapper;
import doumeemes.dao.ext.dto.QueryCompanyExtDTO;
import doumeemes.dao.ext.dto.QueryProceduresExtDTO;
import doumeemes.dao.ext.vo.CompanyExtListVO;
import doumeemes.dao.ext.vo.ProceduresExtListVO;
import doumeemes.service.ext.ProceduresExtService;
import io.swagger.models.Contact;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 工序信息Service实现
 * @author 江蹄蹄
 * @date 2022/04/20 10:57
 */
@Service
public class ProceduresExtServiceImpl implements ProceduresExtService {
 
    @Autowired
    private ProceduresExtMapper proceduresExtMapper;
 
    @Autowired
    private ProceduresMapper proceduresMapper;
    @Autowired
    private DepartmentExtMapper departmentExtMapper;
 
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private CompanyExtMapper companyExtMapper;
 
    @Autowired
    private BomMapper bomMapper;
    @Override
    public PageData<ProceduresExtListVO> findPage(PageWrap<QueryProceduresExtDTO> pageWrap) {
        PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity());
        List<ProceduresExtListVO> result = proceduresExtMapper.selectList(pageWrap.getModel());
        return PageData.from(new PageInfo<>(result));
    }
    @Override
    public  void loadCom(CompanyExtListVO com ) {
        QueryProceduresExtDTO d = new QueryProceduresExtDTO();
        d.setDeleted(Constants.ZERO);
        d.setDmodelCompanyId(com.getId());
        //查询全部企业部门信息
        List<ProceduresExtListVO> clist =    proceduresExtMapper.selectList(d);
        //加入redis缓存,以企业id为编码,缓存企业二维码
        RedisUtil.addObject(redisTemplate,Constants.RedisKeys.COM_PROCEDURE_KEY+com.getId(),clist);
    }
 
    @Override
    public List<ProceduresExtListVO> getListByCondition(QueryProceduresExtDTO dto) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!");
        }
       /* //获取权限范围内的工序
        if(user.getProcedureIds()!=null){
            dto.setIds(user.getProcedureIds());
        }else{
            dto.setUserId(user.getCompanyUser().getId());
        }*/
 
        List<ProceduresExtListVO> result = proceduresExtMapper.selectList(dto);
        return result;
    }
 
    @Override
    public  void loadAll() {
        QueryCompanyExtDTO dto = new QueryCompanyExtDTO();
        dto.setDeleted(Constants.ZERO);
        //查询全部企业信息
        List<CompanyExtListVO> list =    companyExtMapper.selectList(dto);
        if(list != null && list.size()>0){
            QueryProceduresExtDTO d = new QueryProceduresExtDTO();
            d.setDeleted(Constants.ZERO);
            //查询全部企业工序信息
            List<ProceduresExtListVO> codeList =    proceduresExtMapper.selectList(d);
            for(CompanyExtListVO com : list){
                //当前企业的全部工序信息
                List<ProceduresExtListVO> clist = getAllByComId(com.getId(),codeList);
                //加入redis缓存,以企业id为编码,缓存企业工序
                RedisUtil.addObject(redisTemplate,Constants.RedisKeys.COM_PROCEDURE_KEY+com.getId(),clist);
            }
        }
    }
    /**
     * 根据企业编码获取工序
     * @param id
     * @param alllist
     * @return
     */
    private  List<ProceduresExtListVO>  getAllByComId(Integer id, List<ProceduresExtListVO> alllist) {
        List<ProceduresExtListVO> list = null;
        if(alllist!= null){
            for(ProceduresExtListVO d :alllist){
                if(d.getDmodel()!=null && Constants.equalsInteger(id,d.getDmodel().getCompanyId())){
                    if(list == null){
                        list = new ArrayList<>();
                    }
                    list.add(d);
                }
            }
        }
        return  list;
    }
 
    @Override
    public List<ProceduresExtListVO> getGYListByCondition(Integer routeId) {
        List<ProceduresExtListVO> result = proceduresExtMapper.getGYListByCondition(routeId);
        return result;
    }
    @Override
    public List<ProceduresExtListVO>  getSelfList(QueryProceduresExtDTO queryProceduresExtDTO){
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!");
        }
        List<Integer> ids =user.getProcedureIds();
        List<ProceduresExtListVO> result = new ArrayList<>();
        if(ids!=null && ids.size()>0){
            //查询缓存数据
            List<ProceduresExtListVO> clist = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_PROCEDURE_KEY+user.getCompany().getId(),ArrayList.class);
            if(clist!=null){
                for(ProceduresExtListVO p : clist){
                    if(Constants.equalsInteger(p.getDepartId(),user.getCurComDepartment().getId()) && exsistsInList(ids,p.getId())){
                        result.add(p);
                    }
                }
            }
        }
        return result;
    }
 
    private boolean exsistsInList(List<Integer> ids, Integer id) {
        for(Integer idt : ids){
            if(Constants.equalsInteger(idt,id)){
                return true;
            }
        }
        return false;
    }
 
    @Override
    public  ProceduresExtListVO getById(Integer comId, Integer id){
        if(comId==null || id == null){
            return null;
        }
        //查询缓存数据
        List<ProceduresExtListVO> clist = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_PROCEDURE_KEY+comId,ArrayList.class);
       return getModelFromList(clist,id);
    }
 
    private ProceduresExtListVO getModelFromList(List<ProceduresExtListVO> clist, Integer id) {
        if(clist!=null){
            for(ProceduresExtListVO model:clist){
                if(Constants.equalsInteger(id,model.getId())){
                    return model ;
                }
            }
        }
        return null;
    }
 
    @Override
    public  String getNamesByIds(Integer comId, List<Integer> ids){
        if(comId==null || ids == null){
            return null;
        }
        String temp = "";
        //查询缓存数据
        List<ProceduresExtListVO> clist = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_PROCEDURE_KEY+comId,ArrayList.class);
        for(Integer id:ids){
            ProceduresExtListVO model =  getModelFromList(clist,id);
            if(model !=null && StringUtils.isNotBlank(model.getName())){
                if(StringUtils.isNotBlank(temp)){
                    temp+=",";
                }
                temp+=StringUtils.defaultString(model.getName(),"");
            }
        }
        return temp;
 
    }
    @Override
    public  String getNamesByIdStr(Integer comId,String idStr){
        if(comId==null || idStr == null){
            return null;
        }
        String[] ids = idStr.split(",");
        List<Integer> idList = new ArrayList<>();
        for(String s : ids){
            idList.add(Integer.parseInt(s));
        }
        return getNamesByIds(comId,idList);
    }
 
    @Override
    public List<Procedures> getListByMaterialId(Integer materialId){
        //获取物料BOM信息
        List<Bom> bomList = bomMapper.selectList(new QueryWrapper<Bom>().eq("MATERIAL_ID",materialId).eq("STATUS",1));
        if(bomList.size()>Constants.ONE){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,当前物料对应BOM数据异常,存在多条启用BOM!");
        }else if(bomList.size()== Constants.ZERO){
            return new ArrayList<>();
        }
        Bom bom = bomList.get(Constants.ZERO);
       List<Procedures> proceduresList = proceduresMapper.selectList(
                new QueryWrapper<Procedures>().apply(" id in ( select r.PROCEDURE_ID  from route_procedure r where r.ROUTE_ID = '"+bom.getRouteId()+"' )  ")
                        .orderByAsc( " SORTNUM  ")
        );
       return proceduresList;
    }
 
 
}