thjNtf.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. package service
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "mtp20_assisted/global"
  6. "mtp20_assisted/model"
  7. "mtp20_assisted/res/pb"
  8. "mtp20_assisted/utils"
  9. "os"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/gofrs/uuid"
  14. "github.com/nguyenthenguyen/docx"
  15. "go.uber.org/zap"
  16. "google.golang.org/protobuf/proto"
  17. )
  18. // THJNtf 铁合通知处理模型
  19. type THJNtf struct{}
  20. func (t *THJNtf) Process(msg *[]byte) {
  21. // 分解总线包信息
  22. funcode := utils.BytesToUint32((*msg)[0:4])
  23. sessionId := utils.BytesToUint32((*msg)[4:8])
  24. bytes := (*msg)[8:]
  25. global.M2A_LOG.Info("[S->C]", zap.Any("funcode", funcode), zap.Any("sessionId", sessionId), zap.Any("count", len(bytes)))
  26. switch funcode {
  27. case uint32(global.THJPurchaseTradeNtf):
  28. // 等待两秒
  29. <-time.After(2 * time.Second)
  30. onTHJPurchaseTradeNtf(&bytes)
  31. case uint32(global.PurchaseTransferNtf):
  32. <-time.After(2 * time.Second)
  33. onTHJPurchaseTransferNtf(&bytes)
  34. }
  35. }
  36. // onTHJPurchaseTradeNtf 铁合金成交通知
  37. func onTHJPurchaseTradeNtf(bytes *[]byte) {
  38. // Read from docx file
  39. r, err := docx.ReadDocxFile("./static/产能预售合同.docx")
  40. if err != nil {
  41. global.M2A_LOG.Error("读取合同文件失败", zap.Error(err))
  42. return
  43. }
  44. var p pb.THJPurchaseTradeNtf
  45. if err := proto.Unmarshal(*bytes, &p); err != nil {
  46. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  47. return
  48. }
  49. // 铁合金采购成交扩展信息
  50. thjpurchasetradedetail := model.Thjpurchasetradedetail{WRTRADEDETAILID: int64(p.GetWRTradeDetailID())}
  51. if has, err := thjpurchasetradedetail.Get(); err != nil || !has {
  52. global.M2A_LOG.Error("[铁合金成交通知] 获取铁合金采购成交扩展信息失败", zap.Error(err))
  53. return
  54. }
  55. // 交割方式
  56. thjdeliverymode := "否"
  57. if thjpurchasetradedetail.THJDELIVERYMODE == 1 {
  58. thjdeliverymode = "是"
  59. }
  60. // 现货商品信息
  61. wrstandard := model.Wrstandard{WRSTANDARDID: int64(thjpurchasetradedetail.WRSTANDARDID)}
  62. if has, err := wrstandard.Get(); err != nil || !has {
  63. global.M2A_LOG.Error("[铁合金成交通知] 获取现货商品信息失败", zap.Error(err))
  64. return
  65. }
  66. // 仓单预售信息
  67. wrpresaleinfo := model.Wrpresaleinfo{PRESALEAPPLYID: thjpurchasetradedetail.PRESALEAPPLYID}
  68. if has, err := wrpresaleinfo.Get(); err != nil || !has {
  69. global.M2A_LOG.Error("[铁合金成交通知] 获取仓单预售信息失败", zap.Error(err))
  70. return
  71. }
  72. // 甲方信息
  73. userinfo := model.Userinfo{USERID: thjpurchasetradedetail.BUYUSERID}
  74. if has, err := userinfo.Get(); err != nil || !has {
  75. global.M2A_LOG.Error("[铁合金成交通知] 获取甲方信息失败", zap.Error(err))
  76. return
  77. }
  78. division := model.Division{AUTOID: int64(userinfo.DISTRICTID)}
  79. totalAddress, err := division.GetTotalAddressByDistrictID(userinfo.ADDRESS)
  80. if err != nil {
  81. global.M2A_LOG.Error("[铁合金成交通知] 获取甲方地址信息失败", zap.Error(err))
  82. return
  83. }
  84. // 证件号码解密
  85. key, _ := hex.DecodeString(utils.AESSecretKey)
  86. if len(userinfo.CARDNUM) > 0 {
  87. if cardnum, err := hex.DecodeString(userinfo.CARDNUM); err == nil { // hex -> []byte
  88. if c, err := utils.AESDecrypt(cardnum, key); err == nil {
  89. userinfo.CARDNUM = string(c)
  90. }
  91. }
  92. }
  93. // 手机号码解密
  94. if len(userinfo.MOBILE) > 0 {
  95. if phonenum, err := hex.DecodeString(userinfo.MOBILE); err == nil { // hex -> []byte
  96. if mobile, err := utils.AESDecrypt(phonenum, key); err == nil {
  97. userinfo.MOBILE = string(mobile)
  98. }
  99. }
  100. }
  101. // 甲方签约信息
  102. bankaccountsign := model.Bankaccountsign{}
  103. if has, err := bankaccountsign.GetByUserID(uint64(thjpurchasetradedetail.BUYUSERID)); err != nil || !has {
  104. global.M2A_LOG.Error("[铁合金成交通知] 获取甲方签约信息失败", zap.Error(err))
  105. return
  106. }
  107. // 账号解密
  108. if len(bankaccountsign.BANKACCOUNTNO) > 0 {
  109. if tt, err := hex.DecodeString(bankaccountsign.BANKACCOUNTNO); err == nil { // hex -> []byte
  110. if dd, err := utils.AESDecrypt(tt, key); err == nil {
  111. bankaccountsign.BANKACCOUNTNO = string(dd)
  112. }
  113. }
  114. }
  115. // 银行信息
  116. bankinfo := model.Bankbankinfo{BANKID: bankaccountsign.BANKID}
  117. if has, err := bankinfo.Get(); err != nil || !has {
  118. global.M2A_LOG.Error("[铁合金成交通知] 获取银行信息失败", zap.Error(err))
  119. return
  120. }
  121. // 生产厂家信息
  122. selluserinfo := model.Userinfo{USERID: thjpurchasetradedetail.SELLUSERID}
  123. if has, err := selluserinfo.Get(); err != nil || !has {
  124. global.M2A_LOG.Error("[铁合金成交通知] 获取生产厂家信息失败", zap.Error(err))
  125. return
  126. }
  127. docx1 := r.Editable()
  128. // global.M2A_LOG.Info(docx1.GetContent())
  129. docx1.Replace("${WRTRADEDETAILID}", strconv.Itoa(int(thjpurchasetradedetail.WRTRADEDETAILID)), -1)
  130. docx1.Replace("${TRADETIME}", thjpurchasetradedetail.TRADETIME.Format("2006年01月02日"), -1)
  131. docx1.Replace("${DESADDRESS}", totalAddress, -1)
  132. // 判断是公司还是个人
  133. if userinfo.USERINFOTYPE == 1 {
  134. // 个人
  135. docx1.Replace("COMPANY", "", -1)
  136. docx1.Replace("${LEGALPERSONNAME}", userinfo.CUSTOMERNAME, -1)
  137. } else {
  138. // 公司
  139. docx1.Replace("COMPANY", userinfo.CUSTOMERNAME, -1)
  140. docx1.Replace("${LEGALPERSONNAME}", userinfo.LEGALPERSONNAME, -1)
  141. }
  142. docx1.Replace("CARDNUM", userinfo.CARDNUM, -1)
  143. docx1.Replace("${MOBILE}", userinfo.MOBILE, -1)
  144. docx1.Replace("${BANK}", bankinfo.BANKNAME, -1)
  145. docx1.Replace("${BANKACCOUNTNO}", bankaccountsign.BANKACCOUNTNO, -1)
  146. docx1.Replace("${WRSTANDARDNAME}", wrstandard.WRSTANDARDNAME, -1)
  147. docx1.Replace("${SELLUSER}", selluserinfo.CUSTOMERNAME, -1)
  148. docx1.Replace("${LASTAMOUNT}", strconv.FormatFloat(thjpurchasetradedetail.LASTAMOUNT, 'f', -1, 64), -1)
  149. docx1.Replace("${DEPOSITRATE}", strconv.FormatFloat(thjpurchasetradedetail.DEPOSITRATE*100, 'f', -1, 64)+"%", -1)
  150. docx1.Replace("${TRADEQTY}", strconv.Itoa(int(thjpurchasetradedetail.TRADEQTY)), -1)
  151. docx1.Replace("${TRADEPRICE}", strconv.FormatFloat(thjpurchasetradedetail.TRADEPRICE, 'f', -1, 64), -1)
  152. docx1.Replace("${ENDDATEMONTH}", strconv.Itoa(int(wrpresaleinfo.ENDDATE.Local().Month())), -1)
  153. docx1.Replace("${THJDELIVERYMODE}", thjdeliverymode, -1)
  154. docx1.Replace("${ENDDATE}", wrpresaleinfo.ENDDATE.Format("200601"), -1)
  155. docx1.Replace("${STORAGEFEE}", strconv.FormatFloat(wrstandard.STORAGEFEE, 'f', -1, 64), -1)
  156. // 暂存docx文件
  157. if exist, _ := utils.PathExists("./.tmp"); !exist {
  158. os.Mkdir("./.tmp", os.ModePerm)
  159. }
  160. uid, _ := uuid.NewV4()
  161. docxFilename := fmt.Sprintf("%v_%v.docx", strconv.Itoa(int(thjpurchasetradedetail.WRTRADEDETAILID)), uid.String())
  162. pdfFilename := strings.Replace(docxFilename, "docx", "pdf", -1)
  163. docx1.WriteToFile("./.tmp/" + docxFilename)
  164. r.Close()
  165. // 导出pdf到目标目录
  166. folderPath := "Purchase_Contract/" + time.Now().Format("20060102")
  167. savePath := global.M2A_CONFIG.System.StorePath + "/" + folderPath
  168. if exist, _ := utils.PathExists(savePath); !exist {
  169. os.MkdirAll(savePath, os.ModePerm)
  170. }
  171. success := utils.ConvertToPDF("./.tmp/"+docxFilename, savePath)
  172. if !success {
  173. // 转换失败
  174. global.M2A_LOG.Error("[铁合金成交通知] docx转换pdf失败", zap.Error(err))
  175. return
  176. }
  177. // 更新数据库
  178. // pdfFilename = docxFilename // 临时代码
  179. thjpurchasetradedetail.CONTRACTADDRBUY = fmt.Sprintf("./uploadFile/%v/%v", folderPath, pdfFilename)
  180. if err = thjpurchasetradedetail.UpdateContractAddrBuy(); err != nil {
  181. global.M2A_LOG.Error("[铁合金成交通知] 更新数据库失败", zap.Error(err))
  182. return
  183. }
  184. global.M2A_LOG.Info("[铁合金成交通知] 合同生成成功", zap.Any("file", savePath+pdfFilename))
  185. }
  186. // onTHJPurchaseTransferNtf 铁合金协议转让通知
  187. func onTHJPurchaseTransferNtf(bytes *[]byte) {
  188. // Read from docx file
  189. r, err := docx.ReadDocxFile("./static/协议转让合同.docx")
  190. if err != nil {
  191. global.M2A_LOG.Error("读取合同文件失败", zap.Error(err))
  192. return
  193. }
  194. var p pb.PurchaseTransferNtf
  195. if err := proto.Unmarshal(*bytes, &p); err != nil {
  196. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  197. return
  198. }
  199. // 铁合金采购协议表
  200. thjpurchasetransfer := model.Thjpurchasetransfer{TRANSFERID: int64(p.GetTransferID())}
  201. if has, err := thjpurchasetransfer.Get(); err != nil || !has {
  202. global.M2A_LOG.Error("[铁合金协议转让通知] 获取铁合金采购成交扩展信息失败", zap.Error(err))
  203. return
  204. }
  205. // 铁合金采购成交扩展信息
  206. thjpurchasetradedetail := model.Thjpurchasetradedetail{WRTRADEDETAILID: thjpurchasetransfer.WRTRADEDETAILID}
  207. if has, err := thjpurchasetradedetail.Get(); err != nil || !has {
  208. global.M2A_LOG.Error("[铁合金协议转让通知] 获取铁合金采购成交扩展信息失败", zap.Error(err))
  209. return
  210. }
  211. // 现货商品信息
  212. wrstandard := model.Wrstandard{WRSTANDARDID: int64(thjpurchasetradedetail.WRSTANDARDID)}
  213. if has, err := wrstandard.Get(); err != nil || !has {
  214. global.M2A_LOG.Error("[铁合金协议转让通知] 获取现货商品信息失败", zap.Error(err))
  215. return
  216. }
  217. // ******************* 甲方信息(卖方)*******************
  218. a_userinfo := model.Userinfo{USERID: thjpurchasetransfer.SELLUSERID}
  219. if has, err := a_userinfo.Get(); err != nil || !has {
  220. global.M2A_LOG.Error("[铁合金协议转让通知] 获取甲方信息失败", zap.Error(err))
  221. return
  222. }
  223. a_division := model.Division{AUTOID: int64(a_userinfo.DISTRICTID)}
  224. a_totalAddress, err := a_division.GetTotalAddressByDistrictID(a_userinfo.ADDRESS)
  225. if err != nil {
  226. global.M2A_LOG.Error("[铁合金协议转让通知] 获取甲方地址信息失败", zap.Error(err))
  227. return
  228. }
  229. // 证件号码解密
  230. key, _ := hex.DecodeString(utils.AESSecretKey)
  231. if len(a_userinfo.CARDNUM) > 0 {
  232. if cardnum, err := hex.DecodeString(a_userinfo.CARDNUM); err == nil { // hex -> []byte
  233. if c, err := utils.AESDecrypt(cardnum, key); err == nil {
  234. a_userinfo.CARDNUM = string(c)
  235. }
  236. }
  237. }
  238. // 手机号码解密
  239. if len(a_userinfo.MOBILE) > 0 {
  240. if phonenum, err := hex.DecodeString(a_userinfo.MOBILE); err == nil { // hex -> []byte
  241. if mobile, err := utils.AESDecrypt(phonenum, key); err == nil {
  242. a_userinfo.MOBILE = string(mobile)
  243. }
  244. }
  245. }
  246. // 签约信息
  247. a_bankaccountsign := model.Bankaccountsign{}
  248. if has, err := a_bankaccountsign.GetByUserID(uint64(thjpurchasetransfer.SELLUSERID)); err != nil || !has {
  249. global.M2A_LOG.Error("[铁合金协议转让通知] 获取甲方签约信息失败", zap.Error(err))
  250. return
  251. }
  252. // 账号解密
  253. if len(a_bankaccountsign.BANKACCOUNTNO) > 0 {
  254. if tt, err := hex.DecodeString(a_bankaccountsign.BANKACCOUNTNO); err == nil { // hex -> []byte
  255. if dd, err := utils.AESDecrypt(tt, key); err == nil {
  256. a_bankaccountsign.BANKACCOUNTNO = string(dd)
  257. }
  258. }
  259. }
  260. // 银行信息
  261. a_bankinfo := model.Bankbankinfo{BANKID: a_bankaccountsign.BANKID}
  262. if has, err := a_bankinfo.Get(); err != nil || !has {
  263. global.M2A_LOG.Error("[铁合金协议转让通知] 获取银行信息失败", zap.Error(err))
  264. return
  265. }
  266. // ******************* 乙方信息(买方)*******************
  267. b_userinfo := model.Userinfo{USERID: thjpurchasetransfer.BUYUSERID}
  268. if has, err := b_userinfo.Get(); err != nil || !has {
  269. global.M2A_LOG.Error("[铁合金协议转让通知] 获取乙方信息失败", zap.Error(err))
  270. return
  271. }
  272. b_division := model.Division{AUTOID: int64(b_userinfo.DISTRICTID)}
  273. b_totalAddress, err := b_division.GetTotalAddressByDistrictID(b_userinfo.ADDRESS)
  274. if err != nil {
  275. global.M2A_LOG.Error("[铁合金协议转让通知] 获取乙方地址信息失败", zap.Error(err))
  276. return
  277. }
  278. // 证件号码解密
  279. if len(b_userinfo.CARDNUM) > 0 {
  280. if cardnum, err := hex.DecodeString(b_userinfo.CARDNUM); err == nil { // hex -> []byte
  281. if c, err := utils.AESDecrypt(cardnum, key); err == nil {
  282. b_userinfo.CARDNUM = string(c)
  283. }
  284. }
  285. }
  286. // 手机号码解密
  287. if len(b_userinfo.MOBILE) > 0 {
  288. if phonenum, err := hex.DecodeString(b_userinfo.MOBILE); err == nil { // hex -> []byte
  289. if mobile, err := utils.AESDecrypt(phonenum, key); err == nil {
  290. b_userinfo.MOBILE = string(mobile)
  291. }
  292. }
  293. }
  294. // 签约信息
  295. b_bankaccountsign := model.Bankaccountsign{}
  296. if has, err := b_bankaccountsign.GetByUserID(uint64(thjpurchasetransfer.BUYUSERID)); err != nil || !has {
  297. global.M2A_LOG.Error("[铁合金协议转让通知] 获取乙方签约信息失败", zap.Error(err))
  298. return
  299. }
  300. // 账号解密
  301. if len(b_bankaccountsign.BANKACCOUNTNO) > 0 {
  302. if tt, err := hex.DecodeString(b_bankaccountsign.BANKACCOUNTNO); err == nil { // hex -> []byte
  303. if dd, err := utils.AESDecrypt(tt, key); err == nil {
  304. b_bankaccountsign.BANKACCOUNTNO = string(dd)
  305. }
  306. }
  307. }
  308. // 银行信息
  309. b_bankinfo := model.Bankbankinfo{BANKID: b_bankaccountsign.BANKID}
  310. if has, err := b_bankinfo.Get(); err != nil || !has {
  311. global.M2A_LOG.Error("[铁合金协议转让通知] 获取乙方银行信息失败", zap.Error(err))
  312. return
  313. }
  314. docx1 := r.Editable()
  315. docx1.Replace("TRANSFERID", strconv.Itoa(int(thjpurchasetransfer.TRANSFERID)), -1)
  316. if transfertradedate, e := time.Parse("20060102", thjpurchasetransfer.TRANSFERTRADEDATE); e != nil {
  317. docx1.Replace("TRANSFERTRADEDATE", transfertradedate.Format("2006年01月02日"), -1)
  318. }
  319. docx1.Replace("WRSTANDARDNAME", wrstandard.WRSTANDARDNAME, -1)
  320. docx1.Replace("WRTRADEDETAILID", strconv.Itoa(int(thjpurchasetradedetail.WRTRADEDETAILID)), -1)
  321. docx1.Replace("TRANSFERQTY", strconv.Itoa(int(thjpurchasetransfer.TRANSFERQTY)), -1)
  322. docx1.Replace("TRADEAMOUNT", strconv.FormatFloat(thjpurchasetradedetail.TRADEAMOUNT, 'f', -1, 64), -1)
  323. docx1.Replace("TRANSFERAMOUNT", strconv.FormatFloat(thjpurchasetransfer.TRANSFERAMOUNT, 'f', -1, 64), -1)
  324. docx1.Replace("TRANSFERPRICE", strconv.FormatFloat(thjpurchasetransfer.TRANSFERPRICE, 'f', -1, 64), -1)
  325. // ********************* 甲方 *********************
  326. // 判断甲方是公司还是个人
  327. if a_userinfo.USERINFOTYPE == 1 {
  328. // 个人
  329. docx1.Replace("PARTYA_COMPANY", "", -1)
  330. docx1.Replace("PARTYA_LEGALPERSONNAME", a_userinfo.CUSTOMERNAME, -1)
  331. } else {
  332. // 公司
  333. docx1.Replace("PARTYA_COMPANY", a_userinfo.CUSTOMERNAME, -1)
  334. docx1.Replace("PARTYA_LEGALPERSONNAME", a_userinfo.LEGALPERSONNAME, -1)
  335. }
  336. docx1.Replace("PARTYA_DESADDRESS", a_totalAddress, -1)
  337. docx1.Replace("PARTYA_CARDNUM", a_userinfo.CARDNUM, -1)
  338. docx1.Replace("PARTYA_MOBILE", a_userinfo.MOBILE, -1)
  339. docx1.Replace("PARTYA_BANK_BANKACCOUNTNO",
  340. fmt.Sprintf("%v%v", a_bankinfo.BANKNAME, a_bankaccountsign.BANKACCOUNTNO), -1)
  341. // ********************* 乙方 *********************
  342. // 判断乙方是公司还是个人
  343. if b_userinfo.USERINFOTYPE == 1 {
  344. // 个人
  345. docx1.Replace("PARTYB_COMPANY", "", -1)
  346. docx1.Replace("PARTYB_LEGALPERSONNAME", b_userinfo.CUSTOMERNAME, -1)
  347. } else {
  348. // 公司
  349. docx1.Replace("PARTYB_COMPANY", b_userinfo.CUSTOMERNAME, -1)
  350. docx1.Replace("PARTYB_LEGALPERSONNAME", b_userinfo.LEGALPERSONNAME, -1)
  351. }
  352. docx1.Replace("PARTYB_DESADDRESS", b_totalAddress, -1)
  353. docx1.Replace("PARTYB_CARDNUM", b_userinfo.CARDNUM, -1)
  354. docx1.Replace("PARTYB_MOBILE", b_userinfo.MOBILE, -1)
  355. docx1.Replace("PARTYB_BANK_BANKACCOUNTNO",
  356. fmt.Sprintf("%v%v", b_bankinfo.BANKNAME, b_bankaccountsign.BANKACCOUNTNO), -1)
  357. // 暂存docx文件
  358. if exist, _ := utils.PathExists("./.tmp"); !exist {
  359. os.Mkdir("./.tmp", os.ModePerm)
  360. }
  361. uid, _ := uuid.NewV4()
  362. docxFilename := fmt.Sprintf("%v_%v.docx", strconv.Itoa(int(thjpurchasetransfer.TRANSFERID)), uid.String())
  363. pdfFilename := strings.Replace(docxFilename, "docx", "pdf", -1)
  364. docx1.WriteToFile("./.tmp/" + docxFilename)
  365. r.Close()
  366. // 导出pdf到目标目录
  367. folderPath := "Agreement_Assignment/" + time.Now().Format("20060102")
  368. savePath := global.M2A_CONFIG.System.StorePath + "/" + folderPath
  369. if exist, _ := utils.PathExists(savePath); !exist {
  370. os.MkdirAll(savePath, os.ModePerm)
  371. }
  372. success := utils.ConvertToPDF("./.tmp/"+docxFilename, savePath)
  373. if !success {
  374. // 转换失败
  375. global.M2A_LOG.Error("[铁合金协议转让通知] docx转换pdf失败", zap.Error(err))
  376. return
  377. }
  378. // 更新数据库
  379. thjpurchasetransfer.CONTRACTADDR = fmt.Sprintf("./uploadFile/%v/%v", folderPath, pdfFilename)
  380. if err = thjpurchasetransfer.UpdateContractAddr(); err != nil {
  381. global.M2A_LOG.Error("[铁合金协议转让通知] 更新数据库失败", zap.Error(err))
  382. return
  383. }
  384. global.M2A_LOG.Info("[铁合金协议转让通知] 合同生成成功", zap.Any("file", savePath+pdfFilename))
  385. }