base_ent_wait_empower.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package entity
  2. import (
  3. "errors"
  4. "fmt"
  5. . "app.yhyue.com/moapp/jybase/common"
  6. . "app.yhyue.com/moapp/jybase/date"
  7. . "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/db"
  8. )
  9. var Base_ent_wait_empower = base_ent_wait_empower{}
  10. //企业授权表
  11. type base_ent_wait_empower struct {
  12. Id int64
  13. Appid string
  14. Goods_spec_power_id int64 //商品规格权益id
  15. Function_code string //功能代码
  16. Ent_id int64 //企业id
  17. Start_time string //开始时间
  18. End_time string //结束时间
  19. Empower_count int64 //授权数量
  20. Limit_strategy string //限制频率;1d:1天 1m:1个月
  21. }
  22. //根据商品规格获取待授权记录
  23. func (b *base_ent_wait_empower) WaitEmpowersBySpecId(appid string, spec_id, ent_id int64) *[]*base_ent_wait_empower {
  24. list := Mysql_BaseService.SelectBySql(`SELECT c.* FROM base_goods_spec a
  25. INNER JOIN base_goods_spec_power b ON (a.id=? AND a.appid=? AND a.id=b.spec_id)
  26. INNER JOIN base_ent_wait_empower c ON (c.ent_id=? AND c.end_time>? AND b.id=c.goods_spec_power_id)`, spec_id, appid, ent_id, NowFormat(Date_Full_Layout))
  27. return JsonUnmarshal(list, &[]*base_ent_wait_empower{}).(*[]*base_ent_wait_empower)
  28. }
  29. //根据功能代码获取待授权记录
  30. func (b *base_ent_wait_empower) WaitEmpowers(appid, function_code string, ent_id int64) *[]*base_ent_wait_empower {
  31. list := Mysql_BaseService.SelectBySql(`select * from base_ent_wait_empower where appid=? and function_code=? and ent_id=? and end_time>?`, appid, function_code, ent_id, NowFormat(Date_Full_Layout))
  32. return JsonUnmarshal(list, &[]*base_ent_wait_empower{}).(*[]*base_ent_wait_empower)
  33. }
  34. //所有待授权记录
  35. func (b *base_ent_wait_empower) AllWaitEmpowers(appid string, ent_id int64) *[]*base_ent_wait_empower {
  36. list := Mysql_BaseService.SelectBySql(`select * from base_ent_wait_empower where appid=? and ent_id=? and end_time>?`, appid, ent_id, NowFormat(Date_Full_Layout))
  37. return JsonUnmarshal(list, &[]*base_ent_wait_empower{}).(*[]*base_ent_wait_empower)
  38. }
  39. //校验功能代码
  40. func (b *base_ent_wait_empower) CheckBeforeEmpower(appid string, function_code []string, ent_id int64, ent_user_id []int64) (int64, error) {
  41. list := b.AllWaitEmpowers(appid, ent_id)
  42. if list == nil {
  43. return -1, errors.New(fmt.Sprintf("企业%d没有任何待授权的功能代码", ent_id))
  44. }
  45. m := map[string]int64{}
  46. for _, v := range *list {
  47. if v.Empower_count < 0 {
  48. m[v.Function_code] = v.Empower_count
  49. } else {
  50. m[v.Function_code] = m[v.Function_code] + v.Empower_count
  51. }
  52. }
  53. for _, v := range function_code {
  54. if count, ok := m[v]; !ok {
  55. return -1, errors.New(fmt.Sprintf("企业%d没有该待授权%s功能代码", ent_id, v))
  56. } else if count >= 0 && int64(len(ent_user_id)) > count {
  57. return -2, errors.New(fmt.Sprintf("企业%d功能代码%s可授权数量不足", ent_id, v))
  58. }
  59. }
  60. return 1, nil
  61. }
  62. //是否全部授权
  63. func (b *base_ent_wait_empower) IsAllEmpower(appid, function_code string, ent_id int64) bool {
  64. return Mysql_BaseService.CountBySql(`select count(1) as count from base_ent_wait_empower where appid=? and function_code=? and ent_id=? and end_time>? and empower_count=-1`, appid, function_code, ent_id, NowFormat(Date_Full_Layout)) > 0
  65. }