edb_data.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. package data
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/shopspring/decimal"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/models/data_manage"
  8. "hongze/hz_crm_api/utils"
  9. "time"
  10. )
  11. // EdbDataEdit 指标数据修改
  12. type EdbDataEdit struct {
  13. OldCreateDate string `description:"旧的录入日期"`
  14. CreateDate string `description:"新的录入日期"`
  15. TradeCode string `description:"指标唯一编码"`
  16. Close string `description:"录入值"`
  17. }
  18. // BatchEdbData 批量修改指标数据
  19. func BatchEdbData(oldCreateDate, createDate string, list []EdbDataEdit, opUserId int) (failEdbDataList []EdbDataEdit, err error) {
  20. if oldCreateDate == createDate { //修改
  21. err = errors.New("开始日期与结束日期一致!")
  22. return
  23. }
  24. for _, newEdbData := range list {
  25. edbdata := new(models.Edbdata)
  26. edbdata.TradeCode = newEdbData.TradeCode
  27. edbdata.Dt = newEdbData.CreateDate
  28. edbdata.Close = newEdbData.Close
  29. edbdata.ModifyTime = time.Now()
  30. item, err := models.GetDataInfo(newEdbData.TradeCode, createDate)
  31. if err != nil && err.Error() != utils.ErrNoRow() {
  32. failEdbDataList = append(failEdbDataList, newEdbData)
  33. continue
  34. }
  35. if item == nil {
  36. _, err = models.AddEdbdata(edbdata)
  37. if err != nil {
  38. failEdbDataList = append(failEdbDataList, newEdbData)
  39. continue
  40. }
  41. } else {
  42. err = models.EditEdbdata(edbdata)
  43. if err != nil {
  44. failEdbDataList = append(failEdbDataList, newEdbData)
  45. continue
  46. }
  47. }
  48. oldItem, err := models.GetDataInfo(newEdbData.TradeCode, oldCreateDate)
  49. if err != nil && err.Error() != utils.ErrNoRow() {
  50. failEdbDataList = append(failEdbDataList, newEdbData)
  51. continue
  52. }
  53. if oldItem != nil {
  54. record := new(models.EdbdataDeleteRecord)
  55. record.TradeCode = oldItem.TradeCode
  56. record.Dt = oldItem.Dt
  57. record.Close = oldItem.Close
  58. record.ModifyTime = oldItem.ModifyTime
  59. record.CreateTime = time.Now()
  60. record.SysUserId = opUserId
  61. _, err = models.AddEdbdataDeleteRecord(record)
  62. if err != nil {
  63. failEdbDataList = append(failEdbDataList, newEdbData)
  64. continue
  65. }
  66. //删除数据
  67. models.DeleteEdbData(newEdbData.TradeCode, newEdbData.OldCreateDate)
  68. }
  69. }
  70. return
  71. }
  72. // GetEdbDataTbz 获取指标的同比值数据
  73. func GetEdbDataTbz(edbInfo *data_manage.EdbInfo, tmpDataList []*data_manage.EdbDataList, startDateTime time.Time) (dataList []*data_manage.EdbDataList, minValue, maxValue float64, err error) {
  74. dataList = make([]*data_manage.EdbDataList, 0)
  75. // 数据处理
  76. var dateArr []string
  77. dataMap := make(map[string]*data_manage.EdbDataList)
  78. for _, v := range tmpDataList {
  79. dateArr = append(dateArr, v.DataTime)
  80. dataMap[v.DataTime] = v
  81. }
  82. for _, av := range dateArr {
  83. currentItem, ok := dataMap[av]
  84. // 如果找不到当前日期的数据,那么终止当前循环,进入下一循环
  85. if !ok {
  86. continue
  87. }
  88. tmpItem := *currentItem
  89. var isOk bool //是否计算出来结果
  90. //当前日期
  91. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
  92. if tmpErr != nil {
  93. err = tmpErr
  94. return
  95. }
  96. // 如果存在开始日期,同时,当前日期早于开始日期,那么终止当前循环,进入下一循环
  97. if !startDateTime.IsZero() && currentDate.Before(startDateTime) {
  98. continue
  99. }
  100. //上一年的日期
  101. preDate := currentDate.AddDate(-1, 0, 0)
  102. preDateStr := preDate.Format(utils.FormatDate)
  103. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  104. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  105. isOk = true
  106. } else {
  107. if edbInfo.Frequency == "月度" { //向上和向下,各找一个月
  108. for i := 0; i <= 35; i++ {
  109. nextDateDay := preDate.AddDate(0, 0, i)
  110. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  111. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  112. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  113. isOk = true
  114. break
  115. } else {
  116. preDateDay := preDate.AddDate(0, 0, -i)
  117. preDateDayStr := preDateDay.Format(utils.FormatDate)
  118. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  119. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  120. isOk = true
  121. break
  122. }
  123. }
  124. }
  125. } else if edbInfo.Frequency == "季度" || edbInfo.Frequency == "年度" {
  126. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  127. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  128. isOk = true
  129. break
  130. }
  131. } else {
  132. nextDateDay := preDate.AddDate(0, 0, 1)
  133. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  134. preDateDay := preDate.AddDate(0, 0, -1)
  135. preDateDayStr := preDateDay.Format(utils.FormatDate)
  136. for i := 0; i < 35; i++ {
  137. if i >= 1 {
  138. nextDateDay = nextDateDay.AddDate(0, 0, i)
  139. nextDateDayStr = nextDateDay.Format(utils.FormatDate)
  140. }
  141. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  142. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  143. isOk = true
  144. break
  145. } else {
  146. if i >= 1 {
  147. preDateDay = preDate.AddDate(0, 0, -i)
  148. preDateDayStr = nextDateDay.Format(utils.FormatDate)
  149. }
  150. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  151. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  152. isOk = true
  153. break
  154. }
  155. }
  156. }
  157. }
  158. }
  159. if isOk {
  160. if tmpItem.Value > maxValue {
  161. maxValue = tmpItem.Value
  162. }
  163. if tmpItem.Value < minValue {
  164. minValue = tmpItem.Value
  165. }
  166. dataList = append(dataList, &tmpItem)
  167. }
  168. }
  169. return
  170. }
  171. // GetChartEdbSeasonalData 获取指标的季节性数据
  172. func GetChartEdbSeasonalData(calendar, startDate, endDate string, edbInfo *data_manage.EdbInfo) (resultData interface{}, err error) {
  173. var startDateReal string
  174. startDateReal = startDate
  175. calendarPreYear := 0
  176. if calendar == "农历" {
  177. newStartDateReal, tmpErr := time.Parse(utils.FormatDate, startDateReal)
  178. if tmpErr != nil {
  179. err = tmpErr
  180. fmt.Println("time.Parse:" + err.Error())
  181. return
  182. }
  183. calendarPreYear = newStartDateReal.Year() - 1
  184. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  185. startDateReal = newStartDateReal.Format(utils.FormatDate)
  186. }
  187. dataList := make([]*data_manage.EdbDataList, 0)
  188. switch edbInfo.EdbInfoType {
  189. case 0:
  190. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.EdbInfoId, startDateReal, endDate)
  191. case 1:
  192. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, startDateReal, endDate, false)
  193. default:
  194. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  195. }
  196. if err != nil {
  197. return
  198. }
  199. latestDateStr := edbInfo.LatestDate //实际数据的截止日期
  200. latestDate, tmpErr := time.Parse(utils.FormatDate, edbInfo.LatestDate)
  201. if tmpErr != nil {
  202. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + edbInfo.LatestDate))
  203. return
  204. }
  205. latestDateYear := latestDate.Year() //实际数据截止年份
  206. if calendar == "农历" {
  207. if len(dataList) <= 0 {
  208. resultData = new(data_manage.EdbDataResult)
  209. } else {
  210. result, tmpErr := data_manage.AddCalculateQuarterV4(dataList)
  211. if tmpErr != nil {
  212. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  213. return
  214. }
  215. // 处理季节图的截止日期
  216. for k, edbDataItems := range result.List {
  217. var cuttingDataTimestamp int64
  218. // 切割的日期时间字符串
  219. cuttingDataTimeStr := latestDate.AddDate(0, 0, edbDataItems.BetweenDay).Format(utils.FormatDate)
  220. //如果等于最后的实际日期,那么遍历找到该日期对应的时间戳,并将其赋值为 切割时间戳
  221. if edbDataItems.Year >= latestDateYear {
  222. for _, tmpData := range edbDataItems.Items {
  223. if tmpData.DataTime == cuttingDataTimeStr {
  224. cuttingDataTimestamp = tmpData.DataTimestamp
  225. break
  226. }
  227. }
  228. }
  229. edbDataItems.CuttingDataTimestamp = cuttingDataTimestamp
  230. result.List[k] = edbDataItems
  231. }
  232. if result.List[0].Year != calendarPreYear {
  233. itemList := make([]*data_manage.EdbDataList, 0)
  234. items := new(data_manage.EdbDataItems)
  235. //items.Year = calendarPreYear
  236. items.Items = itemList
  237. newResult := new(data_manage.EdbDataResult)
  238. newResult.List = append(newResult.List, items)
  239. newResult.List = append(newResult.List, result.List...)
  240. resultData = newResult
  241. } else {
  242. resultData = result
  243. }
  244. }
  245. } else {
  246. currentYear := time.Now().Year()
  247. quarterDataList := make([]*data_manage.QuarterData, 0)
  248. quarterMap := make(map[int][]*data_manage.EdbDataList)
  249. var quarterArr []int
  250. for _, v := range dataList {
  251. itemDate, tmpErr := time.Parse(utils.FormatDate, v.DataTime)
  252. if tmpErr != nil {
  253. err = errors.New("季度指标日期转换,Err:" + tmpErr.Error() + ";DataTime:" + v.DataTime)
  254. return
  255. }
  256. year := itemDate.Year()
  257. newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
  258. timestamp := newItemDate.UnixNano() / 1e6
  259. v.DataTimestamp = timestamp
  260. if findVal, ok := quarterMap[year]; !ok {
  261. quarterArr = append(quarterArr, year)
  262. findVal = append(findVal, v)
  263. quarterMap[year] = findVal
  264. } else {
  265. findVal = append(findVal, v)
  266. quarterMap[year] = findVal
  267. }
  268. }
  269. for _, v := range quarterArr {
  270. itemList := quarterMap[v]
  271. quarterItem := new(data_manage.QuarterData)
  272. quarterItem.Year = v
  273. quarterItem.DataList = itemList
  274. //如果等于最后的实际日期,那么将切割时间戳记录
  275. if v == latestDateYear {
  276. var cuttingDataTimestamp int64
  277. for _, tmpData := range itemList {
  278. if tmpData.DataTime == latestDateStr {
  279. cuttingDataTimestamp = tmpData.DataTimestamp
  280. break
  281. }
  282. }
  283. quarterItem.CuttingDataTimestamp = cuttingDataTimestamp
  284. } else if v > latestDateYear {
  285. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  286. if len(itemList) > 0 {
  287. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  288. }
  289. }
  290. quarterDataList = append(quarterDataList, quarterItem)
  291. }
  292. resultData = quarterDataList
  293. }
  294. return
  295. }
  296. // TbzDiv 同比值计算
  297. func TbzDiv(a, b float64) float64 {
  298. var valFloat float64
  299. if b != 0 {
  300. af := decimal.NewFromFloat(a)
  301. bf := decimal.NewFromFloat(b)
  302. val, _ := af.Div(bf).Float64()
  303. val = val - 1
  304. valFloat, _ = decimal.NewFromFloat(val).RoundCeil(4).Float64()
  305. } else {
  306. valFloat = 0
  307. }
  308. return valFloat
  309. }
  310. // GetDateData 获取指标的单个日期的值
  311. func GetDateData(edbInfo *data_manage.EdbInfo, date string) (val string, err error) {
  312. var dataList []*data_manage.EdbDataList
  313. switch edbInfo.EdbInfoType {
  314. case 0:
  315. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.EdbInfoId, date, date)
  316. case 1:
  317. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, date, date, false)
  318. default:
  319. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  320. }
  321. if err != nil {
  322. return
  323. }
  324. if len(dataList) <= 0 {
  325. return
  326. }
  327. val = fmt.Sprint(dataList[0].Value)
  328. return
  329. }