target.go 48 KB

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