longzhong.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. // [2025-zsh-时间类型修复-chenhan]
  147. type LongzhongSurveyDataOrm struct {
  148. SurveyDataId int `orm:"column(survey_data_id);pk"`
  149. SurveyProductId int
  150. ProjectQuotaId int64
  151. BreedId string
  152. BreedName string
  153. QuotaId string
  154. QuotaName string
  155. UnitId string
  156. UnitName string
  157. SampleType int64
  158. SampleId string
  159. SampleName string
  160. DeviceId string
  161. Device string
  162. ProductCraftId string
  163. ProductCraft string
  164. ProductLine string
  165. InputMode int64
  166. Frequency int64
  167. InputValue float64
  168. TaskShouldFinishTime int64
  169. CustomId string
  170. CustomType int64
  171. Custom string
  172. QuotaSampleId int64
  173. TaskActualFinishTime int64
  174. AreaName string
  175. ProvinceName string
  176. ResearchStartData int64
  177. ResearchStopData int64
  178. DataTime time.Time
  179. }
  180. // [2025-zsh-时间类型修复-chenhan]
  181. func (obj *LongzhongSurveyDataOrm) toView() *LongzhongSurveyData {
  182. return &LongzhongSurveyData{
  183. SurveyDataId: obj.SurveyDataId,
  184. SurveyProductId: obj.SurveyProductId,
  185. ProjectQuotaId: obj.ProjectQuotaId,
  186. BreedId: obj.BreedId,
  187. BreedName: obj.BreedName,
  188. QuotaId: obj.QuotaId,
  189. QuotaName: obj.QuotaName,
  190. UnitId: obj.UnitId,
  191. UnitName: obj.UnitName,
  192. SampleType: obj.SampleType,
  193. SampleId: obj.SampleId,
  194. SampleName: obj.SampleName,
  195. DeviceId: obj.DeviceId,
  196. Device: obj.Device,
  197. ProductCraftId: obj.ProductCraftId,
  198. ProductCraft: obj.ProductCraft,
  199. ProductLine: obj.ProductLine,
  200. InputMode: obj.InputMode,
  201. Frequency: obj.Frequency,
  202. InputValue: obj.InputValue,
  203. TaskShouldFinishTime: obj.TaskShouldFinishTime,
  204. CustomId: obj.CustomId,
  205. CustomType: obj.CustomType,
  206. Custom: obj.Custom,
  207. QuotaSampleId: obj.QuotaSampleId,
  208. TaskActualFinishTime: obj.TaskActualFinishTime,
  209. AreaName: obj.AreaName,
  210. ProvinceName: obj.ProvinceName,
  211. ResearchStartData: obj.ResearchStartData,
  212. ResearchStopData: obj.ResearchStopData,
  213. DataTime: obj.DataTime.Format(utils.FormatDate),
  214. }
  215. }
  216. func toLongzhongSurveyDataList(list []*LongzhongSurveyDataOrm) (itemList []*LongzhongSurveyData) {
  217. for _, item := range list {
  218. itemList = append(itemList, item.toView())
  219. }
  220. return
  221. }
  222. func GetLongzhongSurveyDataById(lzInfoId int) (items []*LongzhongSurveyData, err error) {
  223. o := orm.NewOrmUsingDB("edb")
  224. sql := `SELECT * FROM longzhong_survey_data WHERE survey_product_id=? ORDER BY data_time DESC `
  225. // [2025-zsh-时间类型修复-chenhan]
  226. var ormList []*LongzhongSurveyDataOrm
  227. _, err = o.Raw(sql, lzInfoId).QueryRows(&ormList)
  228. if err != nil {
  229. return
  230. }
  231. items = toLongzhongSurveyDataList(ormList)
  232. return
  233. }
  234. // GetLzItemList 模糊查询隆众数据库指标列表
  235. func GetLzItemList(keyword string) (items []*data_manage.LongzhongSurveyProduct, err error) {
  236. o := orm.NewOrmUsingDB("edb")
  237. sql := "SELECT * FROM longzhong_survey_product WHERE CONCAT(sample_name,breed_name,custom,quota_name,lz_code) LIKE ? "
  238. // [2025-zsh-时间类型修复-chenhan]
  239. var ormList []*data_manage.LongzhongSurveyProductOrm
  240. _, err = o.Raw(sql, utils.GetLikeKeyword(keyword)).QueryRows(&ormList)
  241. if err != nil {
  242. return
  243. }
  244. items = data_manage.ToLongzhongSurveyProductList(ormList)
  245. return
  246. }
  247. type lzSurveyData struct {
  248. DataTime string `orm:"column(data_time)" description:"日期"`
  249. InputValue string `orm:"column(input_value)" description:"值"`
  250. }
  251. type lzSurveyDataOrm struct {
  252. DataTime time.Time `orm:"column(data_time)" description:"日期"`
  253. InputValue string `orm:"column(input_value)" description:"值"`
  254. }
  255. func (obj *lzSurveyDataOrm) toView() *lzSurveyData {
  256. return &lzSurveyData{
  257. DataTime: obj.DataTime.Format(utils.FormatDate),
  258. InputValue: obj.InputValue,
  259. }
  260. }
  261. func toLzSurveyDataList(list []*lzSurveyDataOrm) (itemList []*lzSurveyData) {
  262. for _, item := range list {
  263. itemList = append(itemList, item.toView())
  264. }
  265. return
  266. }
  267. // GetLzItemListByCode 根据code查询隆众数据列表
  268. func GetLzItemListByCode(lzCode string) (items []*lzSurveyData, err error) {
  269. o := orm.NewOrmUsingDB("edb")
  270. sql := "SELECT * FROM longzhong_survey_data WHERE survey_product_id=? GROUP BY data_time DESC"
  271. // [2025-zsh-时间类型修复-chenhan]
  272. var ormList []*lzSurveyDataOrm
  273. _, err = o.Raw(sql, lzCode).QueryRows(&ormList)
  274. if err != nil {
  275. return
  276. }
  277. items = toLzSurveyDataList(ormList)
  278. return
  279. }
  280. // GetLzProductIdByCode 根据code查询隆众数据列表
  281. func GetLzProductIdByCode(lzCode string) (productId int, err error) {
  282. o := orm.NewOrmUsingDB("edb")
  283. sql := "SELECT survey_product_id FROM longzhong_survey_product WHERE lz_code=?"
  284. err = o.Raw(sql, lzCode).QueryRow(&productId)
  285. return
  286. }
  287. // GetLzProductIdByCode 根据code查询隆众数据列表
  288. func GetLzSurveyDataMaxCountByProductId(surveyProductId int) (productId int, err error) {
  289. o := orm.NewOrmUsingDB("edb")
  290. sql := "SELECT COUNT(1) FROM longzhong_survey_data WHERE survey_product_id=?"
  291. err = o.Raw(sql, surveyProductId).QueryRow(&productId)
  292. return
  293. }