xinyb_
2022-01-22 74e0609e9ae8747041720edbac54ae11bd444fc4
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
package com.yc.MaintenanceFee.service.impl;
 
import com.yc.MaintenanceFee.entiy.PayInfo;
import com.yc.MaintenanceFee.service.MaintainService;
import com.yc.MaintenanceFee.service.PayInfoService;
import com.yc.service.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
 
/**
 * @USER: xinyb_
 * @DATE: 2021-11-22 17:56
 */
@Service
public class MaintainServiceImpl extends BaseService implements MaintainService {
 
    @Autowired
    PayInfoService payInfoService;
 
    @Override
    public boolean savePostingLogic(String docCode) {
        try {
            Integer formId = 150101;
            StringBuffer sb = new StringBuffer();
            sb.append(" set nocount on\n" +
                    " declare @docCode varchar(50)='" + docCode + "' ,@FormId int=" + formId + ",@PostFormId int,@DocStatusValue int,@now datetime=getdate() \n");
            sb.append(" declare @PreDocStatus int,@PostDocStatus int, @EnterCode varchar(50)='SYSTEM',@EnterName varchar(50)='系统管理员',@EnterDate dateTime=@now \n");
            sb.append(" declare @Memo varchar(50), @LinkDocInfo varchar(50)\n");
            //标记为在维护费线支付
            sb.append(" update t" + formId + "H set OnlinePaymentReturnCode='0000' where DocCode=@docCode \n");
            //更新推送消息表的字段
            sb.append(" update t150372 \n" +
                    " set ISpay = 1 ,PayDate=GETDATE() \n" +
                    " where Refdoccode in(select SODoccode from t" + formId + "D where DocCode=@docCode) \n");
            sb.append(createPostSql(formId));
            sb.append(" select @docCode as docCode");
            log.info("do120311sql:" + sb.toString());
            this.jdbcTemplate.execute(sb.toString());
            return true;
        } catch (Exception e) {
            return false;
        }
    }
 
    @Override
    public List<Map<String, Object>> selectMaintenanceFeeNotification(String userCode) {
        try {
            String sql = "select isReceiveMaintenanceFeeNotification,inactive,usercode,username,tel from _sys_LoginUser where isReceiveMaintenanceFeeNotification=1 and inactive=0";
            if (!StringUtils.isBlank(userCode)) {
                sql += " and userCode='" + userCode + "'";
            }
            return super.jdbcTemplate.queryForList(sql);
        } catch (Exception e) {
            return null;
        }
    }
 
    @Override
    public boolean updateT150372(String autoIds) {
        boolean bol = false;
        try {
            String sql = "set nocount on\n" +
                    " declare @autoids varchar(max) = '" + autoIds + "' \n" +
                    " update a set ISpush = 1 ,PushDate = getdate() from t150372 a where a.autoid in (select list from GetInStr( @autoids)) \n" +
                    " select @@ROWCOUNT";
            Integer cunt = super.jdbcTemplate.update(sql);
            if (cunt != null && cunt > 0) {
                bol = true;
            }
        } catch (Exception e) {
            bol = false;
        }
        return bol;
    }
 
    @Override
    public boolean deleteSysMessage(String whCode) {
        boolean bol = false;
        try {
            String sql = "declare @whCode varchar(50)='"+whCode+"',@row int \n" +
                    " delete _sysMessageCount where messid in(select messId from _sysMessage a where a.origfields=@whCode) \n" +
                    " select @row=@@ROWCOUNT \n" +
                    " if(isnull(@row,0)>0) \n" +
                    " begin \n" +
                    " delete _sysMessage where origfields=@whCode \n" +
                    " select @row=@@ROWCOUNT \n" +
                    " end \n" +
                    " select @row";
            Integer count = super.jdbcTemplate.update(sql);
            if (count > 0) {
                bol = true;
            }
        } catch (Exception e) {
            bol = false;
        }
        return bol;
    }
 
    @Override
    public String selectCltCodeDbId(String table, String docCode) throws Exception {
        try {
            String sql="select DocCode,CltCode,DocType,DocDate \n" +
                    " from "+table+" where DocCode = '"+docCode+"'";
            PayInfo info = super.jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(PayInfo.class));
            if (info != null && !StringUtils.isBlank(info.getCltCode())) {
                Integer dbId = payInfoService.queryDbIdByCltCode(info.getCltCode());
                if (dbId == null || dbId == 0) {
                    throw new Exception("获取不到客户编号:" + info.getCltCode() + "的dbId值,请完善【维护客户资料-110203】表内的dbId值");
                }
                return dbId + "";
            } else {
                throw new Exception("客户编号不能为空!");
            }
        } catch (Exception e) {
            throw e;
        }
    }
 
    /**
     * 生成收款单
     *
     * @param whCode
     * @param accountCode
     * @return
     * @throws Exception
     */
    @Override
    public String createSKCode(String whCode, String accountCode) throws Exception {
        try {
            String sql = " declare @linkdocinfo varchar(8000)  \n" +
                    " declare  @FromDocCode varchar(5000) ='" + whCode + "' ,@FromFormId int = 120311  , @usercode varchar(20) = 'SYSTEM'       \n" +
                    " declare @username varchar(50) = '系统管理员',@newdoccode varchar(20) ,@newformid int  \n" +
                    " declare @AccountCode varchar(20) = '" + accountCode + "' \n" +
                    " exec p150105V6  @FromDocCode = @FromDocCode ,@FromFormId =@FromFormId  , @usercode  = @usercode ,       \n" +
                    "    @username =@username,@newdoccode = @newdoccode output,@newformid = @newformid output,@linkdocinfo = @linkdocinfo output      \n" +
                    " update a set AccountCode = @AccountCode  from t150348 a where doccode = @newdoccode\n" +
                    " select @newdoccode as DocCode  ";
            return super.jdbcTemplate.queryForObject(sql, String.class);
        } catch (Exception e) {
            throw e;
        }
    }
 
    @Override
    public List<Map<String, Object>> selectMaintainMsg() {
        try {
            String sql = "declare @today datetime = convert(varchar(10),getdate(),120) \n" +
                    " select a.autoid,a.messagetxt,a.refdoccode,a.refformid,a.refformtype,a.insertactioncode," +
                    " a.insertactionname,a.insertdate ,b.dbid,a.unvaliddate,a.tipcount \n" +
                    " from t150372 a join t110203 b on a.cltcode = b.cltcode \n" +
                    " where a.ScheduledSendDate <= @today and isnull(a.ispush,0) = 0 and isnull(a.ispay,0) = 0";
            return super.jdbcTemplate.queryForList(sql);
        } catch (Exception e) {
            return null;
        }
    }
 
    @Override
    public Integer selectMaintainCunt(String dbId) {
        try {
            String sql = "select COUNT(*) from t120301H a join t110203 b on a.CltCode=b.cltCode and a.DocStatus=100 and isnull(a.ReceivMoney,0) - isnull(a.ShouKuanAmount,0)>0 where b.dbId=" + dbId;
            return super.jdbcTemplate.queryForObject(sql, Integer.class);
        } catch (Exception e) {
            return 0;
        }
    }
 
    @Override
    public Integer updateMessage(String whCode) {
        try {
            String sql = "set nocount on\n" +
                    " declare @whCode varchar(max) = '" + whCode + "' \n" +
                    " update a set a.tipcount = 0 from _sysMessage a where a.FromDbId=338 and a.formid=120311 \n" +
                    " and a.origfields in(select list from GetInStr(@whCode)) \n" +
                    " select @@ROWCOUNT";
            Integer count = super.jdbcTemplate.update(sql);
            return count;
        } catch (Exception e) {
            return 0;
        }
    }
 
    @Override
    public List<String> selectWHCode(String skCode) {
        try {
            String sql = "select SODoccode from t150101D where DocCode='" + skCode + "'";
            return super.jdbcTemplate.query(sql, new RowMapper<String>() {
                @Override
                public String mapRow(ResultSet resultSet, int i) throws SQLException {
                    return resultSet.getString(1);
                }
            }, null);
        } catch (Exception e) {
            return null;
        }
    }
 
    @Override
    public String queryPayDbId(String whCode) {
        try {
            String sql="select b.dbid from t120301H a join t110203 b on a.CltCode=b.cltCode where a.docCode='"+whCode+"'";
            return super.jdbcTemplate.queryForObject(sql,String.class);
        }catch (Exception e){
            return "";
        }
    }
 
 
    /**
     * 通过生成单据保存及过账sql,默认以@doccode为单号变量
     *
     * @param formid
     * @return
     */
    private String createPostSql(int formid) {
        return " --执行保存及过账---维护费支付后进行的保存及过账---\n" +
                " exec p" + formid + "Save @doccode\n" +
                "                         select @PostFormId = FormId,@DocStatusValue = DocStatus from t" + formid + "H where docCode=@docCode\n" +
                "                    select @PreDocStatus = PreDocStatus,@PostDocStatus = a.PostDocStatus from gform a where a.formid = @PostFormId \n" +
                "                if isnull(@DocStatusValue,0) = isnull(@PreDocStatus,0)\n" +
                "                         begin \n" +
                "                            update a set DocStatus = @PostDocStatus,PostCode=@EnterCode,PostName=@EnterName,PostDate=@now\n" +
                "                             from t" + formid + "H a where a.DocCode = @docCode \n" +
                "                             exec p" + formid + "Post @UserCode = @EnterCode,@UserName = @EnterName, \n" +
                "                                @DocCode = @docCode,@FormId = @PostFormId,\n" +
                "                                 @DocStatusValue = @DocStatusValue,@ButtonType ='提交', \n" +
                "                                @Memo=@Memo output ,@LinkDocInfo = @LinkDocInfo output   \n" +
                "                         end\n";
    }
}