power.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package service
  2. import (
  3. "errors"
  4. "time"
  5. . "app.yhyue.com/moapp/jybase/date"
  6. . "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/entity"
  7. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/pb"
  8. )
  9. /*
  10. * 开通权益
  11. * @param appid
  12. * @param goods_code 商品代码
  13. * @param goods_spec_id 商品规格id
  14. * @param account_id 账户id
  15. * @param ent_id 企业id
  16. * @param buy_num 购买数量
  17. * @param start_time 开始时间
  18. * @param end_time 结束时间
  19. * @return error
  20. */
  21. func OpenPower(appid, goods_code string, goods_spec_id, account_id, ent_account_id, ent_id int64, buy_num int64, start_time, end_time string) error {
  22. if appid == "" {
  23. return errors.New("无效的参数appid")
  24. } else if goods_code == "" {
  25. return errors.New("无效的参数goods_code")
  26. } else if account_id == 0 && ent_account_id == 0 {
  27. return errors.New("无效的参数account_id、ent_account_id")
  28. }
  29. if start_time != "" {
  30. if _, err := time.ParseInLocation(Date_Full_Layout, start_time, time.Local); err != nil {
  31. return errors.New("无效的参数start_time," + err.Error())
  32. }
  33. }
  34. if end_time != "" {
  35. if _, err := time.ParseInLocation(Date_Full_Layout, end_time, time.Local); err != nil {
  36. return errors.New("无效的参数end_time," + err.Error())
  37. }
  38. }
  39. list := Base_goods_spec.FindById(goods_spec_id, appid, goods_code)
  40. if list == nil || len(*list) == 0 {
  41. return errors.New("没有找到该商品规格")
  42. } else if Base_power.OpenPower(goods_spec_id, appid, goods_code, account_id, ent_account_id, ent_id, buy_num, start_time, end_time, list) {
  43. return nil
  44. } else {
  45. return errors.New("开通失败")
  46. }
  47. }
  48. /*
  49. * 取消权益
  50. * @param appid
  51. * @param goods_code 商品代码
  52. * @param goods_spec_id 商品规格id
  53. * @param account_id 账户id
  54. * @return 是否成功
  55. */
  56. func CancelPower(appid, goods_code string, goods_spec_id, account_id, ent_account_id int64) (bool, error) {
  57. if appid == "" {
  58. return false, errors.New("无效的参数appid")
  59. } else if goods_code == "" {
  60. return false, errors.New("无效的参数goods_code")
  61. } else if account_id == 0 && ent_account_id == 0 {
  62. return false, errors.New("无效的参数account_id、ent_account_id")
  63. }
  64. return Base_power.CancelPower(appid, goods_code, goods_spec_id, account_id, ent_account_id), nil
  65. }
  66. /*
  67. * 获取待授权详情
  68. * @param appid
  69. * @param function_code 功能代码
  70. * @param ent_id 企业id
  71. * @return 待授权详情
  72. */
  73. func WaitEmpowerDetail(appid, function_code string, ent_id int64) (*WaitEmpowerData, error) {
  74. result := &WaitEmpowerData{}
  75. if appid == "" {
  76. return result, errors.New("无效的参数appid")
  77. } else if function_code == "" {
  78. return result, errors.New("无效的参数function_code")
  79. } else if ent_id == 0 {
  80. return result, errors.New("无效的参数ent_id")
  81. }
  82. bewes := Base_ent_wait_empower.WaitEmpowers(appid, function_code, ent_id)
  83. if bewes != nil {
  84. for _, v := range *bewes {
  85. result.Empower_count = v.Empower_count
  86. result.Limit_strategy = v.Limit_strategy
  87. result.Start_time = v.Start_time
  88. result.End_time = v.End_time
  89. result.Use_count = v.Use_count
  90. break
  91. }
  92. }
  93. return result, nil
  94. }
  95. /*
  96. * 获取已有的权益
  97. * @param appid
  98. * @param account_id 账户id
  99. * @param ent_account_id 企业账户id
  100. * @param ent_id 企业id
  101. * @param ent_user_id 企业用户id
  102. * @return 所有已开通的权益
  103. */
  104. func HasPowers(appid string, account_id, ent_account_id, ent_id, ent_user_id int64) ([]string, error) {
  105. result := []string{}
  106. if appid == "" {
  107. return result, errors.New("无效的参数appid")
  108. } else if account_id == 0 && ent_account_id == 0 {
  109. return result, errors.New("无效的参数account_id、ent_account_id")
  110. }
  111. m := map[string]bool{}
  112. bps := Base_power.FindMyPowers(appid, account_id, ent_account_id)
  113. if bps != nil {
  114. for _, v := range *bps {
  115. if v.Power_type == 1 || (v.Power_type == 2 && Base_ent_empower.HasEmpower(appid, v.Function_code, ent_id, ent_user_id)) {
  116. if m[v.Function_code] {
  117. continue
  118. }
  119. m[v.Function_code] = true
  120. result = append(result, v.Function_code)
  121. }
  122. }
  123. }
  124. return result, nil
  125. }
  126. /*
  127. * 重新授权
  128. * @param appid
  129. * @param function_code 功能代码
  130. * @param ent_id 企业id
  131. * @param ent_user_id 企业用户id
  132. * @return 0:失败 1:成功 -2:数量不足
  133. */
  134. func ReEmpower(appid, function_code string, ent_id int64, ent_user_id []int64) (int64, error) {
  135. if appid == "" {
  136. return 0, errors.New("无效的参数appid")
  137. } else if function_code == "" {
  138. return 0, errors.New("无效的参数function_code")
  139. } else if ent_id == 0 {
  140. return 0, errors.New("无效的参数ent_id")
  141. } else if len(ent_user_id) == 0 {
  142. return 0, errors.New("无效的参数ent_user_id")
  143. }
  144. bewes := Base_ent_wait_empower.WaitEmpowers(appid, function_code, ent_id)
  145. if bewes == nil {
  146. return -1, nil
  147. }
  148. count := 0
  149. for _, v := range *bewes {
  150. count += v.Empower_count
  151. }
  152. if len(ent_user_id) > count {
  153. return -2, nil
  154. }
  155. if Base_ent_empower.ReEmpower(appid, function_code, ent_id, ent_user_id) {
  156. return 1, nil
  157. }
  158. return 0, nil
  159. }
  160. /*
  161. * 取消授权
  162. * @param appid
  163. * @param function_code 功能代码
  164. * @param ent_id 企业id
  165. * @param ent_user_id 企业用户id
  166. * @return 0:失败 1:成功
  167. */
  168. func CancelEmpower(appid, function_code string, ent_id int64, ent_user_id []int64) (int64, error) {
  169. if appid == "" {
  170. return 0, errors.New("无效的参数appid")
  171. } else if function_code == "" {
  172. return 0, errors.New("无效的参数function_code")
  173. } else if ent_id == 0 {
  174. return 0, errors.New("无效的参数ent_id")
  175. } else if len(ent_user_id) == 0 {
  176. return 0, errors.New("无效的参数ent_user_id")
  177. }
  178. if Base_ent_empower.CancelEmpower(appid, function_code, ent_id, ent_user_id) {
  179. return 1, nil
  180. }
  181. return 0, nil
  182. }
  183. /*
  184. * 授权列表
  185. * @param appid
  186. * @param function_code 功能代码
  187. * @param ent_id 企业id
  188. * @param page_num 开始页码,小于1的话,返回所有数据,不进行分页
  189. * @param page_size 每页大小
  190. * @return 总条数,只有第一页的时候才返回, 如果企业下所有人都有权限返回-1
  191. * @return 列表,如果企业下所有人都有权限,返回空数组
  192. */
  193. func EmpowerList(appid, function_code string, ent_id, page_num, page_size int64) (int64, []*pb.Empower, error) {
  194. var count int64
  195. result := []*pb.Empower{}
  196. if appid == "" {
  197. return count, result, errors.New("无效的参数appid")
  198. } else if function_code == "" {
  199. return count, result, errors.New("无效的参数function_code")
  200. } else if ent_id == 0 {
  201. return count, result, errors.New("无效的参数ent_id")
  202. }
  203. list := Base_ent_empower.List(appid, function_code, ent_id, page_num, page_size)
  204. if list != nil {
  205. if len(*list) == 1 && (*list)[0].Ent_user_id == 0 {
  206. count = -1
  207. } else {
  208. for _, v := range *list {
  209. result = append(result, &pb.Empower{
  210. EntUserId: v.Ent_user_id,
  211. })
  212. }
  213. if page_num == 1 {
  214. count = Base_ent_empower.Count(appid, function_code, ent_id)
  215. }
  216. }
  217. }
  218. return count, result, nil
  219. }