calculate.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package admin
  2. import (
  3. "fmt"
  4. "reflect"
  5. "time"
  6. "zhiyuan/models"
  7. "zhiyuan/models/calc"
  8. "zhiyuan/pkg/app"
  9. "zhiyuan/pkg/db"
  10. "zhiyuan/pkg/utils"
  11. "zhiyuan/services/calculate"
  12. "github.com/gin-gonic/gin"
  13. )
  14. type Order struct {
  15. calc.Order
  16. CalcName string `json:"calcName" type:"string" prop:"add:false select:calc.name"`
  17. AdminName string `json:"adminName" type:"string" prop:"add:false select:admin.username"`
  18. }
  19. func (model Order) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel {
  20. return []db.JoinModel{
  21. db.JoinModel{
  22. Model: calc.Calculate{},
  23. As: "calc",
  24. On: []string{
  25. "`calc`.`id` = `" + model.TableName() + "`.`calcId`",
  26. },
  27. },
  28. db.JoinModel{
  29. Model: models.Admin{},
  30. As: "admin",
  31. On: []string{
  32. "`admin`.`id` = `" + model.TableName() + "`.`adminId`",
  33. },
  34. },
  35. }
  36. }
  37. func OrderList(c *gin.Context) {
  38. db.ModelList(reflect.TypeOf(Order{}), map[string]interface{}{}, c)
  39. }
  40. func OrderInfo(c *gin.Context) {
  41. id := utils.ToInt(c.Param("id"))
  42. if id <= 0 {
  43. app.ErrorMsg(c, "id must be a number", nil)
  44. return
  45. }
  46. var model calc.Order
  47. db.GetModel(map[string]interface{}{"id": id}, &model)
  48. if model.ID == 0 {
  49. app.ErrorMsg(c, "订单不存在", nil)
  50. return
  51. }
  52. order, err := calculate.LoadOrder(model)
  53. if err != nil {
  54. app.ErrorMsg(c, err.Error(), nil)
  55. return
  56. }
  57. calc := order.Calc
  58. from := order.GetFrom(true)
  59. app.Success(c, gin.H{"calc": calc, "from": from})
  60. }
  61. func ItemCopy(c *gin.Context) {
  62. id := utils.ToInt(c.Param("id"))
  63. if id <= 0 {
  64. app.ErrorMsg(c, "id must be a number", nil)
  65. return
  66. }
  67. item := calc.GetItemModel(int64(id), true)
  68. if item == nil {
  69. app.ErrorMsg(c, "项目不存在", nil)
  70. return
  71. }
  72. order := time.Now().Unix()
  73. cid, err := item.Copy(item.Name, item.CalcId, item.ParentId, order)
  74. if err != nil {
  75. app.ErrorMsg(c, err.Error(), nil)
  76. return
  77. }
  78. app.Success(c, gin.H{"id": cid})
  79. }
  80. func CalcFile(c *gin.Context) {
  81. calcId := utils.ToInt(c.Param("id"))
  82. if calcId <= 0 {
  83. app.ErrorMsg(c, "id must be a number", nil)
  84. return
  85. }
  86. var calculate *calc.Calculate
  87. db.GetModel(map[string]interface{}{"id": calcId}, &calculate)
  88. if calculate == nil {
  89. app.ErrorMsg(c, "项目不存在", nil)
  90. return
  91. }
  92. file, err := c.FormFile("file")
  93. if err != nil {
  94. app.Error(c, fmt.Sprintf("get form err: %s", err.Error()))
  95. return
  96. }
  97. fileExt := utils.FileExt(file.Filename)
  98. baseName := utils.FileBase(file.Filename)
  99. attachKey := utils.RandomStr() + fileExt
  100. file.Filename = "uploads/" + attachKey
  101. if err := c.SaveUploadedFile(file, file.Filename); err != nil {
  102. app.Error(c, fmt.Sprintf("上传文件失败%s", err.Error()))
  103. return
  104. }
  105. id, err := db.InsertModel(db.Type(calc.File{}), map[string]interface{}{
  106. "calcId": calcId,
  107. "name": baseName,
  108. "path": file.Filename,
  109. })
  110. if err != nil {
  111. app.ErrorMsg(c, err.Error(), nil)
  112. return
  113. }
  114. app.Success(c, gin.H{"id": id})
  115. }
  116. func OrderExport(c *gin.Context) {
  117. id := utils.ToInt(c.Param("id"))
  118. if id <= 0 {
  119. app.ErrorMsg(c, "id must be a number", nil)
  120. return
  121. }
  122. fid := utils.ToInt(c.Param("fid"))
  123. if id <= 0 {
  124. app.ErrorMsg(c, "id must be a number", nil)
  125. return
  126. }
  127. var model calc.Order
  128. db.GetModel(map[string]interface{}{"id": id}, &model)
  129. if model.ID == 0 {
  130. app.ErrorMsg(c, "订单不存在", nil)
  131. return
  132. }
  133. order, err := calculate.LoadOrder(model)
  134. if err != nil {
  135. app.ErrorMsg(c, err.Error(), nil)
  136. return
  137. }
  138. var file calc.File
  139. db.GetModel(map[string]interface{}{"id": fid}, &file)
  140. if file.ID == 0 {
  141. app.ErrorMsg(c, "模板不存在", nil)
  142. return
  143. }
  144. if file.CalcId != model.CalcId {
  145. app.ErrorMsg(c, "套餐不一致", nil)
  146. return
  147. }
  148. filename := order.Export(file.Path, c)
  149. if filename == "" {
  150. app.ErrorMsg(c, "导出失败", nil)
  151. return
  152. }
  153. app.Success(c, gin.H{"path": "export/" + filename, "filename": filename})
  154. }
  155. func OrderInfos(c *gin.Context) {
  156. id := utils.ToInt(c.Param("id"))
  157. if id <= 0 {
  158. app.ErrorMsg(c, "id must be a number", nil)
  159. return
  160. }
  161. type Infos struct {
  162. Ids []int `form:"ids" json:"ids" label:"ID" binding:"required"`
  163. }
  164. var form Infos
  165. if app.Bind(c, &form) != nil {
  166. return
  167. }
  168. calcinfo := calc.GetCalculateModel(int64(id), true)
  169. if calcinfo == nil {
  170. app.ErrorMsg(c, "核算不存在", nil)
  171. return
  172. }
  173. type Order struct {
  174. ID int64 `json:"id"`
  175. Abstract string `json:"abstract"`
  176. From calculate.OrderFrom `json:"form"`
  177. CreatedAt int64 `json:"created_at"`
  178. UpdatedAt int64 `json:"updated_at"`
  179. }
  180. models := make([]calc.Order, 0)
  181. orders := make([]Order, 0)
  182. db.GetModel(map[string]interface{}{"id in": form.Ids}, &models)
  183. for _, model := range models {
  184. order, err := calculate.BindOrder(calcinfo, model)
  185. if err == nil {
  186. orders = append(orders, Order{
  187. ID: model.ID,
  188. Abstract: utils.JsonEncode(order.Abstract()),
  189. From: order.GetFrom(true),
  190. CreatedAt: model.CreatedAt,
  191. UpdatedAt: model.UpdatedAt,
  192. })
  193. }
  194. }
  195. app.Success(c, gin.H{"calc": calcinfo, "orders": orders})
  196. }