target.go 58 KB

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