productivity_service.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "time"
  6. "gopkg.in/guregu/null.v3"
  7. "xiaoniaokuaiyan.com/xiaoniao/entity"
  8. "xiaoniaokuaiyan.com/xiaoniao/util"
  9. )
  10. type ProductivityService struct {
  11. }
  12. func (srv *ProductivityService) Add(pitem *entity.ProductivityInfo) (interface{}, error) {
  13. if pitem.Id != 0 {
  14. return nil, errors.New("productivity info already exists")
  15. }
  16. strSql, kv := util.GenerateInsertSqlFromStruct("t_producer_info", pitem)
  17. db := util.GetWriteSqlDB()
  18. sqlResult, err := db.NamedExec(strSql, kv)
  19. if err != nil {
  20. return nil, err
  21. }
  22. if pid, err := sqlResult.LastInsertId(); err != nil {
  23. return nil, err
  24. } else {
  25. pitem.Id = int(pid)
  26. return pitem, nil
  27. }
  28. }
  29. func (srv *ProductivityService) Update(pitem *entity.ProductivityInfo) (interface{}, error) {
  30. if pitem.Id <= 0 {
  31. return nil, errors.New("can not find record")
  32. }
  33. strSql, kv := util.GenerateUpdateSqlFromStruct("t_producer_info", pitem, fmt.Sprintf("where id =%d", pitem.Id))
  34. db := util.GetWriteSqlDB()
  35. sqlResult, err := db.NamedExec(strSql, kv)
  36. if err != nil {
  37. return nil, err
  38. }
  39. if ra, err := sqlResult.RowsAffected(); err != nil {
  40. return nil, err
  41. } else if ra <= 0 {
  42. return nil, errors.New("update failed, can not find record ")
  43. }
  44. return pitem, nil
  45. }
  46. type producerInfo struct {
  47. GId int `db:"id"`
  48. GNum int `db:"gnum"`
  49. RemainNum null.Int `db:"remain_num"`
  50. PDate null.String `db:"pdate"`
  51. TimeRange null.String `db:"time_range"`
  52. }
  53. const QUERY_DAYS = 30 //todo config
  54. //var TIME_RANGES = []string{
  55. // "08:30-09:30",
  56. // "09:30-10:30",
  57. // "10:30-11:30",
  58. // // "11:30-12:30",
  59. // "12:30-13:30",
  60. // "13:30-14:30",
  61. // "14:30-15:30",
  62. // "15:30-16:30",
  63. //}
  64. //20210123 永久修改产能为上午下午
  65. //20210913 改为早8点
  66. //var TIME_RANGES = []string{
  67. // "08:00-12:00",
  68. // "13:00-16:00",
  69. //}
  70. //20220120 万万没想到,又改了
  71. var TIME_RANGES = []string{
  72. "08:00-10:00",
  73. "10:00-15:00",
  74. }
  75. //20230420 是啊,再一次修改
  76. var TIME_RANGES_KONGFU = map[int][]string{
  77. 100:{"08:00-09:00","09:00-10:00",},
  78. 200:{"08:00-09:00","09:00-10:00","10:00-11:00","11:00-12:00",},
  79. 300:{"08:00-12:00","13:00-15:00",},
  80. }
  81. var TIME_RANGES_ALIAS = []string{
  82. "上午",
  83. "下午",
  84. }
  85. type ptValue struct {
  86. TimeRange string `json:"timeRange"`
  87. RemainNum int64 `json:"remainNum"`
  88. }
  89. type ptValues []*ptValue
  90. func (srv *ProductivityService) PtList(cityId int, fromDay string, ptType int, kongFuType int ) (interface{}, error) {
  91. var qDays = QUERY_DAYS
  92. now, err := time.Parse("2006-01-02", fromDay)
  93. if err != nil {
  94. now = time.Now()
  95. if now.Hour() > 15 {
  96. now = now.Add(time.Hour * 48)
  97. } else {
  98. now = now.Add(time.Hour * 24)
  99. }
  100. }
  101. today := now.Format("2006-01-02")
  102. var timeRanges []string
  103. var bl bool
  104. timeRanges, bl = TIME_RANGES_KONGFU[kongFuType]
  105. if !bl {
  106. timeRanges = TIME_RANGES_KONGFU[100]
  107. }
  108. if ptType == 1 {
  109. timeRanges = TIME_RANGES_ALIAS
  110. } else if ptType == 2 {
  111. //timeRanges = []string{
  112. // //"9:00-12:00",
  113. // //"14:00-17:00",
  114. // "09:00-12:00",
  115. // "13:00-16:00",
  116. //}
  117. qDays = 6
  118. }
  119. strSql := "select t1.id, t1.gnum, t2.remain_num, t2.pdate, t2.time_range from (select id, num as gnum from t_producer_config where city_id = ? and type = ?) t1 left join (select * from t_producer_info where pdate > ?) t2 on t1.id = t2.global_id;"
  120. result := []producerInfo{}
  121. db := util.GetSqlDB()
  122. err = db.Select(&result, strSql, cityId, ptType, today)
  123. if err != nil {
  124. return nil, err
  125. }
  126. if len(result) == 0 {
  127. return nil, errors.New(fmt.Sprintf("1::have not init config for city: %d", cityId))
  128. }
  129. plist := []map[string]interface{}{}
  130. tempKV := map[string]interface{}{}
  131. invalidDays := map[string]interface{}{}
  132. if len(result) > 0 {
  133. for _, pinfo := range result {
  134. if pinfo.TimeRange.String == "*" {
  135. invalidDays[pinfo.PDate.String] = struct{}{}
  136. continue
  137. }
  138. tempKV[pinfo.PDate.String+pinfo.TimeRange.String] = map[string]interface{}{
  139. "pdate": pinfo.PDate.String,
  140. "timeRange": pinfo.TimeRange.String,
  141. "remainNum": pinfo.RemainNum.Int64,
  142. }
  143. }
  144. }
  145. gnum := result[0].GNum
  146. for i := 1; i <= qDays; i++ {
  147. d := time.Duration(int64(time.Hour) * int64(24*i))
  148. tdate := now.Add(d).Format("2006-01-02")
  149. if _, ok := invalidDays[tdate]; ok {
  150. plist = append(plist, map[string]interface{}{
  151. "pdate": tdate,
  152. "timeRange": "*",
  153. "remainNum": int64(0),
  154. })
  155. continue
  156. }
  157. for _, tr := range timeRanges {
  158. if v, ok := tempKV[tdate+tr]; ok {
  159. plist = append(plist, v.(map[string]interface{}))
  160. continue
  161. }
  162. plist = append(plist, map[string]interface{}{
  163. "pdate": tdate,
  164. "timeRange": tr,
  165. "remainNum": int64(gnum),
  166. })
  167. }
  168. }
  169. var resultList = map[string]ptValues{}
  170. var (
  171. pdate string
  172. )
  173. for _, pi := range plist {
  174. val := &ptValue{
  175. TimeRange: pi["timeRange"].(string),
  176. RemainNum: pi["remainNum"].(int64),
  177. }
  178. pdate = pi["pdate"].(string)
  179. resultList[pdate] = append(resultList[pdate], val)
  180. }
  181. return map[string]interface{}{
  182. "pt_gid": result[0].GId,
  183. "pt_list": resultList,
  184. }, nil
  185. }