bid.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package bid
  2. import (
  3. "strings"
  4. "time"
  5. "zhiyuan/pkg/app"
  6. "zhiyuan/pkg/config"
  7. "zhiyuan/pkg/param"
  8. "zhiyuan/pkg/param/material"
  9. bidParam "zhiyuan/pkg/param/material/bid"
  10. itemParam "zhiyuan/pkg/param/material/item"
  11. "zhiyuan/pkg/utils"
  12. "zhiyuan/services/form"
  13. "zhiyuan/services/material/bid"
  14. "zhiyuan/services/material/item"
  15. "zhiyuan/services/material/pkg"
  16. "zhiyuan/services/structs"
  17. "github.com/gin-gonic/gin"
  18. "github.com/tealeg/xlsx/v3"
  19. )
  20. func BidList(c *gin.Context) {
  21. pkgID := utils.ToInt(c.Query("pkg_id"))
  22. if pkgID <= 0 {
  23. app.Error(c, "套餐id有误")
  24. return
  25. }
  26. var pkgInfo *structs.MaterialPkgList
  27. if _, err := pkg.GetPkg(map[string]interface{}{"id": pkgID}, nil, &pkgInfo); err != nil {
  28. app.Error(c, "套餐id有误")
  29. return
  30. }
  31. utils.JsonDecode(pkgInfo.RoomPics).To(&pkgInfo.RoomPicsDecode)
  32. utils.JsonDecode(pkgInfo.Outline).To(&pkgInfo.OutlineDecode)
  33. where := map[string]interface{}{"pkg_id": pkgID}
  34. bidList := make([]*structs.MaterialBidList, 0)
  35. if _, err := bid.GetBidList(where, nil, app.Page{}, &bidList); err != nil {
  36. app.Error(c, err.Error())
  37. return
  38. }
  39. typeIDs := make([]int, 0)
  40. for _, v := range bidList {
  41. if v.TypeIDs != "" {
  42. typeSlice := strings.Split(v.TypeIDs, ",")
  43. for _, vv := range typeSlice {
  44. typeIDs = append(typeIDs, utils.ToInt(vv))
  45. }
  46. }
  47. }
  48. typeListMap := make(map[int]string)
  49. if len(typeIDs) > 0 {
  50. if itemList, err := item.GetTypeList(nil, []string{"id, type_name"}, app.Page{}, nil); err == nil {
  51. for _, v := range itemList {
  52. typeListMap[v.ID] = v.TypeName
  53. }
  54. }
  55. }
  56. bidListGroup := make(map[int]map[int][]*structs.MaterialBidList)
  57. for _, category := range bidParam.Params.Category {
  58. bidListInit := make(map[int][]*structs.MaterialBidList)
  59. for _, v := range material.Params.RoomType {
  60. bidListInit[v.ID] = make([]*structs.MaterialBidList, 0)
  61. }
  62. bidListGroup[category.ID] = bidListInit
  63. }
  64. calcTypeMap := bidParam.GetCalcTypeMap()
  65. unitTypes := itemParam.GetUnitMap()
  66. for _, v := range bidList {
  67. v.CalcTypeName = calcTypeMap[v.CalcType].Name
  68. if v.CalcParam != "" {
  69. utils.JsonDecode(v.CalcParam).To(&v.CalcParamDecode)
  70. }
  71. v.UnitName = unitTypes[v.Unit]
  72. typeNames := ""
  73. typeSlice := strings.Split(v.TypeIDs, ",")
  74. for _, vv := range typeSlice {
  75. typeNames += "/" + typeListMap[utils.ToInt(vv)]
  76. }
  77. v.TypeNames = strings.TrimLeft(typeNames, "/")
  78. bidListGroup[v.Category][v.RoomType] = append(bidListGroup[v.Category][v.RoomType], v)
  79. }
  80. app.Success(c, gin.H{"pkgInfo": pkgInfo, "bidListGroup": bidListGroup})
  81. }
  82. func BidExport(c *gin.Context) {
  83. pkgID := utils.ToInt(c.Query("pkg_id"))
  84. if pkgID <= 0 {
  85. app.Error(c, "套餐id有误")
  86. return
  87. }
  88. var pkgInfo *structs.MaterialPkgList
  89. if _, err := pkg.GetPkg(map[string]interface{}{"id": pkgID}, nil, &pkgInfo); err != nil {
  90. app.Error(c, "套餐id有误")
  91. return
  92. }
  93. where := map[string]interface{}{"pkg_id": pkgID, "level": 1}
  94. bidList := make([]*structs.MaterialBidList, 0)
  95. if _, err := bid.GetBidList(where, nil, app.Page{}, &bidList); err != nil {
  96. app.Error(c, err.Error())
  97. return
  98. }
  99. bidListGroup := make(map[int]map[int][]*structs.MaterialBidList, 0)
  100. for _, v := range param.Params.MaterialRoomType {
  101. bidListGroup[v.ID] = map[int][]*structs.MaterialBidList{
  102. 1: make([]*structs.MaterialBidList, 0),
  103. 2: make([]*structs.MaterialBidList, 0),
  104. }
  105. }
  106. for _, v := range bidList {
  107. bidListGroup[v.RoomType][v.Category] = append(bidListGroup[v.RoomType][v.Category], v)
  108. }
  109. wb := xlsx.NewFile()
  110. time := utils.ToStr(time.Now().UnixNano())
  111. filename := "2021年一户一价产品说明表-" + time + ".xlsx"
  112. fullPath := config.Cfg.App.ExportPath + filename
  113. styleHeader := utils.GetCommonStyle(utils.StyleCfg{IsBold: true, FontSize: 17})
  114. styleBold := utils.GetCommonStyle(utils.StyleCfg{IsBold: true})
  115. styleCommon := utils.GetCommonStyle(utils.StyleCfg{})
  116. sh, err := wb.AddSheet("Sheet1")
  117. if err != nil {
  118. app.Error(c, err.Error())
  119. return
  120. }
  121. sh = utils.SetColWidth(sh, []float64{20, 20, 50, 100})
  122. var row *xlsx.Row
  123. row = utils.AddRow(sh, utils.Row{Height: 60})
  124. utils.AddCell(row, utils.Cell{Value: "2021年一户一价产品说明表", HMerge: 3, Style: styleHeader})
  125. row = utils.AddRow(sh, utils.Row{Height: 40})
  126. utils.AddCell(row, utils.Cell{Value: "房间类型", Style: styleBold})
  127. utils.AddCell(row, utils.Cell{Value: "项目类别", Style: styleBold})
  128. utils.AddCell(row, utils.Cell{Value: "项目名称", Style: styleBold})
  129. utils.AddCell(row, utils.Cell{Value: "项目说明", Style: styleBold})
  130. for _, v := range param.Params.MaterialRoomType {
  131. buildBidLength := len(bidListGroup[v.ID][1])
  132. buildMaterialLength := len(bidListGroup[v.ID][2])
  133. bidLength := buildBidLength + buildMaterialLength
  134. for kk, vv := range bidListGroup[v.ID][1] {
  135. row = utils.AddRow(sh, utils.Row{Height: 30})
  136. if kk == 0 {
  137. utils.AddCell(row, utils.Cell{Value: v.Name, VMerge: bidLength - 1, Style: styleCommon})
  138. utils.AddCell(row, utils.Cell{Value: "施工项", VMerge: buildBidLength - 1, Style: styleCommon})
  139. } else {
  140. utils.AddCell(row, utils.Cell{Value: "", Style: styleCommon})
  141. utils.AddCell(row, utils.Cell{Value: "", Style: styleCommon})
  142. }
  143. utils.AddCell(row, utils.Cell{Value: vv.BidName, Style: styleCommon})
  144. utils.AddCell(row, utils.Cell{Value: vv.Description, Style: styleCommon})
  145. }
  146. for kk, vv := range bidListGroup[v.ID][2] {
  147. row = utils.AddRow(sh, utils.Row{Height: 30})
  148. utils.AddCell(row, utils.Cell{Value: "", Style: styleCommon})
  149. if kk == 0 {
  150. utils.AddCell(row, utils.Cell{Value: "主材项", VMerge: buildMaterialLength - 1, Style: styleCommon})
  151. } else {
  152. utils.AddCell(row, utils.Cell{Value: "", Style: styleCommon})
  153. }
  154. utils.AddCell(row, utils.Cell{Value: vv.BidName, Style: styleCommon})
  155. utils.AddCell(row, utils.Cell{Value: vv.Description, Style: styleCommon})
  156. }
  157. }
  158. if err := wb.Save(fullPath); err != nil {
  159. app.Error(c, err.Error())
  160. return
  161. }
  162. app.Success(c, gin.H{"path": "export/" + filename, "filename": filename})
  163. }
  164. func BidAdd(c *gin.Context) {
  165. var form form.MaterialBidAdd
  166. if app.Bind(c, &form) != nil {
  167. return
  168. }
  169. if id, err := bid.AddBid(form); err != nil {
  170. app.Error(c, err.Error())
  171. return
  172. } else {
  173. app.Success(c, gin.H{"id": id})
  174. }
  175. }
  176. func BidEdit(c *gin.Context) {
  177. id := utils.StrTo(c.Param("id")).MustInt()
  178. if id <= 0 {
  179. app.Error(c, "类型id有误")
  180. return
  181. }
  182. var form form.MaterialBidAdd
  183. if app.Bind(c, &form) != nil {
  184. return
  185. }
  186. if _, err := bid.EditBid(form, id); err != nil {
  187. app.ErrorMsg(c, err.Error(), nil)
  188. return
  189. }
  190. app.Success(c, nil)
  191. }
  192. func BidDel(c *gin.Context) {
  193. id := utils.StrTo(c.Param("id")).MustInt()
  194. if id <= 0 {
  195. app.Error(c, "id有误")
  196. return
  197. }
  198. if _, err := bid.DelBid(id); err != nil {
  199. app.ErrorMsg(c, err.Error(), nil)
  200. return
  201. }
  202. app.Success(c, nil)
  203. }
  204. func BidDetailCalc(c *gin.Context) {
  205. var form form.MaterialBidDetailCalc
  206. if app.Bind(c, &form) != nil {
  207. return
  208. }
  209. bidList := make([]*structs.MaterialBidList, 0)
  210. if _, err := bid.GetBidList(map[string]interface{}{"pkg_id": form.PkgID}, nil, app.Page{}, &bidList); err != nil {
  211. app.Error(c, err.Error())
  212. }
  213. bidListMap := make(map[int][]*structs.MaterialBidList)
  214. type bidGroup struct {
  215. RoomName string `json:"room_name"`
  216. BidItem map[int][]*structs.MaterialBidList `json:"bid_item"`
  217. }
  218. bidListGroup := make(map[int]bidGroup, 0)
  219. for _, v := range material.Params.RoomType {
  220. bidListMap[v.ID] = make([]*structs.MaterialBidList, 0)
  221. bidListGroup[v.ID] = bidGroup{
  222. RoomName: v.Name,
  223. BidItem: map[int][]*structs.MaterialBidList{
  224. 1: make([]*structs.MaterialBidList, 0),
  225. 2: make([]*structs.MaterialBidList, 0),
  226. 12: make([]*structs.MaterialBidList, 0),
  227. 13: make([]*structs.MaterialBidList, 0),
  228. 14: make([]*structs.MaterialBidList, 0),
  229. 15: make([]*structs.MaterialBidList, 0),
  230. 11: make([]*structs.MaterialBidList, 0),
  231. },
  232. }
  233. }
  234. typeListMap := item.GetTypeMapByIds(bid.GetTypeIDs(bidList))
  235. for _, v := range bidList {
  236. if v.CalcParam != "" {
  237. utils.JsonDecode(v.CalcParam).To(&v.CalcParamDecode)
  238. }
  239. v.TypeNames = bid.GetTypeNames(typeListMap, v.TypeIDs)
  240. if v.Level == 1 {
  241. if form.IsInclusive == 1 || v.Category < 10 {
  242. bidListGroup[v.RoomType].BidItem[v.Category] = append(bidListGroup[v.RoomType].BidItem[v.Category], v)
  243. }
  244. }
  245. }
  246. res, err := bid.Calc(form)
  247. if err != nil {
  248. app.Error(c, err.Error())
  249. return
  250. }
  251. app.Success(c, gin.H{
  252. "area": res.HouseArea,
  253. "money": res.TotalPrice,
  254. "bidList": bidListGroup,
  255. "info": res,
  256. })
  257. }