edb_data_lz.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hz_crm_api/utils"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. func AddEdbDataLzBySql(sqlStr string) (err error) {
  11. o := orm.NewOrmUsingDB("data")
  12. _, err = o.Raw(sqlStr).Exec()
  13. return
  14. }
  15. func GetEdbDataLzByEdbCode(edbCode string) (items []*EdbInfoSearchData, err error) {
  16. o := orm.NewOrmUsingDB("data")
  17. sql := ` SELECT * FROM edb_data_lz WHERE edb_code=? ORDER BY data_time DESC LIMIT ? `
  18. _, err = o.Raw(sql, edbCode, utils.EDB_DATA_LIMIT).QueryRows(&items)
  19. return
  20. }
  21. func GetEdbDataLzMaxOrMinDate(edbCode string) (min_date, max_date string, err error) {
  22. o := orm.NewOrmUsingDB("data")
  23. sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date FROM edb_data_lz WHERE edb_code=? `
  24. err = o.Raw(sql, edbCode).QueryRow(&min_date, &max_date)
  25. return
  26. }
  27. type lzSurveyData struct {
  28. DataTime string `orm:"column(data_time)" description:"日期"`
  29. InputValue string `orm:"column(input_value)" description:"值"`
  30. }
  31. func GetLzSurveyDataByTradeCode(condition string, pars []interface{}) (item []*lzSurveyData, err error) {
  32. sql := ` SELECT a.* FROM longzhong_survey_data AS a
  33. INNER JOIN longzhong_survey_product AS b ON a.survey_product_id=b.survey_product_id
  34. WHERE 1=1 `
  35. o := orm.NewOrmUsingDB("edb")
  36. if condition != "" {
  37. sql += condition
  38. }
  39. sql += ` ORDER BY a.data_time DESC `
  40. _, err = o.Raw(sql, pars).QueryRows(&item)
  41. return
  42. }
  43. func GetEdbDataLzByCode(edbCode string) (items []*EdbInfoSearchData, err error) {
  44. o := orm.NewOrmUsingDB("data")
  45. sql := ` SELECT * FROM edb_data_lz WHERE edb_code=? ORDER BY data_time DESC LIMIT ? `
  46. _, err = o.Raw(sql, edbCode, utils.EDB_DATA_LIMIT).QueryRows(&items)
  47. return
  48. }
  49. func GetEdbDataByLz(edbCode, startDate, endDate string) (searchItem *EdbInfoSearch, err error) {
  50. o := orm.NewOrmUsingDB("data")
  51. to, err := o.Begin()
  52. if err != nil {
  53. return
  54. }
  55. defer func() {
  56. if err != nil {
  57. _ = to.Rollback()
  58. } else {
  59. _ = to.Commit()
  60. }
  61. }()
  62. var condition string
  63. var pars []interface{}
  64. if edbCode != "" {
  65. condition += " AND b.lz_code=? "
  66. pars = append(pars, edbCode)
  67. }
  68. lzDataList, err := GetLzSurveyDataByTradeCode(condition, pars)
  69. if err != nil {
  70. return
  71. }
  72. searchItem = new(EdbInfoSearch)
  73. searchItem.EdbCode = edbCode
  74. dataLen := len(lzDataList)
  75. if dataLen > 0 {
  76. var isAdd bool
  77. addSql := ` INSERT INTO edb_data_lz(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  78. nowStr := time.Now().Format(utils.FormatDateTime)
  79. dataList := make([]*EdbInfoSearchData, 0)
  80. for i := 0; i < dataLen; i++ {
  81. item := lzDataList[i]
  82. eDate := item.DataTime
  83. sValue := item.InputValue
  84. dataTime, err := time.Parse(utils.FormatDate, eDate)
  85. if err != nil {
  86. return nil, err
  87. }
  88. timestamp := dataTime.UnixNano() / 1e6
  89. timeStr := fmt.Sprintf("%d", timestamp)
  90. addSql += "("
  91. addSql += "0," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + sValue + "," + "'" + nowStr + "'" +
  92. "," + "'" + nowStr + "'" + "," + "0" + "," + "'" + timeStr + "'"
  93. addSql += "),"
  94. isAdd = true
  95. }
  96. if isAdd {
  97. addSql = strings.TrimRight(addSql, ",")
  98. err = AddEdbDataLzBySql(addSql)
  99. if err != nil {
  100. utils.FileLogData.Info("AddEdbDataLzBySql Err:%s", err.Error())
  101. return
  102. }
  103. }
  104. dataList, err := GetEdbDataLzByCode(edbCode)
  105. if err != nil {
  106. utils.FileLogData.Info("GetEdbDataLzByCode Err:%s", err.Error())
  107. return searchItem, err
  108. }
  109. minDate, maxDate, err := GetEdbDataLzMaxOrMinDate(edbCode)
  110. if err != nil {
  111. return searchItem, err
  112. }
  113. searchItem.DataList = dataList
  114. searchItem.StartDate = minDate
  115. searchItem.EndDate = maxDate
  116. }
  117. if searchItem.DataList == nil {
  118. searchItem.DataList = make([]*EdbInfoSearchData, 0)
  119. }
  120. return
  121. }
  122. func ModifyEdbDataLzStatus(edbInfoId int64, edbCode string) (err error) {
  123. o := orm.NewOrmUsingDB("data")
  124. sql := ` UPDATE edb_data_lz SET edb_info_id=?, status=1,modify_time=NOW() WHERE edb_code=? `
  125. _, err = o.Raw(sql, edbInfoId, edbCode).Exec()
  126. return
  127. }
  128. func GetEdbDataLzByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  129. o := orm.NewOrmUsingDB("data")
  130. sql := ` SELECT COUNT(1) AS count FROM edb_data_lz WHERE edb_code=? AND data_time=? `
  131. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  132. return
  133. }
  134. func ModifyEdbDataLz(edbInfoId int64, dataTime, value string) (err error) {
  135. o := orm.NewOrmUsingDB("data")
  136. sql := ` UPDATE edb_data_lz SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  137. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  138. return
  139. }
  140. // 刷新隆众指标数据
  141. func RefreshEdbDataByLz(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  142. o := orm.NewOrmUsingDB("data")
  143. to, err := o.Begin()
  144. if err != nil {
  145. return
  146. }
  147. defer func() {
  148. if err != nil {
  149. _ = to.Rollback()
  150. } else {
  151. _ = to.Commit()
  152. }
  153. }()
  154. if err != nil {
  155. return
  156. }
  157. edbInfoIdStr := strconv.Itoa(edbInfoId)
  158. //计算数据
  159. var condition string
  160. var pars []interface{}
  161. if edbCode != "" {
  162. condition += " AND b.lz_code=? "
  163. pars = append(pars, edbCode)
  164. }
  165. if startDate != "" {
  166. condition += " AND a.data_time>=? "
  167. pars = append(pars, startDate)
  168. }
  169. if endDate != "" {
  170. condition += " AND a.data_time<=? "
  171. pars = append(pars, endDate)
  172. }
  173. lzDataList, err := GetLzSurveyDataByTradeCode(condition, pars)
  174. addSql := ` INSERT INTO edb_data_lz(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  175. var isAdd bool
  176. for _, v := range lzDataList {
  177. item := v
  178. count, err := GetEdbDataLzByCodeAndDate(edbCode, v.DataTime)
  179. if err != nil && err.Error() != utils.ErrNoRow() {
  180. return err
  181. }
  182. if count <= 0 {
  183. nowStr := time.Now().Format(utils.FormatDateTime)
  184. eDate := item.DataTime
  185. sValue := item.InputValue
  186. dataTime, err := time.Parse(utils.FormatDate, eDate)
  187. if err != nil {
  188. return err
  189. }
  190. timestamp := dataTime.UnixNano() / 1e6
  191. timeStr := fmt.Sprintf("%d", timestamp)
  192. addSql += "("
  193. addSql += edbInfoIdStr + "," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + sValue + "," + "'" + nowStr + "'" +
  194. "," + "'" + nowStr + "'" + "," + "1" + "," + "'" + timeStr + "'"
  195. addSql += "),"
  196. isAdd = true
  197. } else {
  198. err = ModifyEdbDataLz(int64(edbInfoId), v.DataTime, v.InputValue)
  199. if err != nil {
  200. return err
  201. }
  202. }
  203. }
  204. if isAdd {
  205. addSql = strings.TrimRight(addSql, ",")
  206. _, err = to.Raw(addSql).Exec()
  207. if err != nil {
  208. return err
  209. }
  210. }
  211. return
  212. }
  213. // 全部刷新隆众数据
  214. func RefreshAllEdbDataByLz(edbInfoId, source int, edbCode, startDate, endDate string) (err error) {
  215. o := orm.NewOrmUsingDB("data")
  216. to, err := o.Begin()
  217. if err != nil {
  218. return
  219. }
  220. defer func() {
  221. if err != nil {
  222. _ = to.Rollback()
  223. } else {
  224. _ = to.Commit()
  225. }
  226. }()
  227. if err != nil {
  228. return
  229. }
  230. edbInfoIdStr := strconv.Itoa(edbInfoId)
  231. //计算数据
  232. var condition string
  233. var pars []interface{}
  234. if edbCode != "" {
  235. condition += " AND b.lz_code=? "
  236. pars = append(pars, edbCode)
  237. }
  238. if startDate != "" {
  239. condition += " AND a.data_time>=? "
  240. pars = append(pars, startDate)
  241. }
  242. if endDate != "" {
  243. condition += " AND a.data_time<=? "
  244. pars = append(pars, endDate)
  245. }
  246. lzDataList, err := GetLzSurveyDataByTradeCode(condition, pars)
  247. //获取指标所有数据
  248. dataList := make([]*EdbDataBase, 0)
  249. dataTableName := GetEdbDataTableName(source)
  250. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  251. sql = fmt.Sprintf(sql, dataTableName)
  252. _, err = to.Raw(sql, edbInfoId).QueryRows(&dataList)
  253. if err != nil {
  254. return err
  255. }
  256. dataMap := make(map[string]string)
  257. for _, v := range dataList {
  258. dataMap[v.DataTime] = v.Value
  259. }
  260. addSql := ` INSERT INTO edb_data_lz(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  261. var isAdd bool
  262. for _, v := range lzDataList {
  263. item := v
  264. eDate := item.DataTime
  265. sValue := item.InputValue
  266. dataTime, err := time.Parse(utils.FormatDate, eDate)
  267. if err != nil {
  268. return err
  269. }
  270. timestamp := dataTime.UnixNano() / 1e6
  271. timeStr := fmt.Sprintf("%d", timestamp)
  272. saveValue := sValue
  273. if existVal, ok := dataMap[eDate]; !ok {
  274. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  275. isAdd = true
  276. } else {
  277. if existVal != saveValue {
  278. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  279. sql = fmt.Sprintf(sql, dataTableName)
  280. _, err = to.Raw(sql, sValue, edbInfoId, eDate).Exec()
  281. if err != nil {
  282. return err
  283. }
  284. }
  285. }
  286. }
  287. if isAdd {
  288. addSql = strings.TrimRight(addSql, ",")
  289. _, err = to.Raw(addSql).Exec()
  290. if err != nil {
  291. return err
  292. }
  293. }
  294. return
  295. }