YYC
2023-07-13 43a14ad6fbb68a9c0ce787acce28ce446aac13b0
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
package com.ld.igds.m.service;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.transaction.Transactional;
 
import com.ld.igds.constant.RedisConst;
import com.ld.igds.inout.InoutConstant;
import com.ld.igds.inout.dto.InoutData;
import com.ld.igds.inout.dto.InoutParam;
import com.ld.igds.inout.service.InoutService;
import com.ld.igds.m.InoutManageUtil;
import com.ld.igds.models.InoutLossOver;
import com.ld.igds.models.InoutVarietyChange;
import com.ld.igds.util.DateUtil;
import com.ld.igds.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
import com.bstek.dorado.data.provider.Page;
import com.ld.igds.util.ContextUtil;
 
@Component
public class HLossOverService extends HibernateDao {
 
    @Resource
    private RedisUtil redisUtil;
    @Autowired
    private InoutService inoutService;
 
    public void pageData(Page<InoutLossOver> page, Map<String, Object> param)
            throws Exception {
        String hql = " from " + InoutLossOver.class.getName()
                + " where companyId=:companyId";
 
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("companyId", ContextUtil.getCompanyId());
 
        if (null != param) {
 
            String str = (String) param.get("depotId");
            if (StringUtils.isNotEmpty(str)) {
                hql += " and depotId =:depotId";
                args.put("depotId", str);
            }
 
            str = (String) param.get("deptId");
            if (StringUtils.isNotEmpty(str)) {
                hql += " and deptId =:deptId";
                args.put("deptId", str);
            }
 
            str = (String) param.get("foodVariety");
            if (StringUtils.isNotEmpty(str)) {
                hql += " and foodVariety =:foodVariety";
                args.put("foodVariety", str);
            }
 
            str = (String) param.get("type");
            if (StringUtils.isNotEmpty(str)) {
                hql += " and type =:type";
                args.put("type", str);
            }
        }
 
        String count = "select count(*) " + hql;
 
        hql += " order by submitTime desc";
 
        this.pagingQuery(page, hql, count, args);
 
    }
 
    @Transactional(rollbackOn = Exception.class)
    public void saveData(InoutLossOver data) throws Exception {
        Session session = this.getSessionFactory().openSession();
        data.setUpdateTime(new Date());
        data.setUpdateUser(ContextUtil.getLoginUserCName());
 
 
        try {
            if (null == data.getId()) {
                IUser user = ContextUtil.getLoginUser();
                data.setDeptId(ContextUtil.subDeptId(user));
                data.setCompanyId(user.getCompanyId());
                if (null == data.getSubmitTime()) {
                    data.setSubmitTime(new Date());
                }
 
                //获取提交时间日期的01:00:00时间
                Date curDate = DateUtil.getCurZero(data.getSubmitTime());
                curDate = DateUtils.addHours(curDate, 1);
                //如果完成时间在curDate之前,则将完成时间设为当前天的01:00:00
                if (data.getSubmitTime().before(curDate)) {
                    data.setSubmitTime(curDate);
                }
                String id = createId(data.getSubmitTime(), data.getCompanyId(), data.getHwdm());
//                String id = addInoutDataByLossOver(data);
 
                data.setId(id);
 
                session.save(data);
            } else {
                session.update(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }
 
    private String addInoutDataByLossOver(InoutLossOver lossOver) {
        if (null == lossOver.getSubmitTime()) {
            lossOver.setSubmitTime(new Date());
        }
 
        InoutData data = new InoutData();
        data.setRecordStatus(InoutConstant.RECORD_STATUS_ADD);
        data.setProgress(InoutConstant.PROGRESS_RECORD);
        data.setType(lossOver.getType());
        data.setSettleWeight(lossOver.getAmount());
        data.setDepotId(lossOver.getDepotId());
        data.setDeptId(lossOver.getDeptId());
        data.setCompanyId(lossOver.getCompanyId());
        //data.setFoodVariety(lossOver.getFoodVariety());
        data.setCheckStatus(InoutConstant.STATUS_PASS);
 
        String loginUser = lossOver.getUpdateUser();
        data.setRegisterUser(loginUser);
        data.setFullWeightUser(loginUser);
        data.setEmptyWeightUser(loginUser);
        data.setHandleUser(loginUser);
        data.setCompleteUser(loginUser);
        data.setCompleteTime(lossOver.getSubmitTime());
        data.setRegisterTime(data.getCompleteTime());
        data.setFullWeightTime(data.getCompleteTime());
        data.setEmptyWeightTime(data.getCompleteTime());
        if (StringUtils.isEmpty(data.getUserId())) {
            data.setUserId(InoutConstant.DEFAULT_ID_CARD);
        }
 
        return inoutService.addInoutDataByLossOver(data);
    }
 
    @Transactional(rollbackOn = Exception.class)
    public String delData(InoutLossOver data) throws Exception {
        Session session = this.getSessionFactory().openSession();
        try {
            if (null != data.getId()) {
                this.delInoutDataByLossOver(data);
                session.delete(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
 
    private void delInoutDataByLossOver(InoutLossOver lossOver) {
        InoutParam param = new InoutParam();
        param.setCompanyId(lossOver.getCompanyId());
        param.setId(lossOver.getId());
        param.setProgress(InoutConstant.PROGRESS_RECORD);
        param.setRecordStatus(InoutConstant.RECORD_STATUS_DEL);
        param.setMsg(" [" + ContextUtil.getLoginUserCName() + "]执行删除");
        param.setType(lossOver.getType());
        param.setDepotId(lossOver.getDepotId());
        param.setWeight(lossOver.getAmount());
 
 
        inoutService.delInoutDataByLossOver(param);
    }
 
    public List<InoutLossOver> getMaxId(Map<String, Object> param) {
 
        String hql = "from " + InoutLossOver.class.getName() + " where 1 = 1 ";
        Map<String, Object> args = new HashMap<String, Object>();
        if (null != param) {
            String str = (String) param.get("timeKey");
            if (StringUtils.isNotEmpty(str)) {
                hql += " and id like :timeKey";
                args.put("timeKey", str);
            }
        }
        hql += " order by  id desc ";
        return this.query(hql, args);
    }
 
    public String createId(Date date, String companyId, String freightId) {
        // 时间戳标签
        String timeKey = DateFormatUtils.format(date, "yyyyMMdd");
 
        // 从缓存中获取已有的组织编码
        String cacheKey = RedisConst.buildKey(companyId,
                InoutManageUtil.CACHE_LOSS_OVER_ID);
 
        String cacheId = (String) redisUtil.get(cacheKey);
 
        if (null != cacheId && cacheId.indexOf(freightId + timeKey) >= 0) {
            String temp = cacheId.substring(cacheId.length() - 3);
            Integer i = Integer.valueOf(temp);
            cacheId = freightId + timeKey + String.format("%03d", ++i);
        } else {
            Map<String, Object> args = new HashMap<>();
            args.put("timeKey", freightId + timeKey + "%");
            List<InoutLossOver> result = getMaxId(args);
 
            if (null == result || result.size() == 0) {
                cacheId = freightId + timeKey + "001";
            } else {
                int max = 0;
                int num;
                for (InoutLossOver inoutLossOver : result) {
                    num = Integer.parseInt(inoutLossOver.getId().substring(inoutLossOver.getId().length() - 3));
                    max = Math.max(max, num);
                }
                cacheId = freightId + timeKey + String.format("%03d", ++max);
            }
        }
        // 更新缓存
        redisUtil.set(cacheKey, cacheId);
 
        return cacheId;
    }
 
}