package aftersale import ( "fmt" "strings" "time" "zhiyuan/pkg/app" "zhiyuan/pkg/config" "zhiyuan/pkg/db" "zhiyuan/pkg/param" orderParam "zhiyuan/pkg/param/order" "zhiyuan/pkg/utils" "zhiyuan/services/admin" "zhiyuan/services/aftersale" "zhiyuan/services/aftersale/order" "zhiyuan/services/form" "zhiyuan/services/material/brand" "zhiyuan/services/region" "zhiyuan/services/user" "github.com/gin-gonic/gin" "github.com/tealeg/xlsx/v3" ) func OrderAdd(c *gin.Context) { var form form.OrderAdd if app.Bind(c, &form) != nil { return } form.Typ = 2 id, err := order.Add(form) if err != nil { app.Error(c, err.Error()) return } app.Success(c, gin.H{"id": id}) } func OrderEventList(c *gin.Context) { page := app.HandlePageNum(c) params := make(map[string]interface{}) where := map[string]string{ "where": "o.deleted_at=0 AND e.event_type=1", "_order_by": "id desc", } if page.PageSize != 0 { where["_page_size"] = utils.ToStr(page.PageSize) where["_page_num"] = utils.ToStr(page.PageNum) } // state state := utils.ToInt(c.Query("state")) if state != 0 { where["where"] += " AND e.order_state={{state}}" params["state"] = state } // created_date createdDate := c.QueryArray("created_date[]") if createdDate != nil && len(createdDate) == 2 { where["where"] += " AND e.created_at BETWEEN {{created_at_min}} AND {{created_at_max}}" params["created_at_min"] = utils.DateParseUnix(createdDate[0], "Y-m-d") params["created_at_max"] = utils.DateParseUnix(createdDate[1], "Y-m-d") + 86399 } // site_id if adminInfo := admin.GetAdminCache(c.GetInt("adminID")); adminInfo.SiteID > 0 { where["where"] += " AND (site_id={{site_id}} or site_id=0)" params["site_id"] = adminInfo.SiteID } total, err := order.GetCountJoinOrder(where["where"], params) if err != nil { app.Error(c, err.Error()) return } type Auth struct { Revoke bool `json:"revoke"` Check bool `json:"check"` Allot bool `json:"allot"` ReAllot bool `json:"re_allot"` } type OrderList struct { ID int `json:"id"` OrderNo string `json:"order_no"` LinkName string `json:"link_name"` LinkPhone string `json:"link_phone"` Content string `json:"content"` State int `json:"state"` StateName string `json:"state_name"` StateColor string `json:"state_color"` Title string `json:"title"` InWarranty string `json:"in_warranty"` Auth Auth `json:"auth"` Leader int `json:"leader"` CreatedAt string `json:"created_at"` } orderList := make([]OrderList, 0) err = order.GetListJoinOrder(where, params, &orderList) if err != nil { app.Error(c, err.Error()) return } orderStateList := utils.ParseSliceMap(orderParam.Params.State, "id") for k, v := range orderList { v.StateName = utils.ToStr(orderStateList[utils.ToStr(v.State)]["name"]) v.StateColor = utils.ToStr(orderStateList[utils.ToStr(v.State)]["color"]) v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm") v.Auth = Auth{ Check: utils.IsContain(orderParam.Allow.Check, v.State), Revoke: utils.IsContain(orderParam.Allow.Revoke, v.State), ReAllot: utils.IsContain(orderParam.Allow.ReAllot, v.State), Allot: utils.IsContain(orderParam.Allow.Allot, v.State), } if v.InWarranty == "0" { v.InWarranty = "否" } else { v.InWarranty = "是" } orderList[k] = v } data := gin.H{ "list": orderList, "total": total, "limit": page.PageSize, } app.Success(c, data) } func OrderList(c *gin.Context) { page := app.HandlePageNum(c) params := make(map[string]interface{}) where := map[string]string{ "where": "deleted_at=0", "_order_by": "id desc", } if page.PageSize != 0 { where["_page_size"] = utils.ToStr(page.PageSize) where["_page_num"] = utils.ToStr(page.PageNum) } // leader leaderID := utils.ToInt(c.Query("leader_id")) if leaderID > 0 { where["where"] += " AND leader={{leader_id}}" params["leader_id"] = leaderID } // state state := utils.ToInt(c.Query("state")) if state != 0 { where["where"] += " AND if(state=90,90,if(is_force=0,state,100))={{state}}" params["state"] = state } // is_force isForce := utils.ToInt(c.Query("is_force")) if isForce != 0 { if isForce == 2 { where["where"] += " AND is_force!=0" } else { where["where"] += " AND is_force={{isForce}}" params["isForce"] = isForce - 1 } } // type typ := utils.ToInt(c.Query("type")) if typ != 0 { where["where"] += " AND type={{type}}" params["type"] = typ - 1 } // address address := c.Query("address") if address != "" { where["where"] += " AND address LIKE {{address}}" params["address"] = "%" + address + "%" } // inWarranty inWarranty := utils.ToInt(c.Query("in_warranty")) if utils.IsContain([]int{0, 1}, inWarranty) { where["where"] += " AND in_warranty={{in_warranty}}" params["in_warranty"] = inWarranty } // user user := c.Query("user") if user != "" { where["where"] += " AND (link_name LIKE {{user}} OR link_phone LIKE {{user}})" params["user"] = "%" + user + "%" } // site_id if adminInfo := admin.GetAdminCache(c.GetInt("adminID")); adminInfo.SiteID > 0 { where["where"] += " AND (site_id={{site_id}} or site_id=0)" params["site_id"] = adminInfo.SiteID } // state finish_state := utils.ToInt(c.Query("finish_state")) if finish_state != 0 { switch finish_state { case 1: where["where"] += " AND (o.finish_state=1)" break case 2: where["where"] += " AND (o.finish_state=2)" break case 3: where["where"] += " AND (o.finish_state=1 AND UNIX_TIMESTAMP()<=o.end_time)" break case 4: where["where"] += " AND (o.finish_state=1 AND UNIX_TIMESTAMP()>o.end_time)" break case 5: where["_order_by"] = "o.incomplete_count desc" break } } createdDate := c.QueryArray("created_date[]") if createdDate != nil && len(createdDate) == 2 { where["where"] += " AND o.created_at BETWEEN {{created_at_min}} AND {{created_at_max}}" params["created_at_min"] = utils.DateParseUnix(createdDate[0], "Y-m-d") params["created_at_max"] = utils.DateParseUnix(createdDate[1], "Y-m-d") + 86399 } total, err := order.GetCountWidthDetail(where["where"], params) if err != nil { app.Error(c, err.Error()) return } type Auth struct { Revoke bool `json:"revoke"` Check bool `json:"check"` Allot bool `json:"allot"` ReAllot bool `json:"re_allot"` ForceComplete bool `json:"force_complete"` } // 回访信息 type Visit struct { OrderID int `json:"order_id"` Content string `json:"content"` FinishedAt string `json:"finished_at"` } type OrderList struct { ID int `json:"id"` OrderNo string `json:"order_no"` LinkName string `json:"link_name"` LinkPhone string `json:"link_phone"` Content string `json:"content"` State int `json:"state"` CState int `json:"cstate"` StateName string `json:"state_name"` StateColor string `json:"state_color"` InWarranty int `json:"in_warranty"` Remarks string `json:"remarks"` WarrantyStart string `json:"warranty_start"` WarrantyEnd string `json:"warranty_end"` WarrantyPeriod string `json:"warranty_period"` VisitList []*Visit `json:"visit_list"` Address string `json:"address"` IsIssue int `json:"is_issue"` IsForce int `json:"is_force"` Type int `json:"type"` Leader int `json:"leader"` LeaderName string `json:"leader_name"` Auth Auth `json:"auth"` CreatedAt string `json:"created_at"` MaintenanceRemark string `json:"maintenance_remark"` StartTime string `json:"start_time"` EndTime string `json:"end_time"` FinishTime string `json:"finish_time"` IncompleteCount int `json:"incomplete_count"` FinishState int `json:"finish_state"` RepairTimeout int `json:"repair_timeout"` } orderList := make([]OrderList, 0) err = order.GetListWidthDetail(where, params, &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) } } adminMap := make(map[int]string, 0) if len(adminIds) > 0 { adminList, err := admin.GetAdmins(map[string]interface{}{"id in": adminIds}, []string{"id, username"}, app.Page{}, nil) if err == nil { for _, v := range adminList { adminMap[v.ID] = v.Username } } } visitList := make([]*Visit, 0) visitListMap := make(map[int][]*Visit, 0) if len(orderIds) > 0 { if _, err := order.GetVisits(map[string]interface{}{"order_id in": orderIds, "_orderby": "created_at desc", "deleted_at": 0, "state": 2}, nil, &visitList); err == nil { for _, v := range visitList { v.FinishedAt = utils.DateS(v.FinishedAt, "YYYY-MM-DD") visitListMap[v.OrderID] = append(visitListMap[v.OrderID], v) } } } orderStateList := utils.ParseSliceMap(orderParam.Params.State, "id") for k, v := range orderList { v.StateName = utils.ToStr(orderStateList[utils.ToStr(v.CState)]["name"]) v.StateColor = utils.ToStr(orderStateList[utils.ToStr(v.CState)]["color"]) v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm") v.StartTime = utils.DateS(v.StartTime, "YYYY-MM-DD HH:mm") v.EndTime = utils.DateS(v.EndTime, "YYYY-MM-DD HH:mm") v.FinishTime = utils.DateS(v.FinishTime, "YYYY-MM-DD HH:mm") v.Auth = Auth{ Check: utils.IsContain(orderParam.Allow.Check, v.CState), Revoke: utils.IsContain(orderParam.Allow.Revoke, v.CState), Allot: utils.IsContain(orderParam.Allow.Allot, v.CState), ReAllot: utils.IsContain(orderParam.Allow.ReAllot, v.CState), ForceComplete: utils.IsContain(orderParam.Allow.ForceComplete, v.CState), } if visitListMap[v.ID] == nil { v.VisitList = make([]*Visit, 0) } else { v.VisitList = visitListMap[v.ID] } v.LeaderName = adminMap[v.Leader] 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 } data := gin.H{ "list": orderList, "total": total, "limit": page.PageSize, } app.Success(c, data) } func OrderLists(c *gin.Context) { page := app.HandlePageNum(c) params := make(map[string]interface{}) where := map[string]string{ "where": "deleted_at=0", "_order_by": "id desc", } if page.PageSize != 0 { where["_page_size"] = utils.ToStr(page.PageSize) where["_page_num"] = utils.ToStr(page.PageNum) } // leader leaderID := utils.ToInt(c.Query("leader_id")) if leaderID > 0 { where["where"] += " AND leader={{leader_id}}" params["leader_id"] = leaderID } // state state := utils.ToInt(c.Query("state")) if state != 0 { where["where"] += " AND if(state=90,90,if(is_force=0,state,100))={{state}}" params["state"] = state } // is_force isForce := utils.ToInt(c.Query("is_force")) if isForce != 0 { if isForce == 2 { where["where"] += " AND is_force!=0" } else { where["where"] += " AND is_force={{isForce}}" params["isForce"] = isForce - 1 } } // type typ := utils.ToInt(c.Query("type")) if typ != 0 { where["where"] += " AND type={{type}}" params["type"] = typ - 1 } // address address := c.Query("address") if address != "" { where["where"] += " AND address LIKE {{address}}" params["address"] = "%" + address + "%" } // inWarranty inWarranty := utils.ToInt(c.Query("in_warranty")) if utils.IsContain([]int{0, 1}, inWarranty) { where["where"] += " AND in_warranty={{in_warranty}}" params["in_warranty"] = inWarranty } // user user := c.Query("user") if user != "" { where["where"] += " AND (link_name LIKE {{user}} OR link_phone LIKE {{user}})" params["user"] = "%" + user + "%" } createdDate := c.QueryArray("created_date[]") if createdDate != nil && len(createdDate) == 2 { where["where"] += " AND o.created_at BETWEEN {{created_at_min}} AND {{created_at_max}}" params["created_at_min"] = utils.DateParseUnix(createdDate[0], "Y-m-d") params["created_at_max"] = utils.DateParseUnix(createdDate[1], "Y-m-d") + 86399 } total, err := order.GetCountWidthDetail(where["where"], params) if err != nil { app.Error(c, err.Error()) return } type Auth struct { Revoke bool `json:"revoke"` Check bool `json:"check"` Allot bool `json:"allot"` ReAllot bool `json:"re_allot"` ForceComplete bool `json:"force_complete"` } // 回访信息 type Visit struct { OrderID int `json:"order_id"` Content string `json:"content"` FinishedAt string `json:"finished_at"` } type Comment struct { ID int `json:"id"` OrderID int `json:"order_id"` UserID int `json:"user_id"` Star int `json:"star"` Content string `json:"content"` Tags string `json:"tags"` } type OrderList struct { ID int `json:"id"` OrderNo string `json:"order_no"` LinkName string `json:"link_name"` LinkPhone string `json:"link_phone"` Content string `json:"content"` State int `json:"state"` CState int `json:"cstate"` StateName string `json:"state_name"` StateColor string `json:"state_color"` InWarranty int `json:"in_warranty"` Remarks string `json:"remarks"` WarrantyStart string `json:"warranty_start"` WarrantyEnd string `json:"warranty_end"` WarrantyPeriod string `json:"warranty_period"` VisitList []*Visit `json:"visit_list"` Address string `json:"address"` IsIssue int `json:"is_issue"` IsForce int `json:"is_force"` Type int `json:"type"` Leader int `json:"leader"` LeaderName string `json:"leader_name"` Auth Auth `json:"auth"` CreatedAt string `json:"created_at"` Comment *Comment `json:"comment"` } orderList := make([]OrderList, 0) err = order.GetListWidthDetail(where, params, &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) } } adminMap := make(map[int]string, 0) if len(adminIds) > 0 { adminList, err := admin.GetAdmins(map[string]interface{}{"id in": adminIds}, []string{"id, username"}, app.Page{}, nil) if err == nil { for _, v := range adminList { adminMap[v.ID] = v.Username } } } visitList := make([]*Visit, 0) visitListMap := make(map[int][]*Visit, 0) if len(orderIds) > 0 { if _, err := order.GetVisits(map[string]interface{}{"order_id in": orderIds, "_orderby": "created_at desc", "deleted_at": 0, "state": 2}, nil, &visitList); err == nil { for _, v := range visitList { v.FinishedAt = utils.DateS(v.FinishedAt, "YYYY-MM-DD") visitListMap[v.OrderID] = append(visitListMap[v.OrderID], v) } } } commentList := make([]Comment, 0) commentListMap := make(map[int]Comment, 0) if len(orderIds) > 0 { if _, err := order.GetComment(map[string]interface{}{"order_id in": orderIds}, nil, &commentList); err == nil { for _, v := range commentList { commentListMap[v.OrderID] = v } } } orderStateList := utils.ParseSliceMap(orderParam.Params.State, "id") for k, v := range orderList { v.StateName = utils.ToStr(orderStateList[utils.ToStr(v.CState)]["name"]) v.StateColor = utils.ToStr(orderStateList[utils.ToStr(v.CState)]["color"]) v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm") v.Auth = Auth{ Check: utils.IsContain(orderParam.Allow.Check, v.CState), Revoke: utils.IsContain(orderParam.Allow.Revoke, v.CState), Allot: utils.IsContain(orderParam.Allow.Allot, v.CState), ReAllot: utils.IsContain(orderParam.Allow.ReAllot, v.CState), ForceComplete: utils.IsContain(orderParam.Allow.ForceComplete, v.CState), } if visitListMap[v.ID] == nil { v.VisitList = make([]*Visit, 0) } else { v.VisitList = visitListMap[v.ID] } if comment, ok := commentListMap[v.ID]; ok { v.Comment = &comment } v.LeaderName = adminMap[v.Leader] 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 } data := gin.H{ "list": orderList, "total": total, "limit": page.PageSize, } app.Success(c, data) } func OrderCheck(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.ErrorMsg(c, "无效的订单ID", nil) return } var form = form.OrderCheck{ Servicer: c.GetInt("adminID"), } if err := order.Check(form, id); err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) } func OrderRemark(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.ErrorMsg(c, "无效的订单ID", nil) return } var form form.OrderRemark if app.Bind(c, &form) != nil { return } if err := order.Remark(form, id); err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) } func OrderAllot(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.ErrorMsg(c, "无效的订单ID", nil) return } var form form.OrderAllot if app.Bind(c, &form) != nil { return } form.Servicer = c.GetInt("adminID") if err := order.Allot(form, id); 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 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"` RepairID int `json:"repair_id"` InWarranty int `json:"in_warranty"` StateColor string `json:"state_color"` CreatedAt string `json:"created_at"` ScheduleTime string `json:"schedule_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 } 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 } // repair 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 int `json:"audit_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"]) } // 回访信息 type Visit struct { ID int `json:"id"` OrderID int `json:"order_id"` State int `json:"state"` StateName string `json:"state_name"` VisitTime string `json:"visit_time"` Content string `json:"content"` FinishedAt string `json:"finished_at"` } visitList := make([]*Visit, 0) if _, err := order.GetVisits(map[string]interface{}{"order_id": id, "state": 2, "_orderby": "created_at desc", "deleted_at": 0}, nil, &visitList); err != nil { app.Error(c, err.Error()) } for _, v := range visitList { v.FinishedAt = utils.DateS(v.FinishedAt, "YYYY-MM-DD") } // 完结信息 type Issue struct { ID int `json:"id"` Director int `json:"director"` DirectorName string `json:"director_name"` IssueID int `json:"issue_id"` IssueName string `json:"issue_name"` IssueDesc string `json:"issue_desc"` Brand int `json:"brand"` BrandName string `json:"brand_name"` CreatedAt string `json:"created_at"` } issueList := make([]*Issue, 0) if _, err := order.GetIssues(map[string]interface{}{"order_id": id, "_orderby": "created_at desc"}, nil, &issueList); err != nil { app.Error(c, err.Error()) } issueMap := aftersale.GetIssueMapByCache() directorList := utils.ParseSliceMap(orderParam.Params.IssueDirector, "id") brandIds := make([]int, 0) for _, v := range issueList { brandIds = utils.AppendUniqueInt(brandIds, v.Brand) } brandMap := make(map[int]string) if len(brandIds) > 0 { if brandList, err := brand.GetList(map[string]interface{}{"id in": brandIds}, nil, app.Page{}, nil); err == nil { for _, v := range brandList { brandMap[v.ID] = v.BrandName } } } for _, v := range issueList { if issueName, ok := issueMap[v.IssueID]; ok { v.IssueName = issueName } v.DirectorName = utils.ToStr(directorList[utils.ToStr(v.Director)]["name"]) if brandName, ok := brandMap[v.Brand]; ok { v.BrandName = brandName } v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD") } res := gin.H{ "info": orderInfo, "event": eventList, "repair": repairList, "visit": visitList, "issue": issueList, } type Comment struct { ID int `json:"id"` OrderID int `json:"order_id"` UserID int `json:"user_id"` Star int `json:"star"` Content string `json:"content"` Tags string `json:"tags"` } var comment Comment if _, err := order.GetComment(map[string]interface{}{"order_id": id}, nil, &comment); err != nil { app.Error(c, err.Error()) } if comment.ID != 0 { res["comment"] = comment } app.Success(c, res) } func OrderDel(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.Error(c, "工单 id 有误") return } err := order.Del(id) if err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) } func OrderAddIssue(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.Error(c, "工单 id 有误") return } var form form.OrderIssue if app.Bind(c, &form) != nil { return } err := order.Issue(form, id) if err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) } func OrderComplete(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.Error(c, "工单 id 有误") return } var form form.OrderComplete if app.Bind(c, &form) != nil { return } form.Servicer = c.GetInt("adminID") form.IsForce = utils.ToInt(c.Query("is_force")) //if isForce := utils.ToInt(c.Query("is_force")); isForce == 1 { // form.IsForce = 1 //} err := order.Complete(form, id) if err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) } func OrderExport(c *gin.Context) { params := make(map[string]interface{}) where := map[string]string{ "where": "deleted_at=0", "_order_by": "id desc", } // leader leaderID := utils.ToInt(c.Query("leader_id")) if leaderID > 0 { where["where"] += " AND leader={{leader_id}}" params["leader_id"] = leaderID } // state state := utils.ToInt(c.Query("state")) if state != 0 { where["where"] += " AND if(state=90,90,if(is_force=0,state,100))={{state}}" params["state"] = state } // is_force isForce := utils.ToInt(c.Query("is_force")) if isForce != 0 { if isForce == 2 { where["where"] += " AND is_force!=0" } else { where["where"] += " AND is_force={{isForce}}" params["isForce"] = isForce - 1 } } // type typ := utils.ToInt(c.Query("type")) if typ != 0 { where["where"] += " AND type={{type}}" params["type"] = typ - 1 } // address address := c.Query("address") if address != "" { where["where"] += " AND address LIKE {{address}}" params["address"] = "%" + address + "%" } // inWarranty inWarranty := utils.ToInt(c.Query("in_warranty")) if utils.IsContain([]int{0, 1}, inWarranty) { where["where"] += " AND in_warranty={{in_warranty}}" params["in_warranty"] = inWarranty } // user user1 := c.Query("user") if user1 != "" { where["where"] += " AND (link_name LIKE {{user}} OR link_phone LIKE {{user}})" params["user"] = "%" + user1 + "%" } // state finish_state := utils.ToInt(c.Query("finish_state")) if finish_state != 0 { switch finish_state { case 1: where["where"] += " AND (o.finish_state=1)" break case 2: where["where"] += " AND (o.finish_state=2)" break case 3: where["where"] += " AND (o.finish_state=1 AND UNIX_TIMESTAMP()<=o.end_time)" break case 4: where["where"] += " AND (o.finish_state=1 AND UNIX_TIMESTAMP()>o.end_time)" break case 5: where["_order_by"] = "o.incomplete_count desc" break } } createdDate := c.QueryArray("created_date[]") if createdDate != nil && len(createdDate) == 2 { where["where"] += " AND o.created_at BETWEEN {{created_at_min}} AND {{created_at_max}}" params["created_at_min"] = utils.DateParseUnix(createdDate[0], "Y-m-d") params["created_at_max"] = utils.DateParseUnix(createdDate[1], "Y-m-d") + 86399 } if adminInfo := admin.GetAdminCache(c.GetInt("adminID")); adminInfo.SiteID > 0 { where["where"] += " AND site_id = {{site_id}}" params["site_id"] = adminInfo.SiteID } type House struct { ID int `json:"id"` Address string `json:"address"` Designer int `json:"designer"` DesignerName string `json:"designer_name"` District int `json:"district"` DistrictName string `json:"district_name"` Supervisor int `json:"supervisor"` SupervisorName string `json:"supervisor_name"` ProjectManager int `json:"project_manager"` ProjectManagerName string `json:"project_manager_name"` ProjectLeader int `json:"project_leader"` ProjectLeaderName string `json:"project_leader_name"` ProjectStart string `json:"project_start"` ProjectEnd string `json:"project_end"` } type Visit struct { OrderID int `json:"order_id"` Content string `json:"content"` FinishedAt string `json:"finished_at"` } type Detail struct { OrderID int `json:"order_id"` Address string `json:"address"` Content string `json:"content"` } type Issue struct { OrderID int `json:"order_id"` IssueDesc string `json:"issue_desc"` Director int `json:"director"` } type Repair struct { OrderID int `json:"order_id"` WorkerName string `json:"worker_name"` } type Orders struct { ID int `json:"id"` CreatedAt string `json:"created_at"` LinkName string `json:"link_name"` LinkPhone string `json:"link_phone"` UserID int `json:"user_id"` InWarranty int `json:"in_warranty"` Leader int `json:"leader"` LeaderName string `json:"leader_name"` HouseID int `json:"house_id"` House *House `json:"house"` Repair *Repair `json:"repair"` Issue *Issue `json:"issue"` Detail *Detail `json:"detial"` Visit string `json:"visit"` } orderList := make([]*Orders, 0) err := order.GetLists(where, params, app.Page{}, &orderList) houseIds := make([]int, 0) orderIds := make([]int, 0) userIds := make([]int, 0) adminIds := make([]int, 0) for _, v := range orderList { houseIds = append(houseIds, v.HouseID) orderIds = append(orderIds, v.ID) userIds = append(userIds, v.UserID) adminIds = append(adminIds, v.Leader) } houseMap := make(map[int]*House, 0) if len(houseIds) > 0 { houseList := make([]*House, 0) user.GetHouseList(map[string]interface{}{"id in": houseIds}, nil, app.Page{}, &houseList) for _, v := range houseList { adminIds = append(adminIds, v.Designer, v.Supervisor, v.ProjectManager, v.ProjectLeader) houseMap[v.ID] = v } } userMap := make(map[int]string, 0) if len(userIds) > 0 { userList, _ := user.GetList(map[string]interface{}{"id in": userIds}, nil, app.Page{}, nil) for _, v := range userList { userMap[v.ID] = v.Name } } type AdminInfo struct { Username string `json:"username"` State int `json:"state"` } adminMap := make(map[int]AdminInfo, 0) if len(adminIds) > 0 { adminList, _ := admin.GetAdmins(map[string]interface{}{"id in": adminIds}, nil, app.Page{}, nil) for _, v := range adminList { adminMap[v.ID] = AdminInfo{Username: v.Username, State: v.State} } } districtMap := make(map[int]string) for _, v := range param.Params.District { districtMap[v.ID] = v.Name } for k, v := range houseMap { if v.District > 0 { v.DistrictName = districtMap[v.District] } if v.Designer > 0 { v.DesignerName = adminMap[v.Designer].Username } if v.Supervisor > 0 { v.SupervisorName = adminMap[v.Supervisor].Username } if v.ProjectLeader > 0 { v.ProjectLeaderName = adminMap[v.ProjectLeader].Username } if v.ProjectManager > 0 { v.ProjectManagerName = adminMap[v.ProjectManager].Username } if v.Supervisor > 0 { if adminMap[v.Supervisor].State == -1 { v.SupervisorName = "离职项目经理" } else { v.SupervisorName = adminMap[v.Supervisor].Username } } houseMap[k] = v } detialMap := make(map[int]*Detail, 0) if len(orderIds) > 0 { detailList := make([]*Detail, 0) order.GetDetailList(map[string]interface{}{"order_id in": orderIds}, nil, app.Page{}, &detailList) for _, v := range detailList { detialMap[v.OrderID] = v } } issueMap := make(map[int]*Issue, 0) if len(orderIds) > 0 { issueList := make([]*Issue, 0) order.GetIssues(map[string]interface{}{"order_id in": orderIds}, nil, &issueList) for _, v := range issueList { issueMap[v.OrderID] = v } } issueDirectorMap := orderParam.GetIssueDirectorMap() visitList := make([]*Visit, 0) visitListMap := make(map[int][]string, 0) if len(orderIds) > 0 { if _, err := order.GetVisits(map[string]interface{}{"order_id in": orderIds, "_orderby": "created_at desc", "deleted_at": 0, "state": 2}, nil, &visitList); err == nil { for _, v := range visitList { visitListMap[v.OrderID] = append(visitListMap[v.OrderID], v.Content) } } } for _, v := range orderList { if v.Leader > 0 { v.LeaderName = adminMap[v.Leader].Username } if visitListMap[v.ID] != nil { v.Visit = strings.Join(visitListMap[v.ID], "\n") } v.House = houseMap[v.HouseID] } wb := xlsx.NewFile() filename := "售后维修表-" + db.ToString(time.Now().Unix()) + ".xlsx" fullPath := config.Cfg.App.ExportPath + filename styleBold := utils.GetCommonStyle(utils.StyleCfg{IsBold: true, FontSize: 10}) styleCommon := utils.GetCommonStyle(utils.StyleCfg{FontSize: 9}) sh, err := wb.AddSheet("Sheet1") if err != nil { app.Error(c, err.Error()) return } var row *xlsx.Row height := float64(30) row = utils.AddRow(sh, utils.Row{Height: height}) utils.AddCell(row, utils.Cell{Value: "售后维修交接表", HMerge: 22, Style: styleBold}) sh = utils.SetColWidth(sh, []float64{5, 5, 10, 10, 10, 10, 20, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 30, 15, 15, 15, 10, 10}) row = utils.AddRow(sh, utils.Row{Height: height}) utils.AddCell(row, utils.Cell{Value: "序号", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "级别", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "工地性质", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "开工日期", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "竣工", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "报修时间", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "工地名称", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "业主姓名", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "联系电话", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "设计师", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "区域", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "售后报修人", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "工程主管", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "队长", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "项目经理", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "指派项目经理", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "是否过保修", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "责任人", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "报修内容", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "反馈结果", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "客服回访情况", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "解决结果", Style: styleBold}) utils.AddCell(row, utils.Cell{Value: "报销情况", Style: styleBold}) for k, v := range orderList { v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY/MM/DD") if v.House.ProjectStart == "0" { v.House.ProjectStart = "" } else { v.House.ProjectStart = utils.DateS(v.House.ProjectStart, "YYYY/MM/DD") } if v.House.ProjectEnd == "0" { v.House.ProjectEnd = "" } else { v.House.ProjectEnd = utils.DateS(v.House.ProjectEnd, "YYYY/MM/DD") } row = utils.AddRow(sh, utils.Row{Height: height}) utils.AddCell(row, utils.Cell{Value: utils.ToStr(k + 1), Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: "一级", Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: "全包", Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: v.House.ProjectStart, Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: v.House.ProjectEnd, Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: v.CreatedAt, Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: v.House.Address, Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: v.LinkName, Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: v.LinkPhone, Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: v.House.DesignerName, Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: v.House.DistrictName, Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: userMap[v.UserID], Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: v.House.ProjectManagerName, Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: v.House.ProjectLeaderName, Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: v.House.SupervisorName, Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: v.LeaderName, Style: styleCommon}) inWarranty := "是" if v.InWarranty > 0 { inWarranty = "否" } utils.AddCell(row, utils.Cell{Value: inWarranty, Style: styleCommon}) issueDirectorName := "" issueDesc := "" if issue, ok := issueMap[v.ID]; ok { issueDirectorName = issueDirectorMap[issue.Director] issueDesc = issue.IssueDesc } utils.AddCell(row, utils.Cell{Value: issueDirectorName, Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: detialMap[v.ID].Content, Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: issueDesc, Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: v.Visit, Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: "", Style: styleCommon}) utils.AddCell(row, utils.Cell{Value: "", Style: styleCommon}) } if err := wb.Save(fullPath); err != nil { app.Error(c, err.Error()) return } app.Success(c, gin.H{"path": "export/" + filename, "filename": filename}) } func OrderVisitStatist(c *gin.Context) { page := app.HandlePageNums(c) var s db.Select s.TableName = "`zy_as_order_visit`" s.Select = map[string]string{ "date": "`zy_as_order_visit`.`visit_time`", "total": "COUNT(1)", "finished_visit": "COUNT(`zy_as_order_visit`.`state` = 2 OR NULL)", "ufinished_visit": "COUNT(`zy_as_order_visit`.`state` = 1 OR NULL)", "overtime_visit": "COUNT(`zy_as_order_visit`.`finished_at` > `zy_as_order_visit`.`visit_time` OR NULL)", } s.GroupBy = "`zy_as_order_visit`.`visit_time`" s.OrderBy = "`zy_as_order_visit`.`visit_time` DESC" if page.PageSize != 0 { s.Limit = int64(page.PageSize) s.Offset = int64(page.PageSize * (page.PageNum - 1)) } date := utils.ToInt(c.Query("date")) if date != 0 { s.Where = append(s.Where, fmt.Sprintf("`zy_as_order_visit`.`visit_time` = %s", s.Param(date))) } startdate := utils.ToInt(c.Query("startdate")) if startdate != 0 { s.Where = append(s.Where, fmt.Sprintf("`zy_as_order_visit`.`visit_time` >= %s", s.Param(startdate))) } enddate := utils.ToInt(c.Query("enddate")) if enddate != 0 { s.Where = append(s.Where, fmt.Sprintf("`zy_as_order_visit`.`visit_time` <= %s", s.Param(enddate))) } s.Where = append(s.Where, fmt.Sprintf("`zy_as_order_visit`.`deleted_at` = %s", s.Param(0))) query, params := s.Query() list, err := db.QueryMap(query, params, nil) if err != nil { app.ErrorMsg(c, err.Error(), nil) return } if list == nil { list = make([]map[string]interface{}, 0) } count, err := db.GetCount(s, nil) if err != nil { app.ErrorMsg(c, err.Error(), nil) return } app.Success(c, gin.H{ "list": list, "count": count, }) } func RepairCheckList(c *gin.Context) { page := app.HandlePageNum(c) where := map[string]string{ "where": " o.deleted_at=0 and r.audit_type != 0", "_group_by": "r.id", "_order_by": "r.id desc", } param := make(map[string]interface{}) if page.PageSize != 0 { where["_page_size"] = utils.ToStr(page.PageSize) where["_page_num"] = utils.ToStr(page.PageNum) } auditState := utils.ToInt(c.Query("audit_state")) if auditState > 0 { where["where"] = where["where"] + " AND r.audit_state = {{auditState}}" param["auditState"] = auditState - 1 } auditType := utils.ToInt(c.Query("audit_type")) if auditType > 0 { where["where"] = where["where"] + " AND r.audit_type = {{auditType}}" param["auditType"] = auditType } 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 + "%" } // leader leaderID := utils.ToInt(c.Query("leader_id")) if leaderID > 0 { where["where"] += " AND leader={{leader_id}}" param["leader_id"] = leaderID } // address address := c.Query("address") if address != "" { where["where"] += " AND address LIKE {{address}}" param["address"] = "%" + address + "%" } createdDate := c.QueryArray("created_date[]") if createdDate != nil && len(createdDate) == 2 { where["where"] += " AND o.created_at BETWEEN {{created_at_min}} AND {{created_at_max}}" param["created_at_min"] = utils.DateParseUnix(createdDate[0], "Y-m-d") param["created_at_max"] = utils.DateParseUnix(createdDate[1], "Y-m-d") + 86399 } total, err := order.GetCountWidthOrder(where["where"], param) if err != nil { app.Error(c, err.Error()) return } 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 } data := gin.H{ "list": repairList, "total": total, "limit": page.PageSize, } app.Success(c, data) } func RepairCheck(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.Error(c, "工单 id 有误") return } var form form.RepairCheck if app.Bind(c, &form) != nil { return } err := order.RepairCheck(id, form.State, c.GetInt("adminID"), form.Remark) if err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) }