longzhong.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package data_source
  2. import (
  3. "eta/eta_api/models/data_manage"
  4. "eta/eta_api/utils"
  5. "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. )
  8. type Longzhonginfo struct {
  9. LongzhonginfoId int
  10. TradeCode string
  11. SecName string
  12. Unit string
  13. Frequency string
  14. LastModifyDate string
  15. }
  16. func GetLongzhonginfoByClassifyId(classifyId int) (items []*Longzhonginfo, err error) {
  17. sql := `SELECT * FROM longzhonginfo WHERE classify_id=? ORDER BY longzhonginfo_id ASC `
  18. o := orm.NewOrmUsingDB("edb")
  19. _, err = o.Raw(sql, classifyId).QueryRows(&items)
  20. return
  21. }
  22. func GetLongzhongDataMaxCount(classifyId int) (count int, err error) {
  23. o := orm.NewOrmUsingDB("edb")
  24. sql := `SELECT MAX(t.num) AS count FROM (
  25. SELECT COUNT(1) AS num FROM longzhonginfo AS a
  26. INNER JOIN longzhongdata AS b ON a.longzhonginfo_id=b.longzhonginfo_id
  27. WHERE a.classify_id=?
  28. GROUP BY a.longzhonginfo_id
  29. )AS t `
  30. err = o.Raw(sql, classifyId).QueryRow(&count)
  31. return
  32. }
  33. type Longzhongdata struct {
  34. LongzhongdataId int `orm:"column(longzhongdata_id);pk"`
  35. LongzhonginfoId int
  36. TradeCode string
  37. Dt string
  38. Close float64
  39. CreateTime time.Time
  40. ModifyTime time.Time
  41. UnitDesc string
  42. UpdTime string
  43. DisplayTime string
  44. }
  45. func GetLongzhongDataById(lzInfoId int) (items []*Longzhongdata, err error) {
  46. o := orm.NewOrmUsingDB("edb")
  47. sql := `SELECT * FROM longzhongdata WHERE longzhonginfo_id=? ORDER BY dt DESC `
  48. _, err = o.Raw(sql, lzInfoId).QueryRows(&items)
  49. return
  50. }
  51. func GetLongzhongSurveyDataMaxCount(classifyName string) (count int, err error) {
  52. o := orm.NewOrmUsingDB("edb")
  53. sql := `SELECT MAX(t.num) AS count FROM (
  54. SELECT COUNT(1) AS num FROM longzhong_survey_product AS a
  55. INNER JOIN longzhong_survey_data AS b ON a.survey_product_id=b.survey_product_id
  56. WHERE a.breed_name=?
  57. GROUP BY a.survey_product_id
  58. )AS t `
  59. err = o.Raw(sql, classifyName).QueryRow(&count)
  60. return
  61. }
  62. func GetLongzhongSurveyDataMaxCountByFrequency(classifyName string, frequency int) (count int, err error) {
  63. o := orm.NewOrmUsingDB("edb")
  64. sql := `SELECT MAX(t.num) AS count FROM (
  65. SELECT COUNT(1) AS num FROM longzhong_survey_product AS a
  66. INNER JOIN longzhong_survey_data AS b ON a.survey_product_id=b.survey_product_id
  67. WHERE a.breed_name=? AND a.frequency=?
  68. GROUP BY a.survey_product_id
  69. )AS t `
  70. err = o.Raw(sql, classifyName, frequency).QueryRow(&count)
  71. return
  72. }
  73. type ExportLzSurveyDataMaxCount struct {
  74. Count int `description:"最大数据量"`
  75. BreedId int `description:"品种ID"`
  76. }
  77. func GetExportLzSurveyDataMaxCount(breedIds []string) (items []ExportLzSurveyDataMaxCount, err error) {
  78. if len(breedIds) == 0 {
  79. return
  80. }
  81. o := orm.NewOrmUsingDB("edb")
  82. sql := ` SELECT
  83. MAX(t.num) AS count,
  84. t.breed_id
  85. FROM
  86. (
  87. SELECT
  88. COUNT(1) AS num,
  89. a.breed_id AS breed_id
  90. FROM
  91. longzhong_survey_product AS a
  92. INNER JOIN longzhong_survey_data AS b ON a.survey_product_id = b.survey_product_id
  93. WHERE
  94. a.breed_id IN (` + utils.GetOrmInReplace(len(breedIds)) + `)
  95. GROUP BY
  96. a.survey_product_id
  97. ) AS t
  98. GROUP BY
  99. t.breed_id `
  100. _, err = o.Raw(sql, breedIds).QueryRows(&items)
  101. return
  102. }
  103. func GetExportLzSurveyDataByBreedIds(breedIds []string) (items []*LongzhongSurveyData, err error) {
  104. if len(breedIds) == 0 {
  105. return
  106. }
  107. o := orm.NewOrmUsingDB("edb")
  108. fields := ` survey_product_id, breed_id, input_value, data_time `
  109. sql := `SELECT ` + fields + ` FROM longzhong_survey_data WHERE breed_id IN (` + utils.GetOrmInReplace(len(breedIds)) + `) ORDER BY breed_id ASC, survey_product_id ASC, data_time DESC `
  110. _, err = o.Raw(sql, breedIds).QueryRows(&items)
  111. return
  112. }
  113. type LongzhongSurveyData struct {
  114. SurveyDataId int `orm:"column(survey_data_id);pk"`
  115. SurveyProductId int
  116. ProjectQuotaId int64
  117. BreedId string
  118. BreedName string
  119. QuotaId string
  120. QuotaName string
  121. UnitId string
  122. UnitName string
  123. SampleType int64
  124. SampleId string
  125. SampleName string
  126. DeviceId string
  127. Device string
  128. ProductCraftId string
  129. ProductCraft string
  130. ProductLine string
  131. InputMode int64
  132. Frequency int64
  133. InputValue float64
  134. TaskShouldFinishTime int64
  135. CustomId string
  136. CustomType int64
  137. Custom string
  138. QuotaSampleId int64
  139. TaskActualFinishTime int64
  140. AreaName string
  141. ProvinceName string
  142. ResearchStartData int64
  143. ResearchStopData int64
  144. DataTime string
  145. }
  146. func GetLongzhongSurveyDataById(lzInfoId int) (items []*LongzhongSurveyData, err error) {
  147. o := orm.NewOrmUsingDB("edb")
  148. sql := `SELECT * FROM longzhong_survey_data WHERE survey_product_id=? ORDER BY data_time DESC `
  149. _, err = o.Raw(sql, lzInfoId).QueryRows(&items)
  150. return
  151. }
  152. // GetLzItemList 模糊查询隆众数据库指标列表
  153. func GetLzItemList(keyword string) (items []*data_manage.LongzhongSurveyProduct, err error) {
  154. o := orm.NewOrmUsingDB("edb")
  155. sql := "SELECT * FROM longzhong_survey_product WHERE CONCAT(sample_name,breed_name,custom,quota_name,lz_code) LIKE ? "
  156. _, err = o.Raw(sql, utils.GetLikeKeyword(keyword)).QueryRows(&items)
  157. return
  158. }
  159. type lzSurveyData struct {
  160. DataTime string `orm:"column(data_time)" description:"日期"`
  161. InputValue string `orm:"column(input_value)" description:"值"`
  162. }
  163. // GetLzItemListByCode 根据code查询隆众数据列表
  164. func GetLzItemListByCode(lzCode string) (items []*lzSurveyData, err error) {
  165. o := orm.NewOrmUsingDB("edb")
  166. sql := "SELECT * FROM longzhong_survey_data WHERE survey_product_id=? GROUP BY data_time DESC"
  167. _, err = o.Raw(sql, lzCode).QueryRows(&items)
  168. return
  169. }
  170. // GetLzProductIdByCode 根据code查询隆众数据列表
  171. func GetLzProductIdByCode(lzCode string) (productId int, err error) {
  172. o := orm.NewOrmUsingDB("edb")
  173. sql := "SELECT survey_product_id FROM longzhong_survey_product WHERE lz_code=?"
  174. err = o.Raw(sql, lzCode).QueryRow(&productId)
  175. return
  176. }
  177. // GetLzProductIdByCode 根据code查询隆众数据列表
  178. func GetLzSurveyDataMaxCountByProductId(surveyProductId int) (productId int, err error) {
  179. o := orm.NewOrmUsingDB("edb")
  180. sql := "SELECT COUNT(1) FROM longzhong_survey_data WHERE survey_product_id=?"
  181. err = o.Raw(sql, surveyProductId).QueryRow(&productId)
  182. return
  183. }