123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- package manager
- import (
- "reflect"
- "strings"
- "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 OrderList(c *gin.Context) {
- page := app.HandlePageNum(c)
- where := map[string]interface{}{"deleted_at": 0, "_orderby": "id desc"}
- leaderIds, _ := admin.GetAdminByDataAuth(c.GetInt("adminID"))
- if !reflect.DeepEqual(leaderIds, []int{-1}) {
- where["leader in"] = leaderIds
- }
- states := make([]int, 0)
- tabIndex := utils.ToInt(c.Query("tab_index"))
- switch tabIndex {
- case -1:
- states = append(states,
- orderParam.State.Allotted.ID,
- orderParam.State.SupConfirmed.ID,
- orderParam.State.Repairing.ID,
- )
- case 0:
- states = append(states, orderParam.State.Allotted.ID)
- case 1:
- states = append(states,
- orderParam.State.Suspending.ID,
- orderParam.State.SupConfirmed.ID,
- orderParam.State.Repairing.ID,
- orderParam.State.Confirmed.ID,
- )
- case 2:
- states = append(states,
- orderParam.State.Repaired.ID,
- orderParam.State.Completed.ID,
- orderParam.State.ForceCompleted.ID,
- )
- }
- where["if(state=90,90,if(is_force=0,state,100)) in"] = states
- keyword := c.Query("keyword")
- if keyword != "" {
- where["_or"] = []map[string]interface{}{
- {"link_name like": "%" + keyword + "%"},
- {"link_phone like": "%" + keyword + "%"},
- }
- }
- 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"`
- RepairID int `json:"repair_id"`
- State int `json:"state"`
- StateName string `json:"state_name"`
- StateColor string `json:"state_color"`
- CreatedAt string `json:"created_at"`
- WarrantyStart string `json:"warranty_start"`
- WarrantyEnd string `json:"warranty_end"`
- WarrantyPeriod string `json:"warranty_period"`
- Leader int `json:"leader"`
- LeaderInfo *Admin `json:"leader_info"`
- IsForce int `json:"is_force"`
- }
- orderList := make([]OrderList, 0)
- _, err := order.GetList(where, nil, page, &orderList)
- if 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 {
- _, err = 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
- }
- 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
- }
- }
- }
- orderTypeMap := aftersale.GetTypeMapByCache()
- orderStatusList := utils.ParseSliceMap(orderParam.Params.State, "id")
- 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)
- }
- if v.State != orderParam.State.Completed.ID && v.IsForce != 0 {
- v.State = orderParam.State.ForceCompleted.ID
- }
- v.StateName = utils.ToStr(orderStatusList[utils.ToStr(v.State)]["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]
- if v.WarrantyStart != "0" {
- v.WarrantyPeriod = utils.DateS(v.WarrantyStart, "YYYY-MM-DD") + " 至 " + utils.DateS(v.WarrantyEnd, "YYYY-MM-DD")
- } else {
- v.WarrantyPeriod = "保修期外"
- }
- 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 OrderConfirm(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.Error(c, "工单 id 有误")
- return
- }
- err := order.SupConfirm(id, c.GetInt("adminID"))
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, nil)
- }
- func OrderFinish(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.Error(c, "工单 id 有误")
- return
- }
- err := order.Repaired(id, c.GetInt("adminID"))
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, nil)
- }
- func OrderSchedule(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.Error(c, "工单 id 有误")
- return
- }
- var form form.OrderSchedule
- if app.Bind(c, &form) != nil {
- return
- }
- form.Leader = c.GetInt("adminID")
- err := order.Schedule(form, id)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, nil)
- }
- func OrderRepair(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.Error(c, "工单 id 有误")
- return
- }
- var form form.OrderRepair
- if app.Bind(c, &form) != nil {
- return
- }
- form.Leader = c.GetInt("adminID")
- err := order.Repair(form, id)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, nil)
- }
- func OrderRepairInfo(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.Error(c, "工单 id 有误")
- return
- }
- orderInfo, err := order.GetInfoByID(id, []string{"id", "repair_id"}, nil)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- if orderInfo.RepairID == 0 {
- app.Success(c, gin.H{"repair_id": 0})
- return
- }
- type RepairInfo struct {
- State string `json:"state"`
- Duration float64 `json:"duration"`
- WorkerName string `json:"worker_name"`
- WorkerPhone string `json:"worker_phone"`
- Content string `json:"content"`
- Pics string `json:"pics"`
- PicList []string `json:"pic_list"`
- FinishedAt string `json:"finished_at"`
- }
- var repairInfo *RepairInfo
- _, err = order.GetRepairInfoByID(orderInfo.RepairID, nil, &repairInfo)
- if err != nil || repairInfo == nil {
- app.Error(c, "获取反馈信息失败")
- return
- }
- repairInfo.PicList = strings.Split(repairInfo.Pics, ",")
- repairInfo.FinishedAt = utils.DateS(repairInfo.FinishedAt, "YYYY-MM-DD HH:mm")
- app.Success(c, gin.H{"repair_id": orderInfo.RepairID, "repair_info": repairInfo})
- return
- }
- 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"`
- RecentVisitTime string `json:"recent_visit_time"`
- WarrantyStart string `json:"warranty_start"`
- WarrantyEnd string `json:"warranty_end"`
- WarrantyPeriod string `json:"warranty_period"`
- }
- 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]
- orderInfo.RecentVisitTime = utils.DateS(orderInfo.RecentVisitTime, "YYYY-MM-DD")
- if orderInfo.WarrantyStart != "0" {
- orderInfo.WarrantyPeriod = utils.DateS(orderInfo.WarrantyStart, "YYYY-MM-DD") + " 至 " + utils.DateS(orderInfo.WarrantyEnd, "YYYY-MM-DD")
- } else {
- orderInfo.WarrantyPeriod = "保修期外"
- }
- // 订单事件
- type Event 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([]*Event, 0)
- if _, err = order.GetEventList(map[string]interface{}{"order_id": id, "_orderby": "created_at desc", "event_type": 1}, nil, &eventList); 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
- }
- // 维修信息
- type Repair struct {
- ID int `json:"id"`
- WorkerName string `json:"worker_name"`
- WorkerPhone string `json:"worker_phone"`
- ScheduleTime string `json:"schedule_time"`
- FinishedAt string `json:"finished_at"`
- Content string `json:"content"`
- Duration float64 `json:"duration"`
- Pics string `json:"pics"`
- PicList []string `json:"pic_list"`
- State int `json:"state"`
- StateName string `json:"state_name"`
- CreatedAt string `json:"created_at"`
- }
- repairList := make([]*Repair, 0)
- if _, err = order.GetRepairList(map[string]interface{}{"order_id": id, "_orderby": "created_at desc"}, nil, &repairList); err != nil {
- app.Error(c, err.Error())
- return
- }
- repairStatusList := utils.ParseSliceMap(orderParam.Params.RepairState, "id")
- for _, v := range repairList {
- v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD hh:mm")
- v.ScheduleTime = utils.DateS(v.ScheduleTime, "YYYY-MM-DD hh:mm")
- v.FinishedAt = utils.DateS(v.FinishedAt, "YYYY-MM-DD hh:mm")
- v.PicList = utils.ParseImgStr(v.Pics)
- v.StateName = utils.ToStr(repairStatusList[utils.ToStr(v.State)]["name"])
- }
- res := gin.H{
- "info": orderInfo,
- "repair": repairList,
- "event": eventList,
- }
- app.Success(c, res)
- }
|