order.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. package pick
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/tealeg/xlsx/v3"
  6. "strings"
  7. "time"
  8. "zhiyuan/pkg/app"
  9. "zhiyuan/pkg/config"
  10. "zhiyuan/pkg/param/material"
  11. pickParam "zhiyuan/pkg/param/material/pick"
  12. "zhiyuan/pkg/utils"
  13. "zhiyuan/services/admin"
  14. "zhiyuan/services/form"
  15. "zhiyuan/services/material/pick"
  16. "zhiyuan/services/material/pkg"
  17. "zhiyuan/services/structs"
  18. )
  19. func OrderList(c *gin.Context) {
  20. page := app.HandlePageNum(c)
  21. pkgID := utils.StrTo(c.Query("pkg_id")).MustInt()
  22. if pkgID <= 0 {
  23. app.Error(c, "套餐id有误")
  24. return
  25. }
  26. pkgInfo, err := pkg.GetPkg(map[string]interface{}{"id": pkgID}, []string{"id", "room_types", "pkg_name"}, nil)
  27. where := map[string]interface{}{"pkg_id": pkgID, "_orderby": "id desc"}
  28. total, err := pick.CountOrder(where)
  29. if err != nil {
  30. app.Error(c, err.Error())
  31. return
  32. }
  33. type OrderList struct {
  34. ID int `json:"id"`
  35. CustomerName string `json:"customer_name"`
  36. CustomerPhone string `json:"customer_phone"`
  37. Province int `json:"province"`
  38. City int `json:"city"`
  39. Region int `json:"region"`
  40. HouseAddress string `json:"house_address"`
  41. Content string `json:"content"`
  42. HouseArea float64 `json:"house_area"`
  43. HouseStyle string `json:"house_style"`
  44. AdminID int `json:"admin_id"`
  45. AdminName string `json:"admin_name"`
  46. State int `json:"state"`
  47. StateName string `json:"state_name"`
  48. StateColor string `json:"state_color"`
  49. CreatedAt string `json:"created_at"`
  50. }
  51. orderList := make([]OrderList, 0)
  52. if _, err := pick.GetOrders(where, nil, page, &orderList); err != nil {
  53. app.Error(c, err.Error())
  54. return
  55. }
  56. adminIds := make([]int, 0)
  57. for _, v := range orderList {
  58. adminIds = append(adminIds, v.AdminID)
  59. }
  60. adminListMap := make(map[int]string, 0)
  61. if len(adminIds) > 0 {
  62. if adminList, err := admin.GetAdmins(map[string]interface{}{"id in": adminIds}, nil, app.Page{}, nil); err == nil {
  63. for _, v := range adminList {
  64. adminListMap[v.ID] = v.Username
  65. }
  66. }
  67. }
  68. roomTypeMap := make(map[int]string)
  69. for _, v := range material.Params.RoomType {
  70. roomTypeMap[v.ID] = v.ShortName
  71. }
  72. orderStatusList := utils.ParseSliceMap(pickParam.OrderParams.State, "id")
  73. for k, v := range orderList {
  74. v.AdminName = adminListMap[v.AdminID]
  75. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
  76. styleName := ""
  77. for _, style := range utils.JsonDecode(v.HouseStyle).ToMapSlice() {
  78. styleName = utils.ToStr(style["num"]) + roomTypeMap[utils.ToInt(style["type"])] + " " + styleName
  79. }
  80. v.StateName = utils.ToStr(orderStatusList[utils.ToStr(v.State)]["name"])
  81. v.StateColor = utils.ToStr(orderStatusList[utils.ToStr(v.State)]["color"])
  82. v.HouseStyle = styleName
  83. orderList[k] = v
  84. }
  85. data := gin.H{
  86. "pkgInfo": pkgInfo,
  87. "list": orderList,
  88. "total": total,
  89. "limit": page.PageSize,
  90. }
  91. app.Success(c, data)
  92. }
  93. func OrderCheck(c *gin.Context) {
  94. id := utils.StrTo(c.Param("id")).MustInt()
  95. if id <= 0 {
  96. app.ErrorMsg(c, "无效的订单ID", nil)
  97. return
  98. }
  99. var form = form.OrderCheck{
  100. Servicer: c.GetInt("adminID"),
  101. }
  102. if _, err := pick.CheckOrder(form, id); err != nil {
  103. app.Error(c, err.Error())
  104. return
  105. }
  106. app.Success(c, nil)
  107. }
  108. func OrderExport(c *gin.Context) {
  109. id := utils.StrTo(c.Param("id")).MustInt()
  110. if id <= 0 {
  111. app.Error(c, "订单id有误")
  112. return
  113. }
  114. type Item struct {
  115. ID int `json:"id"`
  116. Num int `json:"num"`
  117. Level int `json:"level"`
  118. Remarks string `json:"remarks"`
  119. BrandName string `json:"brand_name"`
  120. ItemName string `json:"item_name"`
  121. UnitName string `json:"unit_name"`
  122. }
  123. type Room struct {
  124. Num int `json:"num"`
  125. Type int `json:"type"`
  126. }
  127. type OrderInfo struct {
  128. ID int `json:"id"`
  129. AdminID int `json:"admin_id"`
  130. AdminName string `json:"admin_name"`
  131. CustomerName string `json:"customer_name"`
  132. CustomerPhone string `json:"customer_phone"`
  133. HouseAddress string `json:"house_address"`
  134. HouseArea float64 `json:"house_area"`
  135. Content string `json:"content"`
  136. Item []map[int]Item `json:"item"`
  137. HouseStyle string `json:"house_style"`
  138. Room []Room `json:"room"`
  139. RoomText string `json:"room_text"`
  140. PkgID int `json:"pkg_id"`
  141. PkgName string `json:"pkg_name"`
  142. }
  143. var orderInfo *OrderInfo
  144. if _, err := pick.GetOrder(map[string]interface{}{"id": id}, nil, &orderInfo); err != nil {
  145. app.Error(c, err.Error())
  146. return
  147. }
  148. if adminInfo, err := admin.GetOne(map[string]interface{}{"id": orderInfo.AdminID}, []string{"username"}, nil); err == nil {
  149. fmt.Println(adminInfo)
  150. orderInfo.AdminName = adminInfo.Username
  151. }
  152. contentData := make([]structs.MaterialPickOrderContent, 0)
  153. utils.JsonDecode(orderInfo.Content).To(&contentData)
  154. item := make([]map[int]Item, 0)
  155. for _, v := range contentData {
  156. itemMap := make(map[int]Item)
  157. for _, pick := range v.Picks {
  158. itemMap[pick.PickID] = Item{
  159. ID: pick.ItemID,
  160. Num: pick.Num,
  161. Remarks: pick.Remarks,
  162. BrandName: pick.BrandName,
  163. ItemName: pick.ItemName,
  164. Level: pick.Level,
  165. UnitName: pick.UnitName,
  166. }
  167. }
  168. item = append(item, itemMap)
  169. }
  170. orderInfo.Item = item
  171. utils.JsonDecode(orderInfo.HouseStyle).To(&orderInfo.Room)
  172. roomTypes := utils.ParseSliceMap(material.Params.RoomType, "id")
  173. for _, v := range orderInfo.Room {
  174. orderInfo.RoomText = orderInfo.RoomText + utils.ToStr(v.Num) + utils.ToStr(roomTypes[utils.ToStr(v.Type)]["short_name"]) + " "
  175. }
  176. strings.TrimRight(orderInfo.RoomText, " ")
  177. if pkgInfo, err := pkg.GetPkg(map[string]interface{}{"id": orderInfo.PkgID}, []string{"id, pkg_name"}, nil); err == nil {
  178. orderInfo.PkgName = pkgInfo.PkgName
  179. }
  180. wb := xlsx.NewFile()
  181. time := utils.ToStr(time.Now().UnixNano())
  182. filename := orderInfo.PkgName + "材料选购确认表-" + orderInfo.CustomerName + "-" + time + ".xlsx"
  183. fullPath := config.Cfg.App.ExportPath + filename
  184. sh, err := wb.AddSheet("Sheet1")
  185. if err != nil {
  186. app.Error(c, err.Error())
  187. return
  188. }
  189. sh.SetColWidth(1, 1, 20)
  190. sh.SetColWidth(2, 2, 30)
  191. sh.SetColWidth(3, 3, 20)
  192. sh.SetColWidth(4, 4, 50)
  193. sh.SetColWidth(5, 5, 20)
  194. sh.SetColWidth(6, 6, 30)
  195. sh.SetColWidth(7, 7, 50)
  196. var row *xlsx.Row
  197. var cell *xlsx.Cell
  198. font := "微软雅黑"
  199. height := float64(36)
  200. styleBold := utils.GetCommonStyle(utils.StyleCfg{IsBold: true})
  201. styleBoldBorderTop := utils.GetCommonStyle(utils.StyleCfg{IsBold: true, IsBorderTop: true})
  202. styleBoldBorderTopRight := utils.GetCommonStyle(utils.StyleCfg{IsBold: true, IsBorderTop: true, IsBorderRight: true})
  203. styleBorderTop := utils.GetCommonStyle(utils.StyleCfg{IsBorderTop: true})
  204. styleBorderRight := utils.GetCommonStyle(utils.StyleCfg{IsBorderRight: true})
  205. styleBorderLeft := utils.GetCommonStyle(utils.StyleCfg{IsBorderLeft: true})
  206. styleBorderTopRight := utils.GetCommonStyle(utils.StyleCfg{IsBorderTop: true, IsBorderRight: true})
  207. styleBorderTopLeft := utils.GetCommonStyle(utils.StyleCfg{IsBorderTop: true, IsBorderLeft: true})
  208. styleBorderTopBottom := utils.GetCommonStyle(utils.StyleCfg{IsBorderTop: true, IsBorderBottom: true})
  209. styleBorderTopBottomRight := utils.GetCommonStyle(utils.StyleCfg{IsBorderTop: true, IsBorderBottom: true, IsBorderRight: true})
  210. styleCommon := utils.GetCommonStyle(utils.StyleCfg{})
  211. rowHeader := sh.AddRow()
  212. rowHeader.SetHeight(48)
  213. cell = rowHeader.AddCell()
  214. cell.Value = orderInfo.PkgName + "材料选购确认表"
  215. cell.HMerge = 6
  216. styleHeader := xlsx.NewStyle()
  217. styleHeader.Font.Size = 20
  218. styleHeader.Font.Bold = true
  219. styleHeader.Font.Name = font
  220. styleHeader.Border.Right = "medium"
  221. styleHeader.Border.RightColor = "00000000"
  222. styleHeader.Fill = *xlsx.NewFill("solid", "00D9D9D9", "FF000000")
  223. styleHeader.Alignment.Horizontal = "center"
  224. styleHeader.Alignment.Vertical = "center"
  225. styleHeader.ApplyFont = true
  226. styleHeader.ApplyFill = true
  227. styleHeader.ApplyBorder = true
  228. cell.SetStyle(styleHeader)
  229. cell = rowHeader.AddCell()
  230. cell = rowHeader.AddCell()
  231. cell = rowHeader.AddCell()
  232. cell = rowHeader.AddCell()
  233. cell = rowHeader.AddCell()
  234. cell = rowHeader.AddCell()
  235. cell.SetStyle(styleHeader)
  236. customerHeader := sh.AddRow()
  237. customerHeader.SetHeight(height)
  238. cell = customerHeader.AddCell()
  239. cell.Value = "客户姓名"
  240. cell.SetStyle(styleBoldBorderTop)
  241. cell = customerHeader.AddCell()
  242. cell.Value = orderInfo.CustomerName
  243. cell.SetStyle(styleBorderTop)
  244. cell = customerHeader.AddCell()
  245. cell.Value = "联系电话"
  246. cell.SetStyle(styleBoldBorderTop)
  247. cell = customerHeader.AddCell()
  248. cell.Value = orderInfo.CustomerPhone
  249. cell.SetStyle(styleBorderTop)
  250. cell = customerHeader.AddCell()
  251. cell.Value = "设计师"
  252. cell.SetStyle(styleBoldBorderTop)
  253. cell = customerHeader.AddCell()
  254. cell.Value = orderInfo.AdminName
  255. cell.SetStyle(styleBorderTop)
  256. cell.HMerge = 1
  257. cell = customerHeader.AddCell()
  258. cell.SetStyle(styleBorderTopRight)
  259. customerHeader = sh.AddRow()
  260. customerHeader.SetHeight(height)
  261. cell = customerHeader.AddCell()
  262. cell.Value = "户型"
  263. cell.SetStyle(styleBold)
  264. cell = customerHeader.AddCell()
  265. cell.Value = orderInfo.RoomText
  266. cell.SetStyle(styleCommon)
  267. cell = customerHeader.AddCell()
  268. cell.Value = "项目地址"
  269. cell.SetStyle(styleBold)
  270. cell = customerHeader.AddCell()
  271. cell.Value = orderInfo.HouseAddress
  272. cell.SetStyle(styleCommon)
  273. cell.HMerge = 3
  274. cell = customerHeader.AddCell()
  275. cell.SetStyle(styleCommon)
  276. cell = customerHeader.AddCell()
  277. cell.SetStyle(styleCommon)
  278. cell = customerHeader.AddCell()
  279. cell.SetStyle(styleBorderRight)
  280. row = sh.AddRow()
  281. row.SetHeight(height)
  282. cell = row.AddCell()
  283. cell.SetStyle(styleBorderTop)
  284. cell.Value = "空间"
  285. cell = row.AddCell()
  286. cell.SetStyle(styleBorderTop)
  287. cell.Value = "项目名称"
  288. cell = row.AddCell()
  289. cell.SetStyle(styleBorderTop)
  290. cell.Value = "材料品牌"
  291. cell = row.AddCell()
  292. cell.SetStyle(styleBorderTop)
  293. cell.Value = "型号"
  294. cell = row.AddCell()
  295. cell.SetStyle(styleBorderTop)
  296. cell.Value = "颜色"
  297. cell = row.AddCell()
  298. cell.SetStyle(styleBorderTop)
  299. cell.Value = "备注"
  300. cell = row.AddCell()
  301. cell.SetStyle(styleBorderTopRight)
  302. cell.Value = "注意事项"
  303. for _, v := range contentData {
  304. for subKey, subItem := range v.Picks {
  305. style := styleCommon
  306. styleLast := styleBorderRight
  307. styleFirst := styleBorderLeft
  308. if subKey == 0 {
  309. style = styleBorderTop
  310. styleLast = styleBoldBorderTopRight
  311. styleFirst = styleBorderTopLeft
  312. }
  313. row = sh.AddRow()
  314. row.SetHeight(height)
  315. cell = row.AddCell()
  316. cell.SetStyle(style)
  317. if subKey == 0 {
  318. cell.VMerge = len(v.Picks) - 1
  319. cell.Value = v.RoomName
  320. } else {
  321. cell.Value = ""
  322. }
  323. cell.Value = v.RoomName
  324. cell = row.AddCell()
  325. cell.SetStyle(styleFirst)
  326. cell.Value = subItem.PickName
  327. cell = row.AddCell()
  328. cell.SetStyle(style)
  329. cell.Value = subItem.BrandName
  330. cell = row.AddCell()
  331. cell.SetStyle(style)
  332. cell.Value = subItem.ItemName
  333. cell = row.AddCell()
  334. cell.SetStyle(style)
  335. cell.Value = subItem.Color
  336. cell = row.AddCell()
  337. cell.SetStyle(style)
  338. cell.Value = subItem.Remarks
  339. cell = row.AddCell()
  340. cell.SetStyle(styleLast)
  341. cell.Value = ""
  342. }
  343. }
  344. styleBoldBorderTop = utils.GetCommonStyle(utils.StyleCfg{IsBold: true, IsBorderTop: true})
  345. footerHeader := sh.AddRow()
  346. footerHeader.SetHeight(height)
  347. cell = footerHeader.AddCell()
  348. cell.Value = "客户签字\r\n(时间)"
  349. cell.SetStyle(styleBoldBorderTop)
  350. cell = footerHeader.AddCell()
  351. cell.SetStyle(styleBorderTopLeft)
  352. cell.HMerge = 1
  353. cell = footerHeader.AddCell()
  354. cell.SetStyle(styleBorderTop)
  355. cell = footerHeader.AddCell()
  356. cell.Value = "设计师签字\r\n(时间)"
  357. cell.SetStyle(styleBoldBorderTop)
  358. cell = footerHeader.AddCell()
  359. cell.SetStyle(styleBorderTop)
  360. cell.HMerge = 2
  361. cell = footerHeader.AddCell()
  362. cell.SetStyle(styleBorderTop)
  363. cell = footerHeader.AddCell()
  364. cell.SetStyle(styleBorderTopRight)
  365. footerRow := sh.AddRow()
  366. footerRow.SetHeight(50)
  367. cell = footerRow.AddCell()
  368. cell.Value = "1、为了不影响工程进度,请选购增项、升级产品的客户在产品下单之前交清此部分的增加费用,以免造成工程延误!\r\n2、此表一式2份,业主1份,公司1份交财务备案并电子档存档。"
  369. cell.HMerge = 6
  370. cell.SetStyle(styleBorderTopBottom)
  371. cell = footerRow.AddCell()
  372. cell.SetStyle(styleBorderTopBottom)
  373. cell = footerRow.AddCell()
  374. cell.SetStyle(styleBorderTopBottom)
  375. cell = footerRow.AddCell()
  376. cell.SetStyle(styleBorderTopBottom)
  377. cell = footerRow.AddCell()
  378. cell.SetStyle(styleBorderTopBottom)
  379. cell = footerRow.AddCell()
  380. cell.SetStyle(styleBorderTopBottom)
  381. cell = footerRow.AddCell()
  382. cell.SetStyle(styleBorderTopBottomRight)
  383. if err := wb.Save(fullPath); err != nil {
  384. app.Error(c, err.Error())
  385. return
  386. }
  387. app.Success(c, gin.H{"path": "export/" + filename, "filename": filename})
  388. }