longzhong.go 6.8 KB

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