target.go 42 KB

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