target.go 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. package models
  2. import (
  3. sql2 "database/sql"
  4. "eta/eta_task/global"
  5. "eta/eta_task/utils"
  6. "gorm.io/gorm"
  7. "time"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. )
  10. type DataList struct {
  11. TradeCode string `gorm:"column:TRADE_CODE" description:"指标编码"`
  12. SecName string `gorm:"column:SEC_NAME" description:"指标名称"`
  13. Unit string `gorm:"column:UNIT" description:"单位"`
  14. Remark string `gorm:"column(REMARK)" description:"备注"`
  15. Frequency string `description:"频度"`
  16. ClassifyId int `description:"分类id"`
  17. ClassifyName string `description:"分类名称"`
  18. Dt string `gorm:"column:DT" description:"录入日期"`
  19. Close float64 `gorm:"column:CLOSE" description:"录入值"`
  20. ModifyTime string `description:"修改时间"`
  21. }
  22. type DataListResp struct {
  23. List []*DataList
  24. Paging *paging.PagingItem `description:"分页数据"`
  25. }
  26. func GetDataList(condition string, pars []interface{}, startSize, pageSize int) (items []*DataList, err error) {
  27. sql := `select a.TRADE_CODE,a.SEC_NAME,a.UNIT,a.frequency,a.classify_id,b.classify_name,c.DT,c.CLOSE,c.modify_time FROM edbdata AS c
  28. inner join edbinfo AS a ON a.TRADE_CODE=c.TRADE_CODE
  29. left join edbdata_classify AS b ON a.classify_id=b.classify_id
  30. where a.classify_id>0`
  31. if condition != "" {
  32. sql += condition
  33. }
  34. sql += ` order by c.DT desc limit ?,? `
  35. o := global.DbMap[utils.DbNameManualIndex]
  36. pars = append(pars, startSize, pageSize)
  37. err = o.Raw(sql, pars...).Find(&items).Error
  38. return
  39. }
  40. func GetDataListCount(condition string, pars []interface{}) (count int, err error) {
  41. sql := ` select count(1) as count FROM edbdata AS c
  42. inner join edbinfo AS a ON a.TRADE_CODE=c.TRADE_CODE
  43. left join edbdata_classify AS b ON a.classify_id=b.classify_id
  44. where a.classify_id>0 `
  45. if condition != "" {
  46. sql += condition
  47. }
  48. o := global.DbMap[utils.DbNameManualIndex]
  49. var countNull sql2.NullInt64
  50. err = o.Raw(sql, pars...).Scan(&countNull).Error
  51. if err != nil {
  52. return
  53. }
  54. if countNull.Valid {
  55. count = int(countNull.Int64)
  56. }
  57. return
  58. }
  59. type DataAddReq struct {
  60. TradeCode string `description:"指标唯一编码"`
  61. CreateDate string `description:"创建日期"`
  62. Close string `description:"录入值"`
  63. }
  64. // AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据
  65. func (m *Edbdata) AfterFind(db *gorm.DB) (err error) {
  66. m.Dt = utils.GormDateStrToDateStr(m.Dt)
  67. return
  68. }
  69. type EdbDataNextDateTime struct {
  70. TradeCode string `gorm:"column:TRADE_CODE;pk" description:"指标编码"`
  71. Dt string `gorm:"column:DT" description:"日期"`
  72. Close string `gorm:"column:CLOSE" description:"值"`
  73. ModifyTime time.Time `gorm:"column:modify_time" description:"修改时间"`
  74. NextDateTime string `description:"下期日期"`
  75. }
  76. // AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据
  77. func (m *EdbDataNextDateTime) AfterFind(db *gorm.DB) (err error) {
  78. m.Dt = utils.GormDateStrToDateStr(m.Dt)
  79. m.NextDateTime = utils.GormDateStrToDateStr(m.NextDateTime)
  80. return
  81. }
  82. func GetDataInfo(tradeCode, creteDate string) (item *Edbdata, err error) {
  83. sql := " SELECT * FROM edbdata WHERE TRADE_CODE=? AND DT=? "
  84. o := global.DbMap[utils.DbNameManualIndex]
  85. err = o.Raw(sql, tradeCode, creteDate).First(&item).Error
  86. return
  87. }
  88. func AddEdbdata(item *Edbdata) (lastId int64, err error) {
  89. o := global.DbMap[utils.DbNameManualIndex]
  90. err = o.Create(item).Error
  91. return
  92. }
  93. type DataEditReq struct {
  94. TradeCode string `description:"指标唯一编码"`
  95. CreateDate string `description:"创建日期"`
  96. Close interface{} `description:"录入值"`
  97. OldCreateDate string `description:"旧的录入日期"`
  98. }
  99. // BatchDataEditReq 批量修改指标
  100. type BatchDataEditReq struct {
  101. OldCreateDate string `description:"旧的录入日期"`
  102. CreateDate string `description:"新的录入日期"`
  103. List []DataEditReq `description:"需要修改的数据"`
  104. }
  105. // EditEdbdata 编辑数据
  106. func EditEdbdata(item *Edbdata) (err error) {
  107. o := global.DbMap[utils.DbNameManualIndex]
  108. sql := ` UPDATE edbdata SET CLOSE = ?,modify_time=NOW() WHERE TRADE_CODE = ? AND DT = ? `
  109. err = o.Exec(sql, item.Close, item.TradeCode, item.Dt).Error
  110. return
  111. }
  112. type EdbdataDeleteRecord struct {
  113. Id int `gorm:"column:id;primaryKey;autoIncrement"`
  114. TradeCode string `gorm:"column:TRADE_CODE" description:"指标编码"`
  115. Dt string `gorm:"column:DT" description:"日期"`
  116. Close string `gorm:"column:CLOSE" description:"值"`
  117. ModifyTime time.Time `gorm:"column:modify_time" description:"修改时间"`
  118. CreateTime time.Time
  119. SysUserId int
  120. }
  121. func AddEdbdataDeleteRecord(item *EdbdataDeleteRecord) (lastId int64, err error) {
  122. o := global.DbMap[utils.DbNameManualIndex]
  123. err = o.Create(item).Error
  124. if err != nil {
  125. return
  126. }
  127. lastId = int64(item.Id)
  128. return
  129. }
  130. // DeleteEdbData 根据指标code和日期删除数据
  131. func DeleteEdbData(tradeCode, dt string) (err error) {
  132. o := global.DbMap[utils.DbNameManualIndex]
  133. sql := ` DELETE FROM edbdata WHERE TRADE_CODE = ? AND DT = ? `
  134. err = o.Exec(sql, tradeCode, dt).Error
  135. return
  136. }
  137. // DeleteAllEdbData 根据指标code删除数据
  138. func DeleteAllEdbData(tradeCode string) (err error) {
  139. o := global.DbMap[utils.DbNameManualIndex]
  140. sql := ` DELETE FROM edbdata WHERE TRADE_CODE = ? `
  141. err = o.Exec(sql, tradeCode).Error
  142. return
  143. }
  144. type Edbinfo struct {
  145. TradeCode string `gorm:"column:TRADE_CODE;autoIncrement:false;primaryKey" description:"指标code"`
  146. SecName string `gorm:"column:SEC_NAME;" description:"指标名称"`
  147. Unit string `gorm:"column:UNIT;" description:"单位"`
  148. Remark string `gorm:"column:REMARK;" description:"备注"`
  149. Frequency string `gorm:"column:frequency" description:"频度"`
  150. ClassifyId int `gorm:"column:classify_id" description:"分类id"`
  151. ClassifyName string `gorm:"-" description:"分类名称"`
  152. CreateDate string `gorm:"column:create_date" description:"创建时间"`
  153. UserId int `gorm:"column:user_id" description:"录入用户id"`
  154. UserName string `gorm:"column:user_name" description:"录入用户名称"`
  155. NoticeTime string `gorm:"column:notice_time" description:"通知时间"`
  156. Mobile string `gorm:"column:mobile" description:"录入者手机号"`
  157. Sort int `gorm:"column:sort" description:"排序"`
  158. ModifyTime string `description:"最近一次更新时间"`
  159. IsJoinEdb int8 `description:"指标库是否已添加:0-否;1-是"`
  160. StartDate string `description:"数据开始日期"`
  161. EndDate string `description:"数据结束日期"`
  162. LatestValue float64 `description:"指标最新值"`
  163. }
  164. type EdbinfoItem struct {
  165. TradeCode string `gorm:"column:TRADE_CODE;primaryKey;autoIncrement:false;" orm:"column(TRADE_CODE);pk" description:"指标code"`
  166. SecName string `gorm:"column:SEC_NAME" orm:"column(SEC_NAME);" description:"指标名称"`
  167. Unit string `gorm:"column:UNIT" orm:"column(UNIT);" description:"单位"`
  168. Remark string `gorm:"column:REMARK" orm:"column(REMARK);" description:"备注"`
  169. Frequency string `description:"频度"`
  170. ClassifyId int `description:"分类id"`
  171. ClassifyName string `description:"分类名称"`
  172. CreateDate string `description:"创建时间"`
  173. UserId int `description:"录入用户id"`
  174. UserName string `description:"录入用户名称"`
  175. NoticeTime string `description:"通知时间"`
  176. Mobile string `description:"录入者手机号"`
  177. ModifyTime string `description:"最近一次更新时间"`
  178. StartDate string `description:"数据开始日期"`
  179. EndDate string `description:"数据结束日期"`
  180. LatestValue float64 `description:"指标最新值"`
  181. }
  182. // EdbParamsInfo 指标数据结构体
  183. type EdbParamsInfo struct {
  184. Unit string `description:"单位" gorm:"column:UNIT"`
  185. Frequency string `gorm:"column:frequency;" description:"单位"`
  186. }
  187. // GetEdbUnitList 获取指标单位
  188. func GetEdbUnitList() (items []*EdbParamsInfo, err error) {
  189. o := global.DbMap[utils.DbNameManualIndex]
  190. sql := `SELECT UNIT,Frequency from edbinfo group by UNIT`
  191. err = o.Raw(sql).Find(&items).Error
  192. return
  193. }
  194. type TargetListResp struct {
  195. List []*EdbinfoItem
  196. Paging *paging.PagingItem `description:"分页数据"`
  197. }
  198. type EdbinfoAddReq struct {
  199. SecName string `description:"指标名称"`
  200. Unit string `description:"单位"`
  201. Frequency string `description:"频度"`
  202. ClassifyId int `description:"分类id"`
  203. NoticeTime string `description:"通知时间"`
  204. }
  205. // GetMaxTradeCode 获取指标最大trade_code
  206. func GetMaxTradeCode() (max_trade_code string, err error) {
  207. sql := " SELECT MAX(TRADE_CODE) AS max_trade_code FROM edbinfo WHERE LEFT(TRADE_CODE,1)='W' "
  208. o := global.DbMap[utils.DbNameManualIndex]
  209. var codeNull sql2.NullString
  210. err = o.Raw(sql).Scan(&codeNull).Error
  211. max_trade_code = "W00"
  212. if codeNull.Valid {
  213. max_trade_code = codeNull.String
  214. }
  215. return
  216. }
  217. func GetEdbinfoBySecName(secName string) (item *Edbinfo, err error) {
  218. sql := `SELECT * FROM edbinfo WHERE SEC_NAME=? `
  219. o := global.DbMap[utils.DbNameManualIndex]
  220. err = o.Raw(sql, secName).First(&item).Error
  221. return
  222. }
  223. func GetEdbinfoByTradeCode(tradeCode string) (item *Edbinfo, err error) {
  224. sql := `SELECT * FROM edbinfo WHERE TRADE_CODE=? `
  225. o := global.DbMap[utils.DbNameManualIndex]
  226. err = o.Raw(sql, tradeCode).First(&item).Error
  227. return
  228. }
  229. func AddEdbinfo(tradeCode, secName, unit, remark, frequency, noticeTime string, classifyId int, userId int, userName string) (err error) {
  230. o := global.DbMap[utils.DbNameManualIndex]
  231. currTime := time.Now().Format(utils.FormatDateTime)
  232. edbInfo := &Edbinfo{
  233. TradeCode: tradeCode,
  234. SecName: secName,
  235. Unit: unit,
  236. Remark: remark,
  237. Frequency: frequency,
  238. ClassifyId: classifyId,
  239. CreateDate: currTime,
  240. UserId: userId,
  241. UserName: userName,
  242. NoticeTime: noticeTime,
  243. Mobile: "",
  244. ModifyTime: currTime,
  245. IsJoinEdb: 0,
  246. }
  247. err = o.Create(edbInfo).Error
  248. return
  249. }
  250. func AddEdbinfoUser(tradeCode, mobile string) (err error) {
  251. o := global.DbMap[utils.DbNameManualIndex]
  252. sql := `INSERT INTO edbinfo_user(TRADE_CODE, mobile) VALUES (?,?)`
  253. err = o.Exec(sql, tradeCode, mobile).Error
  254. return
  255. }
  256. type EdbinfoEditReq struct {
  257. TradeCode string `description:"指标code"`
  258. SecName string `description:"指标名称"`
  259. Unit string `description:"单位"`
  260. Frequency string `description:"频度"`
  261. ClassifyId int `description:"分类id"`
  262. NoticeTime string `description:"通知时间"`
  263. }
  264. type SearchTargetListResp struct {
  265. List []*Edbinfo
  266. }
  267. type EdbdataClassify struct {
  268. ClassifyId int
  269. ClassifyName string
  270. ParentId int
  271. EdbInfoTotal int
  272. TradeCode string
  273. UniqueCode string
  274. }
  275. type EdbdataClassifyList struct {
  276. ClassifyId int
  277. ClassifyName string
  278. ParentId int
  279. Child []*EdbdataClassify `gorm:"-"`
  280. TradeCode string
  281. UniqueCode string
  282. }
  283. func GetEdbdataClassify(userId int64) (items []*EdbdataClassifyList, err error) {
  284. var newItems []*EdbdataClassifyList
  285. o := global.DbMap[utils.DbNameManualIndex]
  286. sql := ` SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE parent_id=0 `
  287. err = o.Raw(sql).Find(&newItems).Error
  288. if err != nil {
  289. return
  290. }
  291. classifyLen := len(newItems)
  292. // 获取子集分类
  293. var allChildItems []*EdbdataClassify
  294. if userId > 0 {
  295. userClassifyList, _ := GetManualUserClassify(int(userId))
  296. var userIdArr []int
  297. for _, v := range userClassifyList {
  298. userIdArr = append(userIdArr, v.ClassifyId)
  299. }
  300. num := len(userClassifyList)
  301. if num > 0 {
  302. childSql := "SELECT a.classify_id,a.classify_name,a.parent_id FROM edbdata_classify AS a WHERE a.is_show=1 and a.classify_id IN(" + utils.GetOrmInReplace(num) + ") ORDER BY a.create_time ASC "
  303. err = o.Raw(childSql, userIdArr).Find(&allChildItems).Error
  304. }
  305. } else {
  306. childSql := "SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE is_show=1 ORDER BY create_time ASC "
  307. err = o.Raw(childSql).Find(&allChildItems).Error
  308. }
  309. if err != nil {
  310. return
  311. }
  312. for i := 0; i < classifyLen; i++ {
  313. var childItems []*EdbdataClassify
  314. parentId := newItems[i].ClassifyId
  315. for _, v := range allChildItems {
  316. if v.ParentId == parentId {
  317. childItems = append(childItems, v)
  318. }
  319. }
  320. newItems[i].Child = childItems
  321. }
  322. for _, v := range newItems {
  323. childLen := len(v.Child)
  324. if childLen > 0 {
  325. items = append(items, v)
  326. }
  327. }
  328. return
  329. }
  330. type ManualUserClassify struct {
  331. ManualUserClassifyId int `gorm:"column:manual_user_classify_id;primaryKey"`
  332. AdminId int
  333. ClassifyId int
  334. CreateTime time.Time
  335. }
  336. func GetManualUserClassify(sysUserId int) (list []*ManualUserClassify, err error) {
  337. o := global.DbMap[utils.DbNameIndex]
  338. sql := `SELECT * FROM manual_user_classify WHERE admin_id=? `
  339. err = o.Raw(sql, sysUserId).Find(&list).Error
  340. return
  341. }
  342. type EdbdataClassifyResp struct {
  343. List []*EdbdataClassifyList
  344. }
  345. func GetTargetBySecName(secName string) (item *Edbinfo, err error) {
  346. sql := `SELECT * FROM edbinfo WHERE SEC_NAME=? `
  347. o := global.DbMap[utils.DbNameManualIndex]
  348. err = o.Raw(sql, secName).First(&item).Error
  349. return
  350. }
  351. // 更新指标数据信息
  352. func (edbinfo *Edbinfo) Update(cols []string) (err error) {
  353. o := global.DbMap[utils.DbNameManualIndex]
  354. err = o.Select(cols).Updates(edbinfo).Error
  355. return
  356. }
  357. //func ModifyTargetClassifyId(tradeCode string, classifyId int) (err error) {
  358. // sql := `UPDATE edbinfo SET classify_id=? WHERE TRADE_CODE=? `
  359. // o := global.DbMap[utils.DbNameManualIndex]
  360. // err = o.Exec(sql, classifyId, tradeCode).Error
  361. // return
  362. //}
  363. //func GetTargetsDataCount(tradeCode, dt string) (count int, err error) {
  364. // sql := `SELECT COUNT(1) AS count FROM edbdata WHERE TRADE_CODE=? AND DT=? `
  365. // o := global.DbMap[utils.DbNameManualIndex]
  366. // err = o.Raw(sql, tradeCode, dt).QueryRow(&count)
  367. // return
  368. //}
  369. // GetTargetsDataList 根据code获取指标数据列表
  370. func GetTargetsDataList(tradeCode string) (items []*Edbdata, err error) {
  371. o := global.DbMap[utils.DbNameManualIndex]
  372. sql := `SELECT * FROM edbdata WHERE TRADE_CODE=? ORDER BY DT ASC `
  373. err = o.Raw(sql, tradeCode).Find(&items).Error
  374. return
  375. }
  376. //func GetTargetsData(tradeCode, dt string) (item *Edbdata, err error) {
  377. // sql := `SELECT * FROM edbdata WHERE TRADE_CODE=? AND DT=? `
  378. // o := global.DbMap[utils.DbNameManualIndex]
  379. // err = o.Raw(sql, tradeCode, dt).First(&item).Error
  380. // return
  381. //}
  382. func ModifyTargetsDataByImport(tradeCode, dt, close string) (err error) {
  383. sql := `UPDATE edbdata SET CLOSE=?,modify_time=NOW() WHERE TRADE_CODE=? AND DT=? `
  384. o := global.DbMap[utils.DbNameManualIndex]
  385. err = o.Exec(sql, close, tradeCode, dt).Error
  386. return
  387. }
  388. func AddTargetsDataByImport(tradeCode, dt, close string) (err error) {
  389. sql := `INSERT INTO edbdata(TRADE_CODE, DT,CLOSE, modify_time)VALUES(?,?,?,NOW()) `
  390. o := global.DbMap[utils.DbNameManualIndex]
  391. err = o.Exec(sql, tradeCode, dt, close).Error
  392. return
  393. }
  394. type EdbdataImportResp struct {
  395. Status int
  396. Msg string
  397. SuccessCount int
  398. FailCount int
  399. IndexCount int `description:"指标数"`
  400. }
  401. type DataListForExport struct {
  402. TradeCode string `gorm:"column:TRADE_CODE" description:"指标code"`
  403. SecName string `gorm:"column:SEC_NAME" description:"指标名称"`
  404. Unit string `gorm:"column:UNIT" description:"单位"`
  405. Frequency string `description:"频度"`
  406. ClassifyId int `description:"分类id"`
  407. NoticeTime string `description:"通知时间"`
  408. ClassifyName string
  409. Dt string `gorm:"column:DT" description:"日期"`
  410. Close float64 `gorm:"column:CLOSE" description:"值"`
  411. }
  412. type DataDeleteReq struct {
  413. TradeCode string `description:"指标唯一编码"`
  414. CreateDate string `description:"数据录入日期"`
  415. }
  416. func DataDelete(tradeCode, createDate, close string, modifyTime time.Time, sysUserId int) (err error) {
  417. to := global.DbMap[utils.DbNameManualIndex].Begin()
  418. defer func() {
  419. if err != nil {
  420. _ = to.Rollback()
  421. } else {
  422. _ = to.Commit()
  423. }
  424. }()
  425. recordSql := ` INSERT INTO edbdata_delete_record(TRADE_CODE,DT,CLOSE,modify_time,create_time,sys_user_id)
  426. VALUES(?,?,?,?,?,?)`
  427. err = to.Exec(recordSql, tradeCode, createDate, close, modifyTime, time.Now(), sysUserId).Error
  428. sql := ` DELETE FROM edbdata WHERE TRADE_CODE = ? AND DT = ? `
  429. err = to.Exec(sql, tradeCode, createDate).Error
  430. return
  431. }
  432. //func GetTargetInfoCount(tradeCode string) (count int, err error) {
  433. // sql := ` SELECT COUNT(1) AS count FROM edbdata AS c
  434. // INNER JOIN edbinfo AS a ON a.TRADE_CODE=c.TRADE_CODE
  435. // WHERE a.TRADE_CODE=? `
  436. // o := global.DbMap[utils.DbNameManualIndex]
  437. // err = o.Raw(sql, tradeCode).QueryRow(&count)
  438. // return
  439. //}
  440. type TargetDeleteReq struct {
  441. TradeCode string `description:"指标唯一编码"`
  442. }
  443. func TargetDelete(tradeCode string) (err error) {
  444. to := global.DbMap[utils.DbNameManualIndex].Begin()
  445. defer func() {
  446. if err != nil {
  447. _ = to.Rollback()
  448. } else {
  449. _ = to.Commit()
  450. }
  451. }()
  452. sql := " DELETE FROM edbinfo WHERE TRADE_CODE = ? "
  453. err = to.Exec(sql, tradeCode).Error
  454. sql = " DELETE FROM edbdata WHERE TRADE_CODE = ? "
  455. err = to.Exec(sql, tradeCode).Error
  456. return
  457. }
  458. type Researcher struct {
  459. AdminId int `description:"系统用户id"`
  460. AdminName string `description:"系统用户名称"`
  461. RealName string `description:"系统用户姓名"`
  462. Role string `description:"系统用户角色"`
  463. Mobile string `description:"手机号"`
  464. TargetCount int `description:"指标数量"`
  465. }
  466. type ResearcherListResp struct {
  467. List []*Researcher
  468. }
  469. func GetResearcherEntry() (items []*Researcher, err error) {
  470. sql := ` SELECT admin_id,admin_name,real_name,mobile,0 as target_count FROM admin WHERE role_type=1 `
  471. o := global.DEFAULT_DB
  472. sql = utils.ReplaceDriverKeywords("", sql)
  473. err = o.Raw(sql).Find(&items).Error
  474. researchLen := len(items)
  475. edbO := global.DbMap[utils.DbNameManualIndex]
  476. for i := 0; i < researchLen; i++ {
  477. var count int
  478. mobile := items[i].Mobile
  479. sqlCount := ` SELECT COUNT(DISTINCT a.TRADE_CODE) AS count FROM edbinfo_user AS a
  480. INNER JOIN edbinfo AS b ON a.TRADE_CODE=b.TRADE_CODE
  481. WHERE a.mobile=? AND b.classify_id>0 `
  482. var countNull sql2.NullInt64
  483. err = edbO.Raw(sqlCount, mobile).Scan(&countNull).Error
  484. if err != nil {
  485. return
  486. }
  487. if countNull.Valid {
  488. count = int(countNull.Int64)
  489. }
  490. items[i].TargetCount = count
  491. }
  492. return
  493. }
  494. func GetResearcherEntryByMobile(mobile string) (items []*Researcher, err error) {
  495. sql := ` SELECT admin_id,admin_name,real_name,mobile,0 as target_count FROM admin WHERE role_type=1 `
  496. if mobile != "" {
  497. sql += ` AND mobile IN(` + mobile + `)`
  498. }
  499. sql = utils.ReplaceDriverKeywords("", sql)
  500. o := global.DEFAULT_DB
  501. err = o.Raw(sql).Find(&items).Error
  502. researchLen := len(items)
  503. edbO := global.DbMap[utils.DbNameManualIndex]
  504. for i := 0; i < researchLen; i++ {
  505. var count int
  506. mobile := items[i].Mobile
  507. sqlCount := ` SELECT COUNT(DISTINCT a.TRADE_CODE) AS count FROM edbinfo_user AS a
  508. INNER JOIN edbinfo AS b ON a.TRADE_CODE=b.TRADE_CODE
  509. WHERE a.mobile=? AND b.classify_id>0 `
  510. var countNull sql2.NullInt64
  511. err = edbO.Raw(sqlCount, mobile).Scan(&countNull).Error
  512. if err != nil {
  513. return
  514. }
  515. if countNull.Valid {
  516. count = int(countNull.Int64)
  517. }
  518. items[i].TargetCount = count
  519. }
  520. return
  521. }
  522. type EdbinfoItems struct {
  523. TradeCode string `gorm:"column:TRADE_CODE;autoIncrement:false;primaryKey" description:"指标编码"`
  524. SecName string `gorm:"column:SEC_NAME;" description:"指标名称"`
  525. Unit string `gorm:"column:UNIT;" description:"单位"`
  526. Remark string `gorm:"column:REMARK;" description:"备注"`
  527. Frequency string `description:"频度"`
  528. ClassifyId int `description:"分类id"`
  529. ClassifyName string `description:"分类名称"`
  530. CreateDate string `description:"创建时间"`
  531. UserId int `description:"录入用户id"`
  532. NoticeTime string `description:"通知时间"`
  533. Mobile string `description:"录入者手机号"`
  534. ModifyDate string `description:"待更新日期"`
  535. Status string `description:"状态:未完成/完成"`
  536. }
  537. type TargetItemsResp struct {
  538. List SortEdbInfo
  539. }
  540. type SortEdbInfo []EdbinfoItems
  541. // 获取此 slice 的长度
  542. func (p SortEdbInfo) Len() int { return len(p) }
  543. // 根据元素的状态降序排序
  544. func (p SortEdbInfo) Less(i, j int) bool {
  545. return p[i].Status > p[j].Status
  546. }
  547. // 交换数据
  548. func (p SortEdbInfo) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  549. // 嵌套结构体 将继承 SortEdbInfo 的所有属性和方法
  550. // 所以相当于SortByName 也实现了 Len() 和 Swap() 方法
  551. type SortByStatus struct{ SortEdbInfo }
  552. // 根据元素的姓名长度降序排序 (此处按照自己的业务逻辑写)
  553. func (p SortByStatus) Less(i, j int) bool {
  554. return len(p.SortEdbInfo[i].Status) > len(p.SortEdbInfo[j].Status)
  555. }
  556. type SortByModifyDate struct{ SortEdbInfo }
  557. // 根据元素的年龄降序排序 (此处按照自己的业务逻辑写)
  558. func (p SortByModifyDate) Less(i, j int) bool {
  559. return p.SortEdbInfo[i].ModifyDate > p.SortEdbInfo[j].ModifyDate
  560. }
  561. type DataCheckResp struct {
  562. Status int `description:"状态:1:该日期已存在数据,是否确认修改?,0:数据不存在"`
  563. Close string `description:"值"`
  564. }
  565. type TargetCheckResp struct {
  566. Status int `description:"状态:1:该指标有关联数据,请先删除数据,0:指标不存在关联数据,可直接删除"`
  567. }
  568. type EdbdataExportList struct {
  569. TradeCode string `gorm:"column:TRADE_CODE;" description:"指标code"`
  570. SecName string `gorm:"column:SEC_NAME;" description:"指标名称"`
  571. Unit string `gorm:"column:UNIT;" description:"单位"`
  572. Remark string `gorm:"column:REMARK;" description:"备注"`
  573. Frequency string `description:"频度"`
  574. ClassifyId int `description:"分类id"`
  575. ClassifyName string `description:"分类名称"`
  576. CreateDate string `description:"创建时间"`
  577. Dt string `gorm:"column:DT;" description:"最新一次录入时间"`
  578. }
  579. // AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据
  580. func (m *EdbdataExportList) AfterFind(db *gorm.DB) (err error) {
  581. m.Dt = utils.GormDateStrToDateStr(m.Dt)
  582. return
  583. }
  584. func GetEdbdataSecName(condition string, pars []interface{}) (items []*EdbdataExportList, err error) {
  585. //sql := `SELECT a.TRADE_CODE,a.SEC_NAME,a.frequency,a.UNIT,MAX(c.DT) AS Dt
  586. // FROM edbdata AS c
  587. // INNER JOIN edbinfo AS a ON a.TRADE_CODE=c.TRADE_CODE
  588. // INNER JOIN edbinfo_user AS d ON a.TRADE_CODE=d.TRADE_CODE
  589. // LEFT JOIN edbdata_classify AS b ON a.classify_id=b.classify_id
  590. // WHERE a.classify_id>0`
  591. sql := `SELECT a.TRADE_CODE,a.SEC_NAME,a.frequency,a.UNIT,MAX(c.DT) AS Dt,b.classify_name
  592. FROM edbdata AS c
  593. INNER JOIN edbinfo AS a ON a.TRADE_CODE=c.TRADE_CODE
  594. LEFT JOIN edbdata_classify AS b ON a.classify_id=b.classify_id
  595. WHERE a.classify_id>0`
  596. if condition != "" {
  597. sql += condition
  598. }
  599. sql += " GROUP BY a.TRADE_CODE ORDER BY a.TRADE_CODE ASC "
  600. o := global.DbMap[utils.DbNameManualIndex]
  601. err = o.Raw(sql, pars...).Find(&items).Error
  602. return
  603. }
  604. func GetEdbDataFrequency(classifyId int) (items []*string, err error) {
  605. sql := `SELECT DISTINCT frequency FROM edbinfo where classify_id=? AND frequency IS NOT NULL ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年度','年度') `
  606. o := global.DbMap[utils.DbNameManualIndex]
  607. err = o.Raw(sql, classifyId).Find(&items).Error
  608. return
  609. }
  610. // GetEdbDataFrequencyByClassifyIdList
  611. // @Description: 根据分类列表获取所有的频度
  612. // @author: Roc
  613. // @datetime 2024-08-15 17:51:13
  614. // @param classifyIdList []int
  615. // @return items []*string
  616. // @return err error
  617. func GetEdbDataFrequencyByClassifyIdList(classifyIdList []int) (items []string, err error) {
  618. num := len(classifyIdList)
  619. if num <= 0 {
  620. return
  621. }
  622. sql := `SELECT DISTINCT frequency FROM edbinfo where classify_id in (` + utils.GetOrmInReplace(num) + `) AND frequency IS NOT NULL ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年度','年度') `
  623. o := global.DbMap[utils.DbNameManualIndex]
  624. err = o.Raw(sql, classifyIdList).Find(&items).Error
  625. return
  626. }
  627. func GetEdbDataFrequencyByKeyord(keyword string) (items []string, err error) {
  628. sql := `SELECT DISTINCT frequency FROM edbinfo where SEC_NAME=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年度','年度') `
  629. o := global.DbMap[utils.DbNameManualIndex]
  630. err = o.Raw(sql, keyword).Find(&items).Error
  631. return
  632. }
  633. type EdbdataList struct {
  634. Dt string `gorm:"column:DT;" description:"录入时间"`
  635. }
  636. // AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据
  637. func (m *EdbdataList) AfterFind(db *gorm.DB) (err error) {
  638. m.Dt = utils.GormDateStrToDateStr(m.Dt)
  639. return
  640. }
  641. func GetEdbdataList(tradeCode string) (items []*EdbdataList, err error) {
  642. sql := ` SELECT DT FROM edbdata WHERE TRADE_CODE IN(` + tradeCode + `) GROUP BY DT ORDER BY DT DESC `
  643. o := global.DbMap[utils.DbNameManualIndex]
  644. err = o.Raw(sql).Find(&items).Error
  645. return
  646. }
  647. type EdbdataItem struct {
  648. TradeCode string `gorm:"column:TRADE_CODE;" description:"指标code"`
  649. Dt string `gorm:"column:DT;" description:"最新一次录入时间"`
  650. Close float64 `gorm:"column:CLOSE;" description:"值"`
  651. }
  652. // AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据
  653. func (m *EdbdataItem) AfterFind(db *gorm.DB) (err error) {
  654. m.Dt = utils.GormDateStrToDateStr(m.Dt)
  655. return
  656. }
  657. func GetEdbdataValueByTradeCode(tradeCode, dt string) (item *EdbdataItem, err error) {
  658. sql := ` SELECT TRADE_CODE,DT,CLOSE FROM edbdata WHERE TRADE_CODE=? AND DT=? `
  659. o := global.DbMap[utils.DbNameManualIndex]
  660. err = o.Raw(sql, tradeCode, dt).First(&item).Error
  661. return
  662. }
  663. func GetEdbdataAllByTradeCode(tradeCode string) (items []*EdbdataItem, err error) {
  664. sql := ` SELECT * FROM edbdata WHERE TRADE_CODE=? `
  665. o := global.DbMap[utils.DbNameManualIndex]
  666. err = o.Raw(sql, tradeCode).Find(&items).Error
  667. return
  668. }
  669. func GetEdbdataClassifyByParentId(parentId int) (items []*EdbdataClassify, err error) {
  670. sql := ` SELECT * FROM edbdata_classify WHERE parent_id=? `
  671. o := global.DbMap[utils.DbNameManualIndex]
  672. err = o.Raw(sql, parentId).Find(&items).Error
  673. return
  674. }
  675. type LzPriceClassify struct {
  676. ProductName string
  677. }
  678. func GetLzPriceClassify() (items []*LzPriceClassify, err error) {
  679. sql := ` SELECT product_name FROM longzhongpriceinfo GROUP BY product_name ORDER BY product_name DESC `
  680. o := global.DbMap[utils.DbNameManualIndex]
  681. err = o.Raw(sql).Find(&items).Error
  682. return
  683. }
  684. type Longzhongpriceinfo struct {
  685. LongzhongpriceinfoId int `gorm:"column:longzhongpriceinfo_id;primaryKey"`
  686. Standard string
  687. ModelName string
  688. Unit string
  689. AreaName string
  690. PriceType string
  691. Memo string
  692. PriceId string
  693. ProductName string
  694. InfoType string
  695. InfoTypeRemark string
  696. MarketName string
  697. ManufactureName string
  698. }
  699. func GetLongzhongpriceinfoByClassifyName(productName string) (items []*Longzhongpriceinfo, err error) {
  700. sql := `SELECT * FROM longzhongpriceinfo WHERE product_name=? ORDER BY longzhongpriceinfo_id ASC `
  701. o := global.DbMap[utils.DbNameManualIndex]
  702. err = o.Raw(sql, productName).Find(&items).Error
  703. return
  704. }
  705. func GetLongzhongPriceDataMaxCount(productName string) (count int, err error) {
  706. o := global.DbMap[utils.DbNameManualIndex]
  707. sql := `SELECT MAX(t.num) AS count FROM (
  708. SELECT COUNT(1) AS num FROM longzhongpriceinfo AS a
  709. INNER JOIN longzhongpricedata AS b ON a.longzhongpriceinfo_id=b.longzhongpriceinfo_id
  710. WHERE a.product_name=?
  711. GROUP BY a.product_name
  712. )AS t `
  713. //err = o.Raw(sql, productName).QueryRow(&count)
  714. var countNull sql2.NullInt64
  715. err = o.Raw(sql, productName).Scan(&countNull).Error
  716. if err != nil {
  717. return
  718. }
  719. if countNull.Valid {
  720. count = int(countNull.Int64)
  721. }
  722. return
  723. }
  724. type LongzhongpricedataItems struct {
  725. LongzhongpricedataId int `gorm:"column:longzhongpricedata_id;primaryKey"`
  726. LongzhongpriceinfoId int
  727. PriceDate string
  728. Memo string
  729. Price float64
  730. CnyPrice float64
  731. ZsyPrice float64
  732. ZshPrice float64
  733. LowPrice float64
  734. HighPrice float64
  735. RisePrice float64
  736. TonPrice float64
  737. PriceType string
  738. UpdateDate string
  739. }
  740. func GetLongzhongPriceDataById(lzPriceInfoId int) (items []*LongzhongpricedataItems, err error) {
  741. o := global.DbMap[utils.DbNameManualIndex]
  742. sql := ` SELECT DISTINCT a.longzhongpriceinfo_id,a.price_date,a.memo,a.price,a.cny_price,a.zsy_price,a.zsh_price,a.low_price,a.high_price,a.rise_price,a.ton_price,a.price_type,a.update_date
  743. FROM longzhongpricedata AS a
  744. WHERE longzhongpriceinfo_id=? ORDER BY price_date DESC `
  745. err = o.Raw(sql, lzPriceInfoId).Find(&items).Error
  746. return
  747. }
  748. func GetLzSurveyClassify() (items []*LzPriceClassify, err error) {
  749. sql := ` SELECT breed_name AS product_name FROM longzhong_survey_product GROUP BY breed_name ORDER BY breed_name DESC `
  750. o := global.DbMap[utils.DbNameManualIndex]
  751. err = o.Raw(sql).Find(&items).Error
  752. return
  753. }
  754. type LongzhongSurveyProduct struct {
  755. SurveyProductId int `gorm:"column:survey_product_id;primaryKey"`
  756. ProjectQuotaId int64
  757. BreedId string
  758. BreedName string
  759. QuotaId string
  760. QuotaName string
  761. UnitId string
  762. UnitName string
  763. SampleType int64
  764. SampleId string
  765. SampleName string
  766. DeviceId string
  767. Device string
  768. ProductCraftId string
  769. ProductCraft string
  770. ProductLine string
  771. InputMode int64
  772. Frequency int64
  773. InputValue string
  774. TaskShouldFinishTime int
  775. CustomId string
  776. CustomType int64
  777. Custom string
  778. QuotaSampleId int64
  779. StartDate string
  780. EndDate string
  781. LzCode string
  782. }
  783. func (obj *LongzhongSurveyProduct) AfterFind(tx *gorm.DB) (err error) {
  784. obj.StartDate = utils.GormDateStrToDateStr(obj.StartDate)
  785. obj.EndDate = utils.GormDateStrToDateStr(obj.EndDate)
  786. return
  787. }
  788. func GetLongzhongSurveyProductByClassifyName(productName string) (items []*LongzhongSurveyProduct, err error) {
  789. sql := `SELECT * FROM longzhong_survey_product WHERE breed_name=? ORDER BY survey_product_id ASC `
  790. o := global.DbMap[utils.DbNameManualIndex]
  791. err = o.Raw(sql, productName).Find(&items).Error
  792. return
  793. }
  794. func GetLzSurveyProductByNameAndFrequency(productName string, frequency int) (items []*LongzhongSurveyProduct, err error) {
  795. sql := `SELECT * FROM longzhong_survey_product WHERE breed_name=? AND frequency=? ORDER BY survey_product_id ASC `
  796. o := global.DbMap[utils.DbNameManualIndex]
  797. err = o.Raw(sql, productName, frequency).Find(&items).Error
  798. return
  799. }
  800. func GetExportLzSurveyProductByBreedIds(breedIds []string) (items []*LongzhongSurveyProduct, err error) {
  801. if len(breedIds) == 0 {
  802. return
  803. }
  804. field := ` survey_product_id, breed_id, breed_name, sample_name, custom, quota_name, lz_code, frequency, unit_name, end_date, input_value `
  805. sql := `SELECT ` + field + ` FROM longzhong_survey_product WHERE breed_id IN (` + utils.GetOrmInReplace(len(breedIds)) + `) ORDER BY breed_id ASC, frequency ASC, survey_product_id ASC `
  806. o := global.DbMap[utils.DbNameManualIndex]
  807. err = o.Raw(sql, breedIds).Find(&items).Error
  808. return
  809. }
  810. func GetLzFrequency(productName string) (items []*int, err error) {
  811. sql := `SELECT DISTINCT frequency FROM longzhong_survey_product WHERE breed_name=? ORDER BY frequency`
  812. o := global.DbMap[utils.DbNameManualIndex]
  813. err = o.Raw(sql, productName).Find(&items).Error
  814. return
  815. }
  816. // EdbInfoItem
  817. type EdbInfoItem struct {
  818. TradeCode string `gorm:"column:TRADE_CODE;autoIncrement:false;primaryKey" description:"指标编码"`
  819. SecName string `gorm:"column:SEC_NAME;" description:"指标名称"`
  820. Unit string `gorm:"column:UNIT;" description:"单位"`
  821. Remark string `gorm:"column:REMARK;" description:"备注"`
  822. Frequency string `description:"频度"`
  823. ClassifyId int `description:"分类id"`
  824. ClassifyName string `description:"分类名称"`
  825. CreateDate string `description:"创建时间"`
  826. UserId int `description:"录入用户id"`
  827. UserName string `description:"录入用户名称"`
  828. NoticeTime string `description:"通知时间"`
  829. Mobile string `description:"录入者手机号"`
  830. ModifyDate string `description:"待更新日期"`
  831. ModifyTime string `description:"最近一次更新时间"`
  832. Status string `description:"状态:未完成/完成"`
  833. IsJoinEdb int8 `description:"指标库是否已添加:0-否;1-是"`
  834. StartDate string `description:"数据开始日期"`
  835. EndDate string `description:"数据结束日期"`
  836. LatestValue float64 `description:"指标最新值"`
  837. DataList []*Edbdata `gorm:"-" description:"指标数据列表"`
  838. NextDataTimeList []*EdbDataNextDateTime `gorm:"-" description:"下期数据时间列表"`
  839. }
  840. // AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据
  841. func (m *EdbInfoItem) AfterFind(db *gorm.DB) (err error) {
  842. m.CreateDate = utils.GormDateStrToDateTimeStr(m.CreateDate)
  843. m.ModifyTime = utils.GormDateStrToDateTimeStr(m.ModifyTime)
  844. m.StartDate = utils.GormDateStrToDateStr(m.StartDate)
  845. m.EndDate = utils.GormDateStrToDateStr(m.EndDate)
  846. return
  847. }
  848. // ConvDate
  849. func (m *EdbInfoItem) ConvDate() {
  850. m.CreateDate = utils.GormDateStrToDateTimeStr(m.CreateDate)
  851. m.ModifyTime = utils.GormDateStrToDateTimeStr(m.ModifyTime)
  852. m.StartDate = utils.GormDateStrToDateStr(m.StartDate)
  853. m.EndDate = utils.GormDateStrToDateStr(m.EndDate)
  854. return
  855. }
  856. type lzSurveyData struct {
  857. DataTime string `gorm:"column:data_time" description:"日期"`
  858. InputValue string `gorm:"column:input_value" description:"值"`
  859. }
  860. // GetLzItemListByCode 根据code查询隆众数据列表
  861. //func GetLzItemListByCode(lzCode string) (items []*lzSurveyData, err error) {
  862. // o := global.DbMap[utils.DbNameManualIndex]
  863. // sql := "SELECT * FROM longzhong_survey_data WHERE survey_product_id=? GROUP BY data_time DESC"
  864. // err = o.Raw(sql, lzCode).Find(&items).Error
  865. // return
  866. //}
  867. // GetEdbDataListByCodes 通过指标ID获取所有数据
  868. func GetEdbDataListByCodes(tradeCode string) (items []*Edbdata, err error) {
  869. sql := ` SELECT TRADE_CODE,DT,round(CLOSE,4) CLOSE,modify_time FROM edbdata WHERE TRADE_CODE IN(` + tradeCode + `) GROUP BY TRADE_CODE,DT ORDER BY DT DESC `
  870. o := global.DbMap[utils.DbNameManualIndex]
  871. err = o.Raw(sql).Find(&items).Error
  872. return
  873. }
  874. // TargetItemListResp 指标数据结构体
  875. type TargetItemListResp struct {
  876. List []*EdbInfoItem
  877. FrequencyList []string
  878. }
  879. // BatchDataDeleteReq 批量删除某日的指标数据请求结构体
  880. type BatchDataDeleteReq struct {
  881. CreateDate string `description:"创建日期"`
  882. TradeCodeList []string `description:"指标唯一编码列表"`
  883. }
  884. // BatchDeleteEdbDataByDate 批量删除某日的指标数据
  885. func BatchDeleteEdbDataByDate(tradeCodes, dt string, opUserId int) (err error) {
  886. o := global.DbMap[utils.DbNameManualIndex]
  887. var list []*Edbdata
  888. sql := ` select * FROM edbdata WHERE TRADE_CODE in (` + tradeCodes + `) AND DT = ? `
  889. err = o.Raw(sql, dt).Find(&list).Error
  890. if err != nil {
  891. return
  892. }
  893. deleteRecordList := make([]*EdbdataDeleteRecord, 0)
  894. for _, edbDataInfo := range list {
  895. deleteRecord := &EdbdataDeleteRecord{
  896. TradeCode: edbDataInfo.TradeCode,
  897. Dt: edbDataInfo.Dt,
  898. Close: edbDataInfo.Close,
  899. ModifyTime: time.Now(),
  900. CreateTime: time.Now(),
  901. SysUserId: opUserId,
  902. }
  903. deleteRecordList = append(deleteRecordList, deleteRecord)
  904. }
  905. if len(deleteRecordList) > 0 {
  906. tmpErr := o.CreateInBatches(deleteRecordList, utils.MultiAddNum).Error
  907. if tmpErr != nil {
  908. err = tmpErr
  909. return
  910. }
  911. }
  912. sql = ` DELETE FROM edbdata WHERE TRADE_CODE in (` + tradeCodes + `) AND DT = ? `
  913. err = o.Exec(sql, dt).Error
  914. return
  915. }
  916. // BatchDeleteEdbData 批量删除指标数据
  917. func BatchDeleteEdbData(tradeCode string, opUserId int) (err error) {
  918. o := global.DbMap[utils.DbNameManualIndex]
  919. var list []*Edbdata
  920. sql := ` select * FROM edbdata WHERE TRADE_CODE = ? `
  921. err = o.Raw(sql, tradeCode).Find(&list).Error
  922. if err != nil {
  923. return
  924. }
  925. deleteRecordList := make([]*EdbdataDeleteRecord, 0)
  926. for _, edbDataInfo := range list {
  927. deleteRecord := &EdbdataDeleteRecord{
  928. TradeCode: edbDataInfo.TradeCode,
  929. Dt: edbDataInfo.Dt,
  930. Close: edbDataInfo.Close,
  931. ModifyTime: time.Now(),
  932. CreateTime: time.Now(),
  933. SysUserId: opUserId,
  934. }
  935. deleteRecordList = append(deleteRecordList, deleteRecord)
  936. }
  937. err = o.CreateInBatches(deleteRecordList, utils.MultiAddNum).Error
  938. if err != nil {
  939. return
  940. }
  941. sql = ` DELETE FROM edbdata WHERE TRADE_CODE = ? `
  942. err = o.Exec(sql, tradeCode).Error
  943. return
  944. }
  945. // GetEdbInfoCountByClassifyId 根据指标分类id获取当前分类下的指标数量
  946. func GetEdbInfoCountByClassifyId(classifyId int) (count int, err error) {
  947. o := global.DbMap[utils.DbNameManualIndex]
  948. sql := `SELECT COUNT(1) AS count FROM ( SELECT a.*,b.CLOSE FROM edbinfo AS a
  949. INNER JOIN edbdata AS b ON a.TRADE_CODE=b.TRADE_CODE
  950. WHERE a.classify_id=? group by a.TRADE_CODE) d `
  951. var countNull sql2.NullInt64
  952. err = o.Raw(sql, classifyId).Scan(&countNull).Error
  953. if err != nil {
  954. return
  955. }
  956. if countNull.Valid {
  957. count = int(countNull.Int64)
  958. }
  959. return
  960. }
  961. // EdbInfoGroupCount 指标分类id获取当前分类下的指标数量
  962. type EdbInfoGroupCount struct {
  963. Count int
  964. ClassifyId int
  965. }
  966. // GetEdbInfoGroupCountByClassifyIds 根据指标分类id获取当前分类下的指标数量
  967. //func GetEdbInfoGroupCountByClassifyIds(classifyIdList []int) (list []*EdbInfoGroupCount, err error) {
  968. // num := len(classifyIdList)
  969. // if num <= 0 {
  970. // return
  971. // }
  972. // o := global.DbMap[utils.DbNameManualIndex]
  973. // sql := `SELECT COUNT(1) AS count,classify_id FROM ( SELECT a.*,b.CLOSE FROM edbinfo AS a
  974. // INNER JOIN edbdata AS b ON a.TRADE_CODE=b.TRADE_CODE
  975. // WHERE a.classify_id in (` + utils.GetOrmInReplace(num) + `) group by a.TRADE_CODE) d
  976. // GROUP BY classify_id `
  977. // err = o.Raw(sql, classifyIdList).Find(&list).Error
  978. // return
  979. //}
  980. // GetExcelData 获取excel样式数据
  981. //func GetExcelData() (list []*data_manage.ExcelStyle, err error) {
  982. // o := global.DbMap[utils.DbNameManualIndex]
  983. // sql := `SELECT * FROM excel_style `
  984. // err = o.Raw(sql).QueryRows(&list)
  985. // return
  986. //}
  987. // AddExcelData 添加excel样式数据
  988. //func AddExcelData(item *data_manage.ExcelStyle) (id int64, err error) {
  989. // o := global.DbMap[utils.DbNameManualIndex]
  990. // id, err = o.Insert(item)
  991. // return
  992. //}
  993. type EdbdataFloat struct {
  994. TradeCode string `gorm:"column:TRADE_CODE;autoIncrement:false;primaryKey" description:"指标编码"`
  995. Dt string `gorm:"column:DT" description:"日期"`
  996. Close float64 `gorm:"column:CLOSE" description:"值"`
  997. ModifyTime time.Time `gorm:"column:modify_time" description:"修改时间"`
  998. }
  999. //func GetTargetsDataFloat(tradeCode, dt string) (item *EdbdataFloat, err error) {
  1000. // sql := `SELECT * FROM edbdata WHERE TRADE_CODE=? AND DT=? `
  1001. // o := global.DbMap[utils.DbNameManualIndex]
  1002. // err = o.Raw(sql, tradeCode, dt).First(&item).Error
  1003. // return
  1004. //}
  1005. func ModifyEdbinfo(tradeCode, unit, frequency string, classifyId int) (err error) {
  1006. sql := `UPDATE edbinfo SET UNIT = ?,frequency=?, classify_id=?, create_date=NOW() WHERE TRADE_CODE=? `
  1007. o := global.DbMap[utils.DbNameManualIndex]
  1008. err = o.Exec(sql, unit, frequency, classifyId, tradeCode).Error
  1009. return
  1010. }
  1011. func DeleteTargetsDataByImport(tradeCode, dt string) (err error) {
  1012. sql := `DELETE FROM edbdata WHERE TRADE_CODE=? AND DT=? `
  1013. o := global.DbMap[utils.DbNameManualIndex]
  1014. err = o.Exec(sql, tradeCode, dt).Error
  1015. return
  1016. }
  1017. // GetEdbinfoListByCodeListGroupByUserId 根据指标code列表、用户分组获取指标信息
  1018. //func GetEdbinfoListByCodeListGroupByUserId(edbCodeList []string) (items []*Edbinfo, err error) {
  1019. // num := len(edbCodeList)
  1020. // if num <= 0 {
  1021. // return
  1022. // }
  1023. // o := global.DbMap[utils.DbNameManualIndex]
  1024. // sql := `SELECT * FROM edbinfo WHERE TRADE_CODE in (` + utils.GetOrmInReplace(num) + `) GROUP BY user_id `
  1025. // err = o.Raw(sql, edbCodeList).Find(&items).Error
  1026. // return
  1027. //}
  1028. // GetEdbinfoListByCodeListByCodeIdList
  1029. // @Description: 根据指标code列表获取列表信息
  1030. // @param edbCodeList
  1031. // @return items
  1032. // @return err
  1033. func GetEdbinfoListByCodeListByCodeIdList(edbCodeList []string) (items []*Edbinfo, err error) {
  1034. num := len(edbCodeList)
  1035. if num <= 0 {
  1036. return
  1037. }
  1038. o := global.DbMap[utils.DbNameManualIndex]
  1039. sql := `SELECT * FROM edbinfo WHERE TRADE_CODE in (` + utils.GetOrmInReplace(num) + `) `
  1040. err = o.Raw(sql, edbCodeList).Find(&items).Error
  1041. return
  1042. }
  1043. // GetEdbinfoListByCodeListByUserId
  1044. // @Description: 根据用户id列表获取指标列表信息
  1045. // @param userIdList
  1046. // @return items
  1047. // @return err
  1048. func GetEdbinfoListByCodeListByUserId(userIdList []int) (items []*Edbinfo, err error) {
  1049. num := len(userIdList)
  1050. if num <= 0 {
  1051. return
  1052. }
  1053. o := global.DbMap[utils.DbNameManualIndex]
  1054. sql := `SELECT * FROM edbinfo WHERE user_id in (` + utils.GetOrmInReplace(num) + `) `
  1055. err = o.Raw(sql, userIdList).Find(&items).Error
  1056. return
  1057. }
  1058. // ModifyEdbinfoUserIdByCodeList 根据指标code列表修改创建人
  1059. func ModifyEdbinfoUserIdByCodeList(edbCodeList []string, userId int, userName string) (err error) {
  1060. num := len(edbCodeList)
  1061. if num <= 0 {
  1062. return
  1063. }
  1064. o := global.DbMap[utils.DbNameManualIndex]
  1065. sql := `UPDATE edbinfo SET user_id = ?,user_name = ? WHERE TRADE_CODE in (` + utils.GetOrmInReplace(num) + `) `
  1066. err = o.Exec(sql, userId, userName, edbCodeList).Error
  1067. return
  1068. }
  1069. // ModifyEdbinfoUserIdByOldUserId
  1070. // @Description: 根据旧用户id修改新用户id
  1071. // @author: Roc
  1072. // @datetime 2024-03-25 17:59:32
  1073. // @param oldUserId int
  1074. // @param userId int
  1075. // @return err error
  1076. func ModifyEdbinfoUserIdByOldUserId(oldUserIdList []int, userId int) (err error) {
  1077. num := len(oldUserIdList)
  1078. if num <= 0 {
  1079. return
  1080. }
  1081. o := global.DbMap[utils.DbNameManualIndex]
  1082. sql := `UPDATE edbinfo SET user_id=? WHERE user_id in (` + utils.GetOrmInReplace(num) + `) `
  1083. err = o.Exec(sql, userId, oldUserIdList).Error
  1084. return
  1085. }
  1086. func GetEdbInfoAdminList() (list []int, err error) {
  1087. sql := `SELECT user_id FROM edbinfo GROUP BY user_id `
  1088. o := global.DbMap[utils.DbNameManualIndex]
  1089. err = o.Raw(sql).Find(&list).Error
  1090. return
  1091. }
  1092. // GetAllChildManualEdbClassify
  1093. // @Description: 获取手工数据中所有的子分类
  1094. // @author: Roc
  1095. // @datetime 2024-07-16 13:27:28
  1096. // @return items []*EdbdataClassify
  1097. // @return err error
  1098. func GetAllChildManualEdbClassify() (items []*EdbdataClassify, err error) {
  1099. o := global.DbMap[utils.DbNameManualIndex]
  1100. sql := ` SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE parent_id > 0 `
  1101. err = o.Raw(sql).Find(&items).Error
  1102. return
  1103. }
  1104. // GetChildManualEdbClassifyByIdList
  1105. // @Description: 获取手工数据中所有的子分类
  1106. // @author: Roc
  1107. // @datetime 2024-07-16 13:33:57
  1108. // @param idList []int
  1109. // @return items []*EdbdataClassify
  1110. // @return err error
  1111. func GetChildManualEdbClassifyByIdList(idList []int) (items []*EdbdataClassify, err error) {
  1112. num := len(idList)
  1113. if num <= 0 {
  1114. return
  1115. }
  1116. o := global.DbMap[utils.DbNameManualIndex]
  1117. sql := ` SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE classify_id in (` + utils.GetOrmInReplace(num) + `) `
  1118. err = o.Raw(sql, idList).Find(&items).Error
  1119. return
  1120. }
  1121. // EdbinfoMaxMinDate
  1122. // @Description: 手工指标的最小最大日期
  1123. type EdbinfoMaxMinDate struct {
  1124. MinDate string
  1125. MaxDate string
  1126. }
  1127. func (mmDate *EdbinfoMaxMinDate) AfterFind(tx *gorm.DB) (err error) {
  1128. mmDate.MinDate = utils.GormDateStrToDateStr(mmDate.MinDate)
  1129. mmDate.MaxDate = utils.GormDateStrToDateStr(mmDate.MaxDate)
  1130. return
  1131. }
  1132. // GetEdbdataMaxMinDate
  1133. // @Description: 获取手工指标的最小最大日期
  1134. // @author: Roc
  1135. // @datetime 2024-08-01 14:27:20
  1136. // @param tradeCode string
  1137. // @return item EdbinfoMaxMinDate
  1138. // @return err error
  1139. func GetEdbdataMaxMinDate(tradeCode string) (item EdbinfoMaxMinDate, err error) {
  1140. o := global.DbMap[utils.DbNameManualIndex]
  1141. sql := ` SELECT MIN(DT) min_date,MAX(DT) max_date FROM edbdata WHERE TRADE_CODE = ? `
  1142. err = o.Raw(sql, tradeCode).First(&item).Error
  1143. return
  1144. }
  1145. // GetEdbdataLatestValue
  1146. // @Description: 获取手工数据的最新值
  1147. // @author: Roc
  1148. // @datetime 2024-08-02 10:33:22
  1149. // @param tradeCode string
  1150. // @return latestValue float64
  1151. // @return err error
  1152. func GetEdbdataLatestValue(tradeCode string) (latestValue float64, err error) {
  1153. o := global.DbMap[utils.DbNameManualIndex]
  1154. sql := ` SELECT CLOSE FROM edbdata WHERE TRADE_CODE = ? ORDER BY DT DESC LIMIT 1`
  1155. //err = o.Raw(sql, tradeCode).QueryRow(&latestValue)
  1156. var valueNull sql2.NullFloat64
  1157. err = o.Raw(sql, tradeCode).Scan(&valueNull).Error
  1158. if err != nil {
  1159. return
  1160. }
  1161. if valueNull.Valid {
  1162. latestValue = valueNull.Float64
  1163. }
  1164. return
  1165. }
  1166. // ModifyEdbinfoMaxMinDate
  1167. // @Description: 修改手工指标的最小最大日期
  1168. // @author: Roc
  1169. // @datetime 2024-08-01 15:33:45
  1170. // @param startDate string
  1171. // @param endDate string
  1172. // @param tradeCode string
  1173. // @return err error
  1174. func ModifyEdbinfoMaxMinDate(tradeCode, startDate, endDate string, latestValue float64) (err error) {
  1175. o := global.DbMap[utils.DbNameManualIndex]
  1176. sql := ` UPDATE edbinfo SET start_date = ?, end_date = ?, latest_value = ? , modify_time = now() WHERE TRADE_CODE = ? `
  1177. err = o.Exec(sql, startDate, endDate, latestValue, tradeCode).Error
  1178. return
  1179. }