package final import ( "fmt" _ "image/gif" _ "image/jpeg" _ "image/png" "strings" "time" "zhiyuan/pkg/db" "zhiyuan/pkg/utils" "zhiyuan/services/admin" "github.com/gin-gonic/gin" ) type FinalMat struct { ID int64 `json:"id" prop:"add:false"` Name string `json:"name" label:"名称" type:"string" prop:"add:false select:concat(zy_final_mat.brand,'\\t',zy_final_mat.series,'\\t',zy_final_mat.model,'\\t',zy_final_mat.specs,'\\t',zy_final_mat.color)" search:"like"` TypeId int64 `json:"type_id" label:"类型" type:"int" prop:"add edit"` AreaId int64 `json:"area_id" label:"区域" type:"int" prop:"add" search:"="` SupplierId int64 `json:"supplier_id" label:"材料商ID" type:"int" prop:"add edit" search:"="` Price float64 `json:"price" label:"单价" type:"float" prop:"edit" default:"0"` ControlPrice float64 `json:"control_price" label:"控价" type:"float" prop:"edit" default:"-1"` SalesPrice float64 `json:"sales_price" label:"销售价" type:"float" prop:"edit" default:"-1"` PurchasePrice float64 `json:"purchase_price" label:"代购价" type:"float" prop:"edit" default:"-1"` SKU string `json:"sku" label:"SKU" type:"string" prop:"edit" search:"like"` Brand string `json:"brand" label:"品牌" type:"string" prop:"add edit" search:"like"` Series string `json:"series" label:"系列" type:"string" prop:"edit" search:"like"` Model string `json:"model" label:"型号" type:"string" prop:"add edit" search:"like"` Specs string `json:"specs" label:"规格" type:"string" prop:"edit" search:"like"` Color string `json:"color" label:"颜色" type:"string" prop:"edit" search:"like"` ControlPriceUse float64 `json:"control_price_use" label:"控价" type:"float" prop:"add:false select:if(zy_final_mat.control_price<0,zy_final_mat.price,zy_final_mat.control_price)"` SalesPriceCalc float64 `json:"sales_price_calc" label:"销售价" type:"float" prop:"add:false select:ROUND(if(zy_final_mat.control_price<0,zy_final_mat.price,zy_final_mat.control_price)/0.73,2)"` SalesPriceUse float64 `json:"sales_price_use" label:"销售价" type:"float" prop:"add:false select:if(zy_final_mat.sales_price<0,ROUND(if(zy_final_mat.control_price<0,zy_final_mat.price,zy_final_mat.control_price)/0.73,2),zy_final_mat.sales_price)"` PurchasePriceCalc float64 `json:"purchase_price_calc" label:"销售价" type:"float" prop:"add:false select:ROUND(if(zy_final_mat.control_price<0,zy_final_mat.price,zy_final_mat.control_price)/0.84,2)"` PurchasePriceUse float64 `json:"purchase_price_use" label:"代购价" type:"float" prop:"add:false select:if(zy_final_mat.purchase_price<0,ROUND(if(zy_final_mat.control_price<0,zy_final_mat.price,zy_final_mat.control_price)/0.84,2),zy_final_mat.purchase_price)"` State int64 `json:"state" label:"状态" type:"int" prop:"edit" default:"0" search:"="` 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 (FinalMat) TableName() string { return "zy_final_mat" } func ControlPrice(controlPrice float64, price float64) float64 { if controlPrice < 0 { return price } return controlPrice } func SalesCalculatePrice(controlPrice float64) float64 { return utils.FloatDiv(controlPrice, 0.73, 2) } func SalesPrice(salesPrice float64, controlPrice float64) float64 { if salesPrice < 0 { return SalesCalculatePrice(controlPrice) } return salesPrice } func PurchaseCalculatePrice(controlPrice float64) float64 { return utils.FloatDiv(controlPrice, 0.84, 2) } func PurchasePrice(purchasePrice float64, controlPrice float64) float64 { if purchasePrice < 0 { return PurchaseCalculatePrice(controlPrice) } return purchasePrice } func (model FinalMat) ControlPrices() float64 { return ControlPrice(model.ControlPrice, model.Price) } func (model FinalMat) SalesPrices() float64 { return SalesPrice(model.SalesPrice, ControlPrice(model.ControlPrice, model.Price)) } func (model FinalMat) PurchasePrices() float64 { return PurchasePrice(model.PurchasePrice, ControlPrice(model.ControlPrice, model.Price)) } func (model FinalMat) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool { if typeid, ok := data["type_id"]; ok { ids := make([]int64, 0) typid, _ := db.ToInt64(typeid) ids = append(ids, typid) var typ FinalMatType db.GetModel(map[string]interface{}{ "id": typid, "deleted_at": 0, }, &typ) if typ.ID != 0 { 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 { ids = append(ids, v.ID) } s.Where = append(s.Where, fmt.Sprintf("`%s`.`type_id` in %s", model.TableName(), s.Param(ids))) } } typeIds := make([]int64, 0) if type_ids, ok := data["type_ids"]; ok { typeids := strings.Split(db.ToString(type_ids), ",") for _, v := range typeids { if v != "" { if t, ok := db.ToInt64(v); ok { typeIds = append(typeIds, t) } } } } if len(typeIds) > 0 { ids := make([]int64, 0) for _, typeId := range typeIds { ids = append(ids, typeId) var typ FinalMatType db.GetModel(map[string]interface{}{ "id": typeId, "deleted_at": 0, }, &typ) if typ.ID != 0 { 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 { ids = append(ids, v.ID) } } } s.Where = append(s.Where, fmt.Sprintf("`%s`.`type_id` in %s", model.TableName(), s.Param(ids))) } if supplier_ids, ok := data["supplier_ids"]; ok { supplierIds := make([]int64, 0) supplierids := strings.Split(db.ToString(supplier_ids), ",") for _, v := range supplierids { if v != "" { if t, ok := db.ToInt64(v); ok { supplierIds = append(supplierIds, t) } } } if len(supplierIds) != 0 { s.Where = append(s.Where, fmt.Sprintf("`%s`.`supplier_id` in %s", model.TableName(), s.Param(supplierIds))) } } if exclude_ids, ok := data["exclude_ids"]; ok { excludeIds := make([]int64, 0) excludeids := strings.Split(db.ToString(exclude_ids), ",") for _, v := range excludeids { if v != "" { if t, ok := db.ToInt64(v); ok { excludeIds = append(excludeIds, t) } } } if len(excludeIds) != 0 { s.Where = append(s.Where, fmt.Sprintf("`%s`.`id` not in %s", model.TableName(), s.Param(excludeIds))) } } return true } func (model FinalMat) ListAfter(c *gin.Context, data map[string]interface{}, list []map[string]interface{}) []map[string]interface{} { /*for n, item := range list { price, _ := db.ToFloat64(item["price"]) controlPrice, _ := db.ToFloat64(item["control_price"]) salesPrice, _ := db.ToFloat64(item["sales_price"]) purchasePrice, _ := db.ToFloat64(item["purchase_price"]) list[n]["control_price_use"] = ControlPrice(controlPrice, price) list[n]["sales_price_use"] = SalesPrice(salesPrice, price) list[n]["purchase_price_use"] = PurchasePrice(purchasePrice, price) }*/ return list } func (FinalMat) OnePrivilege(c *gin.Context, id int64) bool { return true } func (FinalMat) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error { return nil } func (FinalMat) AddAfter(c *gin.Context, id int64, post map[string]interface{}, data map[string]interface{}) { } func (FinalMat) EditPrivilege(c *gin.Context, id int64, data map[string]interface{}, post map[string]interface{}) error { if _, ok := data["price"]; ok { data["state"] = 0 } if state, ok := data["state"]; ok { state, _ := db.ToInt64(state) if state == 0 { db.UpdateModels(db.Type(FinalMaterialCart{}), map[string]interface{}{ "mat_id": id, }, map[string]interface{}{ "deleted_at": time.Now().Unix(), }) } } return nil } func (FinalMat) DelPrivilege(c *gin.Context, id int64) error { return nil } func (FinalMat) Page() bool { return false } func (FinalMat) Count() bool { return true } type FinalMatMobile struct { Typename string `json:"type_name" label:"类型名称" prop:"select:finalmattype.name" search:"like"` Type int64 `json:"type" label:"类型" type:"int" prop:"select:finalmattype.type" search:"="` Typepath string `json:"type_path" prop:"select:finalmattype.path"` Unit string `json:"unit" label:"单位" prop:"select:finalmattype.unit" search:"like"` IsCustom int64 `json:"is_custom" label:"是否定制" prop:"select:finalmattype.is_custom" search:"="` SupplierName string `json:"supplier_name" type:"string" prop:"select:supplier.name"` SupplierAdminId int64 `json:"supplier_admin_id" type:"int" prop:"select:supplier.adminId"` SupplierPhone string `json:"supplier_phone" type:"string" prop:"select:admin.phone"` SupplierHeadImgUrl string `json:"supplier_headimgurl" type:"string" prop:"select:admin.headimgurl"` FinalMat } func (model FinalMatMobile) 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("`%s`.`area_id` = %s", model.TableName(), 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("`%s`.`state` = %s", model.TableName(), s.Param(1))) where := "1=2" 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"), "state": 1, "deleted_at": 0, }, &suppliers) supplierids := make([]int64, 0) for _, v := range suppliers { supplierids = append(supplierids, v.ID) } if len(supplierids) != 0 { where = where + fmt.Sprintf(" OR `%s`.`supplier_id` in %s", model.TableName(), 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) } } } } if len(typeids) == 0 { where1 += " and 1=2" } else { where1 += fmt.Sprintf(" and `%s`.`type_id` in %s", model.TableName(), 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) } } } if len(supplier_ids) == 0 { where1 += " and 1=2" } else { where1 += fmt.Sprintf(" and `%s`.`supplier_id` in %s", model.TableName(), 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.FinalMat.ListPrivilege(c, data, s) } func (model FinalMatMobile) GroupBy() string { return fmt.Sprintf("`%s`.`id`", model.TableName()) } func (model FinalMatMobile) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel { return append(model.FinalMat.LeftJoin(data, s), db.JoinModel{ Model: FinalMatType{}, As: "finalmattype", On: []string{"`finalmattype`.`id` = " + model.TableName() + ".`type_id`"}, }, db.JoinModel{ Model: FinalSupplier{}, As: "supplier", On: []string{"`supplier`.`id` = " + model.TableName() + ".`supplier_id`"}, }, db.JoinModel{ Model: JoinAdmin{}, As: "admin", On: []string{"`admin`.`id` = `supplier`.`adminId`"}, }) } type FinalMatPC struct { Typename string `json:"type_name" label:"类型名称" prop:"select:finalmattype.name" search:"like"` Type int64 `json:"type" label:"类型" type:"int" prop:"select:finalmattype.type" search:"="` Typepath string `json:"type_path" prop:"select:finalmattype.path"` Unit string `json:"unit" label:"单位" prop:"select:finalmattype.unit" search:"like"` SupplierId int64 `json:"supplier_id" type:"int" prop:"select:supplier.id"` SupplierName string `json:"supplier_name" type:"string" prop:"select:supplier.name"` SupplierAdminId string `json:"supplier_admin_id" type:"string" prop:"select:supplier.adminId"` FinalMat } func (model FinalMatPC) ExportFields() []db.ExportField { return []db.ExportField{ { Label: "品牌", Name: "brand", Width: 20, }, { Label: "系列", Name: "series", Width: 20, }, { Label: "型号", Name: "model", Width: 30, }, { Label: "规格", Name: "specs", Width: 20, }, { Label: "颜色", Name: "color", Width: 20, }, { Label: "SKU", Name: "sku", }, { Label: "类型", Name: "type_name", Width: 30, }, { Label: "材料商", Name: "supplier_name", Width: 30, }, { Label: "供货价", Name: "price", Width: 20, }, { Label: "控价", Name: "control_price", Width: 20, }, { Label: "销售核算价", Name: "sales_price_calc", Width: 20, }, { Label: "销售价", Name: "sales_price", Width: 20, }, { Label: "代购核算价", Name: "purchase_price_calc", Width: 20, }, { Label: "代购价", Name: "purchase_price", Width: 20, }, } } func (FinalMatPC) ExportValue(row map[string]interface{}, rowIndex int, field db.ExportField, data []map[string]interface{}) string { switch field.Name { case "type_name": ids := make([]int64, 0) for _, v := range strings.Split(db.ToString(row["type_path"]), ",") { if id, ok := db.ToInt64(v); ok && id != 0 { ids = append(ids, id) } } id, _ := db.ToInt64(db.ToString(row["type_id"])) ids = append(ids, id) types := make([]FinalMatType, 0) db.GetModel(map[string]interface{}{ "id in": ids, "deleted_at": 0, }, &types) typeMap := make(map[int64]FinalMatType) for _, v := range types { typeMap[v.ID] = v } names := make([]string, 0) for _, id := range ids { if typ, ok := typeMap[id]; ok { names = append(names, typ.Name) } } return strings.Join(names, "/") case "control_price": price, _ := db.ToFloat64(row["control_price"]) if price < 0 { return "" } return db.ToString(price) case "sales_price": price, _ := db.ToFloat64(row["sales_price"]) if price < 0 { return "" } return db.ToString(price) case "purchase_price": price, _ := db.ToFloat64(row["purchase_price"]) if price < 0 { return "" } return db.ToString(price) } return db.ToString(row[field.Name]) } func (model FinalMatPC) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool { //if _, ok := data["area_id"]; !ok { // s.Where = append(s.Where, "1=2") //} if control_price, ok := data["control_price"]; ok { controlPrice, _ := db.ToFloat64(control_price) convertSql := "" convert1Sql := "" if convert, ok := data["convert"]; ok { if mp, ok := convert.(map[string]interface{}); ok && len(mp) != 0 { convertSql = "*(CASE finalmattype.name " for name, v := range mp { c, _ := db.ToFloat64(v) convertSql += fmt.Sprintf("WHEN %s THEN %s ", s.Param(name), s.Param(c)) } convertSql += "ELSE 1 END)" } else { c, _ := db.ToFloat64(convert) if c != 0 { convertSql = fmt.Sprintf("*%s", s.Param(c)) } } } if convert, ok := data["converts"]; ok { if mp, ok := convert.(map[string]interface{}); ok && len(mp) != 0 { convertSql = "*(CASE finalmattype.id " for name, v := range mp { c, _ := db.ToFloat64(v) convertSql += fmt.Sprintf("WHEN %s THEN %s ", s.Param(name), s.Param(c)) } convertSql += "ELSE 1 END)" } else { c, _ := db.ToFloat64(convert) if c != 0 { convertSql = fmt.Sprintf("*%s", s.Param(c)) } } } if convert, ok := data["converts1"]; ok { if mp, ok := convert.(map[string]interface{}); ok && len(mp) != 0 { convert1Sql = "*(CASE finalmattype.id " for name, v := range mp { c, _ := db.ToFloat64(v) convert1Sql += fmt.Sprintf("WHEN %s THEN %s ", s.Param(name), s.Param(c)) } convert1Sql += "ELSE 1 END)" } else { c, _ := db.ToFloat64(convert) if c != 0 { convert1Sql = fmt.Sprintf("*%s", s.Param(c)) } } } s.Select["upgrade_unit"] = "finalmattype.unit" if upgrade_unit, ok := data["upgrade_unit"]; ok { s.Select["upgrade_unit"] = fmt.Sprintf("%s", s.Param(upgrade_unit)) } s.Select["upgrade_price"] = fmt.Sprintf("if(if(`%s`.`control_price`<0,`%s`.`price`,`%s`.`control_price`)%s>%s,CEILING(ROUND((if(`%s`.`control_price`<0,`%s`.`price`,`%s`.`control_price`)%s-%s)/0.84%s,4)*100)/100,0)", model.TableName(), model.TableName(), model.TableName(), convertSql, s.Param(controlPrice), model.TableName(), model.TableName(), model.TableName(), convertSql, s.Param(controlPrice), convert1Sql) if standard, ok := data["standard"]; ok { standards, _ := db.ToInt64(standard) if standards == 1 { s.Where = append(s.Where, fmt.Sprintf("if(`%s`.`control_price`<0,`%s`.`price`,`%s`.`control_price`)%s<=%s", model.TableName(), model.TableName(), model.TableName(), convertSql, s.Param(controlPrice))) } } } return model.FinalMat.ListPrivilege(c, data, s) } func (model FinalMatPC) GroupBy() string { return fmt.Sprintf("`%s`.`id`", model.TableName()) } func (model FinalMatPC) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel { return append(model.FinalMat.LeftJoin(data, s), db.JoinModel{ Model: FinalMatType{}, As: "finalmattype", On: []string{"`finalmattype`.`id` = " + model.TableName() + ".`type_id`"}, }, db.JoinModel{ Model: FinalSupplier{}, As: "supplier", On: []string{"`supplier`.`id` = " + model.TableName() + ".`supplier_id`"}, }) } type FinalMatPCSupplier struct { FinalMatPC } func (model FinalMatPCSupplier) GroupBy() string { return "`supplier`.`id`" }