power.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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) (*pb.WaitEmpowerDetailResp, error) {
  74. result := &pb.WaitEmpowerDetailResp{}
  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.Id = v.Id
  86. result.EmpowerCount = v.Empower_count
  87. result.LimitStrategy = v.Limit_strategy
  88. result.StartTime = v.Start_time
  89. result.EndTime = v.End_time
  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. } else if !Base_ent_wait_empower.CheckFunctionCode(appid, function_code, ent_id) {
  144. return 0, errors.New("无效的参数function_code")
  145. }
  146. bewes := Base_ent_wait_empower.WaitEmpowers(appid, function_code, ent_id)
  147. if bewes == nil {
  148. return -1, nil
  149. }
  150. var count int64
  151. for _, v := range *bewes {
  152. if v.Empower_count == -1 {
  153. count = -1
  154. break
  155. }
  156. count += v.Empower_count
  157. }
  158. if count != -1 && int64(len(ent_user_id)) > count {
  159. return -2, nil
  160. }
  161. if Base_ent_empower.ReEmpower(appid, function_code, ent_id, ent_user_id) {
  162. return 1, nil
  163. }
  164. return 0, nil
  165. }
  166. /*
  167. * 授权
  168. * @param appid
  169. * @param function_code 功能代码
  170. * @param ent_id 企业id
  171. * @param ent_user_id 企业用户id
  172. * @return 0:失败 1:成功
  173. */
  174. func Empower(appid, function_code string, ent_id int64, ent_user_id []int64) (int64, error) {
  175. if appid == "" {
  176. return 0, errors.New("无效的参数appid")
  177. } else if function_code == "" {
  178. return 0, errors.New("无效的参数function_code")
  179. } else if ent_id == 0 {
  180. return 0, errors.New("无效的参数ent_id")
  181. } else if len(ent_user_id) == 0 {
  182. return 0, errors.New("无效的参数ent_user_id")
  183. } else if !Base_ent_wait_empower.CheckFunctionCode(appid, function_code, ent_id) {
  184. return 0, errors.New("无效的参数function_code")
  185. }
  186. if Base_ent_empower.Empower(appid, function_code, ent_id, ent_user_id) {
  187. return 1, nil
  188. }
  189. return 0, nil
  190. }
  191. /*
  192. * 取消授权
  193. * @param appid
  194. * @param function_code 功能代码
  195. * @param ent_id 企业id
  196. * @param ent_user_id 企业用户id
  197. * @return 0:失败 1:成功
  198. */
  199. func CancelEmpower(appid, function_code string, ent_id int64, ent_user_id []int64) (int64, error) {
  200. if appid == "" {
  201. return 0, errors.New("无效的参数appid")
  202. } else if function_code == "" {
  203. return 0, errors.New("无效的参数function_code")
  204. } else if ent_id == 0 {
  205. return 0, errors.New("无效的参数ent_id")
  206. } else if len(ent_user_id) == 0 {
  207. return 0, errors.New("无效的参数ent_user_id")
  208. } else if !Base_ent_wait_empower.CheckFunctionCode(appid, function_code, ent_id) {
  209. return 0, errors.New("无效的参数function_code")
  210. }
  211. if Base_ent_empower.CancelEmpower(appid, function_code, ent_id, ent_user_id) {
  212. return 1, nil
  213. }
  214. return 0, nil
  215. }
  216. /*
  217. * 授权列表
  218. * @param appid
  219. * @param function_code 功能代码
  220. * @param ent_id 企业id
  221. * @param page_num 开始页码,小于1的话,返回所有数据,不进行分页
  222. * @param page_size 每页大小
  223. * @return 总条数,只有第一页的时候才返回, 如果企业下所有人都有权限返回-1
  224. * @return 列表,如果企业下所有人都有权限,返回空数组
  225. */
  226. func EmpowerList(appid, function_code string, ent_id, page_num, page_size int64) (int64, []*pb.Empower, error) {
  227. var count int64
  228. result := []*pb.Empower{}
  229. if appid == "" {
  230. return count, result, errors.New("无效的参数appid")
  231. } else if function_code == "" {
  232. return count, result, errors.New("无效的参数function_code")
  233. } else if ent_id == 0 {
  234. return count, result, errors.New("无效的参数ent_id")
  235. }
  236. if Base_ent_wait_empower.IsAllEmpower(appid, function_code, ent_id) {
  237. count = -1
  238. } else {
  239. list := Base_ent_empower.List(appid, function_code, ent_id, page_num, page_size)
  240. if list != nil {
  241. if len(*list) == 1 && (*list)[0].Ent_user_id == 0 {
  242. count = -1
  243. } else {
  244. for _, v := range *list {
  245. result = append(result, &pb.Empower{
  246. EntUserId: v.Ent_user_id,
  247. })
  248. }
  249. if page_num == 1 {
  250. count = Base_ent_empower.Count(appid, function_code, ent_id)
  251. }
  252. }
  253. }
  254. }
  255. return count, result, nil
  256. }