package leader import ( "strings" "time" "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" "zhiyuan/services/user" "github.com/gin-gonic/gin" ) func OrderList(c *gin.Context) { page := app.HandlePageNum(c) where := map[string]string{ "where": " o.deleted_at=0 AND o.leader={{admin_id}}", "_group_by": "o.id", "_order_by": "o.id desc", } param := make(map[string]interface{}) param["admin_id"] = c.GetInt("adminID") //where := map[string]interface{}{"leader": c.GetInt("adminID"), "deleted_at": 0, "_orderby": "id desc"} 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, ) } if tabIndex >= -1 { where["where"] = where["where"] + " AND if(o.state=90,90,if(o.is_force=0,o.state,100)) in {{states}}" param["states"] = states //where["if(state=90,90,if(is_force=0,state,100)) in"] = states } dayDate := utils.GetZeroTime(time.Now()).Unix() listType := utils.ToInt(c.Query("list_type")) if listType == 1 { where["where"] = where["where"] + " AND (o.created_at between {{mindate}} and {{maxdate}})" param["mindate"] = dayDate param["maxdate"] = dayDate + 86399 //where["created_at between"] = []interface{}{dayDate, dayDate + 86399} } else if listType == 2 { where["where"] = where["where"] + " AND (v.visit_time between {{mindate}} and {{maxdate}}) AND v.state=1" param["mindate"] = dayDate param["maxdate"] = dayDate + 86399 //where["recent_visit_time between"] = []interface{}{dayDate, dayDate + 86399} } keyword := c.Query("keyword") if keyword != "" { where["where"] = where["where"] + " AND (o.link_name like {{keyword}} or o.link_phone like {{keyword}})" param["keyword"] = "%" + keyword + "%" /*where["_or"] = []map[string]interface{}{ {"link_name like": "%" + keyword + "%"}, {"link_phone like": "%" + keyword + "%"}, }*/ } type Auth struct { SupConfirm bool `json:"sup_confirm"` Repairing bool `json:"repairing"` Repaired bool `json:"repaired"` Feedback bool `json:"feedback"` Confirm bool `json:"confirm"` } 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"` Auth Auth `json:"auth"` HouseID int `json:"house_id"` Supervisor string `json:"supervisor"` CreatedAt string `json:"created_at"` WarrantyStart string `json:"warranty_start"` WarrantyEnd string `json:"warranty_end"` WarrantyPeriod string `json:"warranty_period"` IsForce int `json:"is_force"` } orderList := make([]OrderList, 0) //_, err := order.GetList(where, nil, page, &orderList) err := order.GetListWidthVisit(where, param, page, &orderList) if err != nil { app.Error(c, err.Error()) return } orderIds := make([]int, 0) houseIds := make([]int, 0) for _, v := range orderList { orderIds = append(orderIds, v.ID) houseIds = append(houseIds, v.HouseID) } adminIds := make([]int, 0) houseSupervisorMap := make(map[int]int, 0) if len(houseIds) > 0 { houseList, _ := user.GetHouseList(map[string]interface{}{"id in": houseIds}, []string{"id", "supervisor"}, app.Page{}, nil) for _, v := range houseList { if v.Supervisor > 0 { adminIds = append(adminIds, v.Supervisor) } houseSupervisorMap[v.ID] = v.Supervisor } } adminMap := make(map[int]string, 0) if len(adminIds) > 0 { if adminList, err := admin.GetAdmins(map[string]interface{}{"id in": adminIds}, []string{"id, username, phone"}, app.Page{}, nil); err == nil { for _, v := range adminList { adminMap[v.ID] = v.Username } } } // 订单详细信息 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 } 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] v.Auth = Auth{ SupConfirm: utils.IsContain(orderParam.Allow.SupConfirm, v.State), Repairing: utils.IsContain(orderParam.Allow.Repairing, v.State), Feedback: utils.IsContain(orderParam.Allow.Feedback, v.State), Repaired: utils.IsContain(orderParam.Allow.Repaired, v.State) && v.RepairID > 0, } if supervisor, ok := adminMap[houseSupervisorMap[v.HouseID]]; ok { v.Supervisor = supervisor } if v.WarrantyStart != "0" { v.WarrantyPeriod = utils.DateS(v.WarrantyStart, "YYYY-MM-DD") + " 至 " + utils.DateS(v.WarrantyEnd, "YYYY-MM-DD") } else { v.WarrantyPeriod = "保修期外" } 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 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"` HouseID int `json:"house_id"` Supervisor string `json:"supervisor"` 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"` MaintenanceRemark string `json:"maintenance_remark"` StartTime string `json:"start_time"` EndTime string `json:"end_time"` FinishTime string `json:"finish_time"` } var orderInfo OrderInfo err := order.GetOneWithDetail("o.id={{id}}", map[string]interface{}{"id": id}, &orderInfo) if err != nil { app.Error(c, err.Error()) return } if houseInfo, _ := user.GetHouseInfoByID(orderInfo.HouseID, []string{"supervisor"}, nil); houseInfo != nil && houseInfo.Supervisor > 0 { if adminInfo, _ := admin.GetInfoByID(houseInfo.Supervisor, []string{"username"}, nil); adminInfo != nil { orderInfo.Supervisor = adminInfo.Username } } 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.StartTime = utils.DateS(orderInfo.StartTime, "YYYY-MM-DD HH:mm") orderInfo.EndTime = utils.DateS(orderInfo.EndTime, "YYYY-MM-DD HH:mm") orderInfo.FinishTime = utils.DateS(orderInfo.FinishTime, "YYYY-MM-DD HH:mm") if orderInfo.WarrantyStart != "0" { orderInfo.WarrantyPeriod = utils.DateS(orderInfo.WarrantyStart, "YYYY-MM-DD") + " 至 " + utils.DateS(orderInfo.WarrantyEnd, "YYYY-MM-DD") } else { orderInfo.WarrantyPeriod = "保修期外" } 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 } 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"` AuditType int `json:"audit_type"` AuditState int `json:"audit_state"` AuditAt string `json:"audit_at"` AuditRemark string `json:"audit_remark"` } 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"]) v.AuditAt = utils.DateS(v.AuditAt, "YYYY-MM-DD HH:mm") } res := gin.H{ "repair": repairList, "info": orderInfo, "event": eventList, } app.Success(c, res) } func RepairCheckList(c *gin.Context) { page := app.HandlePageNum(c) where := map[string]string{ "where": " o.deleted_at=0 and r.audit_type != 0 AND o.leader={{admin_id}}", "_group_by": "r.id", "_order_by": "r.id desc", } param := make(map[string]interface{}) param["admin_id"] = c.GetInt("adminID") auditState := utils.ToInt(c.Query("audit_state")) if auditState > 0 { where["where"] = where["where"] + " AND r.audit_state = {{auditState}}" param["auditState"] = auditState - 1 } keyword := c.Query("keyword") if keyword != "" { where["where"] = where["where"] + " AND (o.link_name like {{keyword}} or o.link_phone like {{keyword}})" param["keyword"] = "%" + keyword + "%" } type Admin struct { ID int `json:"id"` UserName string `json:"username"` Phone string `json:"phone"` } type RepairList struct { ID int `json:"id"` OrderId int `json:"order_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"` AuditType int `json:"audit_type"` AuditState int `json:"audit_state"` AuditAt string `json:"audit_at"` 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"` DetailContent string `json:"detail_content"` DetailPics string `json:"detail_pics"` OrderCreatedAt string `json:"order_created_at"` Leader int `json:"leader"` LeaderInfo *Admin `json:"leader_info"` WarrantyStart string `json:"warranty_start"` WarrantyEnd string `json:"warranty_end"` WarrantyPeriod string `json:"warranty_period"` IsForce int `json:"is_force"` EndTime string `json:"end_time"` } repairList := make([]RepairList, 0) err := order.GetListWidthOrder(where, param, page, &repairList) if err != nil { app.Error(c, err.Error()) return } adminIds := make([]int, 0) for _, v := range repairList { if v.Leader > 0 { adminIds = append(adminIds, v.Leader) } } orderTypeMap := aftersale.GetTypeMapByCache() 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 } } } repairStatusList := utils.ParseSliceMap(orderParam.Params.RepairState, "id") for k, v := range repairList { v.PicList = utils.ParseImgStr(v.Pics) v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD") v.Type = orderTypeMap[v.MainType] + "/" + orderTypeMap[v.SubType] if v.Leader > 0 && adminMap[v.Leader] != nil { v.LeaderInfo = adminMap[v.Leader] } else { v.LeaderInfo = &Admin{} } if v.WarrantyStart != "0" { v.WarrantyPeriod = utils.DateS(v.WarrantyStart, "YYYY-MM-DD") + " 至 " + utils.DateS(v.WarrantyEnd, "YYYY-MM-DD") } else { v.WarrantyPeriod = "保修期外" } v.ScheduleTime = utils.DateS(v.ScheduleTime, "YYYY-MM-DD") v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD") v.OrderCreatedAt = utils.DateS(v.OrderCreatedAt, "YYYY-MM-DD") v.EndTime = utils.DateS(v.EndTime, "YYYY-MM-DD HH:mm") v.FinishedAt = utils.DateS(v.FinishedAt, "YYYY-MM-DD HH:mm") v.AuditAt = utils.DateS(v.AuditAt, "YYYY-MM-DD HH:mm") v.StateName = utils.ToStr(repairStatusList[utils.ToStr(v.State)]["name"]) repairList[k] = v } app.Success(c, repairList) }