package final import ( "errors" "fmt" "zhiyuan/pkg/db" "zhiyuan/pkg/logger" "zhiyuan/pkg/utils" "zhiyuan/pkg/weixin/mp" "zhiyuan/services/admin" "zhiyuan/services/shop" "github.com/gin-gonic/gin" ) type FinalSiteCollect struct { ID int64 `json:"id" prop:"add:false"` ShopID int64 `json:"shop_id" label:"门店" type:"int" prop:"add:false" default:"0" search:"multiple"` Type int64 `json:"type" label:"类型" type:"int" default:"0" search:"multiple"` Total float64 `json:"total" label:"合计金额" type:"float" prop:"add:false" search:"="` State int64 `json:"state" label:"状态" type:"int" prop:"add:false" default:"0" search:"multiple"` CreatedId int64 `json:"created_id" label:"创建人员" type:"int" prop:"add:false" 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 (FinalSiteCollect) TableName() string { return "zy_final_site_collect" } func (FinalSiteCollect) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error { typ, _ := db.ToInt64(post["type"]) signature := db.ToString(post["signature"]) if signature == "" { return errors.New("没有权限") } if typ == 0 { type FinalSiteTableCollects struct { ShopId int64 `json:"shop_id" label:"门店" type:"int" prop:"select:finalsite.shop_id" search:"="` Total float64 `json:"total" label:"合计" type:"float" prop:"select:ROUND(sum(finalsiteitem.price*num))"` FinalSiteTableCollect } if tableIds, ok := db.ToArray(post["table_ids"]); ok { models := make([]FinalSiteTableCollects, 0) db.GetModel(map[string]interface{}{ "id in": tableIds, "collect_id": 0, "state": 2, "deleted_at": 0, }, &models) if len(tableIds) != len(models) { return errors.New("项目错误") } if len(models) == 0 { return errors.New("没有权限") } shopId := models[0].ShopId total := float64(0) for _, v := range models { if v.ShopId != shopId { return errors.New("没有权限") } total += v.Total } data["shop_id"] = shopId data["total"] = total data["state"] = 3 data["created_id"] = c.GetInt("adminID") return nil } } else if typ == 1 { type FinalSettleCollects struct { ShopId int64 `json:"shop_id" label:"门店" type:"int" prop:"select:finalsite.shop_id"` //ProjectLeaderId int `json:"project_leader_id" label:"工程队长" type:"int" prop:"select:finalsite.project_leader_id"` Total float64 `json:"total" label:"合计" type:"float"` FinalSettleMobile } if settleIds, ok := db.ToArray(post["settle_ids"]); ok { models := make([]FinalSettleCollects, 0) db.GetModel(map[string]interface{}{ "id in": settleIds, "collect_id": 0, "state": 3, "deleted_at": 0, }, &models) if len(settleIds) != len(models) { return errors.New("项目错误") } if len(models) == 0 { return errors.New("没有权限") } shopId := models[0].ShopId total := float64(0) //leaderIds := []string{} for _, v := range models { if v.ShopId != shopId { return errors.New("没有权限") } total += v.Total /*leaderId := strconv.Itoa(v.ProjectLeaderId) for _, id := range leaderIds { if id == leaderId { leaderId = "" break } } if leaderId != "" { leaderIds = append(leaderIds, strconv.Itoa(v.ProjectLeaderId)) }*/ } data["shop_id"] = shopId data["total"] = total data["state"] = 3 //data["leader_ids"] = strings.Join(leaderIds, ",") data["created_id"] = c.GetInt("adminID") return nil } } else if typ == 2 { /*type FinalMaterialOrderCollects struct { ProjectLeaderId int `json:"project_leader_id" label:"工程队长" type:"int" prop:"select:finalsite.project_leader_id"` FinalMaterialOrderMobile }*/ if settleIds, ok := db.ToArray(post["settle_ids"]); ok { models := make([]FinalMaterialSettle, 0) db.GetModel(map[string]interface{}{ "id in": settleIds, "collect_id": 0, "state": 2, "deleted_at": 0, }, &models) if len(settleIds) != len(models) { return errors.New("项目错误") } if len(models) == 0 { return errors.New("没有权限") } /*orders := make([]FinalMaterialOrderCollects, 0) db.GetModel(map[string]interface{}{ "settle.id in": settleIds, }, &orders) if len(orders) == 0 { return errors.New("没有权限") }*/ shopId := models[0].ShopId total := float64(0) //leaderIds := []string{} for _, v := range models { if v.ShopId != shopId { return errors.New("没有权限") } total += v.Total } /*for _, v := range orders { leaderId := strconv.Itoa(v.ProjectLeaderId) for _, id := range leaderIds { if id == leaderId { leaderId = "" break } } if leaderId != "" { leaderIds = append(leaderIds, strconv.Itoa(v.ProjectLeaderId)) } }*/ data["shop_id"] = shopId data["total"] = total data["state"] = 4 //data["leader_ids"] = strings.Join(leaderIds, ",") data["created_id"] = c.GetInt("adminID") return nil } } return errors.New("没有权限") } func (FinalSiteCollect) AddAfter(c *gin.Context, id int64, post map[string]interface{}, data map[string]interface{}) { signature := db.ToString(post["signature"]) explain := db.ToString(post["explain"]) pictures := db.ToString(post["pictures"]) typ, _ := db.ToInt64(post["type"]) if typ == 0 { db.InsertModel(db.Type(FinalSiteProcess{}), map[string]interface{}{ "collect_id": id, "explain": explain, "pictures": pictures, "type": 2, "signature": signature, "state": 0, "created_id": c.GetInt("adminID"), }) if tableIds, ok := db.ToArray(post["table_ids"]); ok { db.UpdateModels(db.Type(FinalSiteTable{}), map[string]interface{}{ "id in": tableIds, }, map[string]interface{}{ "state": 3, "collect_id": id, }) } var collect FinalSiteCollect db.GetModel(map[string]interface{}{ "id": id, }, &collect) s, _ := shop.GetOne(map[string]interface{}{ "id": collect.ShopID, }, nil, nil) if s == nil { return } query := "SELECT `id` from `zy_admin` where FIND_IN_SET({{shopID}}, `shop_ids`) and FIND_IN_SET({{roleID}}, `role_ids`) and `state` = 1" params := make(map[string]interface{}) params["shopID"] = collect.ShopID params["roleID"] = 18 admins, _ := db.QueryMap(query, params, nil) for _, admin := range admins { id, _ := db.ToInt64(admin["id"]) SendCollectMessage(int(id), collect.ID, s.ShopName, "工人汇总请审核") } } else if typ == 1 { db.InsertModel(db.Type(FinalSettleProcess{}), map[string]interface{}{ "collect_id": id, "explain": explain, "pictures": pictures, "type": 3, "signature": signature, "state": 0, "created_id": c.GetInt("adminID"), }) if settleIds, ok := db.ToArray(post["settle_ids"]); ok { db.UpdateModels(db.Type(FinalSettle{}), map[string]interface{}{ "id in": settleIds, }, map[string]interface{}{ "state": 4, "collect_id": id, }) } var collect FinalSiteCollect db.GetModel(map[string]interface{}{ "id": id, }, &collect) s, _ := shop.GetOne(map[string]interface{}{ "id": collect.ShopID, }, nil, nil) if s == nil { return } query := "SELECT `id` from `zy_admin` where FIND_IN_SET({{shopID}}, `shop_ids`) and FIND_IN_SET({{roleID}}, `role_ids`) and `state` = 1" params := make(map[string]interface{}) params["shopID"] = collect.ShopID params["roleID"] = 18 admins, _ := db.QueryMap(query, params, nil) for _, admin := range admins { id, _ := db.ToInt64(admin["id"]) SendCollectMessage(int(id), collect.ID, s.ShopName, "结算汇总请审核") } } else if typ == 2 { db.InsertModel(db.Type(FinalMaterialSettleProcess{}), map[string]interface{}{ "collect_id": id, "explain": explain, "pictures": pictures, "type": 2, "signature": signature, "state": 0, "created_id": c.GetInt("adminID"), }) if settleIds, ok := db.ToArray(post["settle_ids"]); ok { db.UpdateModels(db.Type(FinalMaterialSettle{}), map[string]interface{}{ "id in": settleIds, }, map[string]interface{}{ "state": 4, "collect_id": id, }) } } } func (model FinalSiteCollect) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool { if !admin.IsSuperAdmin(c.GetInt("adminID")) { adminID := s.Param(c.GetInt("adminID")) where := fmt.Sprintf("`%s`.`created_id` = %s", model.TableName(), adminID) if admin.CheckAuth([]string{"final:verify"}, c.GetInt("adminID")) { info, _ := admin.GetInfoByID(c.GetInt("adminID"), nil, nil) if info == nil { return false } where = fmt.Sprintf("%s OR FIND_IN_SET(`%s`.`shop_id`, %s)", where, model.TableName(), s.Param(info.ShopIds)) } s.Where = append(s.Where, fmt.Sprintf("(%s)", where)) } if state, ok := data["state"]; ok { if n, ok := db.ToInt64(state); ok { if n != 0 { s.Having = append(s.Having, fmt.Sprintf("`%s`.`state` = %s", model.TableName(), s.Param(n))) } } } return true } func (FinalSiteCollect) OnePrivilege(c *gin.Context, id int64) bool { return true } func (FinalSiteCollect) Page() bool { return false } func (FinalSiteCollect) Count() bool { return true } type FinalSiteCollectMobile struct { CreatedName string `json:"created_name" prop:"select:admin.username"` CreatedPhone string `json:"created_phone" prop:"select:admin.phone"` ShopName string `json:"shop_name" prop:"select:shop.shop_name"` FinalSiteCollect } func (model FinalSiteCollectMobile) GroupBy() string { return fmt.Sprintf("`%s`.`id`", model.TableName()) } type JoinShop struct { FinalSiteCollect } func (JoinShop) TableName() string { return "zy_shop" } func (model FinalSiteCollectMobile) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel { return append(model.FinalSiteCollect.LeftJoin(data, s), db.JoinModel{ Model: JoinShop{}, As: "shop", On: []string{"`shop`.`id` = " + model.TableName() + ".`shop_id`"}, }, db.JoinModel{ Model: JoinAdmin{}, As: "admin", On: []string{"`admin`.`id` = " + model.TableName() + ".`created_id`"}, }) } func SendCollectMessage(adminID int, orderId int64, address string, text string) error { logger.Sugar.Infof("SendCollectMessage, send: %v %v %v %v", adminID, orderId, address, text) client, err := mp.NewClient(1) if err != nil { return err } adminInfo, err := admin.GetInfoByID(adminID, nil, nil) if err != nil { return err } if adminInfo == nil || adminInfo.OpenId == "" { return nil } url := "" if orderId != 0 { url = "https://jz.nczyzs.com/final/#/collect/" + db.ToString(orderId) } ret, err := client.SendTemplateMessage(adminInfo.OpenId, "0tdzToVtXoCUrafmN-ub9J69HKKCfUWEsCan-WkuPKk", url, map[string]interface{}{ "character_string1": map[string]interface{}{ "value": utils.SubStr(db.ToString(orderId), 0, 32), }, "thing2": map[string]interface{}{ "value": utils.SubStr(address, 0, 20), }, "thing5": map[string]interface{}{ "value": utils.SubStr(text, 0, 20), }, }) logger.Sugar.Infof("SendCollectMessage, ret: %v %v", ret, err) return err }