YYC
2023-11-05 a6013fee1895815cda77bfdcb5e5b9f4d190640b
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
package com.ld.igds.inout.service;
 
import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
import com.bstek.dorado.data.provider.Page;
import com.ld.igds.check.dto.CheckItemData;
import com.ld.igds.constant.RedisConst;
import com.ld.igds.m.InoutManageUtil;
import com.ld.igds.models.*;
import com.ld.igds.models.InoutSettle;
import com.ld.igds.util.ContextUtil;
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.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Component
public class HSettleService extends HibernateDao {
 
    public static final String BEAN_ID = "hSettleServiceImpl";
 
    public static final String CACHE_SETTLE_ID = "SETTLE_ID";
 
    @Autowired
    private RedisUtil redisUtil;
 
    public void pageList(Page<InoutSettle> page, Map<String, Object> param)
            throws Exception {
 
        String hql = " from " + InoutSettle.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("contractId");
            if (StringUtils.isNotEmpty(str)) {
                hql += " and contractId=:contractId ";
                args.put("contractId", str);
            }
 
            str = (String) param.get("bizType");
            if (StringUtils.isNotEmpty(str)) {
                hql += " and bizType=:bizType ";
                args.put("bizType", str);
            }
 
 
            str = (String) param.get("deptId");
            if (StringUtils.isNotEmpty(str)) {
                hql += " and deptId=:deptId ";
                args.put("deptId", str);
            }
 
            Date date = (Date) param.get("start");
            if (null != date) {
                hql += " and payTime >= :start ";
                args.put("start", DateUtil.getCurZero(date));
            }
 
            date = (Date) param.get("end");
            if (null != date) {
                hql += " and payTime <= :end ";
                args.put("end", DateUtil.getNextZero(date));
            }
 
        }
        String countHql = "select count(*) " + hql;
        hql += " order by id desc ";
        this.pagingQuery(page, hql, countHql, args);
    }
 
 
    public void saveData(InoutSettle data) {
        Session session = this.getSessionFactory().openSession();
        if (StringUtils.isEmpty(data.getCompanyId())) {
            data.setCompanyId(ContextUtil.getCompanyId());
        }
        if (StringUtils.isEmpty(data.getDeptId())) {
            data.setDeptId(ContextUtil.subDeptId(null));
        }
        if (null == data.getPayTime()) {
            data.setPayTime(new Date());
        }
        try {
            data.setUpdateTime(new Date());
            if (StringUtils.isEmpty(data.getId())) {
                String id = createId(data.getPayTime(), data.getCompanyId());
                data.setId(id);
                session.save(data);
            } else {
                session.update(data);
            }
        } finally {
            session.flush();
            session.close();
        }
    }
 
    public void delData(InoutSettle data) {
        Session session = this.getSessionFactory().openSession();
        try {
            if (null != data.getId()) {
                session.delete(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }
 
    public String createId(Date date, String companyId) {
        // 时间戳标签
        String timeKey = DateFormatUtils.format(date, "yyyyMMdd");
 
        // 从缓存中获取已有的组织编码
        String cacheKey = RedisConst.buildKey(companyId, this.CACHE_SETTLE_ID);
 
        String cacheId = (String) redisUtil.get(cacheKey);
 
        if (null != cacheId && cacheId.indexOf(timeKey) >= 0) {
            String temp = cacheId.substring(cacheId.length() - 4);
            Integer i = Integer.valueOf(temp);
            i++;
            cacheId = timeKey + String.format("%04d", i);
        } else {
            Map<String, Object> args = new HashMap<>();
            args.put("timeKey", timeKey + "%");
            List<InoutSettle> result = getMaxId(args);
 
            if (null == result || result.size() == 0) {
                cacheId = timeKey + "0001";
            } else {
                String temp = result.get(0).getId();
                String tempNum = temp.substring(temp.length() - 4);
                Integer i = Integer.valueOf(tempNum);
                cacheId = timeKey + String.format("%04d", ++i);
            }
        }
        // 更新缓存
        redisUtil.set(cacheKey, cacheId);
 
        return cacheId;
    }
 
    public List<InoutSettle> getMaxId(Map<String, Object> param) {
 
        String hql = "from " + InoutSettle.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);
    }
}