123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- package final
- import (
- "errors"
- "fmt"
- _ "image/gif"
- _ "image/jpeg"
- _ "image/png"
- "strings"
- "zhiyuan/pkg/db"
- "zhiyuan/pkg/utils"
- "zhiyuan/services/admin"
- "github.com/gin-gonic/gin"
- )
- type FinalMaterialCart struct {
- ID int64 `json:"id" prop:"add:false"`
- ItemId int64 `json:"item_id" label:"项目类型ID" type:"int" prop:"edit"`
- AdminId int64 `json:"admin_id" label:"人员ID" type:"int" prop:"add:false"`
- ProcessId int64 `json:"process_id" label:"进度ID" type:"int" prop:"add:false"`
- MatId int64 `json:"mat_id" label:"材料ID" type:"int" prop:"add"`
- Price float64 `json:"price" label:"单价" type:"float" prop:"add:false"`
- Num float64 `json:"num" label:"数量" type:"float" prop:"add edit"`
- Remark string `json:"remark" label:"备注" type:"string" prop:"edit"`
- Total float64 `json:"total" label:"金额" type:"float" prop:"add:false" default:"0"`
- DeletedAt int64 `json:"deleted_at" prop:"add:false select:false"`
- CreatedAt int64 `json:"created_at" prop:"add:false"`
- UpdatedAt int64 `json:"updated_at" prop:"add:false"`
- db.BaseModel
- }
- func (FinalMaterialCart) TableName() string {
- return "zy_final_material_cart"
- }
- func (model FinalMaterialCart) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
- 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)))
- return true
- }
- func (FinalMaterialCart) OnePrivilege(c *gin.Context, id int64) bool {
- return false
- }
- func (FinalMaterialCart) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error {
- data["admin_id"] = c.GetInt("adminID")
- return nil
- }
- func (FinalMaterialCart) AddAfter(c *gin.Context, id int64, post map[string]interface{}, data map[string]interface{}) {
- }
- func (FinalMaterialCart) EditPrivilege(c *gin.Context, id int64, data map[string]interface{}, post map[string]interface{}) error {
- var model FinalMaterialCart
- db.GetModel(map[string]interface{}{
- "id": id,
- "admin_id": c.GetInt("adminID"),
- "deleted_at": 0,
- }, &model)
- if model.ID == 0 {
- return errors.New("购物车错误")
- }
- if num, ok := data["num"]; ok {
- num, _ := db.ToFloat64(num)
- if num < 0 {
- return errors.New("数量错误")
- }
- if _, ok := data["total"]; !ok {
- var mat FinalMat
- db.GetModel(map[string]interface{}{
- "id": model.MatId,
- "deleted_at": 0,
- }, &mat)
- if mat.ID == 0 {
- return errors.New("材料错误")
- }
- data["total"] = utils.FloatMul(num, model.Price, 2)
- }
- }
- return nil
- }
- func (FinalMaterialCart) DelPrivilege(c *gin.Context, id int64) error {
- return nil
- }
- func (FinalMaterialCart) Page() bool {
- return false
- }
- func (FinalMaterialCart) Count() bool {
- return false
- }
- type FinalMaterialCartMobile struct {
- FinalMaterialCart
- Name string `json:"name" label:"名称" type:"string" prop:"select:concat(finalmat.brand,'\\t',finalmat.series,'\\t',finalmat.model,'\\t',finalmat.specs,'\\t',finalmat.color)"`
- TypeId int64 `json:"type_id" label:"类型" type:"int" prop:"select:finalmat.type_id"`
- SupplierId int64 `json:"supplier_id" label:"材料商ID" prop:"select:finalmat.supplier_id" search:"="`
- MatPrice float64 `json:"mat_price" label:"单价" prop:"select:finalmat.price"`
- SKU string `json:"sku" label:"SKU" prop:"select:finalmat.sku"`
- Brand string `json:"brand" label:"品牌" prop:"select:finalmat.brand"`
- Series string `json:"series" label:"系列" prop:"select:finalmat.series"`
- Model string `json:"model" label:"型号" prop:"select:finalmat.model"`
- Specs string `json:"specs" label:"规格" prop:"select:finalmat.specs"`
- Color string `json:"color" label:"颜色" prop:"select:finalmat.color"`
- Typename string `json:"type_name" label:"类型名称" prop:"select:finalmattype.name"`
- Type int64 `json:"type" label:"类型" type:"int" prop:"select:finalmattype.type" search:"="`
- Unit string `json:"unit" label:"单位" prop:"select:finalmattype.unit"`
- IsCustom int64 `json:"is_custom" label:"是否定制" prop:"select:finalmattype.is_custom" search:"="`
- SupplierName string `json:"supplier_name" type:"string" prop:"select:supplier.name"`
- SupplierAdminId string `json:"supplier_admin_id" type:"string" prop:"select:supplier.adminId"`
- SupplierReduce int64 `json:"supplier_reduce" type:"int" prop:"select:supplier.reduce"`
- SupplierPhone string `json:"supplier_phone" type:"string" prop:"select:admin.phone"`
- SupplierHeadImgUrl string `json:"supplier_headimgurl" type:"string" prop:"select:admin.headimgurl"`
- ItemName string `json:"item_name" label:"项目名称" prop:"select:finalmatitem.name"`
- }
- func (model FinalMaterialCartMobile) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
- if site_id, ok := data["site_id"]; ok {
- var site FinalSite
- db.GetModel(map[string]interface{}{
- "id": site_id,
- "deleted_at": 0,
- }, &site)
- if site.AreaId != 0 {
- s.Where = append(s.Where, fmt.Sprintf("`finalmat`.`area_id` = %s", s.Param(site.AreaId)))
- } else {
- s.Where = append(s.Where, "1=2")
- }
- } else {
- if _, ok := data["area_id"]; !ok {
- s.Where = append(s.Where, "1=2")
- }
- }
- s.Where = append(s.Where, fmt.Sprintf("`supplier`.`state` = %s", s.Param(1)))
- s.Where = append(s.Where, fmt.Sprintf("`finalmat`.`state` = %s", s.Param(1)))
- where := "1=2"
- //if admin.CheckAuth([]string{"final:material:take0"}, c.GetInt("adminID")) {
- // where = where + fmt.Sprintf(" OR `finalmattype`.`type` = %s", s.Param(0))
- //}
- if admin.CheckAuth([]string{"final:material:take1"}, c.GetInt("adminID")) || admin.CheckAuth([]string{"final:verify:audit"}, c.GetInt("adminID")) {
- where = where + fmt.Sprintf(" OR `finalmattype`.`type` = %s", s.Param(1))
- }
- suppliers := make([]FinalSupplier, 0)
- db.GetModel(map[string]interface{}{
- "adminId": c.GetInt("adminID"),
- "deleted_at": 0,
- "state": 1,
- }, &suppliers)
- supplierids := make([]int64, 0)
- for _, v := range suppliers {
- supplierids = append(supplierids, v.ID)
- }
- if len(supplierids) != 0 {
- where = where + fmt.Sprintf(" OR `finalmat`.`supplier_id` in %s", s.Param(supplierids))
- }
- where1 := "1=1"
- typeids := make([]int64, 0)
- if material, ok := data["material"]; ok {
- material, _ = db.ToInt64(material)
- if material != 0 {
- var mmodel FinalMaterial
- db.GetModel(map[string]interface{}{
- "id": material,
- "deleted_at": 0,
- }, &mmodel)
- var auxiliary FinalMatAuxiliary
- db.GetModel(map[string]interface{}{
- "id": mmodel.TypeId,
- "deleted_at": 0,
- }, &auxiliary)
- for _, v := range strings.Split(auxiliary.MatTypeIds, ",") {
- id, _ := db.ToInt64(v)
- typeids = append(typeids, id)
- var typ FinalMatType
- db.GetModel(map[string]interface{}{
- "id": id,
- "deleted_at": 0,
- }, &typ)
- path := typ.Path + db.ToString(typ.ID) + ","
- types := make([]FinalMatType, 0)
- db.GetModel(map[string]interface{}{
- "path like": path + "%",
- "deleted_at": 0,
- }, &types)
- for _, v := range types {
- typeids = append(typeids, v.ID)
- }
- }
- //s.Where = append(s.Where, fmt.Sprintf("`finalmat`.`type_id` in %s", s.Param(ids)))
- }
- }
- if len(typeids) == 0 {
- where1 += " and 1=2"
- } else {
- where1 += fmt.Sprintf(" and `finalmat`.`type_id` in %s", s.Param(typeids))
- }
- supplier_ids := make([]int64, 0)
- if brand, ok := data["auxiliary_brand"]; ok {
- brand, _ = db.ToInt64(brand)
- if brand != 0 {
- var bmodel FinalMatAuxiliaryBrand
- db.GetModel(map[string]interface{}{
- "id": brand,
- "deleted_at": 0,
- }, &bmodel)
- for _, v := range strings.Split(bmodel.SupplierIds, ",") {
- id, _ := db.ToInt64(v)
- supplier_ids = append(supplier_ids, id)
- }
- //s.Where = append(s.Where, fmt.Sprintf("`finalmat`.`supplier_id` in %s", s.Param(ids)))
- }
- }
- if len(supplier_ids) == 0 {
- where1 += " and 1=2"
- } else {
- where1 += fmt.Sprintf(" and `finalmat`.`supplier_id` in %s", s.Param(supplier_ids))
- }
- if admin.CheckAuth([]string{"final:material:take0"}, c.GetInt("adminID")) || admin.CheckAuth([]string{"final:verify:audit"}, c.GetInt("adminID")) {
- //where = where + fmt.Sprintf(" OR `finalmattype`.`type` = %s", s.Param(0))
- where = where + fmt.Sprintf(" OR (%s)", where1)
- }
- s.Where = append(s.Where, where)
- return model.FinalMaterialCart.ListPrivilege(c, data, s)
- }
- func (model FinalMaterialCartMobile) GroupBy() string {
- return fmt.Sprintf("`%s`.`id`", model.TableName())
- }
- func (model FinalMaterialCartMobile) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel {
- return append(model.FinalMaterialCart.LeftJoin(data, s), db.JoinModel{
- Model: FinalMat{},
- As: "finalmat",
- On: []string{"`finalmat`.`id` = " + model.TableName() + ".`mat_id`"},
- }, db.JoinModel{
- Model: FinalMatType{},
- As: "finalmattype",
- On: []string{"`finalmattype`.`id` = `finalmat`.`type_id`"},
- }, db.JoinModel{
- Model: FinalSupplier{},
- As: "supplier",
- On: []string{"`supplier`.`id` = `finalmat`.`supplier_id`"},
- }, db.JoinModel{
- Model: JoinAdmin{},
- As: "admin",
- On: []string{"`admin`.`id` = `supplier`.`adminId`"},
- }, db.JoinModel{
- Model: FinalMatItem{},
- As: "finalmatitem",
- On: []string{"`finalmatitem`.`id` = " + model.TableName() + ".`item_id`"},
- })
- }
|