package pick import ( "fmt" "github.com/gin-gonic/gin" "github.com/tealeg/xlsx/v3" "strings" "time" "zhiyuan/pkg/app" "zhiyuan/pkg/config" "zhiyuan/pkg/param/material" pickParam "zhiyuan/pkg/param/material/pick" "zhiyuan/pkg/utils" "zhiyuan/services/admin" "zhiyuan/services/form" "zhiyuan/services/material/pick" "zhiyuan/services/material/pkg" "zhiyuan/services/structs" ) func OrderList(c *gin.Context) { page := app.HandlePageNum(c) pkgID := utils.StrTo(c.Query("pkg_id")).MustInt() if pkgID <= 0 { app.Error(c, "套餐id有误") return } pkgInfo, err := pkg.GetPkg(map[string]interface{}{"id": pkgID}, []string{"id", "room_types", "pkg_name"}, nil) where := map[string]interface{}{"pkg_id": pkgID, "_orderby": "id desc"} total, err := pick.CountOrder(where) if err != nil { app.Error(c, err.Error()) return } type OrderList struct { ID int `json:"id"` CustomerName string `json:"customer_name"` CustomerPhone string `json:"customer_phone"` Province int `json:"province"` City int `json:"city"` Region int `json:"region"` HouseAddress string `json:"house_address"` Content string `json:"content"` HouseArea float64 `json:"house_area"` HouseStyle string `json:"house_style"` AdminID int `json:"admin_id"` AdminName string `json:"admin_name"` State int `json:"state"` StateName string `json:"state_name"` StateColor string `json:"state_color"` CreatedAt string `json:"created_at"` } orderList := make([]OrderList, 0) if _, err := pick.GetOrders(where, nil, page, &orderList); err != nil { app.Error(c, err.Error()) return } adminIds := make([]int, 0) for _, v := range orderList { adminIds = append(adminIds, v.AdminID) } adminListMap := make(map[int]string, 0) if len(adminIds) > 0 { if adminList, err := admin.GetAdmins(map[string]interface{}{"id in": adminIds}, nil, app.Page{}, nil); err == nil { for _, v := range adminList { adminListMap[v.ID] = v.Username } } } roomTypeMap := make(map[int]string) for _, v := range material.Params.RoomType { roomTypeMap[v.ID] = v.ShortName } orderStatusList := utils.ParseSliceMap(pickParam.OrderParams.State, "id") for k, v := range orderList { v.AdminName = adminListMap[v.AdminID] v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm") styleName := "" for _, style := range utils.JsonDecode(v.HouseStyle).ToMapSlice() { styleName = utils.ToStr(style["num"]) + roomTypeMap[utils.ToInt(style["type"])] + " " + styleName } v.StateName = utils.ToStr(orderStatusList[utils.ToStr(v.State)]["name"]) v.StateColor = utils.ToStr(orderStatusList[utils.ToStr(v.State)]["color"]) v.HouseStyle = styleName orderList[k] = v } data := gin.H{ "pkgInfo": pkgInfo, "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 := pick.CheckOrder(form, id); err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) } func OrderExport(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.Error(c, "订单id有误") return } type Item struct { ID int `json:"id"` Num int `json:"num"` Level int `json:"level"` Remarks string `json:"remarks"` BrandName string `json:"brand_name"` ItemName string `json:"item_name"` UnitName string `json:"unit_name"` } type Room struct { Num int `json:"num"` Type int `json:"type"` } type OrderInfo struct { ID int `json:"id"` AdminID int `json:"admin_id"` AdminName string `json:"admin_name"` CustomerName string `json:"customer_name"` CustomerPhone string `json:"customer_phone"` HouseAddress string `json:"house_address"` HouseArea float64 `json:"house_area"` Content string `json:"content"` Item []map[int]Item `json:"item"` HouseStyle string `json:"house_style"` Room []Room `json:"room"` RoomText string `json:"room_text"` PkgID int `json:"pkg_id"` PkgName string `json:"pkg_name"` } var orderInfo *OrderInfo if _, err := pick.GetOrder(map[string]interface{}{"id": id}, nil, &orderInfo); err != nil { app.Error(c, err.Error()) return } if adminInfo, err := admin.GetOne(map[string]interface{}{"id": orderInfo.AdminID}, []string{"username"}, nil); err == nil { fmt.Println(adminInfo) orderInfo.AdminName = adminInfo.Username } contentData := make([]structs.MaterialPickOrderContent, 0) utils.JsonDecode(orderInfo.Content).To(&contentData) item := make([]map[int]Item, 0) for _, v := range contentData { itemMap := make(map[int]Item) for _, pick := range v.Picks { itemMap[pick.PickID] = Item{ ID: pick.ItemID, Num: pick.Num, Remarks: pick.Remarks, BrandName: pick.BrandName, ItemName: pick.ItemName, Level: pick.Level, UnitName: pick.UnitName, } } item = append(item, itemMap) } orderInfo.Item = item utils.JsonDecode(orderInfo.HouseStyle).To(&orderInfo.Room) roomTypes := utils.ParseSliceMap(material.Params.RoomType, "id") for _, v := range orderInfo.Room { orderInfo.RoomText = orderInfo.RoomText + utils.ToStr(v.Num) + utils.ToStr(roomTypes[utils.ToStr(v.Type)]["short_name"]) + " " } strings.TrimRight(orderInfo.RoomText, " ") if pkgInfo, err := pkg.GetPkg(map[string]interface{}{"id": orderInfo.PkgID}, []string{"id, pkg_name"}, nil); err == nil { orderInfo.PkgName = pkgInfo.PkgName } wb := xlsx.NewFile() time := utils.ToStr(time.Now().UnixNano()) filename := orderInfo.PkgName + "材料选购确认表-" + orderInfo.CustomerName + "-" + time + ".xlsx" fullPath := config.Cfg.App.ExportPath + filename sh, err := wb.AddSheet("Sheet1") if err != nil { app.Error(c, err.Error()) return } sh.SetColWidth(1, 1, 20) sh.SetColWidth(2, 2, 30) sh.SetColWidth(3, 3, 20) sh.SetColWidth(4, 4, 50) sh.SetColWidth(5, 5, 20) sh.SetColWidth(6, 6, 30) sh.SetColWidth(7, 7, 50) var row *xlsx.Row var cell *xlsx.Cell font := "微软雅黑" height := float64(36) styleBold := utils.GetCommonStyle(utils.StyleCfg{IsBold: true}) styleBoldBorderTop := utils.GetCommonStyle(utils.StyleCfg{IsBold: true, IsBorderTop: true}) styleBoldBorderTopRight := utils.GetCommonStyle(utils.StyleCfg{IsBold: true, IsBorderTop: true, IsBorderRight: true}) styleBorderTop := utils.GetCommonStyle(utils.StyleCfg{IsBorderTop: true}) styleBorderRight := utils.GetCommonStyle(utils.StyleCfg{IsBorderRight: true}) styleBorderLeft := utils.GetCommonStyle(utils.StyleCfg{IsBorderLeft: true}) styleBorderTopRight := utils.GetCommonStyle(utils.StyleCfg{IsBorderTop: true, IsBorderRight: true}) styleBorderTopLeft := utils.GetCommonStyle(utils.StyleCfg{IsBorderTop: true, IsBorderLeft: true}) styleBorderTopBottom := utils.GetCommonStyle(utils.StyleCfg{IsBorderTop: true, IsBorderBottom: true}) styleBorderTopBottomRight := utils.GetCommonStyle(utils.StyleCfg{IsBorderTop: true, IsBorderBottom: true, IsBorderRight: true}) styleCommon := utils.GetCommonStyle(utils.StyleCfg{}) rowHeader := sh.AddRow() rowHeader.SetHeight(48) cell = rowHeader.AddCell() cell.Value = orderInfo.PkgName + "材料选购确认表" cell.HMerge = 6 styleHeader := xlsx.NewStyle() styleHeader.Font.Size = 20 styleHeader.Font.Bold = true styleHeader.Font.Name = font styleHeader.Border.Right = "medium" styleHeader.Border.RightColor = "00000000" styleHeader.Fill = *xlsx.NewFill("solid", "00D9D9D9", "FF000000") styleHeader.Alignment.Horizontal = "center" styleHeader.Alignment.Vertical = "center" styleHeader.ApplyFont = true styleHeader.ApplyFill = true styleHeader.ApplyBorder = true cell.SetStyle(styleHeader) cell = rowHeader.AddCell() cell = rowHeader.AddCell() cell = rowHeader.AddCell() cell = rowHeader.AddCell() cell = rowHeader.AddCell() cell = rowHeader.AddCell() cell.SetStyle(styleHeader) customerHeader := sh.AddRow() customerHeader.SetHeight(height) cell = customerHeader.AddCell() cell.Value = "客户姓名" cell.SetStyle(styleBoldBorderTop) cell = customerHeader.AddCell() cell.Value = orderInfo.CustomerName cell.SetStyle(styleBorderTop) cell = customerHeader.AddCell() cell.Value = "联系电话" cell.SetStyle(styleBoldBorderTop) cell = customerHeader.AddCell() cell.Value = orderInfo.CustomerPhone cell.SetStyle(styleBorderTop) cell = customerHeader.AddCell() cell.Value = "设计师" cell.SetStyle(styleBoldBorderTop) cell = customerHeader.AddCell() cell.Value = orderInfo.AdminName cell.SetStyle(styleBorderTop) cell.HMerge = 1 cell = customerHeader.AddCell() cell.SetStyle(styleBorderTopRight) customerHeader = sh.AddRow() customerHeader.SetHeight(height) cell = customerHeader.AddCell() cell.Value = "户型" cell.SetStyle(styleBold) cell = customerHeader.AddCell() cell.Value = orderInfo.RoomText cell.SetStyle(styleCommon) cell = customerHeader.AddCell() cell.Value = "项目地址" cell.SetStyle(styleBold) cell = customerHeader.AddCell() cell.Value = orderInfo.HouseAddress cell.SetStyle(styleCommon) cell.HMerge = 3 cell = customerHeader.AddCell() cell.SetStyle(styleCommon) cell = customerHeader.AddCell() cell.SetStyle(styleCommon) cell = customerHeader.AddCell() cell.SetStyle(styleBorderRight) row = sh.AddRow() row.SetHeight(height) cell = row.AddCell() cell.SetStyle(styleBorderTop) cell.Value = "空间" cell = row.AddCell() cell.SetStyle(styleBorderTop) cell.Value = "项目名称" cell = row.AddCell() cell.SetStyle(styleBorderTop) cell.Value = "材料品牌" cell = row.AddCell() cell.SetStyle(styleBorderTop) cell.Value = "型号" cell = row.AddCell() cell.SetStyle(styleBorderTop) cell.Value = "颜色" cell = row.AddCell() cell.SetStyle(styleBorderTop) cell.Value = "备注" cell = row.AddCell() cell.SetStyle(styleBorderTopRight) cell.Value = "注意事项" for _, v := range contentData { for subKey, subItem := range v.Picks { style := styleCommon styleLast := styleBorderRight styleFirst := styleBorderLeft if subKey == 0 { style = styleBorderTop styleLast = styleBoldBorderTopRight styleFirst = styleBorderTopLeft } row = sh.AddRow() row.SetHeight(height) cell = row.AddCell() cell.SetStyle(style) if subKey == 0 { cell.VMerge = len(v.Picks) - 1 cell.Value = v.RoomName } else { cell.Value = "" } cell.Value = v.RoomName cell = row.AddCell() cell.SetStyle(styleFirst) cell.Value = subItem.PickName cell = row.AddCell() cell.SetStyle(style) cell.Value = subItem.BrandName cell = row.AddCell() cell.SetStyle(style) cell.Value = subItem.ItemName cell = row.AddCell() cell.SetStyle(style) cell.Value = subItem.Color cell = row.AddCell() cell.SetStyle(style) cell.Value = subItem.Remarks cell = row.AddCell() cell.SetStyle(styleLast) cell.Value = "" } } styleBoldBorderTop = utils.GetCommonStyle(utils.StyleCfg{IsBold: true, IsBorderTop: true}) footerHeader := sh.AddRow() footerHeader.SetHeight(height) cell = footerHeader.AddCell() cell.Value = "客户签字\r\n(时间)" cell.SetStyle(styleBoldBorderTop) cell = footerHeader.AddCell() cell.SetStyle(styleBorderTopLeft) cell.HMerge = 1 cell = footerHeader.AddCell() cell.SetStyle(styleBorderTop) cell = footerHeader.AddCell() cell.Value = "设计师签字\r\n(时间)" cell.SetStyle(styleBoldBorderTop) cell = footerHeader.AddCell() cell.SetStyle(styleBorderTop) cell.HMerge = 2 cell = footerHeader.AddCell() cell.SetStyle(styleBorderTop) cell = footerHeader.AddCell() cell.SetStyle(styleBorderTopRight) footerRow := sh.AddRow() footerRow.SetHeight(50) cell = footerRow.AddCell() cell.Value = "1、为了不影响工程进度,请选购增项、升级产品的客户在产品下单之前交清此部分的增加费用,以免造成工程延误!\r\n2、此表一式2份,业主1份,公司1份交财务备案并电子档存档。" cell.HMerge = 6 cell.SetStyle(styleBorderTopBottom) cell = footerRow.AddCell() cell.SetStyle(styleBorderTopBottom) cell = footerRow.AddCell() cell.SetStyle(styleBorderTopBottom) cell = footerRow.AddCell() cell.SetStyle(styleBorderTopBottom) cell = footerRow.AddCell() cell.SetStyle(styleBorderTopBottom) cell = footerRow.AddCell() cell.SetStyle(styleBorderTopBottom) cell = footerRow.AddCell() cell.SetStyle(styleBorderTopBottomRight) if err := wb.Save(fullPath); err != nil { app.Error(c, err.Error()) return } app.Success(c, gin.H{"path": "export/" + filename, "filename": filename}) }