package customer import ( "zhiyuan/pkg/app" orderParam "zhiyuan/pkg/param/order" "zhiyuan/pkg/utils" "zhiyuan/services/admin" "zhiyuan/services/aftersale" "zhiyuan/services/aftersale/order" "zhiyuan/services/form" "zhiyuan/services/region" "github.com/gin-gonic/gin" ) func OrderAdd(c *gin.Context) { var form form.OrderAdd if app.Bind(c, &form) != nil { return } form.UserID = c.GetInt("userID") form.Typ = 1 id, err := order.Add(form) if err != nil { app.ErrorMsg(c, err.Error(), nil) return } app.Success(c, gin.H{"id": id}) } func OrderList(c *gin.Context) { page := app.HandlePageNum(c) where := map[string]interface{}{"user_id": c.GetInt("userID"), "deleted_at": 0, "_orderby": "id desc"} states := make([]int, 0) tabIndex := utils.ToInt(c.Query("tab_index")) switch tabIndex { case 0: states = []int{ orderParam.State.Revoked.ID, orderParam.State.Unfinished.ID, orderParam.State.Created.ID, orderParam.State.Checked.ID, orderParam.State.Allotted.ID, orderParam.State.SupConfirmed.ID, orderParam.State.Suspending.ID, } case 1: states = append(states, orderParam.State.Repairing.ID, ) case 2: states = append(states, orderParam.State.Confirmed.ID, orderParam.State.Repaired.ID, orderParam.State.Completed.ID, ) } where["state in"] = states type Auth struct { Revoke bool `json:"revoke"` Confirm bool `json:"confirm"` Comment bool `json:"comment"` } type Admin struct { ID int `json:"id"` UserName string `json:"username"` Phone string `json:"phone"` } type OrderList struct { ID int `json:"id"` OrderNo string `json:"order_no"` MainType int `json:"main_type"` SubType int `json:"sub_type"` Type string `json:"type"` LinkName string `json:"link_name"` LinkPhone string `json:"link_phone"` Address string `json:"address"` Content string `json:"content"` Pics []string `json:"pics"` CommentID int `json:"comment_id"` State int `json:"state"` StateName string `json:"state_name"` StateColor string `json:"state_color"` Auth Auth `json:"auth"` CreatedAt string `json:"created_at"` Leader int `json:"leader"` LeaderInfo *Admin `json:"leader_info"` } orderList := make([]OrderList, 0) if _, err := order.GetList(where, nil, page, &orderList); err != nil { app.Error(c, err.Error()) return } orderIds := make([]int, 0) adminIds := make([]int, 0) for _, v := range orderList { orderIds = append(orderIds, v.ID) if v.Leader > 0 { adminIds = append(adminIds, v.Leader) } } // 订单详细信息 type OrderDetailList struct { OrderID int `json:"order_id"` Province int `json:"province"` City int `json:"city"` Region int `json:"region"` Address string `json:"address"` Content string `json:"content"` Pics string `json:"pics"` } orderDetailList := make([]OrderDetailList, 0) detailMap := make(map[int]OrderDetailList, 0) regionCodes := make([]int, 0) if len(orderIds) > 0 { order.GetDetailList(map[string]interface{}{"order_id in ": orderIds}, nil, app.Page{}, &orderDetailList) for _, v := range orderDetailList { detailMap[v.OrderID] = v regionCodes = append(regionCodes, v.Region, v.City, v.Province) } } regionMap, err := region.GetNameByCodes(regionCodes) if err != nil { app.Error(c, err.Error()) return } orderTypeMap := aftersale.GetTypeMapByCache() orderStatusList := utils.ParseSliceMap(orderParam.Params.State, "id") adminMap := make(map[int]*Admin, 0) if len(adminIds) > 0 { adminList := make([]*Admin, 0) if _, err := admin.GetAdmins(map[string]interface{}{"id in": adminIds}, []string{"id, username, phone"}, app.Page{}, &adminList); err == nil { for _, v := range adminList { adminMap[v.ID] = v } } } for k, v := range orderList { if detail, ok := detailMap[v.ID]; ok { v.Content = detail.Content v.Pics = utils.ParseImgStr(detail.Pics) v.Address = region.GetFullAddressByCodes(detail.Province, detail.City, detail.Region, detail.Address, "", regionMap) } v.StateName = utils.ToStr(orderStatusList[utils.ToStr(v.State)]["customer_name"]) v.StateColor = utils.ToStr(orderStatusList[utils.ToStr(v.State)]["color"]) v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD") v.Type = orderTypeMap[v.MainType] + "/" + orderTypeMap[v.SubType] v.Auth = Auth{ Revoke: utils.IsContain(orderParam.Allow.Revoke, v.State), Confirm: utils.IsContain(orderParam.Allow.Confirm, v.State), } if v.Leader > 0 && adminMap[v.Leader] != nil { v.LeaderInfo = adminMap[v.Leader] } else { v.LeaderInfo = &Admin{} } orderList[k] = v } app.Success(c, orderList) } func OrderRevoke(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.Error(c, "工单 id 有误") return } var form form.OrderRevoke if app.Bind(c, &form) != nil { return } form.UserID = c.GetInt("userID") err := order.Revoke(form, id) if err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) } func OrderComment(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.Error(c, "工单 id 有误") return } var form form.OrderComment if app.Bind(c, &form) != nil { return } form.UserID = c.GetInt("userID") err := order.Comment(form, id) if err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) } func OrderConfirm(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.Error(c, "工单 id 有误") return } err := order.Confirm(id, c.GetInt("userID")) if err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) } func OrderInfo(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.Error(c, "工单 id 有误") return } type Admin struct { ID int `json:"id"` UserName string `json:"username"` Phone string `json:"phone"` } type OrderInfo struct { ID int `json:"id"` OrderNo string `json:"order_no"` MainType int `json:"main_type"` SubType int `json:"sub_type"` Type string `json:"type"` LinkName string `json:"link_name"` LinkPhone string `json:"link_phone"` Province int `json:"province"` City int `json:"city"` Region int `json:"region"` Address string `json:"address"` Content string `json:"content"` Pics string `json:"pics"` PicList []string `json:"pic_list"` State int `json:"state"` StateName string `json:"state_name"` StateColor string `json:"state_color"` CreatedAt string `json:"created_at"` Leader int `json:"leader"` LeaderInfo *Admin `json:"leader_info"` } var orderInfo OrderInfo err := order.GetOneWithDetail("o.id={{id}}", map[string]interface{}{"id": id}, &orderInfo) if err != nil { app.Error(c, err.Error()) return } adminIds := make([]int, 0) if orderInfo.Leader > 0 { adminIds = append(adminIds, orderInfo.Leader) } adminMap := make(map[int]*Admin, 0) if len(adminIds) > 0 { adminList := make([]*Admin, 0) if _, err := admin.GetAdmins(map[string]interface{}{"id in": adminIds}, []string{"id, username, phone"}, app.Page{}, &adminList); err == nil { for _, v := range adminList { adminMap[v.ID] = v } } } if orderInfo.Leader > 0 && adminMap[orderInfo.Leader] != nil { orderInfo.LeaderInfo = adminMap[orderInfo.Leader] } else { orderInfo.LeaderInfo = &Admin{} } orderTypeMap := aftersale.GetTypeMapByCache() orderStatusList := utils.ParseSliceMap(orderParam.Params.State, "id") orderInfo.PicList = utils.ParseImgStr(orderInfo.Pics) orderInfo.Address = region.GetFullAddressByCodes(orderInfo.Province, orderInfo.City, orderInfo.Region, orderInfo.Address, "", nil) orderInfo.StateName = utils.ToStr(orderStatusList[utils.ToStr(orderInfo.State)]["name"]) orderInfo.StateColor = utils.ToStr(orderStatusList[utils.ToStr(orderInfo.State)]["color"]) orderInfo.CreatedAt = utils.DateS(orderInfo.CreatedAt, "YYYY-MM-DD") orderInfo.Type = orderTypeMap[orderInfo.MainType] + "/" + orderTypeMap[orderInfo.SubType] type OrderEvent struct { Title string `json:"title"` Content string `json:"content"` Pics string `json:"pics"` PicList []string `json:"pic_list"` CreatedAt string `json:"created_at"` } eventList := make([]*OrderEvent, 0) _, err = order.GetEventList(map[string]interface{}{"order_id": id, "_orderby": "created_at desc", "event_type": 1}, nil, &eventList) if err != nil { app.Error(c, err.Error()) return } for k, v := range eventList { v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD hh:mm") v.PicList = utils.ParseImgStr(v.Pics) eventList[k] = v } res := gin.H{ "info": orderInfo, "event": eventList, } app.Success(c, res) }