123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- 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})
- }
|