edb_data_calculate_rjz.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package models
  2. import (
  3. "errors"
  4. "eta_gn/eta_index_lib/global"
  5. "eta_gn/eta_index_lib/utils"
  6. "fmt"
  7. "github.com/shopspring/decimal"
  8. "gorm.io/gorm"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. func AddCalculateRjz(req *EdbInfoCalculateBatchSaveReq, fromEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName string) (edbInfo *EdbInfo, err error) {
  14. to := global.DEFAULT_DmSQL.Begin()
  15. defer func() {
  16. if err != nil {
  17. to.Rollback()
  18. } else {
  19. to.Commit()
  20. }
  21. }()
  22. if req.EdbInfoId <= 0 {
  23. edbInfo = new(EdbInfo)
  24. edbInfo.Source = utils.DATA_SOURCE_CALCULATE_RJZ
  25. edbInfo.SourceName = "日均值"
  26. edbInfo.EdbCode = edbCode
  27. edbInfo.EdbName = req.EdbName
  28. edbInfo.EdbNameSource = req.EdbName
  29. edbInfo.Frequency = req.Frequency
  30. edbInfo.Unit = req.Unit
  31. edbInfo.ClassifyId = req.ClassifyId
  32. edbInfo.SysUserId = sysUserId
  33. edbInfo.SysUserRealName = sysUserRealName
  34. edbInfo.CreateTime = time.Now()
  35. edbInfo.ModifyTime = time.Now()
  36. edbInfo.UniqueCode = uniqueCode
  37. edbInfo.CalculateFormula = req.Formula
  38. edbInfo.EdbNameEn = req.EdbName
  39. edbInfo.UnitEn = req.Unit
  40. edbInfo.EdbType = 2
  41. edbInfo.Sort = GetAddEdbMaxSortByClassifyId(req.ClassifyId, utils.EDB_INFO_TYPE)
  42. tmpErr := to.Create(edbInfo).Error
  43. if tmpErr != nil {
  44. err = tmpErr
  45. return
  46. }
  47. {
  48. calculateMappingItem := new(EdbInfoCalculateMapping)
  49. calculateMappingItem.CreateTime = time.Now()
  50. calculateMappingItem.ModifyTime = time.Now()
  51. calculateMappingItem.Sort = 1
  52. calculateMappingItem.EdbCode = edbCode
  53. calculateMappingItem.EdbInfoId = edbInfo.EdbInfoId
  54. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  55. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  56. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  57. calculateMappingItem.FromSource = fromEdbInfo.Source
  58. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  59. calculateMappingItem.FromTag = ""
  60. calculateMappingItem.Source = edbInfo.Source
  61. calculateMappingItem.SourceName = edbInfo.SourceName
  62. calculateMappingItem.FromSubSource = fromEdbInfo.SubSource
  63. err = to.Create(calculateMappingItem).Error
  64. if err != nil {
  65. return
  66. }
  67. }
  68. } else {
  69. edbInfo, err = GetEdbInfoById(req.EdbInfoId)
  70. if err != nil {
  71. return
  72. }
  73. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_RJZ, utils.DATA_SUB_SOURCE_EDB)
  74. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  75. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  76. err = to.Exec(deleteSql, req.EdbInfoId).Error
  77. if err != nil {
  78. return
  79. }
  80. }
  81. err = refreshAllCalculateRjz(to, edbInfo.EdbInfoId, edbInfo.Source, fromEdbInfo, edbCode, "", "")
  82. return
  83. }
  84. func EditCalculateRjz(edbInfo *EdbInfo, req *EdbInfoCalculateBatchEditReq, fromEdbInfo *EdbInfo) (err error) {
  85. to := global.DEFAULT_DmSQL.Begin()
  86. defer func() {
  87. if err != nil {
  88. to.Rollback()
  89. } else {
  90. to.Commit()
  91. }
  92. }()
  93. edbInfo.EdbName = req.EdbName
  94. edbInfo.EdbNameSource = req.EdbName
  95. edbInfo.Frequency = req.Frequency
  96. edbInfo.Unit = req.Unit
  97. edbInfo.ClassifyId = req.ClassifyId
  98. edbInfo.EdbNameEn = req.EdbNameEn
  99. edbInfo.UnitEn = req.UnitEn
  100. edbInfo.ModifyTime = time.Now()
  101. err = to.Model(edbInfo).Select([]string{"EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "ModifyTime", "EdbNameEn", "UnitEn"}).Updates(edbInfo).Error
  102. if err != nil {
  103. return
  104. }
  105. var existCondition string
  106. var existPars []interface{}
  107. existCondition += " AND edb_info_id=? AND from_edb_info_id=? "
  108. existPars = append(existPars, edbInfo.EdbInfoId, req.FromEdbInfoId)
  109. count, err := GetEdbInfoCalculateCountByCondition(existCondition, existPars)
  110. if err != nil {
  111. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  112. return
  113. }
  114. if count > 0 { // 指标未被替换,无需重新计算
  115. return
  116. }
  117. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
  118. err = to.Exec(sql, edbInfo.EdbInfoId).Error
  119. if err != nil {
  120. return
  121. }
  122. sql = ` DELETE FROM edb_data_calculate_rjz WHERE edb_info_id = ? `
  123. err = to.Exec(sql, edbInfo.EdbInfoId).Error
  124. if err != nil {
  125. return
  126. }
  127. {
  128. calculateMappingItem := &EdbInfoCalculateMapping{
  129. EdbInfoCalculateMappingId: 0,
  130. EdbInfoId: edbInfo.EdbInfoId,
  131. Source: utils.DATA_SOURCE_CALCULATE_RJZ,
  132. SourceName: "日均值",
  133. EdbCode: edbInfo.EdbCode,
  134. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  135. FromEdbCode: fromEdbInfo.EdbCode,
  136. FromEdbName: fromEdbInfo.EdbName,
  137. FromSource: fromEdbInfo.Source,
  138. FromSourceName: fromEdbInfo.SourceName,
  139. FromTag: "",
  140. Sort: 1,
  141. CreateTime: time.Now(),
  142. ModifyTime: time.Now(),
  143. FromSubSource: fromEdbInfo.SubSource,
  144. }
  145. err = to.Create(calculateMappingItem).Error
  146. if err != nil {
  147. return
  148. }
  149. }
  150. err = refreshAllCalculateRjz(to, edbInfo.EdbInfoId, edbInfo.Source, fromEdbInfo, edbInfo.EdbCode, "", "")
  151. return
  152. }
  153. func RefreshAllCalculateRjz(edbInfoId, source int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate string) (err error) {
  154. to := global.DEFAULT_DmSQL.Begin()
  155. defer func() {
  156. if err != nil {
  157. to.Rollback()
  158. } else {
  159. to.Commit()
  160. }
  161. }()
  162. err = refreshAllCalculateRjz(to, edbInfoId, source, fromEdbInfo, edbCode, startDate, endDate)
  163. return
  164. }
  165. func refreshAllCalculateRjz(to *gorm.DB, edbInfoId, source int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate string) (err error) {
  166. edbInfoIdStr := strconv.Itoa(edbInfoId)
  167. dataList, err := GetEdbDataListAll(fromEdbInfo.Source, fromEdbInfo.SubSource, FindEdbDataListAllCond{
  168. EdbInfoId: fromEdbInfo.EdbInfoId,
  169. StartDataTime: startDate,
  170. StartDataTimeCond: ">=",
  171. }, 0)
  172. if err != nil {
  173. return err
  174. }
  175. var dateArr []string
  176. dataMap := make(map[string]*EdbInfoSearchData)
  177. for _, v := range dataList {
  178. dateArr = append(dateArr, v.DataTime)
  179. dataMap[v.DataTime] = v
  180. }
  181. fmt.Println("source:", source)
  182. dataTableName := GetEdbDataTableName(source, utils.DATA_SUB_SOURCE_EDB)
  183. existDataList, err := GetAllEdbDataListByTo(to, edbInfoId, source, 0)
  184. if err != nil {
  185. return err
  186. }
  187. existDataMap := make(map[string]string)
  188. for _, v := range existDataList {
  189. existDataMap[v.DataTime] = v.Value
  190. }
  191. fmt.Println("existDataMap:", existDataMap)
  192. addSql := ` INSERT INTO edb_data_calculate_rjz(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  193. var isAdd bool
  194. existAddDataMap := make(map[string]string)
  195. for _, av := range dateArr {
  196. currentItem := dataMap[av]
  197. if currentItem != nil {
  198. currentDate, err := time.ParseInLocation(utils.FormatDate, av, time.Local)
  199. if err != nil {
  200. return err
  201. }
  202. days := GetRjzFrequencyDays(currentDate, fromEdbInfo.Frequency)
  203. val := rjzDiv(currentItem.Value, days)
  204. timestamp := currentDate.UnixNano() / 1e6
  205. timestampStr := fmt.Sprintf("%d", timestamp)
  206. if existVal, ok := existDataMap[av]; !ok {
  207. if _, existOk := existAddDataMap[av]; !existOk {
  208. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  209. isAdd = true
  210. }
  211. existAddDataMap[av] = av
  212. } else {
  213. existValDecimal, err := decimal.NewFromString(existVal)
  214. existStr := existValDecimal.String()
  215. if existStr != val {
  216. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  217. sql = fmt.Sprintf(sql, dataTableName)
  218. err = to.Exec(sql, val, edbInfoId, av).Error
  219. if err != nil {
  220. return err
  221. }
  222. }
  223. }
  224. }
  225. }
  226. if isAdd {
  227. addSql = strings.TrimRight(addSql, ",")
  228. err = to.Exec(addSql).Error
  229. if err != nil {
  230. return err
  231. }
  232. }
  233. return
  234. }
  235. func GetRjzFrequencyDays(currDate time.Time, frequency string) (days int) {
  236. switch frequency {
  237. case "周度", "周":
  238. days = 7
  239. case "旬度":
  240. if currDate.Day() <= 20 { // 1旬和2旬都是10天
  241. days = 10
  242. } else {
  243. currT := time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, time.Local)
  244. nextT := time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  245. days = utils.GetTimeSubDay(currT, nextT)
  246. }
  247. case "月度":
  248. currMonthFd := time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Local)
  249. nextMonthFd := time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, time.Local)
  250. days = utils.GetTimeSubDay(currMonthFd, nextMonthFd)
  251. case "季度":
  252. curr0T := time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, time.Local)
  253. curr1T := time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, time.Local)
  254. curr2T := time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, time.Local)
  255. curr3T := time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, time.Local)
  256. curr4T := time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, time.Local)
  257. if currDate.Month() < 4 { // 1季度
  258. days = utils.GetTimeSubDay(curr0T, curr1T)
  259. } else if currDate.Month() < 7 { // 2季度
  260. days = utils.GetTimeSubDay(curr1T, curr2T)
  261. } else if currDate.Month() < 10 { // 3季度
  262. days = utils.GetTimeSubDay(curr2T, curr3T)
  263. } else {
  264. days = utils.GetTimeSubDay(curr3T, curr4T)
  265. }
  266. case "年度":
  267. currentYearFd := time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, time.Local)
  268. nextYearFd := currentYearFd.AddDate(1, 0, 0)
  269. days = utils.GetTimeSubDay(currentYearFd, nextYearFd)
  270. case "半年度":
  271. curr0T := time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, time.Local)
  272. curr1T := time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, time.Local)
  273. curr2T := time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, time.Local)
  274. if currDate.Month() <= 6 { // 上半年
  275. days = utils.GetTimeSubDay(curr0T, curr1T)
  276. } else {
  277. days = utils.GetTimeSubDay(curr1T, curr2T)
  278. }
  279. default:
  280. days = 1
  281. return
  282. }
  283. return
  284. }
  285. func rjzDiv(a float64, b int) string {
  286. var valStr string
  287. if b != 0 {
  288. af := decimal.NewFromFloat(a)
  289. bf := decimal.NewFromFloat(float64(b))
  290. val, _ := af.Div(bf).Float64()
  291. valStr = decimal.NewFromFloat(val).Round(4).String()
  292. } else {
  293. valStr = "0"
  294. }
  295. return valStr
  296. }