order.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. package manager
  2. import (
  3. "reflect"
  4. "strings"
  5. "zhiyuan/pkg/app"
  6. orderParam "zhiyuan/pkg/param/order"
  7. "zhiyuan/pkg/utils"
  8. "zhiyuan/services/admin"
  9. "zhiyuan/services/aftersale"
  10. "zhiyuan/services/aftersale/order"
  11. "zhiyuan/services/form"
  12. "zhiyuan/services/region"
  13. "github.com/gin-gonic/gin"
  14. )
  15. func OrderList(c *gin.Context) {
  16. page := app.HandlePageNum(c)
  17. where := map[string]interface{}{"deleted_at": 0, "_orderby": "id desc"}
  18. leaderIds, _ := admin.GetAdminByDataAuth(c.GetInt("adminID"))
  19. if !reflect.DeepEqual(leaderIds, []int{-1}) {
  20. where["leader in"] = leaderIds
  21. }
  22. states := make([]int, 0)
  23. tabIndex := utils.ToInt(c.Query("tab_index"))
  24. switch tabIndex {
  25. case -1:
  26. states = append(states,
  27. orderParam.State.Allotted.ID,
  28. orderParam.State.SupConfirmed.ID,
  29. orderParam.State.Repairing.ID,
  30. )
  31. case 0:
  32. states = append(states, orderParam.State.Allotted.ID)
  33. case 1:
  34. states = append(states,
  35. orderParam.State.Suspending.ID,
  36. orderParam.State.SupConfirmed.ID,
  37. orderParam.State.Repairing.ID,
  38. orderParam.State.Confirmed.ID,
  39. )
  40. case 2:
  41. states = append(states,
  42. orderParam.State.Repaired.ID,
  43. orderParam.State.Completed.ID,
  44. orderParam.State.ForceCompleted.ID,
  45. )
  46. }
  47. where["if(state=90,90,if(is_force=0,state,100)) in"] = states
  48. keyword := c.Query("keyword")
  49. if keyword != "" {
  50. where["_or"] = []map[string]interface{}{
  51. {"link_name like": "%" + keyword + "%"},
  52. {"link_phone like": "%" + keyword + "%"},
  53. }
  54. }
  55. type Admin struct {
  56. ID int `json:"id"`
  57. UserName string `json:"username"`
  58. Phone string `json:"phone"`
  59. }
  60. type OrderList struct {
  61. ID int `json:"id"`
  62. OrderNo string `json:"order_no"`
  63. MainType int `json:"main_type"`
  64. SubType int `json:"sub_type"`
  65. Type string `json:"type"`
  66. LinkName string `json:"link_name"`
  67. LinkPhone string `json:"link_phone"`
  68. Address string `json:"address"`
  69. Content string `json:"content"`
  70. Pics []string `json:"pics"`
  71. RepairID int `json:"repair_id"`
  72. State int `json:"state"`
  73. StateName string `json:"state_name"`
  74. StateColor string `json:"state_color"`
  75. CreatedAt string `json:"created_at"`
  76. WarrantyStart string `json:"warranty_start"`
  77. WarrantyEnd string `json:"warranty_end"`
  78. WarrantyPeriod string `json:"warranty_period"`
  79. Leader int `json:"leader"`
  80. LeaderInfo *Admin `json:"leader_info"`
  81. IsForce int `json:"is_force"`
  82. }
  83. orderList := make([]OrderList, 0)
  84. _, err := order.GetList(where, nil, page, &orderList)
  85. if err != nil {
  86. app.Error(c, err.Error())
  87. return
  88. }
  89. orderIds := make([]int, 0)
  90. adminIds := make([]int, 0)
  91. for _, v := range orderList {
  92. orderIds = append(orderIds, v.ID)
  93. if v.Leader > 0 {
  94. adminIds = append(adminIds, v.Leader)
  95. }
  96. }
  97. // 订单详细信息
  98. type OrderDetailList struct {
  99. OrderID int `json:"order_id"`
  100. Province int `json:"province"`
  101. City int `json:"city"`
  102. Region int `json:"region"`
  103. Address string `json:"address"`
  104. Content string `json:"content"`
  105. Pics string `json:"pics"`
  106. }
  107. orderDetailList := make([]OrderDetailList, 0)
  108. detailMap := make(map[int]OrderDetailList, 0)
  109. regionCodes := make([]int, 0)
  110. if len(orderIds) > 0 {
  111. _, err = order.GetDetailList(map[string]interface{}{"order_id in ": orderIds}, nil, app.Page{}, &orderDetailList)
  112. for _, v := range orderDetailList {
  113. detailMap[v.OrderID] = v
  114. regionCodes = append(regionCodes, v.Region, v.City, v.Province)
  115. }
  116. }
  117. regionMap, err := region.GetNameByCodes(regionCodes)
  118. if err != nil {
  119. app.Error(c, err.Error())
  120. return
  121. }
  122. adminMap := make(map[int]*Admin, 0)
  123. if len(adminIds) > 0 {
  124. adminList := make([]*Admin, 0)
  125. if _, err := admin.GetAdmins(map[string]interface{}{"id in": adminIds}, []string{"id, username, phone"}, app.Page{}, &adminList); err == nil {
  126. for _, v := range adminList {
  127. adminMap[v.ID] = v
  128. }
  129. }
  130. }
  131. orderTypeMap := aftersale.GetTypeMapByCache()
  132. orderStatusList := utils.ParseSliceMap(orderParam.Params.State, "id")
  133. for k, v := range orderList {
  134. if detail, ok := detailMap[v.ID]; ok {
  135. v.Content = detail.Content
  136. v.Pics = utils.ParseImgStr(detail.Pics)
  137. v.Address = region.GetFullAddressByCodes(detail.Province, detail.City, detail.Region, detail.Address, "", regionMap)
  138. }
  139. if v.State != orderParam.State.Completed.ID && v.IsForce != 0 {
  140. v.State = orderParam.State.ForceCompleted.ID
  141. }
  142. v.StateName = utils.ToStr(orderStatusList[utils.ToStr(v.State)]["name"])
  143. v.StateColor = utils.ToStr(orderStatusList[utils.ToStr(v.State)]["color"])
  144. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD")
  145. v.Type = orderTypeMap[v.MainType] + "/" + orderTypeMap[v.SubType]
  146. if v.WarrantyStart != "0" {
  147. v.WarrantyPeriod = utils.DateS(v.WarrantyStart, "YYYY-MM-DD") + " 至 " + utils.DateS(v.WarrantyEnd, "YYYY-MM-DD")
  148. } else {
  149. v.WarrantyPeriod = "保修期外"
  150. }
  151. if v.Leader > 0 && adminMap[v.Leader] != nil {
  152. v.LeaderInfo = adminMap[v.Leader]
  153. } else {
  154. v.LeaderInfo = &Admin{}
  155. }
  156. orderList[k] = v
  157. }
  158. app.Success(c, orderList)
  159. }
  160. func OrderConfirm(c *gin.Context) {
  161. id := utils.StrTo(c.Param("id")).MustInt()
  162. if id <= 0 {
  163. app.Error(c, "工单 id 有误")
  164. return
  165. }
  166. err := order.SupConfirm(id, c.GetInt("adminID"))
  167. if err != nil {
  168. app.Error(c, err.Error())
  169. return
  170. }
  171. app.Success(c, nil)
  172. }
  173. func OrderFinish(c *gin.Context) {
  174. id := utils.StrTo(c.Param("id")).MustInt()
  175. if id <= 0 {
  176. app.Error(c, "工单 id 有误")
  177. return
  178. }
  179. err := order.Repaired(id, c.GetInt("adminID"))
  180. if err != nil {
  181. app.Error(c, err.Error())
  182. return
  183. }
  184. app.Success(c, nil)
  185. }
  186. func OrderSchedule(c *gin.Context) {
  187. id := utils.StrTo(c.Param("id")).MustInt()
  188. if id <= 0 {
  189. app.Error(c, "工单 id 有误")
  190. return
  191. }
  192. var form form.OrderSchedule
  193. if app.Bind(c, &form) != nil {
  194. return
  195. }
  196. form.Leader = c.GetInt("adminID")
  197. err := order.Schedule(form, id)
  198. if err != nil {
  199. app.Error(c, err.Error())
  200. return
  201. }
  202. app.Success(c, nil)
  203. }
  204. func OrderRepair(c *gin.Context) {
  205. id := utils.StrTo(c.Param("id")).MustInt()
  206. if id <= 0 {
  207. app.Error(c, "工单 id 有误")
  208. return
  209. }
  210. var form form.OrderRepair
  211. if app.Bind(c, &form) != nil {
  212. return
  213. }
  214. form.Leader = c.GetInt("adminID")
  215. err := order.Repair(form, id)
  216. if err != nil {
  217. app.Error(c, err.Error())
  218. return
  219. }
  220. app.Success(c, nil)
  221. }
  222. func OrderRepairInfo(c *gin.Context) {
  223. id := utils.StrTo(c.Param("id")).MustInt()
  224. if id <= 0 {
  225. app.Error(c, "工单 id 有误")
  226. return
  227. }
  228. orderInfo, err := order.GetInfoByID(id, []string{"id", "repair_id"}, nil)
  229. if err != nil {
  230. app.Error(c, err.Error())
  231. return
  232. }
  233. if orderInfo.RepairID == 0 {
  234. app.Success(c, gin.H{"repair_id": 0})
  235. return
  236. }
  237. type RepairInfo struct {
  238. State string `json:"state"`
  239. Duration float64 `json:"duration"`
  240. WorkerName string `json:"worker_name"`
  241. WorkerPhone string `json:"worker_phone"`
  242. Content string `json:"content"`
  243. Pics string `json:"pics"`
  244. PicList []string `json:"pic_list"`
  245. FinishedAt string `json:"finished_at"`
  246. }
  247. var repairInfo *RepairInfo
  248. _, err = order.GetRepairInfoByID(orderInfo.RepairID, nil, &repairInfo)
  249. if err != nil || repairInfo == nil {
  250. app.Error(c, "获取反馈信息失败")
  251. return
  252. }
  253. repairInfo.PicList = strings.Split(repairInfo.Pics, ",")
  254. repairInfo.FinishedAt = utils.DateS(repairInfo.FinishedAt, "YYYY-MM-DD HH:mm")
  255. app.Success(c, gin.H{"repair_id": orderInfo.RepairID, "repair_info": repairInfo})
  256. return
  257. }
  258. func OrderInfo(c *gin.Context) {
  259. id := utils.StrTo(c.Param("id")).MustInt()
  260. if id <= 0 {
  261. app.Error(c, "工单 id 有误")
  262. return
  263. }
  264. type Admin struct {
  265. ID int `json:"id"`
  266. UserName string `json:"username"`
  267. Phone string `json:"phone"`
  268. }
  269. type OrderInfo struct {
  270. ID int `json:"id"`
  271. OrderNo string `json:"order_no"`
  272. MainType int `json:"main_type"`
  273. SubType int `json:"sub_type"`
  274. Type string `json:"type"`
  275. LinkName string `json:"link_name"`
  276. LinkPhone string `json:"link_phone"`
  277. Province int `json:"province"`
  278. City int `json:"city"`
  279. Region int `json:"region"`
  280. Address string `json:"address"`
  281. Content string `json:"content"`
  282. Pics string `json:"pics"`
  283. PicList []string `json:"pic_list"`
  284. State int `json:"state"`
  285. StateName string `json:"state_name"`
  286. StateColor string `json:"state_color"`
  287. CreatedAt string `json:"created_at"`
  288. Leader int `json:"leader"`
  289. LeaderInfo *Admin `json:"leader_info"`
  290. RecentVisitTime string `json:"recent_visit_time"`
  291. WarrantyStart string `json:"warranty_start"`
  292. WarrantyEnd string `json:"warranty_end"`
  293. WarrantyPeriod string `json:"warranty_period"`
  294. }
  295. var orderInfo OrderInfo
  296. err := order.GetOneWithDetail("o.id={{id}}", map[string]interface{}{"id": id}, &orderInfo)
  297. if err != nil {
  298. app.Error(c, err.Error())
  299. return
  300. }
  301. adminIds := make([]int, 0)
  302. if orderInfo.Leader > 0 {
  303. adminIds = append(adminIds, orderInfo.Leader)
  304. }
  305. adminMap := make(map[int]*Admin, 0)
  306. if len(adminIds) > 0 {
  307. adminList := make([]*Admin, 0)
  308. if _, err := admin.GetAdmins(map[string]interface{}{"id in": adminIds}, []string{"id, username, phone"}, app.Page{}, &adminList); err == nil {
  309. for _, v := range adminList {
  310. adminMap[v.ID] = v
  311. }
  312. }
  313. }
  314. if orderInfo.Leader > 0 && adminMap[orderInfo.Leader] != nil {
  315. orderInfo.LeaderInfo = adminMap[orderInfo.Leader]
  316. } else {
  317. orderInfo.LeaderInfo = &Admin{}
  318. }
  319. orderTypeMap := aftersale.GetTypeMapByCache()
  320. orderStatusList := utils.ParseSliceMap(orderParam.Params.State, "id")
  321. orderInfo.PicList = utils.ParseImgStr(orderInfo.Pics)
  322. orderInfo.Address = region.GetFullAddressByCodes(orderInfo.Province, orderInfo.City, orderInfo.Region, orderInfo.Address, "", nil)
  323. orderInfo.StateName = utils.ToStr(orderStatusList[utils.ToStr(orderInfo.State)]["name"])
  324. orderInfo.StateColor = utils.ToStr(orderStatusList[utils.ToStr(orderInfo.State)]["color"])
  325. orderInfo.CreatedAt = utils.DateS(orderInfo.CreatedAt, "YYYY-MM-DD")
  326. orderInfo.Type = orderTypeMap[orderInfo.MainType] + "/" + orderTypeMap[orderInfo.SubType]
  327. orderInfo.RecentVisitTime = utils.DateS(orderInfo.RecentVisitTime, "YYYY-MM-DD")
  328. if orderInfo.WarrantyStart != "0" {
  329. orderInfo.WarrantyPeriod = utils.DateS(orderInfo.WarrantyStart, "YYYY-MM-DD") + " 至 " + utils.DateS(orderInfo.WarrantyEnd, "YYYY-MM-DD")
  330. } else {
  331. orderInfo.WarrantyPeriod = "保修期外"
  332. }
  333. // 订单事件
  334. type Event struct {
  335. Title string `json:"title"`
  336. Content string `json:"content"`
  337. Pics string `json:"pics"`
  338. PicList []string `json:"pic_list"`
  339. CreatedAt string `json:"created_at"`
  340. }
  341. eventList := make([]*Event, 0)
  342. if _, err = order.GetEventList(map[string]interface{}{"order_id": id, "_orderby": "created_at desc", "event_type": 1}, nil, &eventList); err != nil {
  343. app.Error(c, err.Error())
  344. return
  345. }
  346. for k, v := range eventList {
  347. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD hh:mm")
  348. v.PicList = utils.ParseImgStr(v.Pics)
  349. eventList[k] = v
  350. }
  351. // 维修信息
  352. type Repair struct {
  353. ID int `json:"id"`
  354. WorkerName string `json:"worker_name"`
  355. WorkerPhone string `json:"worker_phone"`
  356. ScheduleTime string `json:"schedule_time"`
  357. FinishedAt string `json:"finished_at"`
  358. Content string `json:"content"`
  359. Duration float64 `json:"duration"`
  360. Pics string `json:"pics"`
  361. PicList []string `json:"pic_list"`
  362. State int `json:"state"`
  363. StateName string `json:"state_name"`
  364. CreatedAt string `json:"created_at"`
  365. }
  366. repairList := make([]*Repair, 0)
  367. if _, err = order.GetRepairList(map[string]interface{}{"order_id": id, "_orderby": "created_at desc"}, nil, &repairList); err != nil {
  368. app.Error(c, err.Error())
  369. return
  370. }
  371. repairStatusList := utils.ParseSliceMap(orderParam.Params.RepairState, "id")
  372. for _, v := range repairList {
  373. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD hh:mm")
  374. v.ScheduleTime = utils.DateS(v.ScheduleTime, "YYYY-MM-DD hh:mm")
  375. v.FinishedAt = utils.DateS(v.FinishedAt, "YYYY-MM-DD hh:mm")
  376. v.PicList = utils.ParseImgStr(v.Pics)
  377. v.StateName = utils.ToStr(repairStatusList[utils.ToStr(v.State)]["name"])
  378. }
  379. res := gin.H{
  380. "info": orderInfo,
  381. "repair": repairList,
  382. "event": eventList,
  383. }
  384. app.Success(c, res)
  385. }