longzhong.go 6.7 KB

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