target.go 48 KB

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