final_material_cart.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package final
  2. import (
  3. "errors"
  4. "fmt"
  5. _ "image/gif"
  6. _ "image/jpeg"
  7. _ "image/png"
  8. "strings"
  9. "zhiyuan/pkg/db"
  10. "zhiyuan/pkg/utils"
  11. "zhiyuan/services/admin"
  12. "github.com/gin-gonic/gin"
  13. )
  14. type FinalMaterialCart struct {
  15. ID int64 `json:"id" prop:"add:false"`
  16. ItemId int64 `json:"item_id" label:"项目类型ID" type:"int" prop:"edit"`
  17. AdminId int64 `json:"admin_id" label:"人员ID" type:"int" prop:"add:false"`
  18. ProcessId int64 `json:"process_id" label:"进度ID" type:"int" prop:"add:false"`
  19. MatId int64 `json:"mat_id" label:"材料ID" type:"int" prop:"add"`
  20. Price float64 `json:"price" label:"单价" type:"float" prop:"add:false"`
  21. Num float64 `json:"num" label:"数量" type:"float" prop:"add edit"`
  22. Remark string `json:"remark" label:"备注" type:"string" prop:"edit"`
  23. Total float64 `json:"total" label:"金额" type:"float" prop:"add:false" default:"0"`
  24. DeletedAt int64 `json:"deleted_at" prop:"add:false select:false"`
  25. CreatedAt int64 `json:"created_at" prop:"add:false"`
  26. UpdatedAt int64 `json:"updated_at" prop:"add:false"`
  27. db.BaseModel
  28. }
  29. func (FinalMaterialCart) TableName() string {
  30. return "zy_final_material_cart"
  31. }
  32. func (model FinalMaterialCart) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
  33. s.Where = append(s.Where, fmt.Sprintf("`%s`.`admin_id` = %s AND `%s`.`process_id` = %s", model.TableName(), s.Param(c.GetInt("adminID")), model.TableName(), s.Param(0)))
  34. return true
  35. }
  36. func (FinalMaterialCart) OnePrivilege(c *gin.Context, id int64) bool {
  37. return false
  38. }
  39. func (FinalMaterialCart) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error {
  40. data["admin_id"] = c.GetInt("adminID")
  41. return nil
  42. }
  43. func (FinalMaterialCart) AddAfter(c *gin.Context, id int64, post map[string]interface{}, data map[string]interface{}) {
  44. }
  45. func (FinalMaterialCart) EditPrivilege(c *gin.Context, id int64, data map[string]interface{}, post map[string]interface{}) error {
  46. var model FinalMaterialCart
  47. db.GetModel(map[string]interface{}{
  48. "id": id,
  49. "admin_id": c.GetInt("adminID"),
  50. "deleted_at": 0,
  51. }, &model)
  52. if model.ID == 0 {
  53. return errors.New("购物车错误")
  54. }
  55. if num, ok := data["num"]; ok {
  56. num, _ := db.ToFloat64(num)
  57. if num < 0 {
  58. return errors.New("数量错误")
  59. }
  60. if _, ok := data["total"]; !ok {
  61. var mat FinalMat
  62. db.GetModel(map[string]interface{}{
  63. "id": model.MatId,
  64. "deleted_at": 0,
  65. }, &mat)
  66. if mat.ID == 0 {
  67. return errors.New("材料错误")
  68. }
  69. data["total"] = utils.FloatMul(num, model.Price, 2)
  70. }
  71. }
  72. return nil
  73. }
  74. func (FinalMaterialCart) DelPrivilege(c *gin.Context, id int64) error {
  75. return nil
  76. }
  77. func (FinalMaterialCart) Page() bool {
  78. return false
  79. }
  80. func (FinalMaterialCart) Count() bool {
  81. return false
  82. }
  83. type FinalMaterialCartMobile struct {
  84. FinalMaterialCart
  85. Name string `json:"name" label:"名称" type:"string" prop:"select:concat(finalmat.brand,'\\t',finalmat.series,'\\t',finalmat.model,'\\t',finalmat.specs,'\\t',finalmat.color)"`
  86. TypeId int64 `json:"type_id" label:"类型" type:"int" prop:"select:finalmat.type_id"`
  87. SupplierId int64 `json:"supplier_id" label:"材料商ID" prop:"select:finalmat.supplier_id" search:"="`
  88. MatPrice float64 `json:"mat_price" label:"单价" prop:"select:finalmat.price"`
  89. SKU string `json:"sku" label:"SKU" prop:"select:finalmat.sku"`
  90. Brand string `json:"brand" label:"品牌" prop:"select:finalmat.brand"`
  91. Series string `json:"series" label:"系列" prop:"select:finalmat.series"`
  92. Model string `json:"model" label:"型号" prop:"select:finalmat.model"`
  93. Specs string `json:"specs" label:"规格" prop:"select:finalmat.specs"`
  94. Color string `json:"color" label:"颜色" prop:"select:finalmat.color"`
  95. Typename string `json:"type_name" label:"类型名称" prop:"select:finalmattype.name"`
  96. Type int64 `json:"type" label:"类型" type:"int" prop:"select:finalmattype.type" search:"="`
  97. Unit string `json:"unit" label:"单位" prop:"select:finalmattype.unit"`
  98. IsCustom int64 `json:"is_custom" label:"是否定制" prop:"select:finalmattype.is_custom" search:"="`
  99. SupplierName string `json:"supplier_name" type:"string" prop:"select:supplier.name"`
  100. SupplierAdminId string `json:"supplier_admin_id" type:"string" prop:"select:supplier.adminId"`
  101. SupplierReduce int64 `json:"supplier_reduce" type:"int" prop:"select:supplier.reduce"`
  102. SupplierPhone string `json:"supplier_phone" type:"string" prop:"select:admin.phone"`
  103. SupplierHeadImgUrl string `json:"supplier_headimgurl" type:"string" prop:"select:admin.headimgurl"`
  104. ItemName string `json:"item_name" label:"项目名称" prop:"select:finalmatitem.name"`
  105. }
  106. func (model FinalMaterialCartMobile) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
  107. if site_id, ok := data["site_id"]; ok {
  108. var site FinalSite
  109. db.GetModel(map[string]interface{}{
  110. "id": site_id,
  111. "deleted_at": 0,
  112. }, &site)
  113. if site.AreaId != 0 {
  114. s.Where = append(s.Where, fmt.Sprintf("`finalmat`.`area_id` = %s", s.Param(site.AreaId)))
  115. } else {
  116. s.Where = append(s.Where, "1=2")
  117. }
  118. } else {
  119. if _, ok := data["area_id"]; !ok {
  120. s.Where = append(s.Where, "1=2")
  121. }
  122. }
  123. s.Where = append(s.Where, fmt.Sprintf("`supplier`.`state` = %s", s.Param(1)))
  124. s.Where = append(s.Where, fmt.Sprintf("`finalmat`.`state` = %s", s.Param(1)))
  125. where := "1=2"
  126. //if admin.CheckAuth([]string{"final:material:take0"}, c.GetInt("adminID")) {
  127. // where = where + fmt.Sprintf(" OR `finalmattype`.`type` = %s", s.Param(0))
  128. //}
  129. if admin.CheckAuth([]string{"final:material:take1"}, c.GetInt("adminID")) || admin.CheckAuth([]string{"final:verify:audit"}, c.GetInt("adminID")) {
  130. where = where + fmt.Sprintf(" OR `finalmattype`.`type` = %s", s.Param(1))
  131. }
  132. suppliers := make([]FinalSupplier, 0)
  133. db.GetModel(map[string]interface{}{
  134. "adminId": c.GetInt("adminID"),
  135. "deleted_at": 0,
  136. "state": 1,
  137. }, &suppliers)
  138. supplierids := make([]int64, 0)
  139. for _, v := range suppliers {
  140. supplierids = append(supplierids, v.ID)
  141. }
  142. if len(supplierids) != 0 {
  143. where = where + fmt.Sprintf(" OR `finalmat`.`supplier_id` in %s", s.Param(supplierids))
  144. }
  145. where1 := "1=1"
  146. typeids := make([]int64, 0)
  147. if material, ok := data["material"]; ok {
  148. material, _ = db.ToInt64(material)
  149. if material != 0 {
  150. var mmodel FinalMaterial
  151. db.GetModel(map[string]interface{}{
  152. "id": material,
  153. "deleted_at": 0,
  154. }, &mmodel)
  155. var auxiliary FinalMatAuxiliary
  156. db.GetModel(map[string]interface{}{
  157. "id": mmodel.TypeId,
  158. "deleted_at": 0,
  159. }, &auxiliary)
  160. for _, v := range strings.Split(auxiliary.MatTypeIds, ",") {
  161. id, _ := db.ToInt64(v)
  162. typeids = append(typeids, id)
  163. var typ FinalMatType
  164. db.GetModel(map[string]interface{}{
  165. "id": id,
  166. "deleted_at": 0,
  167. }, &typ)
  168. path := typ.Path + db.ToString(typ.ID) + ","
  169. types := make([]FinalMatType, 0)
  170. db.GetModel(map[string]interface{}{
  171. "path like": path + "%",
  172. "deleted_at": 0,
  173. }, &types)
  174. for _, v := range types {
  175. typeids = append(typeids, v.ID)
  176. }
  177. }
  178. //s.Where = append(s.Where, fmt.Sprintf("`finalmat`.`type_id` in %s", s.Param(ids)))
  179. }
  180. }
  181. if len(typeids) == 0 {
  182. where1 += " and 1=2"
  183. } else {
  184. where1 += fmt.Sprintf(" and `finalmat`.`type_id` in %s", s.Param(typeids))
  185. }
  186. supplier_ids := make([]int64, 0)
  187. if brand, ok := data["auxiliary_brand"]; ok {
  188. brand, _ = db.ToInt64(brand)
  189. if brand != 0 {
  190. var bmodel FinalMatAuxiliaryBrand
  191. db.GetModel(map[string]interface{}{
  192. "id": brand,
  193. "deleted_at": 0,
  194. }, &bmodel)
  195. for _, v := range strings.Split(bmodel.SupplierIds, ",") {
  196. id, _ := db.ToInt64(v)
  197. supplier_ids = append(supplier_ids, id)
  198. }
  199. //s.Where = append(s.Where, fmt.Sprintf("`finalmat`.`supplier_id` in %s", s.Param(ids)))
  200. }
  201. }
  202. if len(supplier_ids) == 0 {
  203. where1 += " and 1=2"
  204. } else {
  205. where1 += fmt.Sprintf(" and `finalmat`.`supplier_id` in %s", s.Param(supplier_ids))
  206. }
  207. if admin.CheckAuth([]string{"final:material:take0"}, c.GetInt("adminID")) || admin.CheckAuth([]string{"final:verify:audit"}, c.GetInt("adminID")) {
  208. //where = where + fmt.Sprintf(" OR `finalmattype`.`type` = %s", s.Param(0))
  209. where = where + fmt.Sprintf(" OR (%s)", where1)
  210. }
  211. s.Where = append(s.Where, where)
  212. return model.FinalMaterialCart.ListPrivilege(c, data, s)
  213. }
  214. func (model FinalMaterialCartMobile) GroupBy() string {
  215. return fmt.Sprintf("`%s`.`id`", model.TableName())
  216. }
  217. func (model FinalMaterialCartMobile) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel {
  218. return append(model.FinalMaterialCart.LeftJoin(data, s), db.JoinModel{
  219. Model: FinalMat{},
  220. As: "finalmat",
  221. On: []string{"`finalmat`.`id` = " + model.TableName() + ".`mat_id`"},
  222. }, db.JoinModel{
  223. Model: FinalMatType{},
  224. As: "finalmattype",
  225. On: []string{"`finalmattype`.`id` = `finalmat`.`type_id`"},
  226. }, db.JoinModel{
  227. Model: FinalSupplier{},
  228. As: "supplier",
  229. On: []string{"`supplier`.`id` = `finalmat`.`supplier_id`"},
  230. }, db.JoinModel{
  231. Model: JoinAdmin{},
  232. As: "admin",
  233. On: []string{"`admin`.`id` = `supplier`.`adminId`"},
  234. }, db.JoinModel{
  235. Model: FinalMatItem{},
  236. As: "finalmatitem",
  237. On: []string{"`finalmatitem`.`id` = " + model.TableName() + ".`item_id`"},
  238. })
  239. }