LicenseInfo_upfile_controllers.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. package controllers
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  6. "xugu_license/internal/global"
  7. "xugu_license/internal/models"
  8. "xugu_license/internal/module/license"
  9. "xugu_license/internal/utils"
  10. "github.com/gin-gonic/gin"
  11. )
  12. var reqLicense struct {
  13. page int
  14. pageSize int
  15. }
  16. // 获取用户已被分配的license信息
  17. func GetLicenseInfo(c *gin.Context) {
  18. userAny, exists := c.Get("userInfo")
  19. if !exists {
  20. global.Logger.Errorln("GetLicenseInfo: 用户信息不存在")
  21. c.JSON(http.StatusUnauthorized, gin.H{"error": "用户信息不存在"})
  22. c.Abort()
  23. return
  24. }
  25. //获取当前用户信息
  26. userInfo := userAny.(*models.UserInfo)
  27. //设置页 和偏移
  28. page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
  29. pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
  30. if userInfo.Role == "admin" {
  31. applications, total, err := models.GetLicenseInfoAllOrSingle(page, pageSize, "", "")
  32. if err != nil {
  33. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  34. return
  35. }
  36. c.JSON(http.StatusOK, gin.H{
  37. "data": applications,
  38. "page": page,
  39. "pageSize": pageSize,
  40. "total": total,
  41. })
  42. } else { //非管理员用户
  43. type licInfo struct {
  44. UniqueID string
  45. Creator string
  46. ApplicationDate string
  47. AssociatedProject string
  48. License1 string
  49. License2 string
  50. SalesPerson string
  51. SalesEmail string
  52. SupportPerson string
  53. SupportEmail string
  54. TotalNodes string
  55. Company string
  56. ProductName string
  57. Version string
  58. NodeCount string
  59. Processor string
  60. OperatingSystem string
  61. MasterMacAddress string
  62. SecondaryMasterMacAddress string
  63. LicenseFlage string
  64. }
  65. applications, total, err := models.GetLicenseInfoAllOrSingle(page, pageSize, "", userInfo.UniqueID)
  66. if err != nil {
  67. global.Logger.Errorln("指定用户名查询", err.Error())
  68. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  69. return
  70. }
  71. var licInfoList []licInfo
  72. for _, v := range applications {
  73. licInfo := licInfo{
  74. UniqueID: v.UniqueID,
  75. Creator: v.Creator,
  76. ApplicationDate: v.ApplicationDate,
  77. AssociatedProject: v.AssociatedProject,
  78. License1: v.License1.String,
  79. License2: v.License2.String,
  80. SalesPerson: v.SalesPerson,
  81. SalesEmail: v.SalesEmail,
  82. SupportPerson: v.SupportPerson,
  83. SupportEmail: v.SupportEmail,
  84. TotalNodes: v.TotalNodes,
  85. Company: v.Company,
  86. ProductName: v.ProductName,
  87. Version: v.Version,
  88. NodeCount: v.NodeCount,
  89. Processor: v.Processor,
  90. OperatingSystem: v.OperatingSystem,
  91. MasterMacAddress: v.MasterMacAddress,
  92. SecondaryMasterMacAddress: v.SecondaryMasterMacAddress.String,
  93. LicenseFlage: v.LicenseFlage,
  94. }
  95. licInfoList = append(licInfoList, licInfo)
  96. }
  97. //fmt.Printf("licInfoList %#v", licInfoList)
  98. c.JSON(http.StatusOK, gin.H{
  99. "data": licInfoList,
  100. "page": page,
  101. "pageSize": pageSize,
  102. "total": total,
  103. })
  104. }
  105. }
  106. // 定义接收 JSON 数据的结构体
  107. type UniqueIDRequest struct {
  108. UniqueID string `json:"uniqueID"`
  109. }
  110. // func DelLicenseInfoRow(c *gin.Context) {
  111. // // 解析 JSON 请求体
  112. // var request UniqueIDRequest
  113. // if err := c.ShouldBindJSON(&request); err != nil {
  114. // c.JSON(400, gin.H{
  115. // "error": err.Error(),
  116. // })
  117. // return
  118. // }
  119. // err := models.DelLicenseApplicationAndInfoRow(request.UniqueID)
  120. // if err != nil {
  121. // global.Logger.Errorln("删除一行License信息失败 ", err.Error())
  122. // c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintln("删除失败: ", err.Error())})
  123. // return
  124. // }
  125. // c.JSON(http.StatusOK, gin.H{
  126. // "success": true,
  127. // "data": "删除成功",
  128. // })
  129. // }
  130. func GenerateLicenseStr(c *gin.Context) {
  131. var request UniqueIDRequest
  132. if err := c.ShouldBindJSON(&request); err != nil {
  133. global.Logger.Errorln("解析request失败 : ", err.Error())
  134. c.JSON(400, gin.H{
  135. "error": fmt.Sprintln("解析请求失败: ", err.Error()),
  136. })
  137. return
  138. }
  139. applications, _, err := models.GetLicenseInfoAllOrSingle(1, 1, request.UniqueID, "")
  140. if err != nil {
  141. global.Logger.Errorln("LicenseInfo数据查询失败: ", err.Error())
  142. c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintln("数据查询失败: ", err.Error())})
  143. return
  144. }
  145. pj := license.ProjectInfo{
  146. ProjectName: applications[0].Version,
  147. UserName: applications[0].Company,
  148. UserAddr: "未填写",
  149. SerialNumber: "未填写",
  150. }
  151. ei := license.EnvironmentInfo{
  152. CpuSN: "未填写",
  153. BaseboardSN: "未填写",
  154. MacAddr: applications[0].MasterMacAddress,
  155. DiskID: "未填写",
  156. IPAddr: "未填写",
  157. }
  158. LicType := utils.SwitchLicenseType(applications[0].Version)
  159. lI := license.LicenseInfo{
  160. GenDate: "2024-07-15",
  161. ExpireDate: "9999-12-31",
  162. LicenseType: LicType,
  163. LicenseVersion: 1,
  164. HardType: 3,
  165. }
  166. res := true
  167. if res {
  168. if applications[0].SecondaryMasterMacAddress.String != "" {
  169. // var licStr2 []byte
  170. // ei.MacAddr = applications[0].SecondaryMasterMacAddress
  171. // licStr2 = license.GenerateLicense(pj, ei, lI)
  172. err = models.UpdateLicenseStr(applications[0].UniqueID, []byte("b4j6z4rE2IfG1av0wIPT7YnvyGZFHxwIBikMGjgCLQILR0xsT1NHiuzoi+Dqq+bmiNDEiuPyitDVgdvlRmYbFAk+MAAGASlPTkdMbE9"), []byte("b4j6z4rE2IfG1av0wIPT7YnvyGZFHxwIBikMGjgCLQILR0xsT1NHiuzoi+Dqq+bmiNDEiuPyitDVgdvlRmYbFAk+MAAGASlPTkdMbE9"))
  173. } else {
  174. //fmt.Println("licStr licStr2", licStr, licStr2)
  175. //插入到数据库
  176. err = models.UpdateLicenseStr(applications[0].UniqueID, []byte("b4j6z4rE2IfG1av0wIPT7YnvyGZFHxwIBikMGjgCLQILR0xsT1NHiuzoi+Dqq+bmiNDEiuPyitDVgdvlRmYbFAk+MAAGASlPTkdMbE9"), nil)
  177. }
  178. } else {
  179. licStr := license.GenerateLicense(pj, ei, lI)
  180. //生成副主节点license
  181. if applications[0].SecondaryMasterMacAddress.String != "" {
  182. var licStr2 []byte
  183. ei.MacAddr = applications[0].SecondaryMasterMacAddress.String
  184. licStr2 = license.GenerateLicense(pj, ei, lI)
  185. err = models.UpdateLicenseStr(applications[0].UniqueID, licStr, licStr2)
  186. } else {
  187. //插入到数据库
  188. err = models.UpdateLicenseStr(applications[0].UniqueID, licStr, nil)
  189. }
  190. }
  191. if err != nil {
  192. fmt.Println(err)
  193. if err := c.ShouldBindJSON(&request); err != nil {
  194. c.JSON(400, gin.H{
  195. "error": err.Error(),
  196. })
  197. return
  198. }
  199. }
  200. // if err != nil {
  201. // fmt.Println(err)
  202. // if err := c.ShouldBindJSON(&request); err != nil {
  203. // global.Logger.Errorln("LicenseInfo数据查询失败: ", err.Error())
  204. // c.JSON(400, gin.H{
  205. // "error": fmt.Sprintln("数据查询失败: ", err.Error()),
  206. // })
  207. // return
  208. // }
  209. // }
  210. c.JSON(http.StatusOK, gin.H{
  211. "success": true,
  212. "message": "License生成成功!",
  213. })
  214. //xlsx.ExcelToMail(lic, licStr, licStr2)
  215. }
  216. func DistributeLicenseByUser(c *gin.Context) {
  217. // 从token中解析出user_id
  218. u, err := models.GetAllUser()
  219. if err != nil {
  220. c.JSON(http.StatusBadRequest, gin.H{
  221. "error": err.Error(),
  222. })
  223. return
  224. }
  225. fmt.Println("sdsad u", u)
  226. type allUserInfo struct {
  227. }
  228. c.JSON(http.StatusOK, gin.H{
  229. "message": "success",
  230. "data": u,
  231. })
  232. }
  233. // func DistributeLicense(c *gin.Context) {
  234. // type DistributeLicenseRequest struct {
  235. // LicenseUniqueID string `json:"LicenseUniqueID"`
  236. // OperatorUniqueID string `json:"OperatorUniqueID"`
  237. // UserUniqueIDs []string `json:"UserUniqueIDs"`
  238. // UserAccounts []string `json:"UserAccounts"`
  239. // UserNames []string `json:"UserNames"`
  240. // Emails string `json:"emails"`
  241. // }
  242. // //判断是否发邮件
  243. // //数据库查询license信息
  244. // var request DistributeLicenseRequest
  245. // if err := c.ShouldBindJSON(&request); err != nil {
  246. // global.Logger.Errorln("邮件解析请求解析失败 %s", err.Error())
  247. // c.JSON(400, gin.H{
  248. // "error": fmt.Sprintln("邮件解析请求解析失败 %s", err.Error()),
  249. // })
  250. // return
  251. // }
  252. // fmt.Println("DistributeLicenseRequest", request)
  253. // //查询该license是否已经分发给了该用户
  254. // var licToUser []string
  255. // for i, v := range request.UserUniqueIDs {
  256. // fmt.Println("request.UserUniqueIDs", request.LicenseUniqueID, v)
  257. // if isTurn, err := models.CheckLicenseToUser(request.LicenseUniqueID, v); err != nil {
  258. // global.Logger.Errorln("该license查询是否分发给用户失败 ", v, err.Error())
  259. // c.JSON(400, gin.H{
  260. // "success": false,
  261. // "error": fmt.Sprintln("该license查询是否分发给用户失败: %s", v, err.Error()),
  262. // })
  263. // return
  264. // } else if isTurn {
  265. // global.Logger.Info("该license已经分发给了该用户 ", v)
  266. // licToUser = append(licToUser, request.UserNames[i])
  267. // }
  268. // }
  269. // if len(licToUser) != 0 {
  270. // c.JSON(400, gin.H{
  271. // "success": false,
  272. // "error": fmt.Sprintf("该license已经分发给了该用户: %s\n", licToUser),
  273. // })
  274. // return
  275. // }
  276. // applications, _, err := models.GetLicenseInfoAllOrSingle(1, 1, request.LicenseUniqueID, "")
  277. // if err != nil {
  278. // c.JSON(http.StatusBadRequest, gin.H{"success": false,
  279. // "error": err.Error()})
  280. // return
  281. // }
  282. // if applications[0].License1.String == "" {
  283. // global.Logger.Errorln("license未生成 ")
  284. // c.JSON(http.StatusBadRequest, gin.H{"success": false,
  285. // "error": "license未生成"})
  286. // return
  287. // }
  288. // //邮箱不空则发送
  289. // var EmailArray []string
  290. // if request.Emails != "" {
  291. // fmt.Println("request.Emails", request.Emails)
  292. // EmailArray = strings.Split(request.Emails, ",")
  293. // fmt.Println("EmailArray", EmailArray)
  294. // em, err := email.BuildEmail(applications[0].LicenseApplication, EmailArray, applications[0].License1.String, applications[0].License2.String)
  295. // if err != nil {
  296. // global.Logger.Errorln("邮件生成失败", err.Error())
  297. // c.JSON(400, gin.H{
  298. // "success": false,
  299. // "error": fmt.Sprintln("邮件生成失败: ", err.Error()),
  300. // })
  301. // return
  302. // }
  303. // err = email.SendEmail(em)
  304. // if err != nil {
  305. // global.Logger.Errorln("邮件发送失败", err.Error())
  306. // c.JSON(400, gin.H{
  307. // "success": false,
  308. // "error": fmt.Sprintln("邮件发送失败: ", err.Error()),
  309. // })
  310. // return
  311. // }
  312. // }
  313. // for i, v := range request.UserUniqueIDs {
  314. // err = models.InsertlicenseRecordByUserRow(applications[0].UniqueID, v, request.UserAccounts[i], request.OperatorUniqueID)
  315. // if err != nil {
  316. // global.Logger.Errorln("数据库插入失败: ", err.Error())
  317. // c.JSON(400, gin.H{
  318. // "success": false,
  319. // "error": fmt.Sprintln("插入失败: ", err.Error()),
  320. // })
  321. // return
  322. // }
  323. // }
  324. // if len(EmailArray) != 0 {
  325. // for _, v := range EmailArray {
  326. // err = models.InsertlicenseRecordByEmailRow(applications[0].UniqueID, v, request.OperatorUniqueID)
  327. // if err != nil {
  328. // global.Logger.Errorln("数据库插入失败: ", err.Error())
  329. // c.JSON(400, gin.H{
  330. // "error": fmt.Sprintln("插入失败: ", err.Error()),
  331. // })
  332. // return
  333. // }
  334. // }
  335. // }
  336. // c.JSON(http.StatusOK, gin.H{
  337. // "success": true,
  338. // "data": "分发成功!",
  339. // })
  340. // }
  341. // func GetlicenseRecordInfo(c *gin.Context) {
  342. // type licenseRecordInfoRequest struct {
  343. // UniqueID string `json:"uniqueID"`
  344. // Id int `json:"id"`
  345. // }
  346. // var request UniqueIDRequest
  347. // if err := c.ShouldBindJSON(&request); err != nil {
  348. // global.Logger.Errorln("解析请求失败 ", err.Error())
  349. // c.JSON(400, gin.H{
  350. // "error": fmt.Sprintln("解析请求失败: ", err.Error()),
  351. // })
  352. // return
  353. // }
  354. // LicUers, err := models.GetlicenseRecordByUser(request.UniqueID)
  355. // if err != nil {
  356. // global.Logger.Errorln("数据查询失败 ", err.Error())
  357. // c.JSON(400, gin.H{
  358. // "error": fmt.Sprintln("数据查询失败: ", err.Error()),
  359. // })
  360. // return
  361. // }
  362. // LicEmails, err := models.GetlicenseRecordByEmail(request.UniqueID)
  363. // if err != nil {
  364. // global.Logger.Errorln("数据查询失败 ", err.Error())
  365. // c.JSON(400, gin.H{
  366. // "error": fmt.Sprintln("数据查询失败: ", err.Error()),
  367. // })
  368. // return
  369. // }
  370. // licR := models.LicenseRecord{
  371. // LicenseRecordToUser: LicUers,
  372. // LicenseRecordToEmails: LicEmails,
  373. // }
  374. // fmt.Println("分发历史applications", licR)
  375. // c.JSON(http.StatusOK, gin.H{
  376. // "success": true,
  377. // "data": licR,
  378. // })
  379. // }
  380. // 获取用户所拥有的license信息
  381. func GetlicenseRecordInfoByUser(c *gin.Context) {
  382. type licenseByUserRequest struct {
  383. Id int `json:"id"`
  384. UserName string `json:"UserName"`
  385. UniqueID string `json:"UniqueID"`
  386. Page int `json:"page"`
  387. PageSize int `json:"pageSize"`
  388. }
  389. var request licenseByUserRequest
  390. if err := c.ShouldBindJSON(&request); err != nil {
  391. global.Logger.Errorln("解析请求失败 ", err.Error())
  392. c.JSON(400, gin.H{
  393. "error": fmt.Sprintln("解析请求失败: ", err.Error()),
  394. })
  395. return
  396. }
  397. applications, total, err := models.GetLicenseInfoAllOrSingle(request.Page, request.PageSize, "", request.UniqueID)
  398. if err != nil {
  399. global.Logger.Errorln("数据插入失败 ", err.Error())
  400. c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintln("数据插入失败: ", err.Error())})
  401. return
  402. }
  403. c.JSON(http.StatusOK, gin.H{
  404. "success": true,
  405. "data": applications,
  406. "total": total,
  407. })
  408. }
  409. // func UpdateLicense(c *gin.Context) {
  410. // type RecvLicense struct {
  411. // UniqueID string `json:"UniqueID" binding:"required"`
  412. // Creator string `json:"Creator" binding:"required"`
  413. // ApplicationDate string `json:"ApplicationDate" binding:"required"`
  414. // AssociatedProject string `json:"AssociatedProject" binding:"required"`
  415. // SalesPerson string `json:"SalesPerson" binding:"required"`
  416. // SalesEmail string `json:"SalesEmail" binding:"required,email"`
  417. // SupportPerson string `json:"SupportPerson" binding:"required"`
  418. // SupportEmail string `json:"SupportEmail" binding:"required,email"`
  419. // TotalNodes string `json:"TotalNodes" binding:"required"`
  420. // Company string `json:"Company" binding:"required"`
  421. // ProductName string `json:"ProductName" binding:"required"`
  422. // Version string `json:"Version" binding:"required"`
  423. // NodeCount string `json:"NodeCount" binding:"required"`
  424. // }
  425. // var license RecvLicense
  426. // // 绑定JSON数据到License结构体
  427. // if err := c.ShouldBindJSON(&license); err != nil {
  428. // c.JSON(http.StatusBadRequest, gin.H{
  429. // "success": false,
  430. // "error": err.Error(),
  431. // })
  432. // return
  433. // }
  434. // fmt.Printf(" license.ApplicationDate: %#v ", license)
  435. // err := models.UpdatelicenseInfoRow(models.LicenseInfo{
  436. // UniqueID: license.UniqueID,
  437. // LicenseApplication: models.LicenseApplication{
  438. // Creator: license.Creator,
  439. // ApplicationDate: license.ApplicationDate,
  440. // AssociatedProject: license.AssociatedProject,
  441. // SalesPerson: license.SalesPerson,
  442. // SalesEmail: license.SalesEmail,
  443. // SupportPerson: license.SupportPerson,
  444. // SupportEmail: license.SupportEmail,
  445. // TotalNodes: license.TotalNodes,
  446. // Company: license.Company,
  447. // ProductName: license.ProductName,
  448. // Version: license.Version,
  449. // NodeCount: license.NodeCount,
  450. // },
  451. // })
  452. // if err != nil {
  453. // global.Logger.Errorln("数据插入失败 ", err.Error())
  454. // c.JSON(http.StatusBadRequest, gin.H{"success": false, "error": fmt.Sprintln("数据插入失败: ", err.Error())})
  455. // return
  456. // }
  457. // c.JSON(http.StatusOK, gin.H{
  458. // "success": true,
  459. // })
  460. // }