license_upfile_models.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. package models
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "time"
  6. "xugu_license/internal/global"
  7. "github.com/google/uuid"
  8. )
  9. type LicenseApplication struct {
  10. Creator string // 创建人
  11. ApplicationDate string //申请日期
  12. AssociatedProject string // 关联项目
  13. SalesPerson string //销售人员
  14. SalesEmail string // 销售接收邮箱
  15. SupportPerson string //技服人员
  16. SupportEmail string // 技服接收邮箱
  17. TotalNodes string //总节点数
  18. Company string //使用单位
  19. ProductName string // 产品名称
  20. Version string //版本
  21. NodeCount string //节点数
  22. Processor string //处理器
  23. OperatingSystem string //操作系统
  24. MasterMacAddress string // 主MasterMac地址
  25. SecondaryMasterMacAddress sql.NullString // 副MasterMac地址
  26. }
  27. type LicenseInfo struct {
  28. ID int `json:"id"`
  29. UserID int `json:"user_id"`
  30. UpUser string `json:"up_user"`
  31. UpTime sql.NullTime `json:"up_time"`
  32. DelTime sql.NullTime `json:"del_time"`
  33. License1 sql.NullString
  34. License2 sql.NullString
  35. LicenseFlage string
  36. UniqueID string
  37. LicenseApplication
  38. }
  39. // 删除LicenseApplication和LicenseInfo表中的数据
  40. // func DelLicenseApplicationAndInfoRow(uniqueID string) error {
  41. // fmt.Println("UniqueID12313", uniqueID)
  42. // tx, err := global.XuguDB.Begin()
  43. // if err != nil {
  44. // return fmt.Errorf("begin transaction: %v", err)
  45. // }
  46. // defer func() {
  47. // if err != nil {
  48. // tx.Rollback()
  49. // } else {
  50. // err = tx.Commit()
  51. // }
  52. // }()
  53. // // "update LicenseApplication set DelTime = ? where UniqueID = ?",
  54. // //DELETE FROM LicenseInfo WHERE LicenseApplicationUniqueID = ?
  55. // // _, err = tx.Exec("DELETE FROM LicenseInfo WHERE LicenseApplicationUniqueID = ?", uniqueID)
  56. // // if err != nil {
  57. // // return err
  58. // // }
  59. // // _, err = tx.Exec("DELETE FROM LicenseApplication WHERE UniqueID = ?", uniqueID)
  60. // // if err != nil {
  61. // // return err
  62. // // }
  63. // _, err = tx.Exec("UPDATE LicenseApplication SET DelTime = ? WHERE UniqueID = ?", time.Now(), uniqueID)
  64. // if err != nil {
  65. // return err
  66. // }
  67. // return nil
  68. // }
  69. func InsertLicenseApplicationAndInfoRow(licenseInfo LicenseApplication, userId int, userName string) error {
  70. tx, err := global.XuguDB.Begin()
  71. if err != nil {
  72. return fmt.Errorf("begin transaction: %v", err)
  73. }
  74. defer func() {
  75. if err != nil {
  76. tx.Rollback()
  77. } else {
  78. err = tx.Commit()
  79. }
  80. }()
  81. // 插入 LicenseApplication 表
  82. uniqueID := uuid.New()
  83. insertLicenseApplication := `
  84. INSERT INTO LicenseApplication (
  85. UniqueID, UserId, UpUser, UpTime, DelTime,
  86. Creator, ApplicationDate, AssociatedProject, SalesPerson, SalesEmail,
  87. SupportPerson, SupportEmail,TotalNodes, Company,ProductName, Version,
  88. NodeCount, Processor, OperatingSystem,MasterMacAddress, SecondaryMasterMacAddress
  89. ) VALUES (
  90. ?, ?, ?, ?, ?,
  91. ?, ?, ?, ?, ?,
  92. ?, ?, ?, ?, ?,
  93. ?, ?, ?, ?, ?,?
  94. )`
  95. _, err = tx.Exec(insertLicenseApplication,
  96. uniqueID, userId, userName, time.Now(), nil,
  97. licenseInfo.Creator, licenseInfo.ApplicationDate, licenseInfo.AssociatedProject,
  98. licenseInfo.SalesPerson, licenseInfo.SalesEmail, licenseInfo.SupportPerson,
  99. licenseInfo.SupportEmail, licenseInfo.TotalNodes, licenseInfo.Company, licenseInfo.ProductName,
  100. licenseInfo.Version, licenseInfo.NodeCount, licenseInfo.Processor,
  101. licenseInfo.OperatingSystem, licenseInfo.MasterMacAddress,
  102. licenseInfo.SecondaryMasterMacAddress)
  103. if err != nil {
  104. return err
  105. }
  106. //插入 LicenseInfo 表
  107. _, err = tx.Exec(`
  108. INSERT INTO LicenseInfo (
  109. LicenseApplicationUniqueID, LicenseFlage
  110. ) VALUES (?, ?)
  111. `,
  112. uniqueID, "未生成")
  113. if err != nil {
  114. fmt.Println("InsertLicenseApplicationRow", err)
  115. return err
  116. }
  117. return nil
  118. }
  119. // 获取所有或单个license信息,或者指定某个用户能查询到的所有license信息
  120. func GetLicenseInfoAllOrSingle(page int, pageSize int, uniqueID string, userName string) ([]LicenseInfo, int, error) {
  121. offset := (page - 1) * pageSize
  122. // 查询总记录数
  123. var total int
  124. err := global.XuguDB.QueryRow(`SELECT COUNT(*) FROM LicenseApplication WHERE DelTime IS NULL`).Scan(&total)
  125. if err != nil {
  126. return nil, 0, fmt.Errorf("count query error: %v", err)
  127. }
  128. var query string
  129. if uniqueID != "" {
  130. query = fmt.Sprintf(`SELECT
  131. la.id,
  132. la.UniqueID,
  133. la.UserId,
  134. la.UpUser,
  135. la.UpTime,
  136. la.DelTime,
  137. la.Creator,
  138. la.ApplicationDate,
  139. la.AssociatedProject,
  140. la.SalesPerson,
  141. la.SalesEmail,
  142. la.SupportPerson,
  143. la.SupportEmail,
  144. la.TotalNodes,
  145. la.Company,
  146. la.ProductName,
  147. la.Version,
  148. la.NodeCount,
  149. la.Processor,
  150. la.OperatingSystem,
  151. la.MasterMacAddress,
  152. la.SecondaryMasterMacAddress,
  153. li.lic1,
  154. li.lic2
  155. FROM
  156. LicenseApplication la
  157. INNER JOIN
  158. LicenseInfo li
  159. ON
  160. la.UniqueID = li.LicenseApplicationUniqueID
  161. WHERE
  162. la.DelTime IS NULL
  163. AND la.UniqueID = '%s' LIMIT ? OFFSET ?`, uniqueID)
  164. } else if userName != "" { //使用用户名查询
  165. err := global.XuguDB.QueryRow(`SELECT COUNT(*) FROM licenseRecordToUser lr WHERE lr.userUNIQUEID LIKE '%%%s%%'`, userName).Scan(&total)
  166. if err != nil {
  167. global.Logger.Errorln("查询总数失败 ", err.Error())
  168. return nil, 0, fmt.Errorf("查询总数失败: %v", err)
  169. }
  170. query = fmt.Sprintf(`
  171. SELECT la.*,li.lic1, li.lic2, lr.upTime
  172. FROM LicenseApplication la
  173. JOIN licenseRecordToUser lr ON la.UniqueID = lr.LicenseUniqueID
  174. JOIN LicenseInfo li ON la.UniqueID = li.LicenseApplicationUniqueID
  175. WHERE lr.userUNIQUEID LIKE '%%%s%%' and la.deltime is null
  176. LIMIT ? OFFSET ?`, userName)
  177. rows, err := global.XuguDB.Query(query, pageSize, offset)
  178. if err != nil {
  179. global.Logger.Errorln("查询数据失败 ", err.Error())
  180. return nil, 0, fmt.Errorf("query error: %v", err)
  181. }
  182. defer rows.Close()
  183. var results []LicenseInfo
  184. for rows.Next() {
  185. var li LicenseInfo
  186. err = rows.Scan(
  187. &li.ID,
  188. &li.UniqueID,
  189. &li.UserID,
  190. &li.UpUser,
  191. &li.UpTime,
  192. &li.DelTime,
  193. &li.LicenseApplication.Creator,
  194. &li.LicenseApplication.ApplicationDate,
  195. &li.LicenseApplication.AssociatedProject,
  196. &li.LicenseApplication.SalesPerson,
  197. &li.LicenseApplication.SalesEmail,
  198. &li.LicenseApplication.SupportPerson,
  199. &li.LicenseApplication.SupportEmail,
  200. &li.LicenseApplication.TotalNodes,
  201. &li.LicenseApplication.Company,
  202. &li.LicenseApplication.ProductName,
  203. &li.LicenseApplication.Version,
  204. &li.LicenseApplication.NodeCount,
  205. &li.LicenseApplication.Processor,
  206. &li.LicenseApplication.OperatingSystem,
  207. &li.LicenseApplication.MasterMacAddress,
  208. &li.LicenseApplication.SecondaryMasterMacAddress,
  209. &li.License1,
  210. &li.License2,
  211. &li.UpTime,
  212. )
  213. if err != nil {
  214. global.Logger.Errorln("数据查询失败: ", err.Error())
  215. // return nil, fmt.Errorf("scan row: %v", err)
  216. return nil, 0, fmt.Errorf("scan row error: %v", err)
  217. }
  218. results = append(results, li)
  219. }
  220. if err = rows.Err(); err != nil {
  221. global.Logger.Errorln("rows数据查询失败: ", err.Error())
  222. return nil, 0, fmt.Errorf("rows error: %v", err)
  223. }
  224. // fmt.Printf("\n usernaame %s\n", userName)
  225. // fmt.Printf("\n LIMIT ? OFFSET ?`, %d,%d\n", pageSize, offset)
  226. // fmt.Printf("\nresults123 %#v\n", results)
  227. return results, total, nil
  228. } else {
  229. query = `
  230. SELECT
  231. la.id,
  232. la.UniqueID,
  233. la.UserId,
  234. la.UpUser,
  235. la.UpTime,
  236. la.DelTime,
  237. la.Creator,
  238. la.ApplicationDate,
  239. la.AssociatedProject,
  240. la.SalesPerson,
  241. la.SalesEmail,
  242. la.SupportPerson,
  243. la.SupportEmail,
  244. la.TotalNodes,
  245. la.Company,
  246. la.ProductName,
  247. la.Version,
  248. la.NodeCount,
  249. la.Processor,
  250. la.OperatingSystem,
  251. la.MasterMacAddress,
  252. la.SecondaryMasterMacAddress,
  253. li.Lic1,
  254. li.Lic2,
  255. li.LicenseFlage
  256. FROM
  257. LicenseApplication la
  258. INNER JOIN
  259. LicenseInfo li
  260. ON
  261. la.UniqueID = li.LicenseApplicationUniqueID
  262. WHERE
  263. la.DelTime IS NULL
  264. LIMIT ? OFFSET ?
  265. `
  266. }
  267. rows, err := global.XuguDB.Query(query, pageSize, offset)
  268. if err != nil {
  269. global.Logger.Errorln("数据查询失败: ", err.Error())
  270. return nil, 0, fmt.Errorf("query error: %v", err)
  271. }
  272. defer rows.Close()
  273. var results []LicenseInfo
  274. for rows.Next() {
  275. var li LicenseInfo
  276. if uniqueID != "" {
  277. err = rows.Scan(
  278. &li.ID,
  279. &li.UniqueID,
  280. &li.UserID,
  281. &li.UpUser,
  282. &li.UpTime,
  283. &li.DelTime,
  284. &li.LicenseApplication.Creator,
  285. &li.LicenseApplication.ApplicationDate,
  286. &li.LicenseApplication.AssociatedProject,
  287. &li.LicenseApplication.SalesPerson,
  288. &li.LicenseApplication.SalesEmail,
  289. &li.LicenseApplication.SupportPerson,
  290. &li.LicenseApplication.SupportEmail,
  291. &li.LicenseApplication.TotalNodes,
  292. &li.LicenseApplication.Company,
  293. &li.LicenseApplication.ProductName,
  294. &li.LicenseApplication.Version,
  295. &li.LicenseApplication.NodeCount,
  296. &li.LicenseApplication.Processor,
  297. &li.LicenseApplication.OperatingSystem,
  298. &li.LicenseApplication.MasterMacAddress,
  299. &li.LicenseApplication.SecondaryMasterMacAddress,
  300. &li.License1,
  301. &li.License2,
  302. )
  303. } else {
  304. err = rows.Scan(
  305. &li.ID,
  306. &li.UniqueID,
  307. &li.UserID,
  308. &li.UpUser,
  309. &li.UpTime,
  310. &li.DelTime,
  311. &li.LicenseApplication.Creator,
  312. &li.LicenseApplication.ApplicationDate,
  313. &li.LicenseApplication.AssociatedProject,
  314. &li.LicenseApplication.SalesPerson,
  315. &li.LicenseApplication.SalesEmail,
  316. &li.LicenseApplication.SupportPerson,
  317. &li.LicenseApplication.SupportEmail,
  318. &li.LicenseApplication.TotalNodes,
  319. &li.LicenseApplication.Company,
  320. &li.LicenseApplication.ProductName,
  321. &li.LicenseApplication.Version,
  322. &li.LicenseApplication.NodeCount,
  323. &li.LicenseApplication.Processor,
  324. &li.LicenseApplication.OperatingSystem,
  325. &li.LicenseApplication.MasterMacAddress,
  326. &li.LicenseApplication.SecondaryMasterMacAddress,
  327. &li.License1,
  328. &li.License2,
  329. &li.LicenseFlage,
  330. )
  331. }
  332. if err != nil {
  333. global.Logger.Errorln("数据查询失败: ", err.Error())
  334. return nil, 0, fmt.Errorf("scan row error: %v", err)
  335. }
  336. results = append(results, li)
  337. }
  338. if err = rows.Err(); err != nil {
  339. global.Logger.Errorln("数据查询失败: ", err.Error())
  340. return nil, 0, fmt.Errorf("rows error: %v", err)
  341. }
  342. return results, total, nil
  343. }
  344. // 插入生成好的lic1,2的信息
  345. func UpdateLicenseStr(uniqueID string, lic1 []byte, lic2 []byte) error {
  346. tx, err := global.XuguDB.Begin()
  347. if err != nil {
  348. global.Logger.Errorln("begin transaction失败: ", err.Error())
  349. return fmt.Errorf("begin transaction: %v", err)
  350. }
  351. defer func() {
  352. if err != nil {
  353. tx.Rollback()
  354. } else {
  355. err = tx.Commit()
  356. }
  357. }()
  358. //插入 LicenseInfo 表
  359. if lic2 == nil {
  360. _, err = tx.Exec(`
  361. UPDATE LicenseInfo SET LicenseFlage = ? ,lic1 = ? WHERE LicenseApplicationUniqueID = ?
  362. `,
  363. "已生成", string(lic1), uniqueID)
  364. } else {
  365. _, err = tx.Exec(`
  366. UPDATE LicenseInfo SET LicenseFlage = ? ,lic1 = ?,lic2 = ? WHERE LicenseApplicationUniqueID = ?
  367. `,
  368. "已生成", string(lic1), string(lic2), uniqueID)
  369. }
  370. if err != nil {
  371. global.Logger.Errorln("插入lic1 ,2 失败: ", err.Error())
  372. return err
  373. }
  374. return nil
  375. }
  376. // 插入分发记录表
  377. // func InsertlicenseRecordByUserRow(LicenseUniqueID string, userUniqueID string, UserAccount string, operatorUniqueID string) error {
  378. // tx, err := global.XuguDB.Begin()
  379. // if err != nil {
  380. // global.Logger.Errorln("begin transaction失败: ", err.Error())
  381. // return fmt.Errorf("begin transaction: %v", err)
  382. // }
  383. // defer func() {
  384. // if err != nil {
  385. // tx.Rollback()
  386. // } else {
  387. // err = tx.Commit()
  388. // }
  389. // }()
  390. // _, err = tx.Exec(`
  391. // Insert into licenseRecordToUser(LicenseUniqueID ,userUniqueID,UserAccount,upTime,operatorUniqueID) values(?,?,?,?,?)`,
  392. // LicenseUniqueID, userUniqueID, UserAccount, time.Now(), operatorUniqueID)
  393. // if err != nil {
  394. // global.Logger.Errorln("插入分发记录用户表失败: ", err.Error())
  395. // return err
  396. // }
  397. // return nil
  398. // }
  399. // 插入分发记录表
  400. // func InsertlicenseRecordByEmailRow(LicenseUniqueID string, emails string, operatorUniqueID string) error {
  401. // tx, err := global.XuguDB.Begin()
  402. // if err != nil {
  403. // global.Logger.Errorln("begin transaction失败: ", err.Error())
  404. // return fmt.Errorf("begin transaction: %v", err)
  405. // }
  406. // defer func() {
  407. // if err != nil {
  408. // tx.Rollback()
  409. // } else {
  410. // err = tx.Commit()
  411. // }
  412. // }()
  413. // _, err = tx.Exec(`
  414. // Insert into licenseRecordToEmails(LicenseUniqueID ,emails,operatorUniqueID,upTime) values(?,?,?,?)`,
  415. // LicenseUniqueID, emails, operatorUniqueID, time.Now())
  416. // if err != nil {
  417. // global.Logger.Errorln("插入分发记录邮箱表失败: ", err.Error())
  418. // return err
  419. // }
  420. // return nil
  421. // }
  422. // // 搜索分发记录表
  423. // func GetlicenseRecordByUser(uniqueID string) ([]LicenseRecordToUser, error) {
  424. // fmt.Println("搜索分发记录表 uniqueID", uniqueID)
  425. // if uniqueID == "" {
  426. // return nil, fmt.Errorf("uniqueID is empty")
  427. // }
  428. // sqlLicUser := `select LicenseUniqueID,userUNIQUEID,UserAccount,operatorUniqueID,upTime from licenseRecordToUser where LicenseUniqueID = ? AND deltime IS NULL`
  429. // rows, err := global.XuguDB.Query(sqlLicUser, uniqueID)
  430. // if err != nil {
  431. // global.Logger.Errorln("query data: ", err.Error())
  432. // return nil, fmt.Errorf("query error: %v", err)
  433. // }
  434. // defer rows.Close()
  435. // fmt.Println("执行这里")
  436. // var results []LicenseRecordToUser
  437. // for rows.Next() {
  438. // var lRU LicenseRecordToUser
  439. // err = rows.Scan(
  440. // &lRU.LicenseUniqueID,
  441. // &lRU.UserUniqueID,
  442. // &lRU.UserAccount,
  443. // &lRU.OperatorUniqueID,
  444. // &lRU.UpTime,
  445. // )
  446. // if err != nil {
  447. // global.Logger.Errorln("scan row: ", err.Error())
  448. // return nil, fmt.Errorf("scan row error: %v", err)
  449. // }
  450. // results = append(results, lRU)
  451. // }
  452. // fmt.Println("LicenseRecordToUser ", results)
  453. // return results, nil
  454. // }
  455. // func GetlicenseRecordByEmail(uniqueID string) ([]LicenseRecordToEmails, error) {
  456. // if uniqueID == "" {
  457. // return nil, fmt.Errorf("uniqueID is empty")
  458. // }
  459. // sqlLicEmail := `select LicenseUniqueID,emails,operatorUniqueID ,upTime from licenseRecordToEmails where LicenseUniqueID = ? AND deltime IS NULL;`
  460. // rows, err := global.XuguDB.Query(sqlLicEmail, uniqueID)
  461. // if err != nil {
  462. // global.Logger.Errorln("query data: ", err.Error())
  463. // return nil, fmt.Errorf("query error: %v", err)
  464. // }
  465. // fmt.Println("执行这里")
  466. // var results []LicenseRecordToEmails
  467. // for rows.Next() {
  468. // var lRU LicenseRecordToEmails
  469. // err = rows.Scan(
  470. // &lRU.LicenseUniqueID,
  471. // &lRU.Emails,
  472. // &lRU.OperatorUniqueID,
  473. // &lRU.UpTime,
  474. // )
  475. // if err != nil {
  476. // global.Logger.Errorln("scan row: ", err.Error())
  477. // return nil, fmt.Errorf("scan row error: %v", err)
  478. // }
  479. // results = append(results, lRU)
  480. // }
  481. // if err = rows.Err(); err != nil {
  482. // global.Logger.Errorln("rows error: ", err.Error())
  483. // return nil, fmt.Errorf("rows error: %v", err)
  484. // }
  485. // return results, nil
  486. // }
  487. // func UpdatelicenseInfoRow(license LicenseInfo) error {
  488. // tx, err := global.XuguDB.Begin()
  489. // if err != nil {
  490. // return fmt.Errorf("begin transaction: %v", err)
  491. // }
  492. // defer func() {
  493. // if err != nil {
  494. // tx.Rollback()
  495. // } else {
  496. // err = tx.Commit()
  497. // }
  498. // }()
  499. // _, err = tx.Exec(`
  500. // UPDATE LICENSEAPPLICATION SET Creator =? ,ApplicationDate =? ,AssociatedProject = ? ,
  501. // SalesPerson = ? , SalesEmail = ? , SupportPerson = ? , SupportEmail = ? , TotalNodes = ? ,
  502. // Company = ? , ProductName = ? , Version = ? , NodeCount = ?
  503. // WHERE UniqueID =?;
  504. // `, license.Creator, license.ApplicationDate, license.AssociatedProject,
  505. // license.SalesPerson, license.SalesEmail, license.SupportPerson, license.SupportEmail, license.TotalNodes,
  506. // license.Company, license.ProductName, license.Version, license.NodeCount, license.UniqueID)
  507. // if err != nil {
  508. // global.Logger.Errorln("UpdatelicenseInfoRow失败 ", err.Error())
  509. // return err
  510. // }
  511. // return nil
  512. // }