jiazx0107@163.com
2023-05-17 a87eff704037284e46568c7c7b8a60caaae884c2
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package com.ld.igds.m.service;
 
import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
import com.bstek.dorado.data.provider.Page;
import com.ld.igds.constant.Constant;
import com.ld.igds.inout.InoutConstant;
import com.ld.igds.m.dto.NoticeDto;
import com.ld.igds.m.dto.NoticeParam;
import com.ld.igds.models.InoutNoticeIn;
import com.ld.igds.models.InoutNoticeOut;
import com.ld.igds.util.ContextUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.hibernate.Session;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.*;
 
@Repository
public class HInoutNoticeService extends HibernateDao {
 
    @Autowired
    private InoutCommonService inoutManagerService;
 
    public void pageQueryIn(Page<InoutNoticeIn> page, Map<String, Object> param) throws Exception {
 
        String hql = " from " + InoutNoticeIn.class.getName()
                + " where companyId =:companyId";
 
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("companyId", ContextUtil.getCompanyId());
 
        Object year = param.get("year");
        if (year == null) {
            param.put("year", DateFormatUtils.format(new Date(), "yyyy"));
        }
 
        String str = (String) param.get("deptId");
        if (StringUtils.isNotEmpty(str)) {
            hql += " and deptId =:deptId";
            args.put("deptId", str);
        }
 
        str = (String) param.get("id");
        if (StringUtils.isNotEmpty(str)) {
            hql += " and id=:id";
            args.put("id", str);
        }
 
        str = (String) param.get("customerId");
        if (StringUtils.isNotEmpty(str)) {
            hql += " and customerId =:customerId";
            args.put("customerId", str);
        }
 
        Object obj = param.get("year");
        if (null != obj) {
            hql += " and year =:year";
            args.put("year", obj + "");
        }
 
        str = (String) param.get("auditStatus");
        if (StringUtils.isNotEmpty(str)) {
            hql += " and auditStatus =:auditStatus";
            args.put("auditStatus", str);
        }
 
        str = (String) param.get("completeStatus");
        if (StringUtils.isNotEmpty(str)) {
            hql += " and completeStatus =:completeStatus";
            args.put("completeStatus", str);
        }
 
        str = (String) param.get("foodVariety");
        if (StringUtils.isNotEmpty(str)) {
            hql += " and foodVariety =:foodVariety";
            args.put("foodVariety", str);
        }
 
        String countHql = " select count(*) " + hql;
        hql += " order by createTime";
        this.pagingQuery(page, hql, countHql, args);
    }
 
    public String saveOrUpdateIn(InoutNoticeIn data) {
 
        //新增入库通知单时,需要进行唯一性判断。即保证单位、粮食品种、年份唯一,且不存在未完成状态的单子。
        if (Constant.YN_Y.equals(data.getTag())) {
            data.setCompanyId(ContextUtil.getCompanyId());
 
            NoticeParam param = new NoticeParam();
            param.setCompanyId(data.getCompanyId());
            param.setDeptId(data.getDeptId());
            param.setCustomerId(data.getCustomerId());
            param.setFoodVariety(data.getFoodVariety());
            param.setYear(data.getYear());
            //若有合同信息,添加合同信息
            if(StringUtils.isNotEmpty(data.getContractId())){
                param.setContractId(data.getContractId());
            }
            //若有计划信息,添加计划信息
            if(StringUtils.isNotEmpty(data.getPlanId())){
                param.setPlanId(data.getPlanId());
            }
            param.setCompleteStatus(InoutConstant.COMPLETE_STATUS_NONE);
            Integer integer = inoutManagerService.queryNoticeIn(param);
            if (integer > 0) {
                return "新增的入库通知单已存在,不能再次新增,请核查!";
            }
        }
 
        Session session = this.getSessionFactory().openSession();
 
        try {
            if (Constant.YN_Y.equals(data.getTag())) {
                data.setCreateUser(ContextUtil.getLoginUserCName());
                data.setAuditStatus(InoutConstant.STATUS_NONE);
                data.setCompleteStatus(InoutConstant.COMPLETE_STATUS_NONE);
                session.save(data);
            } else {
                session.update(data);
            }
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
 
    public String delDataIn(InoutNoticeIn data) {
        Session session = this.getSessionFactory().openSession();
        try {
            session.delete(data);
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
 
    public void pageQueryOut(Page<InoutNoticeOut> page, Map<String, Object> param) throws Exception {
 
        String hql = " from " + InoutNoticeOut.class.getName()
                + " where companyId =:companyId";
 
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("companyId", ContextUtil.getCompanyId());
 
        Object year = param.get("year");
        if (year == null) {
            param.put("year", DateFormatUtils.format(new Date(), "yyyy"));
        }
 
        String str = (String) param.get("deptId");
        if (StringUtils.isNotEmpty(str)) {
            hql += " and deptId =:deptId";
            args.put("deptId", str);
        }
 
        str = (String) param.get("id");
        if (StringUtils.isNotEmpty(str)) {
            hql += " and id =:id";
            args.put("id", str);
        }
 
        str = (String) param.get("customerId");
        if (StringUtils.isNotEmpty(str)) {
            hql += " and customerId =:customerId";
            args.put("customerId", str);
        }
 
        Object obj = param.get("year");
        if (null != obj) {
            hql += " and year =:year";
            args.put("year", obj + "");
        }
 
        str = (String) param.get("auditStatus");
        if (StringUtils.isNotEmpty(str)) {
            hql += " and auditStatus =:auditStatus";
            args.put("auditStatus", str);
        }
 
        str = (String) param.get("completeStatus");
        if (StringUtils.isNotEmpty(str)) {
            hql += " and completeStatus =:completeStatus";
            args.put("completeStatus", str);
        }
 
        str = (String) param.get("foodVariety");
        if (StringUtils.isNotEmpty(str)) {
            hql += " and foodVariety =:foodVariety";
            args.put("foodVariety", str);
        }
 
        String countHql = " select count(*) " + hql;
        hql += " order by depotId + 0";
        this.pagingQuery(page, hql, countHql, args);
    }
 
    public String saveOrUpdateOut(InoutNoticeOut data) {
 
        //新增出库通知单时,需要进行唯一性判断。即保证单位、仓库、粮食品种、年份唯一,且不存在未完成状态的单子。
        if (Constant.YN_Y.equals(data.getTag())) {
            data.setCompanyId(ContextUtil.getCompanyId());
            NoticeParam param = new NoticeParam();
            param.setCompanyId(data.getCompanyId());
            param.setDeptId(data.getDeptId());
 
            param.setCustomerId(data.getCustomerId());
            param.setDepotId(data.getDepotId());
            param.setFoodVariety(data.getFoodVariety());
            param.setYear(data.getYear());
            //若有合同信息,添加合同信息
            if(StringUtils.isNotEmpty(data.getContractId())){
                param.setContractId(data.getContractId());
            }
            //若有计划信息,添加计划信息
            if(StringUtils.isNotEmpty(data.getPlanId())){
                param.setPlanId(data.getPlanId());
            }
            param.setCompleteStatus(InoutConstant.COMPLETE_STATUS_NONE);
            Integer integer = inoutManagerService.queryNoticeOut(param);
            if (integer > 0) {
                return "新增的出库通知单已存在,不能再次新增,请核查!";
            }
        }
        Session session = this.getSessionFactory().openSession();
 
        try {
            if (Constant.YN_Y.equals(data.getTag())) {
                data.setCreateUser(ContextUtil.getLoginUserCName());
                data.setAuditStatus(InoutConstant.STATUS_NONE);
                data.setCompleteStatus(InoutConstant.COMPLETE_STATUS_NONE);
                session.save(data);
            } else {
                session.update(data);
            }
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
 
    public String delDataOut(InoutNoticeOut data) {
        Session session = this.getSessionFactory().openSession();
        try {
            session.delete(data);
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
 
    /**
     * 根据参数查询出入库通知单
     * @param parameter
     * @return
     */
    public List<NoticeDto> queryNoticeByKey(Map<String, Object> parameter) {
        if (parameter == null) {
            return null;
        }
        String type = (String) parameter.get("type");
        if (StringUtils.isEmpty(type)) {
            return null;
        }
        String hql = " from ";
        if (InoutConstant.TYPE_IN.equals(type)) {
            hql += InoutNoticeIn.class.getName();
        }else {
            hql += InoutNoticeOut.class.getName();
        }
        hql += " where companyId =:companyId and deptId =:deptId";
 
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("companyId", ContextUtil.getCompanyId());
        args.put("deptId", ContextUtil.subDeptId(null));
 
        String str = (String) parameter.get("customerId");
        if (StringUtils.isNotEmpty(str)) {
            hql += " and customerId =:customerId";
            args.put("customerId", str);
        }
 
        str = (String) parameter.get("foodVariety");
        if (StringUtils.isNotEmpty(str)) {
            hql += " and foodVariety =:foodVariety";
            args.put("foodVariety", str);
        }
 
        if(InoutConstant.TYPE_OUT.equals(type)){
            str = (String) parameter.get("depotId");
            if (StringUtils.isNotEmpty(str)) {
                hql += " and depotId =:depotId";
                args.put("depotId", str);
            }
        }
 
        str = (String) parameter.get("key");
        if (StringUtils.isNoneEmpty(str)) {
            hql += " and (customerId like:customerId or customerName like:customerName) ";
            args.put("customerId", "%" + str + "%");
            args.put("customerName", "%" + str + "%");
        }
 
 
        hql += " order by id ";
 
        List<Object> list1 = this.query(hql, args);
        List<NoticeDto> list = new ArrayList<>();
        if(list1 == null || list1.isEmpty()){
            return null;
        }
        NoticeDto dto;
        for (Object o : list1) {
            dto = new NoticeDto();
            BeanUtils.copyProperties(o,dto);
            list.add(dto);
        }
        return list;
    }
 
    /**
     * 根据id获取通知单信息
     * @param id
     * @param type
     * @return
     */
    public NoticeDto queryNoticeById(String id, String type) {
 
        if(StringUtils.isEmpty(id) || StringUtils.isEmpty(type)){
            return null;
        }
        String hql = " from ";
        if (InoutConstant.TYPE_IN.equals(type)) {
            hql += InoutNoticeIn.class.getName();
        }else {
            hql += InoutNoticeOut.class.getName();
        }
        hql += " where companyId =:companyId and deptId =:deptId and id =:id";
 
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("companyId", ContextUtil.getCompanyId());
        args.put("deptId", ContextUtil.subDeptId(null));
        args.put("id", id);
 
        hql += " order by id ";
 
        List<Object> list = this.query(hql, args);
        if(list == null || list.isEmpty()){
            return null;
        }
        NoticeDto dto = new NoticeDto();
        BeanUtils.copyProperties(list.get(0), dto);
        return dto;
    }
}