power.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 function_code 功能代码
  99. * @param ent_id 企业id
  100. * @return 待授权详情
  101. */
  102. func WaitEmpowerDetailBySpecId(appid string, spec_id, ent_id int64) (*pb.WaitEmpowerDetailsResp, error) {
  103. result := &pb.WaitEmpowerDetailsResp{}
  104. if appid == "" {
  105. return result, errors.New("无效的参数appid")
  106. } else if spec_id == 0 {
  107. return result, errors.New("无效的参数spec_id")
  108. } else if ent_id == 0 {
  109. return result, errors.New("无效的参数ent_id")
  110. }
  111. bewes := Base_ent_wait_empower.WaitEmpowersBySpecId(appid, spec_id, ent_id)
  112. if bewes != nil {
  113. for _, v := range *bewes {
  114. result.WaitEmpowerDetails = append(result.WaitEmpowerDetails, &pb.WaitEmpowerDetailResp{
  115. Id: v.Id,
  116. EmpowerCount: v.Empower_count,
  117. LimitStrategy: v.Limit_strategy,
  118. StartTime: v.Start_time,
  119. EndTime: v.End_time,
  120. })
  121. }
  122. }
  123. return result, nil
  124. }
  125. /*
  126. * 获取已有的权益
  127. * @param appid
  128. * @param account_id 账户id
  129. * @param ent_account_id 企业账户id
  130. * @param ent_id 企业id
  131. * @param ent_user_id 企业用户id
  132. * @return 所有已开通的权益
  133. */
  134. func HasPowers(appid string, account_id, ent_account_id, ent_id, ent_user_id int64) ([]string, error) {
  135. result := []string{}
  136. if appid == "" {
  137. return result, errors.New("无效的参数appid")
  138. } else if account_id == 0 && ent_account_id == 0 {
  139. return result, errors.New("无效的参数account_id、ent_account_id")
  140. }
  141. m := map[string]bool{}
  142. bps := Base_power.FindMyPowers(appid, account_id, ent_account_id)
  143. if bps != nil {
  144. for _, v := range *bps {
  145. if v.Power_type == 1 || (v.Power_type == 2 && Base_ent_empower.HasEmpower(appid, v.Function_code, ent_id, ent_user_id)) {
  146. if m[v.Function_code] {
  147. continue
  148. }
  149. m[v.Function_code] = true
  150. result = append(result, v.Function_code)
  151. }
  152. }
  153. }
  154. return result, nil
  155. }
  156. /*
  157. * 重新授权
  158. * @param appid
  159. * @param function_code 功能代码
  160. * @param ent_id 企业id
  161. * @param ent_user_id 企业用户id
  162. * @return 0:失败 1:成功 -2:数量不足
  163. */
  164. func ReEmpower(appid, function_code string, ent_id int64, ent_user_id []int64) (int64, error) {
  165. if appid == "" {
  166. return 0, errors.New("无效的参数appid")
  167. } else if function_code == "" {
  168. return 0, errors.New("无效的参数function_code")
  169. } else if ent_id == 0 {
  170. return 0, errors.New("无效的参数ent_id")
  171. } else if len(ent_user_id) == 0 {
  172. return 0, errors.New("无效的参数ent_user_id")
  173. } else if !Base_ent_wait_empower.CheckFunctionCode(appid, function_code, ent_id) {
  174. return 0, errors.New("无效的参数function_code")
  175. }
  176. bewes := Base_ent_wait_empower.WaitEmpowers(appid, function_code, ent_id)
  177. if bewes == nil {
  178. return -1, nil
  179. }
  180. var count int64
  181. for _, v := range *bewes {
  182. if v.Empower_count == -1 {
  183. count = -1
  184. break
  185. }
  186. count += v.Empower_count
  187. }
  188. if count != -1 && int64(len(ent_user_id)) > count {
  189. return -2, nil
  190. }
  191. if Base_ent_empower.ReEmpower(appid, function_code, ent_id, ent_user_id) {
  192. return 1, nil
  193. }
  194. return 0, nil
  195. }
  196. /*
  197. * 授权
  198. * @param appid
  199. * @param function_code 功能代码
  200. * @param ent_id 企业id
  201. * @param ent_user_id 企业用户id
  202. * @return 0:失败 1:成功
  203. */
  204. func Empower(appid, function_code string, ent_id int64, ent_user_id []int64) (int64, error) {
  205. if appid == "" {
  206. return 0, errors.New("无效的参数appid")
  207. } else if function_code == "" {
  208. return 0, errors.New("无效的参数function_code")
  209. } else if ent_id == 0 {
  210. return 0, errors.New("无效的参数ent_id")
  211. } else if len(ent_user_id) == 0 {
  212. return 0, errors.New("无效的参数ent_user_id")
  213. } else if !Base_ent_wait_empower.CheckFunctionCode(appid, function_code, ent_id) {
  214. return 0, errors.New("无效的参数function_code")
  215. }
  216. if Base_ent_empower.Empower(appid, function_code, ent_id, ent_user_id) {
  217. return 1, nil
  218. }
  219. return 0, nil
  220. }
  221. /*
  222. * 取消授权
  223. * @param appid
  224. * @param function_code 功能代码
  225. * @param ent_id 企业id
  226. * @param ent_user_id 企业用户id
  227. * @return 0:失败 1:成功
  228. */
  229. func CancelEmpower(appid, function_code string, ent_id int64, ent_user_id []int64) (int64, error) {
  230. if appid == "" {
  231. return 0, errors.New("无效的参数appid")
  232. } else if function_code == "" {
  233. return 0, errors.New("无效的参数function_code")
  234. } else if ent_id == 0 {
  235. return 0, errors.New("无效的参数ent_id")
  236. } else if len(ent_user_id) == 0 {
  237. return 0, errors.New("无效的参数ent_user_id")
  238. } else if !Base_ent_wait_empower.CheckFunctionCode(appid, function_code, ent_id) {
  239. return 0, errors.New("无效的参数function_code")
  240. }
  241. if Base_ent_empower.CancelEmpower(appid, function_code, ent_id, ent_user_id) {
  242. return 1, nil
  243. }
  244. return 0, nil
  245. }
  246. /*
  247. * 授权列表
  248. * @param appid
  249. * @param function_code 功能代码
  250. * @param ent_id 企业id
  251. * @param page_num 开始页码,小于1的话,返回所有数据,不进行分页
  252. * @param page_size 每页大小
  253. * @return 总条数,只有第一页的时候才返回, 如果企业下所有人都有权限返回-1
  254. * @return 列表,如果企业下所有人都有权限,返回空数组
  255. */
  256. func EmpowerList(appid, function_code string, ent_id, page_num, page_size int64) (int64, []*pb.Empower, error) {
  257. var count int64
  258. result := []*pb.Empower{}
  259. if appid == "" {
  260. return count, result, errors.New("无效的参数appid")
  261. } else if function_code == "" {
  262. return count, result, errors.New("无效的参数function_code")
  263. } else if ent_id == 0 {
  264. return count, result, errors.New("无效的参数ent_id")
  265. }
  266. if Base_ent_wait_empower.IsAllEmpower(appid, function_code, ent_id) {
  267. count = -1
  268. } else {
  269. list := Base_ent_empower.List(appid, function_code, ent_id, page_num, page_size)
  270. if list != nil {
  271. if len(*list) == 1 && (*list)[0].Ent_user_id == 0 {
  272. count = -1
  273. } else {
  274. for _, v := range *list {
  275. result = append(result, &pb.Empower{
  276. EntUserId: v.Ent_user_id,
  277. })
  278. }
  279. if page_num == 1 {
  280. count = Base_ent_empower.Count(appid, function_code, ent_id)
  281. }
  282. }
  283. }
  284. }
  285. return count, result, nil
  286. }