license_oa_models.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. package models
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "log"
  6. "time"
  7. "xugu_license/internal/global"
  8. )
  9. type OALicenseInfo struct {
  10. LicInfo TargetOALicenseInfo
  11. GenrateInfo LicenseGenerateInfo
  12. }
  13. type TargetOALicenseInfo struct {
  14. ID sql.NullInt32 `json:"id"` // 自增主键,从1开始,每次递增1
  15. OAId sql.NullInt32 `json:"OA_Id"`
  16. UniqueID sql.NullString `json:"unique_id"` // 每行数据的唯一值ID,理论上有oa的REQUESTID就可以了
  17. OARequestID sql.NullInt64 `json:"oa_request_id"` // oa里的申请单请求ID,假设唯一标识
  18. OARequestName sql.NullString `json:"oa_request_name"` // 请求名称
  19. OARequestNameNew sql.NullString `json:"oa_request_name_new"` // 新请求名称
  20. OARequestNameHTMLNew sql.NullString `json:"oa_request_name_html_new"` // 新请求名称(HTML格式)
  21. OAGLXMID sql.NullInt64 `json:"oa_glxm_id"` // 关联项目ID
  22. OAGLXMName sql.NullString `json:"oa_glxm_name"` // 关联项目
  23. OASQSJ sql.NullString `json:"oa_sqsj"` // 申请时间
  24. OASalespersonName sql.NullString `json:"oa_salesperson_name"` // 销售人员名称
  25. OAXSJSYX sql.NullString `json:"oa_xsjsyx"` // 销售邮箱
  26. OAOperationsPersonName sql.NullString `json:"oa_operations_person_name"` // 运维人员名称
  27. OAJFJSYX sql.NullString `json:"oa_jfjsyx"` // 运维邮箱
  28. OASYDW sql.NullString `json:"oa_sydw"` // 使用单位
  29. OAXMXXMS sql.NullString `json:"oa_xmxxms"` // 项目详细描述
  30. OAJDS sql.NullInt64 `json:"oa_jds"` // 节点数
  31. OANodeCount sql.NullInt64 `json:"oa_node_count"` // 总节点数
  32. OAProductCode sql.NullString `json:"oa_product_code"` // 产品编号
  33. OAProductName sql.NullString `json:"oa_product_name"` // 产品名称
  34. OAProductVersion sql.NullString `json:"oa_product_version"` // 产品版本
  35. OACPU sql.NullString `json:"oa_cpu"` // CPU 信息
  36. OAOperatingSystem sql.NullString `json:"oa_operating_system"` // 操作系统信息
  37. OAMainMAC sql.NullString `json:"oa_main_mac"` // 主 MAC 地址
  38. OASecondMAC sql.NullString `json:"oa_second_mac"` // 副 MAC 地址
  39. OACreationDate sql.NullString `json:"oa_creation_date"` // 创建日期
  40. OACreationTime sql.NullString `json:"oa_creation_time"` // 创建时间
  41. OALastOperateDate sql.NullString `json:"oa_last_operate_date"` // 最后操作日期
  42. OALastOperateTime sql.NullString `json:"oa_last_operate_time"` // 最后操作时间
  43. CaptureTime sql.NullTime `json:"capture_time"` // 抓取时间,用于记录数据抓取的时间
  44. DelTime sql.NullTime `json:"del_Time"` //该行的删除时间
  45. LastOperateTime sql.NullTime `json:"LAST_OPERATE_TIME"` // 该表的行最后操作时间,非oa表中的最后操作时间'
  46. }
  47. type LicenseGenerateInfo struct {
  48. ID sql.NullInt32 `json:"id"` // 自增主键,从1开始,每次递增1
  49. OAId sql.NullInt32 `json:"OA_Id"`
  50. LicenseUniqueID sql.NullString `json:"license_unique_id"` // LicenseApplication表的唯一值ID
  51. LicenseFlage sql.NullString `json:"license_flage"` // License分发状态
  52. Lic1 sql.NullString `json:"lic1"` // 主License
  53. Lic2 sql.NullString `json:"lic2"` // 副License
  54. CreatorGenerate sql.NullTime `json:"creator_generate"` // 生成时间
  55. }
  56. // LicenseRecord 包含 LicenseRecordToUser 和 LicenseRecordToEmails 两个结构体
  57. type LicenseRecord struct {
  58. LicenseRecordToUser []LicenseRecordToUser `json:"license_record_to_user"`
  59. LicenseRecordToEmails []LicenseRecordToEmails `json:"license_record_to_emails"`
  60. }
  61. type LicenseRecordToUser struct {
  62. ID int `json:"id"`
  63. LicenseUniqueID string `json:"license_unique_id"`
  64. UserUniqueID string `json:"user_unique_id"`
  65. UserAccount string `json:"user_account"`
  66. OperatorUniqueID string `json:"operator_unique_id"`
  67. UpTime time.Time `json:"up_time"`
  68. DelTime time.Time `json:"del_time"` // 删除或失效的时间
  69. }
  70. type LicenseRecordToEmails struct {
  71. ID int `json:"id"`
  72. LicenseUniqueID string `json:"license_unique_id"`
  73. Emails string `json:"emails"`
  74. OperatorUniqueID string `json:"operator_unique_id"`
  75. UpTime time.Time `json:"up_time"`
  76. DelTime time.Time `json:"del_time"` // 删除或失效的时间
  77. }
  78. // 获取多个或单个license信息,或者指定某个用户能查询到的所有license信息
  79. func GetOALicenseInfo(page int, pageSize int, permission string, licUniqueID string, userUniqueID string, OA_REQUESTID string) (*[]OALicenseInfo, int, error) {
  80. //offset := (page - 1) * pageSize
  81. // 查询总记录数
  82. // var total int
  83. // err := global.XuguDB.QueryRow(`SELECT COUNT(*) FROM target_OA_license WHERE del_Time IS NULL`).Scan(&total)
  84. // if err != nil {
  85. // return nil, 0, fmt.Errorf("GetOALicenseInfocount query error: %v", err)
  86. // }
  87. switch {
  88. // 根据LicUniqueID查询指定页的数据
  89. case licUniqueID != "":
  90. data, err := getSingleLicInfoByUniqueID(licUniqueID)
  91. if err != nil {
  92. return nil, 0, fmt.Errorf("getOAlicInfoToUniqueID query error: %v", err)
  93. }
  94. return &[]OALicenseInfo{*data}, 1, nil
  95. // 根据userUniqueID查询指定页的数据 未完成
  96. case userUniqueID != "":
  97. data, err := getLicInfoByUserUniqueID(userUniqueID, pageSize, page)
  98. if err != nil {
  99. return nil, 0, fmt.Errorf("getOAlicInfoToUniqueID query error: %v", err)
  100. }
  101. return data, 1, nil
  102. //case permission == "read_license":
  103. //读取所有的lic信息
  104. case permission == "read_all_license":
  105. data, total, err := getAllLicInfo(userUniqueID, pageSize, page)
  106. if err != nil {
  107. return nil, 0, fmt.Errorf("getOAlicInfoToUniqueID query error: %v", err)
  108. }
  109. return data, total, nil
  110. //读取所有
  111. case OA_REQUESTID != "":
  112. data, err := getLicInfoByOA_REQUESTID(OA_REQUESTID)
  113. if err != nil {
  114. return nil, 0, fmt.Errorf("getOAlicInfoToUniqueID query error: %v", err)
  115. }
  116. return data, len(*data), nil
  117. }
  118. return nil, 0, fmt.Errorf("getOAlicInfoToUniqueID 没有指定查询条件")
  119. }
  120. func getAllLicInfo(userUniqueID string, limit int, offset int) (*[]OALicenseInfo, int, error) {
  121. fmt.Println("getAllLicInfo", limit, offset)
  122. total := 0
  123. err := global.XuguDB.QueryRow(`
  124. SELECT COUNT(*) from (SELECT OA_REQUESTID AS RequestCount
  125. FROM TARGET_OA_LICENSE
  126. where Del_Time IS NULL
  127. GROUP BY OA_REQUESTID
  128. )
  129. HAVING COUNT(*) > 1;`).Scan(&total)
  130. if err != nil {
  131. return nil, 0, fmt.Errorf("count query error: %v", err)
  132. }
  133. sql := `
  134. SELECT *
  135. FROM SYSDBA.TARGET_OA_LICENSE tl
  136. INNER JOIN
  137. License_generate_Info li
  138. ON
  139. tl.Unique_ID = li.License_UniqueID
  140. WHERE OA_REQUESTID IN(
  141. SELECT OA_REQUESTID FROM (
  142. SELECT OA_REQUESTID ,rownum AS a FROM (SELECT DISTINCT OA_REQUESTID FROM TARGET_OA_LICENSE) a) s
  143. WHERE a BETWEEN ? AND ?)
  144. AND tl.del_Time IS NULL
  145. `
  146. rows, err := global.XuguDB.Query(sql, limit, offset)
  147. if err != nil {
  148. global.Logger.Errorln(" getAllLicInfo rows not found ", err.Error())
  149. return nil, 0, fmt.Errorf("getAllLicInfo query error: %v", err)
  150. }
  151. defer rows.Close()
  152. var results []OALicenseInfo
  153. for rows.Next() {
  154. var info OALicenseInfo
  155. err := rows.Scan(
  156. &info.LicInfo.ID, &info.LicInfo.UniqueID, &info.LicInfo.OAId, &info.LicInfo.OARequestID,
  157. &info.LicInfo.OARequestName, &info.LicInfo.OARequestNameNew, &info.LicInfo.OARequestNameHTMLNew,
  158. &info.LicInfo.OAGLXMID, &info.LicInfo.OAGLXMName,
  159. &info.LicInfo.OASQSJ, &info.LicInfo.OASalespersonName, &info.LicInfo.OAXSJSYX,
  160. &info.LicInfo.OAOperationsPersonName, &info.LicInfo.OAJFJSYX, &info.LicInfo.OASYDW,
  161. &info.LicInfo.OAXMXXMS, &info.LicInfo.OAJDS, &info.LicInfo.OANodeCount,
  162. &info.LicInfo.OAProductCode, &info.LicInfo.OAProductName, &info.LicInfo.OAProductVersion,
  163. &info.LicInfo.OACPU, &info.LicInfo.OAOperatingSystem, &info.LicInfo.OAMainMAC,
  164. &info.LicInfo.OASecondMAC, &info.LicInfo.OACreationDate, &info.LicInfo.OACreationTime,
  165. &info.LicInfo.OALastOperateDate, &info.LicInfo.OALastOperateTime, &info.LicInfo.CaptureTime,
  166. &info.LicInfo.DelTime, &info.LicInfo.LastOperateTime,
  167. &info.GenrateInfo.ID, &info.GenrateInfo.OAId, &info.GenrateInfo.LicenseUniqueID, &info.GenrateInfo.LicenseFlage,
  168. &info.GenrateInfo.Lic1, &info.GenrateInfo.Lic2, &info.GenrateInfo.CreatorGenerate,
  169. )
  170. if err != nil {
  171. global.Logger.Errorln(" getAllLicInfo scan error: ", err.Error())
  172. return nil, 0, fmt.Errorf("getAllLicInfo scan error: %v", err)
  173. }
  174. results = append(results, info)
  175. }
  176. // 打印结果或进一步处理
  177. // for _, result := range results {
  178. // fmt.Println(result)
  179. // }
  180. return &results, total, nil
  181. }
  182. func getSingleLicInfoByUniqueID(unique_ID string) (*OALicenseInfo, error) {
  183. sql := `
  184. SELECT
  185. la.ID, la.Unique_ID, la.OA_ID, la.OA_REQUESTID, la.OA_REQUESTNAME, la.OA_REQUESTNAMENEW,
  186. la.OA_REQUESTNAMEHTMLNEW, la.OA_GLXMID ,la.OA_GLXMNAME ,la.OA_SQSJ, la.OA_SALESPERSONNAME, la.OA_XSJSYX,
  187. la.OA_OPERATIONSPERSONNAME, la.OA_JFJSYX, la.OA_SYDW, la.OA_XMXXMS, la.OA_JDS,
  188. la.OA_NODECOUNT, la.OA_PRODUCTCODE, la.OA_PRODUCTNAME, la.OA_PRODUCTVERSION,
  189. la.OA_CPU, la.OA_OPERATINGSYSTEM, la.OA_MAINMAC, la.OA_SECONDMAC, la.OA_CREATIONDATE,
  190. la.OA_CREATIONTIME, la.OA_LASTOPERATEDATE, la.OA_LASTOPERATETIME, la.capture_Time,
  191. la.del_Time, la.LAST_OPERATE_TIME,
  192. li.ID AS License_ID, li.OA_ID, li.License_UniqueID, li.License_Flage, li.lic1, li.lic2, li.Creator_generate
  193. FROM
  194. target_OA_license la
  195. INNER JOIN
  196. License_generate_Info li
  197. ON
  198. la.Unique_ID = li.License_UniqueID
  199. WHERE
  200. la.del_Time IS NULL
  201. AND
  202. la.Unique_ID = ?`
  203. rows, err := global.XuguDB.Query(sql, unique_ID)
  204. if err != nil {
  205. global.Logger.Errorln("getOAlicInfoToUniqueID 数据查询失败: ", err.Error())
  206. return nil, fmt.Errorf("getOAlicInfoToUniqueID 数据查询失败: %v", err)
  207. }
  208. defer rows.Close()
  209. var info OALicenseInfo
  210. rows.Next()
  211. err = rows.Scan(
  212. &info.LicInfo.ID, &info.LicInfo.UniqueID, &info.LicInfo.OAId, &info.LicInfo.OARequestID,
  213. &info.LicInfo.OARequestName, &info.LicInfo.OARequestNameNew, &info.LicInfo.OARequestNameHTMLNew,
  214. &info.LicInfo.OAGLXMID, &info.LicInfo.OAGLXMName,
  215. &info.LicInfo.OASQSJ, &info.LicInfo.OASalespersonName, &info.LicInfo.OAXSJSYX,
  216. &info.LicInfo.OAOperationsPersonName, &info.LicInfo.OAJFJSYX, &info.LicInfo.OASYDW,
  217. &info.LicInfo.OAXMXXMS, &info.LicInfo.OAJDS, &info.LicInfo.OANodeCount,
  218. &info.LicInfo.OAProductCode, &info.LicInfo.OAProductName, &info.LicInfo.OAProductVersion,
  219. &info.LicInfo.OACPU, &info.LicInfo.OAOperatingSystem, &info.LicInfo.OAMainMAC,
  220. &info.LicInfo.OASecondMAC, &info.LicInfo.OACreationDate, &info.LicInfo.OACreationTime,
  221. &info.LicInfo.OALastOperateDate, &info.LicInfo.OALastOperateTime, &info.LicInfo.CaptureTime,
  222. &info.LicInfo.DelTime, &info.LicInfo.LastOperateTime,
  223. &info.GenrateInfo.ID, &info.GenrateInfo.OAId, &info.GenrateInfo.LicenseUniqueID, &info.GenrateInfo.LicenseFlage,
  224. &info.GenrateInfo.Lic1, &info.GenrateInfo.Lic2, &info.GenrateInfo.CreatorGenerate,
  225. )
  226. if err != nil {
  227. log.Fatal(err)
  228. }
  229. return &info, nil
  230. }
  231. func getLicInfoByUserUniqueID(userUniqueID string, limit int, offset int) (*[]OALicenseInfo, error) {
  232. sql := `
  233. SELECT la.ID, la.Unique_ID, la.OA_REQUESTID, la.OA_REQUESTNAME, la.OA_REQUESTNAMENEW,
  234. la.OA_REQUESTNAMEHTMLNEW,
  235. la.OA_GLXMID ,la.OA_GLXMNAME,
  236. la.OA_SQSJ, la.OA_SALESPERSONNAME, la.OA_XSJSYX,
  237. la.OA_OPERATIONSPERSONNAME, la.OA_JFJSYX, la.OA_SYDW, la.OA_XMXXMS, la.OA_JDS,
  238. la.OA_NODECOUNT, la.OA_PRODUCTCODE, la.OA_PRODUCTNAME, la.OA_PRODUCTVERSION,
  239. la.OA_CPU, la.OA_OPERATINGSYSTEM, la.OA_MAINMAC, la.OA_SECONDMAC, la.OA_CREATIONDATE,
  240. la.OA_CREATIONTIME, la.OA_LASTOPERATEDATE, la.OA_LASTOPERATETIME, la.capture_Time,
  241. la.del_Time, la.LAST_OPERATE_TIME,
  242. li.ID AS License_ID, li.License_UniqueID, li.License_Flage, li.lic1, li.lic2, li.Creator_generate
  243. FROM SYSDBA.TARGET_OA_LICENSE la
  244. INNER JOIN
  245. License_generate_Info li
  246. ON
  247. la.Unique_ID = li.License_UniqueID
  248. INNER JOIN
  249. LICENSERECORDTOUSER LU
  250. ON
  251. la.UNIQUE_ID = lu.LICENSE_UNIQUEID
  252. WHERE OA_REQUESTID IN(
  253. SELECT OA_REQUESTID FROM (
  254. SELECT OA_REQUESTID ,rownum AS a FROM (SELECT DISTINCT OA_REQUESTID FROM TARGET_OA_LICENSE) a) s
  255. WHERE a BETWEEN ? AND ?)
  256. AND la.del_Time IS NULL
  257. AND lu.USER_UNIQUEID = ?
  258. `
  259. rows, err := global.XuguDB.Query(sql, limit, offset, userUniqueID)
  260. if err != nil {
  261. global.Logger.Errorln(" getAllLicInfo rows not found ", err.Error())
  262. return nil, fmt.Errorf("getAllLicInfo query error: %v", err)
  263. }
  264. defer rows.Close()
  265. var results []OALicenseInfo
  266. for rows.Next() {
  267. var info OALicenseInfo
  268. err := rows.Scan(
  269. &info.LicInfo.ID, &info.LicInfo.UniqueID, &info.LicInfo.OARequestID,
  270. &info.LicInfo.OARequestName, &info.LicInfo.OARequestNameNew, &info.LicInfo.OARequestNameHTMLNew,
  271. &info.LicInfo.OAGLXMID, &info.LicInfo.OAGLXMName,
  272. &info.LicInfo.OASQSJ, &info.LicInfo.OASalespersonName, &info.LicInfo.OAXSJSYX,
  273. &info.LicInfo.OAOperationsPersonName, &info.LicInfo.OAJFJSYX, &info.LicInfo.OASYDW,
  274. &info.LicInfo.OAXMXXMS, &info.LicInfo.OAJDS, &info.LicInfo.OANodeCount,
  275. &info.LicInfo.OAProductCode, &info.LicInfo.OAProductName, &info.LicInfo.OAProductVersion,
  276. &info.LicInfo.OACPU, &info.LicInfo.OAOperatingSystem, &info.LicInfo.OAMainMAC,
  277. &info.LicInfo.OASecondMAC, &info.LicInfo.OACreationDate, &info.LicInfo.OACreationTime,
  278. &info.LicInfo.OALastOperateDate, &info.LicInfo.OALastOperateTime, &info.LicInfo.CaptureTime,
  279. &info.LicInfo.DelTime, &info.LicInfo.LastOperateTime,
  280. &info.GenrateInfo.ID, &info.GenrateInfo.LicenseUniqueID, &info.GenrateInfo.LicenseFlage,
  281. &info.GenrateInfo.Lic1, &info.GenrateInfo.Lic2, &info.GenrateInfo.CreatorGenerate,
  282. )
  283. if err != nil {
  284. global.Logger.Errorln(" getAllLicInfo scan error: ", err.Error())
  285. return nil, fmt.Errorf("getAllLicInfo scan error: %v", err)
  286. }
  287. results = append(results, info)
  288. }
  289. // 打印结果或进一步处理
  290. for _, result := range results {
  291. fmt.Println(result)
  292. }
  293. return &results, nil
  294. }
  295. func getLicInfoByOA_REQUESTID(unique_ID string) (*[]OALicenseInfo, error) {
  296. sql := `
  297. SELECT
  298. la.ID, la.Unique_ID, la.OA_ID, la.OA_REQUESTID, la.OA_REQUESTNAME, la.OA_REQUESTNAMENEW,
  299. la.OA_REQUESTNAMEHTMLNEW, la.OA_GLXMID ,la.OA_GLXMNAME ,la.OA_SQSJ, la.OA_SALESPERSONNAME, la.OA_XSJSYX,
  300. la.OA_OPERATIONSPERSONNAME, la.OA_JFJSYX, la.OA_SYDW, la.OA_XMXXMS, la.OA_JDS,
  301. la.OA_NODECOUNT, la.OA_PRODUCTCODE, la.OA_PRODUCTNAME, la.OA_PRODUCTVERSION,
  302. la.OA_CPU, la.OA_OPERATINGSYSTEM, la.OA_MAINMAC, la.OA_SECONDMAC, la.OA_CREATIONDATE,
  303. la.OA_CREATIONTIME, la.OA_LASTOPERATEDATE, la.OA_LASTOPERATETIME, la.capture_Time,
  304. la.del_Time, la.LAST_OPERATE_TIME,
  305. li.ID AS License_ID, li.OA_ID, li.License_UniqueID, li.License_Flage, li.lic1, li.lic2, li.Creator_generate
  306. FROM
  307. target_OA_license la
  308. INNER JOIN
  309. License_generate_Info li
  310. ON
  311. la.Unique_ID = li.License_UniqueID
  312. WHERE
  313. la.del_Time IS NULL
  314. AND
  315. la.OA_REQUESTID = ?`
  316. rows, err := global.XuguDB.Query(sql, unique_ID)
  317. if err != nil {
  318. global.Logger.Errorln("getLicInfoByOAUniqueID 数据查询失败: ", err.Error())
  319. return nil, fmt.Errorf("getLicInfoByOAUniqueID 数据查询失败: %v", err)
  320. }
  321. defer rows.Close()
  322. var rest []OALicenseInfo
  323. for rows.Next() {
  324. var info OALicenseInfo
  325. err = rows.Scan(
  326. &info.LicInfo.ID, &info.LicInfo.UniqueID, &info.LicInfo.OAId, &info.LicInfo.OARequestID,
  327. &info.LicInfo.OARequestName, &info.LicInfo.OARequestNameNew, &info.LicInfo.OARequestNameHTMLNew,
  328. &info.LicInfo.OAGLXMID, &info.LicInfo.OAGLXMName,
  329. &info.LicInfo.OASQSJ, &info.LicInfo.OASalespersonName, &info.LicInfo.OAXSJSYX,
  330. &info.LicInfo.OAOperationsPersonName, &info.LicInfo.OAJFJSYX, &info.LicInfo.OASYDW,
  331. &info.LicInfo.OAXMXXMS, &info.LicInfo.OAJDS, &info.LicInfo.OANodeCount,
  332. &info.LicInfo.OAProductCode, &info.LicInfo.OAProductName, &info.LicInfo.OAProductVersion,
  333. &info.LicInfo.OACPU, &info.LicInfo.OAOperatingSystem, &info.LicInfo.OAMainMAC,
  334. &info.LicInfo.OASecondMAC, &info.LicInfo.OACreationDate, &info.LicInfo.OACreationTime,
  335. &info.LicInfo.OALastOperateDate, &info.LicInfo.OALastOperateTime, &info.LicInfo.CaptureTime,
  336. &info.LicInfo.DelTime, &info.LicInfo.LastOperateTime,
  337. &info.GenrateInfo.ID, &info.GenrateInfo.OAId, &info.GenrateInfo.LicenseUniqueID, &info.GenrateInfo.LicenseFlage,
  338. &info.GenrateInfo.Lic1, &info.GenrateInfo.Lic2, &info.GenrateInfo.CreatorGenerate,
  339. )
  340. if err != nil {
  341. log.Fatal(err)
  342. }
  343. rest = append(rest, info)
  344. }
  345. return &rest, nil
  346. }
  347. // 插入生成好的lic1,2的信息
  348. func UpdateOALicenseStr(uniqueID string, lic1 []byte, lic2 []byte) error {
  349. tx, err := global.XuguDB.Begin()
  350. if err != nil {
  351. global.Logger.Errorln("begin transaction失败: ", err.Error())
  352. return fmt.Errorf("begin transaction: %v", err)
  353. }
  354. defer func() {
  355. if err != nil {
  356. tx.Rollback()
  357. } else {
  358. err = tx.Commit()
  359. }
  360. }()
  361. //插入 LicenseInfo 表
  362. if lic2 == nil {
  363. _, err = tx.Exec(`
  364. UPDATE License_generate_Info SET License_Flage = ? ,lic1 = ?,Creator_generate =? WHERE License_UniqueID = ?
  365. `,
  366. "已生成", string(lic1), time.Now(), uniqueID)
  367. } else {
  368. _, err = tx.Exec(`
  369. UPDATE License_generate_Info SET License_Flage = ? ,lic1 = ?,lic2 = ?,Creator_generate =? WHERE License_UniqueID = ?
  370. `,
  371. "已生成", string(lic1), string(lic2), time.Now(), uniqueID)
  372. }
  373. if err != nil {
  374. global.Logger.Errorln("插入lic1 ,2 失败: ", err.Error())
  375. return err
  376. }
  377. return nil
  378. }
  379. // 修改一行前端可改的license信息
  380. func UpdatelicenseInfoRow(license OALicenseInfo) error {
  381. tx, err := global.XuguDB.Begin()
  382. if err != nil {
  383. return fmt.Errorf("begin transaction: %v", err)
  384. }
  385. defer func() {
  386. if err != nil {
  387. tx.Rollback()
  388. } else {
  389. err = tx.Commit()
  390. }
  391. }()
  392. _, err = tx.Exec(`
  393. UPDATE target_OA_license SET OA_REQUESTNAME =? ,OA_CREATIONDATE =? ,OA_GLXMNAME = ? ,
  394. OA_SALESPERSONNAME = ? , OA_XSJSYX = ? , OA_OPERATIONSPERSONNAME = ? , OA_JFJSYX = ? , OA_JDS = ? ,
  395. OA_SYDW = ? , OA_PRODUCTNAME = ? , OA_PRODUCTVERSION = ? , OA_NODECOUNT = ?
  396. WHERE Unique_ID =?;
  397. `, license.LicInfo.OARequestName.String, license.LicInfo.OACreationDate.String, license.LicInfo.OAGLXMName.String,
  398. license.LicInfo.OASalespersonName.String, license.LicInfo.OAXSJSYX.String, license.LicInfo.OAOperationsPersonName.String, license.LicInfo.OAJFJSYX.String, license.LicInfo.OAJDS.Int64,
  399. license.LicInfo.OASYDW.String, license.LicInfo.OAProductName.String, license.LicInfo.OAProductVersion.String, license.LicInfo.OANodeCount.Int64, license.LicInfo.UniqueID.String)
  400. if err != nil {
  401. global.Logger.Errorln("UpdatelicenseInfoRow失败 ", err.Error())
  402. return err
  403. }
  404. return nil
  405. }
  406. // 插入分发记录用户表
  407. func InsertlicenseRecordByUserRow(LicenseUniqueID string, userUniqueID string, UserAccount string, operatorUniqueID string) error {
  408. tx, err := global.XuguDB.Begin()
  409. if err != nil {
  410. global.Logger.Errorln("begin transaction失败: ", err.Error())
  411. return fmt.Errorf("begin transaction: %v", err)
  412. }
  413. defer func() {
  414. if err != nil {
  415. tx.Rollback()
  416. } else {
  417. err = tx.Commit()
  418. }
  419. }()
  420. _, err = tx.Exec(`
  421. Insert into licenseRecordToUser(License_UniqueID ,user_UniqueID,User_Account,up_Time,operator_UniqueID) values(?,?,?,?,?)`,
  422. LicenseUniqueID, userUniqueID, UserAccount, time.Now(), operatorUniqueID)
  423. if err != nil {
  424. global.Logger.Errorln("插入分发记录用户表失败: ", err.Error())
  425. return err
  426. }
  427. return nil
  428. }
  429. // 插入分发记录邮箱表
  430. func InsertlicenseRecordByEmailRow(LicenseUniqueID string, emails string, operatorUniqueID string) error {
  431. tx, err := global.XuguDB.Begin()
  432. if err != nil {
  433. global.Logger.Errorln("begin transaction失败: ", err.Error())
  434. return fmt.Errorf("begin transaction: %v", err)
  435. }
  436. defer func() {
  437. if err != nil {
  438. tx.Rollback()
  439. } else {
  440. err = tx.Commit()
  441. }
  442. }()
  443. _, err = tx.Exec(`
  444. Insert into licenseRecordToEmails(License_UniqueID ,emails,operator_UniqueID,up_Time) values(?,?,?,?)`,
  445. LicenseUniqueID, emails, operatorUniqueID, time.Now())
  446. if err != nil {
  447. global.Logger.Errorln("插入分发记录邮箱表失败: ", err.Error())
  448. return err
  449. }
  450. return nil
  451. }
  452. // 搜索分发记录用户表
  453. func GetlicenseRecordByUser(uniqueID string) ([]LicenseRecordToUser, error) {
  454. fmt.Println("搜索分发记录表 uniqueID", uniqueID)
  455. if uniqueID == "" {
  456. return nil, fmt.Errorf("uniqueID is empty")
  457. }
  458. sqlLicUser := `select License_UniqueID,user_UNIQUEID,User_Account,operator_UniqueID,up_Time from licenseRecordToUser where License_UniqueID = ? AND del_time IS NULL`
  459. rows, err := global.XuguDB.Query(sqlLicUser, uniqueID)
  460. if err != nil {
  461. global.Logger.Errorln("query data: ", err.Error())
  462. return nil, fmt.Errorf("query error: %v", err)
  463. }
  464. defer rows.Close()
  465. var results []LicenseRecordToUser
  466. for rows.Next() {
  467. var lRU LicenseRecordToUser
  468. err = rows.Scan(
  469. &lRU.LicenseUniqueID,
  470. &lRU.UserUniqueID,
  471. &lRU.UserAccount,
  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. fmt.Println("LicenseRecordToUser ", results)
  482. return results, nil
  483. }
  484. // 搜索分发记录邮箱表
  485. func GetlicenseRecordByEmail(uniqueID string) ([]LicenseRecordToEmails, error) {
  486. if uniqueID == "" {
  487. return nil, fmt.Errorf("uniqueID is empty")
  488. }
  489. sqlLicEmail := `select License_UniqueID,emails,operator_UniqueID ,up_Time from licenseRecordToEmails where License_UniqueID = ? AND del_time IS NULL;`
  490. rows, err := global.XuguDB.Query(sqlLicEmail, uniqueID)
  491. if err != nil {
  492. global.Logger.Errorln("query data: ", err.Error())
  493. return nil, fmt.Errorf("query error: %v", err)
  494. }
  495. var results []LicenseRecordToEmails
  496. for rows.Next() {
  497. var lRU LicenseRecordToEmails
  498. err = rows.Scan(
  499. &lRU.LicenseUniqueID,
  500. &lRU.Emails,
  501. &lRU.OperatorUniqueID,
  502. &lRU.UpTime,
  503. )
  504. if err != nil {
  505. global.Logger.Errorln("scan row: ", err.Error())
  506. return nil, fmt.Errorf("scan row error: %v", err)
  507. }
  508. results = append(results, lRU)
  509. }
  510. if err = rows.Err(); err != nil {
  511. global.Logger.Errorln("rows error: ", err.Error())
  512. return nil, fmt.Errorf("rows error: %v", err)
  513. }
  514. return results, nil
  515. }
  516. // 删除LicenseApplication和LicenseInfo表中的数据
  517. func DelLicenseInfoRow(uniqueID string) error {
  518. fmt.Println("UniqueID12313", uniqueID)
  519. tx, err := global.XuguDB.Begin()
  520. if err != nil {
  521. return fmt.Errorf("begin transaction: %v", err)
  522. }
  523. defer func() {
  524. if err != nil {
  525. tx.Rollback()
  526. } else {
  527. err = tx.Commit()
  528. }
  529. }()
  530. // "update LicenseApplication set DelTime = ? where UniqueID = ?",
  531. //DELETE FROM LicenseInfo WHERE LicenseApplicationUniqueID = ?
  532. // _, err = tx.Exec("DELETE FROM LicenseInfo WHERE LicenseApplicationUniqueID = ?", uniqueID)
  533. // if err != nil {
  534. // return err
  535. // }
  536. // _, err = tx.Exec("DELETE FROM LicenseApplication WHERE UniqueID = ?", uniqueID)
  537. // if err != nil {
  538. // return err
  539. // }
  540. _, err = tx.Exec("UPDATE target_OA_license SET Del_Time = ? WHERE Unique_ID = ?", time.Now(), uniqueID)
  541. if err != nil {
  542. return err
  543. }
  544. return nil
  545. }
  546. type SQLResult struct {
  547. OARequestName sql.NullString `json:"oa_request_name"` // 请求名称 (wr.REQUESTNAME)
  548. OARequestNameNew sql.NullString `json:"oa_request_name_new"` // 新请求名称 (wr.REQUESTNAMENEW)
  549. OARequestNameHTMLNew sql.NullString `json:"oa_request_name_html_new"` // 新请求名称(HTML格式) (wr.REQUESTNAMEHTMLNEW)
  550. OAGLXMID sql.NullInt64 `json:"oa_glxm_id"` // 关联项目ID (fm.glxm)
  551. OAGLXMName sql.NullString `json:"oa_glxm_name"` // 关联项目 (PP.NAME)
  552. OASQSJ sql.NullString `json:"oa_sqsj"` // 申请时间 (fm.SQSJ)
  553. OASalespersonName sql.NullString `json:"oa_salesperson_name"` // 销售人员名称 (hrm1.LASTNAME)
  554. OAOperationsPersonName sql.NullString `json:"oa_operations_person_name"` // 运维人员名称 (hrm2.LASTNAME)
  555. OAXSJSYX sql.NullString `json:"oa_xsjsyx"` // 销售邮箱 (fm.XSJSYX)
  556. OAJFJSYX sql.NullString `json:"oa_jfjsyx"` // 运维邮箱 (fm.JFJSYX)
  557. OASYDW sql.NullString `json:"oa_sydw"` // 使用单位 (fm.SYDW)
  558. OAXMXXMS sql.NullString `json:"oa_xmxxms"` // 项目详细描述 (fm.XMXXMS)
  559. OAJDS sql.NullInt64 `json:"oa_jds"` // 节点数 (fm.JDS)
  560. OANodeCount sql.NullInt64 `json:"oa_node_count"` // 总节点数 (fmd.JDS)
  561. OAProductCode sql.NullString `json:"oa_product_code"` // 产品编号 (ws1.SELECTNAME)
  562. OAProductVersion sql.NullString `json:"oa_product_version"` // 产品版本 (ws2.SELECTNAME)
  563. CLQ sql.NullString `json:"clq"` // 车辆情况 (fmd.CLQ)
  564. CZXT sql.NullString `json:"czxt"` // 操作系统 (fmd.CZXT)
  565. IP sql.NullString `json:"ip"` // IP 地址 (fmd.IP)
  566. MAC sql.NullString `json:"mac"` // MAC 地址 (fmd.MAC)
  567. OACreationDate sql.NullString `json:"oa_creation_date"` // 创建日期 (wr.CREATEDATE)
  568. OACreationTime sql.NullString `json:"oa_creation_time"` // 创建时间 (wr.CREATETIME)
  569. OALastOperateDate sql.NullString `json:"oa_last_operate_date"` // 最后操作日期 (wr.LASTOPERATEDATE)
  570. OALastOperateTime sql.NullString `json:"oa_last_operate_time"` // 最后操作时间 (wr.LASTOPERATETIME)
  571. }
  572. // 检验与oa库数据是否一致
  573. func CheckLicenseInfoInOADB(licInfo *TargetOALicenseInfo) (bool, error) {
  574. //fmt.Println("该死的id", licInfo.ID)
  575. if !licInfo.OAId.Valid {
  576. return false, fmt.Errorf("uniqueID is empty")
  577. }
  578. sql := `
  579. SELECT
  580. wr.REQUESTNAME,
  581. wr.REQUESTNAMENEW, wr.REQUESTNAMEHTMLNEW,
  582. fm.glxm,PP.NAME,fm.SQSJ, hrm1.LASTNAME,
  583. hrm2.LASTNAME, fm.XSJSYX, fm.JFJSYX,
  584. fm.SYDW, fm.XMXXMS, fm.JDS,
  585. fmd.JDS, ws1.SELECTNAME,
  586. ws2.SELECTNAME, fmd.CLQ, fmd.CZXT,
  587. fmd.IP, fmd.MAC,wr.CREATEDATE, wr.CREATETIME,
  588. wr.LASTOPERATEDATE, wr.LASTOPERATETIME
  589. FROM XUGU.formtable_main_146 fm
  590. LEFT JOIN XUGU.HRMRESOURCE hrm1 ON TO_NUMBER(fm.XSRY) = hrm1.id
  591. LEFT JOIN XUGU.HRMRESOURCE hrm2 ON TO_NUMBER(fm.jfry) = hrm2.id
  592. LEFT JOIN XUGU.FORMTABLE_MAIN_146_dt1 fmd ON fmd.mainid = fm.id
  593. LEFT JOIN XUGU.WORKFLOW_SELECTITEM ws1 ON fmd.cpmc = ws1.SELECTVALUE AND ws1.FIELDID = 14627
  594. LEFT JOIN XUGU.WORKFLOW_SELECTITEM ws2 ON fmd.BB = ws2.SELECTVALUE AND ws2.FIELDID = 14628
  595. LEFT JOIN XUGU.WORKFLOW_REQUESTBASE WR ON fm.REQUESTID = WR.REQUESTID
  596. LEFT JOIN XUGU.PRJ_PROJECTINFO PP ON fm.glxm = PP.ID
  597. where fmd.id = ?
  598. PARALLEL 8;
  599. `
  600. var record SQLResult
  601. //Wvar sqsj string
  602. err := global.OaDB.QueryRow(sql, licInfo.OAId.Int32).Scan(
  603. &record.OARequestName,
  604. &record.OARequestNameNew,
  605. &record.OARequestNameHTMLNew,
  606. &record.OAGLXMID,
  607. &record.OAGLXMName,
  608. &record.OASQSJ,
  609. &record.OASalespersonName,
  610. &record.OAOperationsPersonName,
  611. &record.OAXSJSYX,
  612. &record.OAJFJSYX,
  613. &record.OASYDW,
  614. &record.OAXMXXMS,
  615. &record.OAJDS,
  616. &record.OANodeCount,
  617. &record.OAProductCode,
  618. &record.OAProductVersion,
  619. &record.CLQ,
  620. &record.CZXT,
  621. &record.IP,
  622. &record.MAC,
  623. &record.OACreationDate,
  624. &record.OACreationTime,
  625. &record.OALastOperateDate,
  626. &record.OALastOperateTime,
  627. )
  628. if err != nil {
  629. //global.Logger.Errorln("query data: ", err.Error())
  630. return false, fmt.Errorf("CheckLicenseInfoInOADB query error: %v", err)
  631. }
  632. // 将字符串转换为 time.Time 对象
  633. //record.OASQSJ.Time, err = time.Parse("2006-01-02", sqsj)
  634. if err != nil {
  635. //fmt.Println("Error parsing date:", err)
  636. return false, fmt.Errorf("CheckLicenseInfoInOADB 将字符串转换为 time.Time Error : %v", err)
  637. }
  638. if isEqual := compareAndCopy(record, licInfo); !isEqual {
  639. return false, fmt.Errorf("与oa数据不一致")
  640. }
  641. return true, nil
  642. }
  643. func compareAndCopy(sqlResult SQLResult, target *TargetOALicenseInfo) bool {
  644. // 标志位,初始为 true,表示默认所有值相等
  645. isEqual := true
  646. // 比较 OARequestName
  647. if sqlResult.OARequestName != target.OARequestName {
  648. target.OARequestName = sqlResult.OARequestName
  649. isEqual = false
  650. }
  651. // 比较 OARequestNameNew
  652. if sqlResult.OARequestNameNew != target.OARequestNameNew {
  653. target.OARequestNameNew = sqlResult.OARequestNameNew
  654. isEqual = false
  655. }
  656. // 比较 OARequestNameHTMLNew
  657. if sqlResult.OARequestNameHTMLNew != target.OARequestNameHTMLNew {
  658. target.OARequestNameHTMLNew = sqlResult.OARequestNameHTMLNew
  659. isEqual = false
  660. }
  661. // 比较 OAGLXMID
  662. if sqlResult.OAGLXMID != target.OAGLXMID {
  663. target.OAGLXMID = sqlResult.OAGLXMID
  664. isEqual = false
  665. }
  666. // 比较 OAGLXMName
  667. if sqlResult.OAGLXMName != target.OAGLXMName {
  668. target.OAGLXMName = sqlResult.OAGLXMName
  669. isEqual = false
  670. }
  671. // 比较 OASQSJ
  672. if sqlResult.OASQSJ != target.OASQSJ {
  673. target.OASQSJ = sqlResult.OASQSJ
  674. isEqual = false
  675. }
  676. // 比较 OASalespersonName
  677. if sqlResult.OASalespersonName != target.OASalespersonName {
  678. target.OASalespersonName = sqlResult.OASalespersonName
  679. isEqual = false
  680. }
  681. // 比较 OAOperationsPersonName
  682. if sqlResult.OAOperationsPersonName != target.OAOperationsPersonName {
  683. target.OAOperationsPersonName = sqlResult.OAOperationsPersonName
  684. isEqual = false
  685. }
  686. // 比较 OAXSJSYX
  687. if sqlResult.OAXSJSYX != target.OAXSJSYX {
  688. target.OAXSJSYX = sqlResult.OAXSJSYX
  689. isEqual = false
  690. }
  691. // 比较 OAJFJSYX
  692. if sqlResult.OAJFJSYX != target.OAJFJSYX {
  693. target.OAJFJSYX = sqlResult.OAJFJSYX
  694. isEqual = false
  695. }
  696. // 比较 OASYDW
  697. if sqlResult.OASYDW != target.OASYDW {
  698. target.OASYDW = sqlResult.OASYDW
  699. isEqual = false
  700. }
  701. // 比较 OAXMXXMS
  702. if sqlResult.OAXMXXMS != target.OAXMXXMS {
  703. target.OAXMXXMS = sqlResult.OAXMXXMS
  704. isEqual = false
  705. }
  706. // 比较 OAJDS
  707. if sqlResult.OAJDS != target.OAJDS {
  708. target.OAJDS = sqlResult.OAJDS
  709. isEqual = false
  710. }
  711. // 比较 OANodeCount
  712. if sqlResult.OANodeCount != target.OANodeCount {
  713. target.OANodeCount = sqlResult.OANodeCount
  714. isEqual = false
  715. }
  716. // 比较 OAProductCode
  717. // if sqlResult.OAProductCode != target.OAProductCode {
  718. // target.OAProductCode = sqlResult.OAProductCode
  719. // isEqual = false
  720. // }
  721. // 比较 OAProductVersion
  722. if sqlResult.OAProductVersion != target.OAProductVersion {
  723. target.OAProductVersion = sqlResult.OAProductVersion
  724. isEqual = false
  725. }
  726. // 比较 CLQ
  727. if sqlResult.CLQ != target.OACPU {
  728. target.OACPU = sqlResult.CLQ
  729. isEqual = false
  730. }
  731. // 比较 CZXT
  732. if sqlResult.CZXT != target.OAOperatingSystem {
  733. target.OAOperatingSystem = sqlResult.CZXT
  734. isEqual = false
  735. }
  736. // 比较 IP
  737. if sqlResult.IP != target.OAMainMAC {
  738. target.OAMainMAC = sqlResult.IP
  739. isEqual = false
  740. }
  741. // 比较 MAC
  742. if sqlResult.MAC != target.OASecondMAC {
  743. target.OASecondMAC = sqlResult.MAC
  744. isEqual = false
  745. }
  746. // 比较 OACreationDate
  747. if sqlResult.OACreationDate != target.OACreationDate {
  748. target.OACreationDate = sqlResult.OACreationDate
  749. isEqual = false
  750. }
  751. // 比较 OACreationTime
  752. if sqlResult.OACreationTime != target.OACreationTime {
  753. target.OACreationTime = sqlResult.OACreationTime
  754. isEqual = false
  755. }
  756. // 比较 OALastOperateDate
  757. if sqlResult.OALastOperateDate != target.OALastOperateDate {
  758. target.OALastOperateDate = sqlResult.OALastOperateDate
  759. isEqual = false
  760. }
  761. // 比较 OALastOperateTime
  762. if sqlResult.OALastOperateTime != target.OALastOperateTime {
  763. target.OALastOperateTime = sqlResult.OALastOperateTime
  764. isEqual = false
  765. }
  766. // 返回标志位
  767. return isEqual
  768. }