123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- package bid
- import (
- "strings"
- "time"
- "zhiyuan/pkg/app"
- "zhiyuan/pkg/config"
- "zhiyuan/pkg/param"
- "zhiyuan/pkg/param/material"
- bidParam "zhiyuan/pkg/param/material/bid"
- itemParam "zhiyuan/pkg/param/material/item"
- "zhiyuan/pkg/utils"
- "zhiyuan/services/form"
- "zhiyuan/services/material/bid"
- "zhiyuan/services/material/item"
- "zhiyuan/services/material/pkg"
- "zhiyuan/services/structs"
- "github.com/gin-gonic/gin"
- "github.com/tealeg/xlsx/v3"
- )
- func BidList(c *gin.Context) {
- pkgID := utils.ToInt(c.Query("pkg_id"))
- if pkgID <= 0 {
- app.Error(c, "套餐id有误")
- return
- }
- var pkgInfo *structs.MaterialPkgList
- if _, err := pkg.GetPkg(map[string]interface{}{"id": pkgID}, nil, &pkgInfo); err != nil {
- app.Error(c, "套餐id有误")
- return
- }
- utils.JsonDecode(pkgInfo.RoomPics).To(&pkgInfo.RoomPicsDecode)
- utils.JsonDecode(pkgInfo.Outline).To(&pkgInfo.OutlineDecode)
- where := map[string]interface{}{"pkg_id": pkgID}
- bidList := make([]*structs.MaterialBidList, 0)
- if _, err := bid.GetBidList(where, nil, app.Page{}, &bidList); err != nil {
- app.Error(c, err.Error())
- return
- }
- typeIDs := make([]int, 0)
- for _, v := range bidList {
- if v.TypeIDs != "" {
- typeSlice := strings.Split(v.TypeIDs, ",")
- for _, vv := range typeSlice {
- typeIDs = append(typeIDs, utils.ToInt(vv))
- }
- }
- }
- typeListMap := make(map[int]string)
- if len(typeIDs) > 0 {
- if itemList, err := item.GetTypeList(nil, []string{"id, type_name"}, app.Page{}, nil); err == nil {
- for _, v := range itemList {
- typeListMap[v.ID] = v.TypeName
- }
- }
- }
- bidListGroup := make(map[int]map[int][]*structs.MaterialBidList)
- for _, category := range bidParam.Params.Category {
- bidListInit := make(map[int][]*structs.MaterialBidList)
- for _, v := range material.Params.RoomType {
- bidListInit[v.ID] = make([]*structs.MaterialBidList, 0)
- }
- bidListGroup[category.ID] = bidListInit
- }
- calcTypeMap := bidParam.GetCalcTypeMap()
- unitTypes := itemParam.GetUnitMap()
- for _, v := range bidList {
- v.CalcTypeName = calcTypeMap[v.CalcType].Name
- if v.CalcParam != "" {
- utils.JsonDecode(v.CalcParam).To(&v.CalcParamDecode)
- }
- v.UnitName = unitTypes[v.Unit]
- typeNames := ""
- typeSlice := strings.Split(v.TypeIDs, ",")
- for _, vv := range typeSlice {
- typeNames += "/" + typeListMap[utils.ToInt(vv)]
- }
- v.TypeNames = strings.TrimLeft(typeNames, "/")
- bidListGroup[v.Category][v.RoomType] = append(bidListGroup[v.Category][v.RoomType], v)
- }
- app.Success(c, gin.H{"pkgInfo": pkgInfo, "bidListGroup": bidListGroup})
- }
- func BidExport(c *gin.Context) {
- pkgID := utils.ToInt(c.Query("pkg_id"))
- if pkgID <= 0 {
- app.Error(c, "套餐id有误")
- return
- }
- var pkgInfo *structs.MaterialPkgList
- if _, err := pkg.GetPkg(map[string]interface{}{"id": pkgID}, nil, &pkgInfo); err != nil {
- app.Error(c, "套餐id有误")
- return
- }
- where := map[string]interface{}{"pkg_id": pkgID, "level": 1}
- bidList := make([]*structs.MaterialBidList, 0)
- if _, err := bid.GetBidList(where, nil, app.Page{}, &bidList); err != nil {
- app.Error(c, err.Error())
- return
- }
- bidListGroup := make(map[int]map[int][]*structs.MaterialBidList, 0)
- for _, v := range param.Params.MaterialRoomType {
- bidListGroup[v.ID] = map[int][]*structs.MaterialBidList{
- 1: make([]*structs.MaterialBidList, 0),
- 2: make([]*structs.MaterialBidList, 0),
- }
- }
- for _, v := range bidList {
- bidListGroup[v.RoomType][v.Category] = append(bidListGroup[v.RoomType][v.Category], v)
- }
- wb := xlsx.NewFile()
- time := utils.ToStr(time.Now().UnixNano())
- filename := "2021年一户一价产品说明表-" + time + ".xlsx"
- fullPath := config.Cfg.App.ExportPath + filename
- styleHeader := utils.GetCommonStyle(utils.StyleCfg{IsBold: true, FontSize: 17})
- styleBold := utils.GetCommonStyle(utils.StyleCfg{IsBold: true})
- styleCommon := utils.GetCommonStyle(utils.StyleCfg{})
- sh, err := wb.AddSheet("Sheet1")
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- sh = utils.SetColWidth(sh, []float64{20, 20, 50, 100})
- var row *xlsx.Row
- row = utils.AddRow(sh, utils.Row{Height: 60})
- utils.AddCell(row, utils.Cell{Value: "2021年一户一价产品说明表", HMerge: 3, Style: styleHeader})
- row = utils.AddRow(sh, utils.Row{Height: 40})
- 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 _, v := range param.Params.MaterialRoomType {
- buildBidLength := len(bidListGroup[v.ID][1])
- buildMaterialLength := len(bidListGroup[v.ID][2])
- bidLength := buildBidLength + buildMaterialLength
- for kk, vv := range bidListGroup[v.ID][1] {
- row = utils.AddRow(sh, utils.Row{Height: 30})
- if kk == 0 {
- utils.AddCell(row, utils.Cell{Value: v.Name, VMerge: bidLength - 1, Style: styleCommon})
- utils.AddCell(row, utils.Cell{Value: "施工项", VMerge: buildBidLength - 1, Style: styleCommon})
- } else {
- utils.AddCell(row, utils.Cell{Value: "", Style: styleCommon})
- utils.AddCell(row, utils.Cell{Value: "", Style: styleCommon})
- }
- utils.AddCell(row, utils.Cell{Value: vv.BidName, Style: styleCommon})
- utils.AddCell(row, utils.Cell{Value: vv.Description, Style: styleCommon})
- }
- for kk, vv := range bidListGroup[v.ID][2] {
- row = utils.AddRow(sh, utils.Row{Height: 30})
- utils.AddCell(row, utils.Cell{Value: "", Style: styleCommon})
- if kk == 0 {
- utils.AddCell(row, utils.Cell{Value: "主材项", VMerge: buildMaterialLength - 1, Style: styleCommon})
- } else {
- utils.AddCell(row, utils.Cell{Value: "", Style: styleCommon})
- }
- utils.AddCell(row, utils.Cell{Value: vv.BidName, Style: styleCommon})
- utils.AddCell(row, utils.Cell{Value: vv.Description, 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 BidAdd(c *gin.Context) {
- var form form.MaterialBidAdd
- if app.Bind(c, &form) != nil {
- return
- }
- if id, err := bid.AddBid(form); err != nil {
- app.Error(c, err.Error())
- return
- } else {
- app.Success(c, gin.H{"id": id})
- }
- }
- func BidEdit(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.Error(c, "类型id有误")
- return
- }
- var form form.MaterialBidAdd
- if app.Bind(c, &form) != nil {
- return
- }
- if _, err := bid.EditBid(form, id); err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- app.Success(c, nil)
- }
- func BidDel(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.Error(c, "id有误")
- return
- }
- if _, err := bid.DelBid(id); err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- app.Success(c, nil)
- }
- func BidDetailCalc(c *gin.Context) {
- var form form.MaterialBidDetailCalc
- if app.Bind(c, &form) != nil {
- return
- }
- bidList := make([]*structs.MaterialBidList, 0)
- if _, err := bid.GetBidList(map[string]interface{}{"pkg_id": form.PkgID}, nil, app.Page{}, &bidList); err != nil {
- app.Error(c, err.Error())
- }
- bidListMap := make(map[int][]*structs.MaterialBidList)
- type bidGroup struct {
- RoomName string `json:"room_name"`
- BidItem map[int][]*structs.MaterialBidList `json:"bid_item"`
- }
- bidListGroup := make(map[int]bidGroup, 0)
- for _, v := range material.Params.RoomType {
- bidListMap[v.ID] = make([]*structs.MaterialBidList, 0)
- bidListGroup[v.ID] = bidGroup{
- RoomName: v.Name,
- BidItem: map[int][]*structs.MaterialBidList{
- 1: make([]*structs.MaterialBidList, 0),
- 2: make([]*structs.MaterialBidList, 0),
- 12: make([]*structs.MaterialBidList, 0),
- 13: make([]*structs.MaterialBidList, 0),
- 14: make([]*structs.MaterialBidList, 0),
- 15: make([]*structs.MaterialBidList, 0),
- 11: make([]*structs.MaterialBidList, 0),
- },
- }
- }
- typeListMap := item.GetTypeMapByIds(bid.GetTypeIDs(bidList))
- for _, v := range bidList {
- if v.CalcParam != "" {
- utils.JsonDecode(v.CalcParam).To(&v.CalcParamDecode)
- }
- v.TypeNames = bid.GetTypeNames(typeListMap, v.TypeIDs)
- if v.Level == 1 {
- if form.IsInclusive == 1 || v.Category < 10 {
- bidListGroup[v.RoomType].BidItem[v.Category] = append(bidListGroup[v.RoomType].BidItem[v.Category], v)
- }
- }
- }
- res, err := bid.Calc(form)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, gin.H{
- "area": res.HouseArea,
- "money": res.TotalPrice,
- "bidList": bidListGroup,
- "info": res,
- })
- }
|