order.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package customer
  2. import (
  3. "zhiyuan/pkg/app"
  4. orderParam "zhiyuan/pkg/param/order"
  5. "zhiyuan/pkg/utils"
  6. "zhiyuan/services/admin"
  7. "zhiyuan/services/aftersale"
  8. "zhiyuan/services/aftersale/order"
  9. "zhiyuan/services/form"
  10. "zhiyuan/services/region"
  11. "github.com/gin-gonic/gin"
  12. )
  13. func OrderAdd(c *gin.Context) {
  14. var form form.OrderAdd
  15. if app.Bind(c, &form) != nil {
  16. return
  17. }
  18. form.UserID = c.GetInt("userID")
  19. form.Typ = 1
  20. id, err := order.Add(form)
  21. if err != nil {
  22. app.ErrorMsg(c, err.Error(), nil)
  23. return
  24. }
  25. app.Success(c, gin.H{"id": id})
  26. }
  27. func OrderList(c *gin.Context) {
  28. page := app.HandlePageNum(c)
  29. where := map[string]interface{}{"user_id": c.GetInt("userID"), "deleted_at": 0, "_orderby": "id desc"}
  30. states := make([]int, 0)
  31. tabIndex := utils.ToInt(c.Query("tab_index"))
  32. switch tabIndex {
  33. case 0:
  34. states = []int{
  35. orderParam.State.Revoked.ID,
  36. orderParam.State.Unfinished.ID,
  37. orderParam.State.Created.ID,
  38. orderParam.State.Checked.ID,
  39. orderParam.State.Allotted.ID,
  40. orderParam.State.SupConfirmed.ID,
  41. orderParam.State.Suspending.ID,
  42. }
  43. case 1:
  44. states = append(states,
  45. orderParam.State.Repairing.ID,
  46. )
  47. case 2:
  48. states = append(states,
  49. orderParam.State.Confirmed.ID,
  50. orderParam.State.Repaired.ID,
  51. orderParam.State.Completed.ID,
  52. )
  53. }
  54. where["state in"] = states
  55. type Auth struct {
  56. Revoke bool `json:"revoke"`
  57. Confirm bool `json:"confirm"`
  58. Comment bool `json:"comment"`
  59. }
  60. type Admin struct {
  61. ID int `json:"id"`
  62. UserName string `json:"username"`
  63. Phone string `json:"phone"`
  64. }
  65. type OrderList struct {
  66. ID int `json:"id"`
  67. OrderNo string `json:"order_no"`
  68. MainType int `json:"main_type"`
  69. SubType int `json:"sub_type"`
  70. Type string `json:"type"`
  71. LinkName string `json:"link_name"`
  72. LinkPhone string `json:"link_phone"`
  73. Address string `json:"address"`
  74. Content string `json:"content"`
  75. Pics []string `json:"pics"`
  76. CommentID int `json:"comment_id"`
  77. State int `json:"state"`
  78. StateName string `json:"state_name"`
  79. StateColor string `json:"state_color"`
  80. Auth Auth `json:"auth"`
  81. CreatedAt string `json:"created_at"`
  82. Leader int `json:"leader"`
  83. LeaderInfo *Admin `json:"leader_info"`
  84. }
  85. orderList := make([]OrderList, 0)
  86. if _, err := order.GetList(where, nil, page, &orderList); err != nil {
  87. app.Error(c, err.Error())
  88. return
  89. }
  90. orderIds := make([]int, 0)
  91. adminIds := make([]int, 0)
  92. for _, v := range orderList {
  93. orderIds = append(orderIds, v.ID)
  94. if v.Leader > 0 {
  95. adminIds = append(adminIds, v.Leader)
  96. }
  97. }
  98. // 订单详细信息
  99. type OrderDetailList struct {
  100. OrderID int `json:"order_id"`
  101. Province int `json:"province"`
  102. City int `json:"city"`
  103. Region int `json:"region"`
  104. Address string `json:"address"`
  105. Content string `json:"content"`
  106. Pics string `json:"pics"`
  107. }
  108. orderDetailList := make([]OrderDetailList, 0)
  109. detailMap := make(map[int]OrderDetailList, 0)
  110. regionCodes := make([]int, 0)
  111. if len(orderIds) > 0 {
  112. order.GetDetailList(map[string]interface{}{"order_id in ": orderIds}, nil, app.Page{}, &orderDetailList)
  113. for _, v := range orderDetailList {
  114. detailMap[v.OrderID] = v
  115. regionCodes = append(regionCodes, v.Region, v.City, v.Province)
  116. }
  117. }
  118. regionMap, err := region.GetNameByCodes(regionCodes)
  119. if err != nil {
  120. app.Error(c, err.Error())
  121. return
  122. }
  123. orderTypeMap := aftersale.GetTypeMapByCache()
  124. orderStatusList := utils.ParseSliceMap(orderParam.Params.State, "id")
  125. adminMap := make(map[int]*Admin, 0)
  126. if len(adminIds) > 0 {
  127. adminList := make([]*Admin, 0)
  128. if _, err := admin.GetAdmins(map[string]interface{}{"id in": adminIds}, []string{"id, username, phone"}, app.Page{}, &adminList); err == nil {
  129. for _, v := range adminList {
  130. adminMap[v.ID] = v
  131. }
  132. }
  133. }
  134. for k, v := range orderList {
  135. if detail, ok := detailMap[v.ID]; ok {
  136. v.Content = detail.Content
  137. v.Pics = utils.ParseImgStr(detail.Pics)
  138. v.Address = region.GetFullAddressByCodes(detail.Province, detail.City, detail.Region, detail.Address, "", regionMap)
  139. }
  140. v.StateName = utils.ToStr(orderStatusList[utils.ToStr(v.State)]["customer_name"])
  141. v.StateColor = utils.ToStr(orderStatusList[utils.ToStr(v.State)]["color"])
  142. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD")
  143. v.Type = orderTypeMap[v.MainType] + "/" + orderTypeMap[v.SubType]
  144. v.Auth = Auth{
  145. Revoke: utils.IsContain(orderParam.Allow.Revoke, v.State),
  146. Confirm: utils.IsContain(orderParam.Allow.Confirm, v.State),
  147. }
  148. if v.Leader > 0 && adminMap[v.Leader] != nil {
  149. v.LeaderInfo = adminMap[v.Leader]
  150. } else {
  151. v.LeaderInfo = &Admin{}
  152. }
  153. orderList[k] = v
  154. }
  155. app.Success(c, orderList)
  156. }
  157. func OrderRevoke(c *gin.Context) {
  158. id := utils.StrTo(c.Param("id")).MustInt()
  159. if id <= 0 {
  160. app.Error(c, "工单 id 有误")
  161. return
  162. }
  163. var form form.OrderRevoke
  164. if app.Bind(c, &form) != nil {
  165. return
  166. }
  167. form.UserID = c.GetInt("userID")
  168. err := order.Revoke(form, id)
  169. if err != nil {
  170. app.Error(c, err.Error())
  171. return
  172. }
  173. app.Success(c, nil)
  174. }
  175. func OrderComment(c *gin.Context) {
  176. id := utils.StrTo(c.Param("id")).MustInt()
  177. if id <= 0 {
  178. app.Error(c, "工单 id 有误")
  179. return
  180. }
  181. var form form.OrderComment
  182. if app.Bind(c, &form) != nil {
  183. return
  184. }
  185. form.UserID = c.GetInt("userID")
  186. err := order.Comment(form, id)
  187. if err != nil {
  188. app.Error(c, err.Error())
  189. return
  190. }
  191. app.Success(c, nil)
  192. }
  193. func OrderConfirm(c *gin.Context) {
  194. id := utils.StrTo(c.Param("id")).MustInt()
  195. if id <= 0 {
  196. app.Error(c, "工单 id 有误")
  197. return
  198. }
  199. err := order.Confirm(id, c.GetInt("userID"))
  200. if err != nil {
  201. app.Error(c, err.Error())
  202. return
  203. }
  204. app.Success(c, nil)
  205. }
  206. func OrderInfo(c *gin.Context) {
  207. id := utils.StrTo(c.Param("id")).MustInt()
  208. if id <= 0 {
  209. app.Error(c, "工单 id 有误")
  210. return
  211. }
  212. type Admin struct {
  213. ID int `json:"id"`
  214. UserName string `json:"username"`
  215. Phone string `json:"phone"`
  216. }
  217. type OrderInfo struct {
  218. ID int `json:"id"`
  219. OrderNo string `json:"order_no"`
  220. MainType int `json:"main_type"`
  221. SubType int `json:"sub_type"`
  222. Type string `json:"type"`
  223. LinkName string `json:"link_name"`
  224. LinkPhone string `json:"link_phone"`
  225. Province int `json:"province"`
  226. City int `json:"city"`
  227. Region int `json:"region"`
  228. Address string `json:"address"`
  229. Content string `json:"content"`
  230. Pics string `json:"pics"`
  231. PicList []string `json:"pic_list"`
  232. State int `json:"state"`
  233. StateName string `json:"state_name"`
  234. StateColor string `json:"state_color"`
  235. CreatedAt string `json:"created_at"`
  236. Leader int `json:"leader"`
  237. LeaderInfo *Admin `json:"leader_info"`
  238. }
  239. var orderInfo OrderInfo
  240. err := order.GetOneWithDetail("o.id={{id}}", map[string]interface{}{"id": id}, &orderInfo)
  241. if err != nil {
  242. app.Error(c, err.Error())
  243. return
  244. }
  245. adminIds := make([]int, 0)
  246. if orderInfo.Leader > 0 {
  247. adminIds = append(adminIds, orderInfo.Leader)
  248. }
  249. adminMap := make(map[int]*Admin, 0)
  250. if len(adminIds) > 0 {
  251. adminList := make([]*Admin, 0)
  252. if _, err := admin.GetAdmins(map[string]interface{}{"id in": adminIds}, []string{"id, username, phone"}, app.Page{}, &adminList); err == nil {
  253. for _, v := range adminList {
  254. adminMap[v.ID] = v
  255. }
  256. }
  257. }
  258. if orderInfo.Leader > 0 && adminMap[orderInfo.Leader] != nil {
  259. orderInfo.LeaderInfo = adminMap[orderInfo.Leader]
  260. } else {
  261. orderInfo.LeaderInfo = &Admin{}
  262. }
  263. orderTypeMap := aftersale.GetTypeMapByCache()
  264. orderStatusList := utils.ParseSliceMap(orderParam.Params.State, "id")
  265. orderInfo.PicList = utils.ParseImgStr(orderInfo.Pics)
  266. orderInfo.Address = region.GetFullAddressByCodes(orderInfo.Province, orderInfo.City, orderInfo.Region, orderInfo.Address, "", nil)
  267. orderInfo.StateName = utils.ToStr(orderStatusList[utils.ToStr(orderInfo.State)]["name"])
  268. orderInfo.StateColor = utils.ToStr(orderStatusList[utils.ToStr(orderInfo.State)]["color"])
  269. orderInfo.CreatedAt = utils.DateS(orderInfo.CreatedAt, "YYYY-MM-DD")
  270. orderInfo.Type = orderTypeMap[orderInfo.MainType] + "/" + orderTypeMap[orderInfo.SubType]
  271. type OrderEvent struct {
  272. Title string `json:"title"`
  273. Content string `json:"content"`
  274. Pics string `json:"pics"`
  275. PicList []string `json:"pic_list"`
  276. CreatedAt string `json:"created_at"`
  277. }
  278. eventList := make([]*OrderEvent, 0)
  279. _, err = order.GetEventList(map[string]interface{}{"order_id": id, "_orderby": "created_at desc", "event_type": 1}, nil, &eventList)
  280. if err != nil {
  281. app.Error(c, err.Error())
  282. return
  283. }
  284. for k, v := range eventList {
  285. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD hh:mm")
  286. v.PicList = utils.ParseImgStr(v.Pics)
  287. eventList[k] = v
  288. }
  289. res := gin.H{
  290. "info": orderInfo,
  291. "event": eventList,
  292. }
  293. app.Success(c, res)
  294. }