main.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. package main
  2. import (
  3. "go.uber.org/zap"
  4. "gorm.io/gorm"
  5. "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  6. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  7. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  8. "strings"
  9. )
  10. var (
  11. Mgo *mongodb.MongodbSim //87 环境,采集 存储的MongoDB
  12. MysqlDB *gorm.DB
  13. )
  14. func main() {
  15. Init()
  16. dealProject() // 处理项目、债券数据
  17. dealProjectBondRelation() //更新项目和债券的关联关系
  18. }
  19. // dealProject 处理项目相关数据
  20. func dealProject() {
  21. tables := strings.Split(GF.Mongob.List, ",")
  22. detailNames := strings.Split(GF.Mongob.Detail, ",")
  23. //table := GF.Mongob.List
  24. //detailName := GF.Mongob.Detail //专项债详细表
  25. sess := Mgo.GetMgoConn()
  26. defer Mgo.DestoryMongoConn(sess)
  27. for k, table := range tables {
  28. detailName := detailNames[k]
  29. /**
  30. 这里测试用,使用 “新建成都至达州至万州铁路(南充段)(万源市)” 这个项目测试,他有变更信息
  31. */
  32. //where1 := map[string]interface{}{
  33. // //"projectName": "新建成都至达州至万州铁路(南充段)(万源市)",
  34. // "projectName": "新建成都至达州至万州铁路(南充段)(万源市)",
  35. //}
  36. //query := sess.DB("py_theme").C(table).Find(where1).Select(nil).Iter()
  37. query := sess.DB("py_theme").C(table).Find(nil).Select(nil).Iter()
  38. count := 0
  39. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  40. if count%100 == 0 {
  41. log.Info("current:", zap.Int("count", count), zap.Any("projectName", tmp["projectName"]), zap.String(table, detailName))
  42. }
  43. projectName := util.ObjToString(tmp["projectName"])
  44. //
  45. where := map[string]interface{}{
  46. "project.jcxx.projectName": projectName,
  47. }
  48. detail, _ := Mgo.FindOne(detailName, where)
  49. if len(*detail) == 0 {
  50. continue
  51. }
  52. //1.项目数据
  53. if project, ok := (*detail)["project"].(map[string]interface{}); ok {
  54. //1.基础信息
  55. var projectId int
  56. if jcxx, ok := project["jcxx"].(map[string]interface{}); ok {
  57. projectId = dealProjectBaseInfo(jcxx, projectName)
  58. }
  59. //2.还本付息
  60. if hbfx, ok := project["hbfx"].(map[string]interface{}); ok {
  61. dealProjectRepayment(hbfx, projectName, projectId)
  62. }
  63. //3.变更
  64. if bg, ok := project["bg"].([]interface{}); ok {
  65. if len(bg) > 0 {
  66. dealProjectChange(bg, projectName, projectId)
  67. }
  68. }
  69. //4.发行明细
  70. if fxmx, ok := project["fxmx"].([]interface{}); ok {
  71. if len(fxmx) > 0 {
  72. dealProjectIssueDetails(fxmx, projectName, projectId)
  73. }
  74. }
  75. }
  76. //2.处理债券信息
  77. if bonds, ok := (*detail)["bond"].([]interface{}); ok {
  78. dealBondInfo(bonds, projectName)
  79. }
  80. }
  81. log.Info("dealProject over ", zap.Int("total", count))
  82. }
  83. }
  84. // dealProjectBaseInfo 处理项目基本信息
  85. func dealProjectBaseInfo(jcxx map[string]interface{}, projectName string) (projectID int) {
  86. project_base_info_exist := ProjectBaseInfo{}
  87. err := MysqlDB.Where(&ProjectBaseInfo{ProjectName: projectName}).First(&project_base_info_exist).Error
  88. if err != nil && err != gorm.ErrRecordNotFound {
  89. // 处理查询错误
  90. log.Error("dealProjectBaseInfo;Error checking for existing project", zap.Error(err))
  91. }
  92. //当前项目基本信息已经存在
  93. if project_base_info_exist.ID > 0 {
  94. // 直接修改已存在记录的字段
  95. project_base_info_exist.TotalInvestment = util.Float64All(jcxx["totalInvestment"])
  96. project_base_info_exist.Area = util.ObjToString(jcxx["regionName"])
  97. project_base_info_exist.City = strings.ReplaceAll(util.ObjToString(jcxx["cityName"]), "本级", "")
  98. project_base_info_exist.District = strings.ReplaceAll(util.ObjToString(jcxx["countyName"]), "本级", "")
  99. project_base_info_exist.Capital = util.Float64All(jcxx["capital"])
  100. project_base_info_exist.ApplyTotalBonds = util.Float64All(jcxx["applyDebt"])
  101. project_base_info_exist.OtherDebtFinancing = util.Float64All(jcxx["portfolioFinancing"])
  102. project_base_info_exist.SpecialDebtCapital = util.Float64All(jcxx["specialDebtAsCapital"])
  103. project_base_info_exist.ExpectedReturn = util.Float64All(jcxx["expectedReturn"])
  104. project_base_info_exist.ProjectCost = util.IntAll(jcxx["projectCost"])
  105. project_base_info_exist.ProjectDomain = util.ObjToString(jcxx["projectTypeName3"])
  106. project_base_info_exist.ProjectOwner = util.ObjToString(jcxx["projectSubject"])
  107. project_base_info_exist.StartDate = util.ObjToString(jcxx["startDate"])
  108. project_base_info_exist.EndDate = util.ObjToString(jcxx["endDate"])
  109. project_base_info_exist.OperationStartDate = util.ObjToString(jcxx["operationStartDate"])
  110. project_base_info_exist.OperationEndDate = util.ObjToString(jcxx["operationEndDate"])
  111. project_base_info_exist.SourceIncome = util.ObjToString(jcxx["sourceIncome"])
  112. project_base_info_exist.ConstructionContent = util.ObjToString(jcxx["constructionContent"])
  113. project_base_info_exist.Remarks = util.ObjToString(jcxx["remarks"])
  114. project_base_info_exist.OtherDebtFinancingSource = util.ObjToString(jcxx["portfolioFinancingSource"])
  115. project_base_info_exist.CostIncomePercent = util.ObjToString(jcxx["costIncomePercent"])
  116. project_base_info_exist.CoverageMultiple = util.Float64All(jcxx["coverageMultiple"])
  117. project_base_info_exist.CompetentDepartment = util.ObjToString(jcxx["implementingAgency"])
  118. project_base_info_exist.AccountingFirm = util.ObjToString(jcxx["accountingFirm"])
  119. project_base_info_exist.LawFirm = util.ObjToString(jcxx["lawFirm"])
  120. project_base_info_exist.UpdateDate = util.ObjToString(jcxx["updateTime"])
  121. project_base_info_exist.CreateDate = util.ObjToString(jcxx["createTime"])
  122. // 使用 Save 来更新
  123. err = MysqlDB.Save(&project_base_info_exist).Error
  124. if err != nil {
  125. log.Info("dealProject Save (Update) ", zap.Error(err))
  126. }
  127. return project_base_info_exist.ID
  128. } else {
  129. project_base_info := ProjectBaseInfo{
  130. ProjectName: projectName,
  131. TotalInvestment: util.Float64All(jcxx["totalInvestment"]),
  132. Area: util.ObjToString(jcxx["regionName"]),
  133. City: strings.ReplaceAll(util.ObjToString(jcxx["cityName"]), "本级", ""),
  134. District: strings.ReplaceAll(util.ObjToString(jcxx["countyName"]), "本级", ""),
  135. Capital: util.Float64All(jcxx["capital"]),
  136. ApplyTotalBonds: util.Float64All(jcxx["applyDebt"]),
  137. OtherDebtFinancing: util.Float64All(jcxx["portfolioFinancing"]),
  138. SpecialDebtCapital: util.Float64All(jcxx["specialDebtAsCapital"]),
  139. ExpectedReturn: util.Float64All(jcxx["expectedReturn"]),
  140. ProjectCost: util.IntAll(jcxx["projectCost"]),
  141. ProjectDomain: util.ObjToString(jcxx["projectTypeName3"]), // 项目领域
  142. ProjectOwner: util.ObjToString(jcxx["projectSubject"]), //项目业主
  143. StartDate: util.ObjToString(jcxx["startDate"]),
  144. EndDate: util.ObjToString(jcxx["endDate"]),
  145. OperationStartDate: util.ObjToString(jcxx["operationStartDate"]),
  146. OperationEndDate: util.ObjToString(jcxx["operationEndDate"]),
  147. SourceIncome: util.ObjToString(jcxx["sourceIncome"]),
  148. ConstructionContent: util.ObjToString(jcxx["constructionContent"]),
  149. Remarks: util.ObjToString(jcxx["remarks"]),
  150. OtherDebtFinancingSource: util.ObjToString(jcxx["portfolioFinancingSource"]),
  151. CostIncomePercent: util.ObjToString(jcxx["costIncomePercent"]),
  152. CoverageMultiple: util.Float64All(jcxx["coverageMultiple"]),
  153. CompetentDepartment: util.ObjToString(jcxx["implementingAgency"]),
  154. AccountingFirm: util.ObjToString(jcxx["accountingFirm"]),
  155. LawFirm: util.ObjToString(jcxx["lawFirm"]),
  156. CreateDate: util.ObjToString(jcxx["createTime"]),
  157. UpdateDate: util.ObjToString(jcxx["updateTime"]),
  158. }
  159. err = MysqlDB.Create(&project_base_info).Error
  160. if err != nil {
  161. log.Info("dealProjectBaseInfo;dealProject Create ", zap.Error(err), zap.String("project", projectName))
  162. }
  163. return project_base_info.ID
  164. }
  165. }
  166. // dealProjectRepayment 处理项目-还本付息
  167. func dealProjectRepayment(hbfx map[string]interface{}, projectName string, projectID int) {
  168. project_repayment_exist := ProjectRepayment{}
  169. err := MysqlDB.Where(&ProjectRepayment{ProjectName: projectName, BondName: util.ObjToString(hbfx["bondName"])}).First(&project_repayment_exist).Error
  170. if err != nil && err != gorm.ErrRecordNotFound {
  171. // 处理查询错误
  172. log.Error("dealProjectRepayment;Error checking for existing project", zap.Error(err))
  173. }
  174. if project_repayment_exist.ID > 0 {
  175. project_repayment_exist.IssueTerm = util.IntAll(hbfx["issueTerm"])
  176. project_repayment_exist.PayInterestMethodName = util.ObjToString(hbfx["payInterestMethodName"])
  177. project_repayment_exist.ValueDate = util.ObjToString(hbfx["valueDate"])
  178. project_repayment_exist.InterestDate = util.ObjToString(hbfx["payInterestDate"])
  179. project_repayment_exist.LastInterestDate = util.ObjToString(hbfx["latelyPayInterestDate"])
  180. project_repayment_exist.ReminderRepayDays = util.IntAll(hbfx["days"])
  181. project_repayment_exist.MaturityDate = util.ObjToString(hbfx["expiryDate"])
  182. project_repayment_exist.DebtService = util.Float64All(hbfx["repayCapitalWithInterest"])
  183. project_repayment_exist.RedemptionMethod = util.ObjToString(hbfx["redemptionMethod"])
  184. project_repayment_exist.CumulativePayInterest = util.IntAll(hbfx["cumulativePayInterest"])
  185. project_repayment_exist.IsEarlyRepayPrincipal = util.ObjToString(hbfx["isEarlyRepayPrincipal"])
  186. project_repayment_exist.Remarks = util.ObjToString(hbfx["remarks"])
  187. // 使用 Save 来更新
  188. err = MysqlDB.Save(&project_repayment_exist).Error
  189. if err != nil {
  190. log.Info("dealProjectRepayment Save (Update) ", zap.Error(err))
  191. }
  192. } else {
  193. project_payment := ProjectRepayment{
  194. ProjectName: projectName,
  195. ProjectID: projectID,
  196. BondName: util.ObjToString(hbfx["bondName"]),
  197. IssueTerm: util.IntAll(hbfx["issueTerm"]),
  198. PayInterestMethodName: util.ObjToString(hbfx["payInterestMethodName"]),
  199. ValueDate: util.ObjToString(hbfx["valueDate"]),
  200. InterestDate: util.ObjToString(hbfx["payInterestDate"]),
  201. LastInterestDate: util.ObjToString(hbfx["latelyPayInterestDate"]),
  202. ReminderRepayDays: util.IntAll(hbfx["days"]), //提醒还款天数
  203. MaturityDate: util.ObjToString(hbfx["expiryDate"]), //到期日
  204. DebtService: util.Float64All(hbfx["repayCapitalWithInterest"]), //还本付息(万元)
  205. RedemptionMethod: util.ObjToString(hbfx["redemptionMethod"]),
  206. CumulativePayInterest: util.IntAll(hbfx["cumulativePayInterest"]),
  207. IsEarlyRepayPrincipal: util.ObjToString(hbfx["isEarlyRepayPrincipal"]),
  208. Remarks: util.ObjToString(hbfx["remarks"]),
  209. }
  210. err = MysqlDB.Create(&project_payment).Error
  211. if err != nil {
  212. log.Info("dealProjectRepayment;dealProject Create ", zap.Error(err), zap.String("project", projectName))
  213. }
  214. }
  215. }
  216. // dealProjectChange 处理项目变更
  217. func dealProjectChange(bg []interface{}, projectName string, projectId int) {
  218. for _, v := range bg {
  219. if bgda, ok := v.(map[string]interface{}); ok {
  220. project_change := ProjectChange{
  221. ProjectName: projectName,
  222. ProjectID: projectId,
  223. ChangeContent: util.ObjToString(bgda["changeContent"]),
  224. UpdateReason: util.ObjToString(bgda["updateReason"]),
  225. SubmitTime: util.ObjToString(bgda["submitTime"]),
  226. }
  227. err := MysqlDB.Create(&project_change).Error
  228. if err != nil {
  229. log.Info("dealProjectChange; Create err", zap.Error(err))
  230. }
  231. }
  232. }
  233. }
  234. // dealProjectIssueDetails 处理项目发行明细
  235. func dealProjectIssueDetails(fxmx []interface{}, projectName string, projectId int) {
  236. for _, v := range fxmx {
  237. if fx, ok := v.(map[string]interface{}); ok {
  238. project_bach_name := util.ObjToString(fx["projectBatchName"])
  239. issue_detail_exist := ProjectIssueDetails{}
  240. err := MysqlDB.Where(&ProjectIssueDetails{ProjectName: projectName, ProjectBachName: project_bach_name, BondName: util.ObjToString(fx["bondName"])}).First(&issue_detail_exist).Error
  241. if err != nil && err != gorm.ErrRecordNotFound {
  242. // 处理查询错误
  243. log.Error("dealProjectIssueDetails;Error checking for existing project", zap.Error(err))
  244. }
  245. // 存在
  246. if issue_detail_exist.ID > 0 {
  247. issue_detail_exist.FirstPublishDate = util.ObjToString(fx["firstPublishDate"])
  248. issue_detail_exist.BatchNum = util.IntAll(fx["batchNum"])
  249. issue_detail_exist.PresentIssueAmount = util.Float64All(fx["presentIssueAmount"])
  250. issue_detail_exist.IssueInterestRate = util.Float64All(fx["issueInterestRate"])
  251. issue_detail_exist.PresentAsSpecialAmount = util.Float64All(fx["presentAsSpecialAmount"])
  252. issue_detail_exist.TotalIssueAmount = util.Float64All(fx["totalIssueAmount"])
  253. issue_detail_exist.ReviseLog = util.ObjToString(fx["revise_log"])
  254. err = MysqlDB.Save(&issue_detail_exist).Error
  255. if err != nil {
  256. log.Info("dealProjectIssueDetails Save (Update) ", zap.Error(err))
  257. }
  258. } else {
  259. issue_detail := ProjectIssueDetails{
  260. ProjectName: projectName,
  261. ProjectID: projectId,
  262. ProjectBachName: project_bach_name,
  263. BondName: util.ObjToString(fx["bondName"]),
  264. FirstPublishDate: util.ObjToString(fx["firstPublishDate"]),
  265. BatchNum: util.IntAll(fx["batchNum"]),
  266. PresentIssueAmount: util.Float64All(fx["presentIssueAmount"]),
  267. IssueInterestRate: util.Float64All(fx["issueInterestRate"]),
  268. PresentAsSpecialAmount: util.Float64All(fx["presentAsSpecialAmount"]),
  269. TotalIssueAmount: util.Float64All(fx["totalIssueAmount"]),
  270. ReviseLog: util.ObjToString(fx["revise_log"]),
  271. }
  272. err := MysqlDB.Create(&issue_detail).Error
  273. if err != nil {
  274. log.Info("dealProjectIssueDetails; Create err", zap.Error(err))
  275. }
  276. }
  277. }
  278. }
  279. }
  280. // dealBondInfo 处理债券信息
  281. func dealBondInfo(bonds []interface{}, projectName string) {
  282. for _, v := range bonds {
  283. if bond, ok := v.(map[string]interface{}); ok {
  284. //1.基本信息
  285. if jbxx, ok := bond["jbxx"].(map[string]interface{}); ok {
  286. dealBondBase(jbxx, projectName)
  287. }
  288. //2.债券-修改记录;xgjl
  289. if xgjl, ok := bond["xgjl"].([]interface{}); ok && len(xgjl) > 0 {
  290. dealBondChange(xgjl)
  291. }
  292. ////3.相关小项目
  293. //if xgxxx, ok := bond["xgxxx"].([]interface{}); ok && len(xgxxx) > 0 {
  294. // dealRelationProject(xgxxx)
  295. //}
  296. }
  297. }
  298. }
  299. // dealBondBase 处理债券基本信息
  300. func dealBondBase(jbxx map[string]interface{}, projectName string) {
  301. bond_info_exist := BondInfo{}
  302. err := MysqlDB.Where(&BondInfo{BondName: util.ObjToString(jbxx["bondName"]), BondNo: util.ObjToString(jbxx["bondNo"])}).First(&bond_info_exist).Error
  303. if err != nil && err != gorm.ErrRecordNotFound {
  304. // 处理查询错误
  305. log.Error("dealBondBase;Error checking for existing project", zap.Error(err))
  306. }
  307. if bond_info_exist.ID > 0 {
  308. bond_info_exist.BondShortName = util.ObjToString(jbxx["bondName"])
  309. bond_info_exist.Area = util.ObjToString(jbxx["regionName"])
  310. bond_info_exist.BondNature = util.ObjToString(jbxx["bondType1Name"])
  311. bond_info_exist.BondType = util.ObjToString(jbxx["bondType2Name"])
  312. bond_info_exist.OfficialProjectType = util.ObjToString(jbxx["projectType1Name"])
  313. bond_info_exist.TotalAmount = util.Float64All(jbxx["totalAmount"])
  314. bond_info_exist.IssueDate = util.ObjToString(jbxx["issueDate"])
  315. bond_info_exist.IssuePlace = util.ObjToString(jbxx["issuePlaceName"])
  316. bond_info_exist.IssueTerm = util.IntAll(jbxx["issueTerm"])
  317. bond_info_exist.IssueInterestRate = util.ObjToString(jbxx["issueInterestRate"])
  318. bond_info_exist.IssuePhase = util.ObjToString(jbxx["issuePhase"])
  319. bond_info_exist.WayOfPayInterest = util.ObjToString(jbxx["payInterestMethodName"])
  320. bond_info_exist.NewBondAmount = util.Float64All(jbxx["newBondAmount"])
  321. bond_info_exist.CounterBondAmount = util.Float64All(jbxx["counterBondAmount"])
  322. bond_info_exist.RefinancingBondAmount = util.Float64All(jbxx["refinancingBondAmount"])
  323. bond_info_exist.RedemptionMethod = util.ObjToString(jbxx["redemptionMethod"])
  324. bond_info_exist.ValueDate = util.ObjToString(jbxx["valueDate"])
  325. bond_info_exist.ExpiryDate = util.ObjToString(jbxx["expiryDate"])
  326. bond_info_exist.PayInterestDate = util.ObjToString(jbxx["payInterestDate"])
  327. bond_info_exist.LatePayInterestDate = util.ObjToString(jbxx["latelyPayInterestDate"])
  328. bond_info_exist.IsEarlyRepayPrincipal = util.ObjToString(jbxx["isEarlyRepayPrincipal"])
  329. bond_info_exist.CumulativePayInterest = util.Float64All(jbxx["cumulativePayInterest"])
  330. bond_info_exist.IsCounterBond = util.ObjToString(jbxx["isCounterBond"])
  331. err = MysqlDB.Save(&bond_info_exist).Error
  332. if err != nil {
  333. log.Info("dealBondBase Save (Update) ", zap.Error(err))
  334. }
  335. } else {
  336. bond_info := BondInfo{
  337. BondName: util.ObjToString(jbxx["bondName"]),
  338. BondShortName: util.ObjToString(jbxx["bondShortName"]),
  339. BondNo: util.ObjToString(jbxx["bondNo"]),
  340. Area: util.ObjToString(jbxx["regionName"]),
  341. BondNature: util.ObjToString(jbxx["bondType1Name"]),
  342. BondType: util.ObjToString(jbxx["bondType2Name"]),
  343. OfficialProjectType: util.ObjToString(jbxx["projectType1Name"]),
  344. TotalAmount: util.Float64All(jbxx["totalAmount"]),
  345. IssueDate: util.ObjToString(jbxx["issueDate"]),
  346. IssuePlace: util.ObjToString(jbxx["issuePlaceName"]),
  347. IssueTerm: util.IntAll(jbxx["issueTerm"]),
  348. IssueInterestRate: util.ObjToString(jbxx["issueInterestRate"]),
  349. IssuePhase: util.ObjToString(jbxx["issuePhase"]),
  350. WayOfPayInterest: util.ObjToString(jbxx["payInterestMethodName"]),
  351. NewBondAmount: util.Float64All(jbxx["newBondAmount"]),
  352. CounterBondAmount: util.Float64All(jbxx["counterBondAmount"]),
  353. RefinancingBondAmount: util.Float64All(jbxx["refinancingBondAmount"]),
  354. RedemptionMethod: util.ObjToString(jbxx["redemptionMethod"]),
  355. ValueDate: util.ObjToString(jbxx["valueDate"]),
  356. ExpiryDate: util.ObjToString(jbxx["expiryDate"]),
  357. PayInterestDate: util.ObjToString(jbxx["payInterestDate"]),
  358. LatePayInterestDate: util.ObjToString(jbxx["latelyPayInterestDate"]),
  359. RemindPayDays: util.IntAll(jbxx["days"]),
  360. LastPayInterest: util.Float64All(jbxx["lastPayInterest"]),
  361. IsEarlyRepayPrincipal: util.ObjToString(jbxx["isEarlyRepayPrincipal"]),
  362. CumulativePayInterest: util.Float64All(jbxx["cumulativePayInterest"]),
  363. IsCounterBond: util.ObjToString(jbxx["isCounterBond"]),
  364. }
  365. //
  366. err = MysqlDB.Create(&bond_info).Error
  367. if err != nil {
  368. log.Info("dealBondBase;dealProject Create ", zap.Error(err), zap.String("project", projectName))
  369. }
  370. }
  371. }
  372. // dealBondChange 修改债券修改信息
  373. func dealBondChange(xgjl []interface{}) {
  374. }
  375. // dealProjectBondRelation 处理项目债券关联关系
  376. func dealProjectBondRelation() {
  377. //detailName := GF.Mongob.Detail //专项债详细表
  378. detailNames := strings.Split(GF.Mongob.Detail, ",")
  379. sess := Mgo.GetMgoConn()
  380. defer Mgo.DestoryMongoConn(sess)
  381. for _, detailName := range detailNames {
  382. query := sess.DB("py_theme").C(detailName).Find(nil).Select(nil).Iter()
  383. count := 0
  384. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  385. if count%100 == 0 {
  386. log.Info("current:", zap.Int("count", count))
  387. }
  388. if project, ok := tmp["project"].(map[string]interface{}); ok {
  389. //1.基础信息
  390. if jcxx, ok := project["jcxx"].(map[string]interface{}); ok {
  391. project_name := util.ObjToString(jcxx["projectName"])
  392. //fmt.Println(project_name)
  393. project_base_info_exist := ProjectBaseInfo{}
  394. err := MysqlDB.Where(&ProjectBaseInfo{ProjectName: project_name}).First(&project_base_info_exist).Error
  395. if err != nil && err != gorm.ErrRecordNotFound {
  396. // 处理查询错误
  397. log.Error("dealProjectBondRelation;Error checking for existing project", zap.Error(err))
  398. } else {
  399. if project_base_info_exist.ID > 0 {
  400. //1.更新项目的创建时间
  401. project_base_info_exist.CreateDate = util.ObjToString(jcxx["createTime"])
  402. project_base_info_exist.UpdateDate = util.ObjToString(jcxx["updateTime"])
  403. err = MysqlDB.Save(&project_base_info_exist).Error
  404. if err != nil {
  405. log.Info("dealProjectBondRelation Save (Update) ", zap.Error(err))
  406. }
  407. //2.更新项目和债券的对应关系
  408. if fxmx, ok := project["fxmx"].([]interface{}); ok {
  409. for _, v := range fxmx {
  410. if fx, ok := v.(map[string]interface{}); ok {
  411. bond_name := util.ObjToString(fx["bondName"])
  412. bond_info_exist := BondInfo{}
  413. err := MysqlDB.Where(&BondInfo{BondName: bond_name}).First(&bond_info_exist).Error
  414. if err != nil && err != gorm.ErrRecordNotFound {
  415. // 处理查询错误
  416. log.Error("dealProjectBondRelation;Error checking for existing project", zap.Error(err))
  417. } else {
  418. // 整理对应关系
  419. relation := ProjectBondRelation{
  420. ProjectID: project_base_info_exist.ID,
  421. BondID: bond_info_exist.ID,
  422. }
  423. result := MysqlDB.FirstOrCreate(&relation, ProjectBondRelation{
  424. ProjectID: relation.ProjectID,
  425. BondID: relation.BondID,
  426. })
  427. if result.Error != nil {
  428. // 处理错误
  429. log.Error("dealProjectBondRelation;Error checking for existing project", zap.Error(err), zap.String(project_name, bond_name))
  430. }
  431. }
  432. }
  433. }
  434. }
  435. }
  436. }
  437. }
  438. }
  439. }
  440. log.Info("dealProjectBondRelation", zap.Any("数据处理完毕", count))
  441. }
  442. }