target.go 48 KB

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