package admin import ( "fmt" "reflect" "time" "zhiyuan/models" "zhiyuan/models/calc" "zhiyuan/pkg/app" "zhiyuan/pkg/db" "zhiyuan/pkg/utils" "zhiyuan/services/calculate" "github.com/gin-gonic/gin" ) type Order struct { calc.Order CalcName string `json:"calcName" type:"string" prop:"add:false select:calc.name"` AdminName string `json:"adminName" type:"string" prop:"add:false select:admin.username"` } func (model Order) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel { return []db.JoinModel{ db.JoinModel{ Model: calc.Calculate{}, As: "calc", On: []string{ "`calc`.`id` = `" + model.TableName() + "`.`calcId`", }, }, db.JoinModel{ Model: models.Admin{}, As: "admin", On: []string{ "`admin`.`id` = `" + model.TableName() + "`.`adminId`", }, }, } } func OrderList(c *gin.Context) { db.ModelList(reflect.TypeOf(Order{}), map[string]interface{}{}, c) } func OrderInfo(c *gin.Context) { id := utils.ToInt(c.Param("id")) if id <= 0 { app.ErrorMsg(c, "id must be a number", nil) return } var model calc.Order db.GetModel(map[string]interface{}{"id": id}, &model) if model.ID == 0 { app.ErrorMsg(c, "订单不存在", nil) return } order, err := calculate.LoadOrder(model) if err != nil { app.ErrorMsg(c, err.Error(), nil) return } calc := order.Calc from := order.GetFrom(true) app.Success(c, gin.H{"calc": calc, "from": from}) } func ItemCopy(c *gin.Context) { id := utils.ToInt(c.Param("id")) if id <= 0 { app.ErrorMsg(c, "id must be a number", nil) return } item := calc.GetItemModel(int64(id), true) if item == nil { app.ErrorMsg(c, "项目不存在", nil) return } order := time.Now().Unix() cid, err := item.Copy(item.Name, item.CalcId, item.ParentId, order) if err != nil { app.ErrorMsg(c, err.Error(), nil) return } app.Success(c, gin.H{"id": cid}) } func CalcFile(c *gin.Context) { calcId := utils.ToInt(c.Param("id")) if calcId <= 0 { app.ErrorMsg(c, "id must be a number", nil) return } var calculate *calc.Calculate db.GetModel(map[string]interface{}{"id": calcId}, &calculate) if calculate == nil { app.ErrorMsg(c, "项目不存在", nil) return } file, err := c.FormFile("file") if err != nil { app.Error(c, fmt.Sprintf("get form err: %s", err.Error())) return } fileExt := utils.FileExt(file.Filename) baseName := utils.FileBase(file.Filename) attachKey := utils.RandomStr() + fileExt file.Filename = "uploads/" + attachKey if err := c.SaveUploadedFile(file, file.Filename); err != nil { app.Error(c, fmt.Sprintf("上传文件失败%s", err.Error())) return } id, err := db.InsertModel(db.Type(calc.File{}), map[string]interface{}{ "calcId": calcId, "name": baseName, "path": file.Filename, }) if err != nil { app.ErrorMsg(c, err.Error(), nil) return } app.Success(c, gin.H{"id": id}) } func OrderExport(c *gin.Context) { id := utils.ToInt(c.Param("id")) if id <= 0 { app.ErrorMsg(c, "id must be a number", nil) return } fid := utils.ToInt(c.Param("fid")) if id <= 0 { app.ErrorMsg(c, "id must be a number", nil) return } var model calc.Order db.GetModel(map[string]interface{}{"id": id}, &model) if model.ID == 0 { app.ErrorMsg(c, "订单不存在", nil) return } order, err := calculate.LoadOrder(model) if err != nil { app.ErrorMsg(c, err.Error(), nil) return } var file calc.File db.GetModel(map[string]interface{}{"id": fid}, &file) if file.ID == 0 { app.ErrorMsg(c, "模板不存在", nil) return } if file.CalcId != model.CalcId { app.ErrorMsg(c, "套餐不一致", nil) return } filename := order.Export(file.Path, c) if filename == "" { app.ErrorMsg(c, "导出失败", nil) return } app.Success(c, gin.H{"path": "export/" + filename, "filename": filename}) } func OrderInfos(c *gin.Context) { id := utils.ToInt(c.Param("id")) if id <= 0 { app.ErrorMsg(c, "id must be a number", nil) return } type Infos struct { Ids []int `form:"ids" json:"ids" label:"ID" binding:"required"` } var form Infos if app.Bind(c, &form) != nil { return } calcinfo := calc.GetCalculateModel(int64(id), true) if calcinfo == nil { app.ErrorMsg(c, "核算不存在", nil) return } type Order struct { ID int64 `json:"id"` Abstract string `json:"abstract"` From calculate.OrderFrom `json:"form"` CreatedAt int64 `json:"created_at"` UpdatedAt int64 `json:"updated_at"` } models := make([]calc.Order, 0) orders := make([]Order, 0) db.GetModel(map[string]interface{}{"id in": form.Ids}, &models) for _, model := range models { order, err := calculate.BindOrder(calcinfo, model) if err == nil { orders = append(orders, Order{ ID: model.ID, Abstract: utils.JsonEncode(order.Abstract()), From: order.GetFrom(true), CreatedAt: model.CreatedAt, UpdatedAt: model.UpdatedAt, }) } } app.Success(c, gin.H{"calc": calcinfo, "orders": orders}) }