power.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package service
  2. import (
  3. "database/sql"
  4. "errors"
  5. "time"
  6. . "app.yhyue.com/moapp/jybase/date"
  7. . "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/db"
  8. . "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/entity"
  9. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/pb"
  10. )
  11. /*
  12. * 开通权益
  13. * @param appid
  14. * @param goods_code 商品代码
  15. * @param goods_spec_id 商品规格id
  16. * @param account_id 账户id
  17. * @param ent_id 企业id
  18. * @param buy_num 购买数量
  19. * @param start_time 开始时间
  20. * @param end_time 结束时间
  21. * @return error
  22. */
  23. 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 {
  24. if appid == "" {
  25. return errors.New("无效的参数appid")
  26. } else if goods_code == "" {
  27. return errors.New("无效的参数goods_code")
  28. } else if account_id == 0 && ent_account_id == 0 {
  29. return errors.New("无效的参数account_id、ent_account_id")
  30. }
  31. if start_time != "" {
  32. if _, err := time.ParseInLocation(Date_Full_Layout, start_time, time.Local); err != nil {
  33. return errors.New("无效的参数start_time," + err.Error())
  34. }
  35. }
  36. if end_time != "" {
  37. if _, err := time.ParseInLocation(Date_Full_Layout, end_time, time.Local); err != nil {
  38. return errors.New("无效的参数end_time," + err.Error())
  39. }
  40. }
  41. list := Base_goods_spec.FindById(goods_spec_id, appid, goods_code)
  42. if list == nil || len(*list) == 0 {
  43. return errors.New("没有找到该商品规格")
  44. } 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) {
  45. return nil
  46. } else {
  47. return errors.New("开通失败")
  48. }
  49. }
  50. /*
  51. * 取消权益
  52. * @param appid
  53. * @param goods_code 商品代码
  54. * @param goods_spec_id 商品规格id
  55. * @param account_id 账户id
  56. * @return 是否成功
  57. */
  58. func CancelPower(appid, goods_code string, goods_spec_id, account_id, ent_account_id int64) (bool, error) {
  59. if appid == "" {
  60. return false, errors.New("无效的参数appid")
  61. } else if goods_code == "" {
  62. return false, errors.New("无效的参数goods_code")
  63. } else if account_id == 0 && ent_account_id == 0 {
  64. return false, errors.New("无效的参数account_id、ent_account_id")
  65. }
  66. return Base_power.CancelPower(appid, goods_code, goods_spec_id, account_id, ent_account_id), nil
  67. }
  68. /*
  69. * 根据功能代码获取待授权详情
  70. * @param appid
  71. * @param function_code 功能代码
  72. * @param ent_id 企业id
  73. * @return 待授权详情
  74. */
  75. func WaitEmpowerDetail(appid, function_code string, ent_id int64) (*pb.WaitEmpowerDetailResp, error) {
  76. result := &pb.WaitEmpowerDetailResp{}
  77. if appid == "" {
  78. return result, errors.New("无效的参数appid")
  79. } else if function_code == "" {
  80. return result, errors.New("无效的参数function_code")
  81. } else if ent_id == 0 {
  82. return result, errors.New("无效的参数ent_id")
  83. }
  84. bewes := Base_ent_wait_empower.WaitEmpowers(appid, function_code, ent_id)
  85. if bewes != nil {
  86. for _, v := range *bewes {
  87. result.Id = v.Id
  88. result.EmpowerCount = v.Empower_count
  89. result.LimitStrategy = v.Limit_strategy
  90. result.StartTime = v.Start_time
  91. result.EndTime = v.End_time
  92. break
  93. }
  94. }
  95. return result, nil
  96. }
  97. /*
  98. * 根据商品规格获取待授权详情
  99. * @param appid
  100. * @param function_code 功能代码
  101. * @param ent_id 企业id
  102. * @return 待授权详情
  103. */
  104. func WaitEmpowerDetailBySpecId(appid string, spec_id, ent_id int64) (*pb.WaitEmpowerDetailsResp, error) {
  105. result := &pb.WaitEmpowerDetailsResp{}
  106. if appid == "" {
  107. return result, errors.New("无效的参数appid")
  108. } else if spec_id == 0 {
  109. return result, errors.New("无效的参数spec_id")
  110. } else if ent_id == 0 {
  111. return result, errors.New("无效的参数ent_id")
  112. }
  113. bewes := Base_ent_wait_empower.WaitEmpowersBySpecId(appid, spec_id, ent_id)
  114. if bewes != nil {
  115. for _, v := range *bewes {
  116. result.WaitEmpowerDetails = append(result.WaitEmpowerDetails, &pb.WaitEmpowerDetailResp{
  117. Id: v.Id,
  118. EmpowerCount: v.Empower_count,
  119. LimitStrategy: v.Limit_strategy,
  120. StartTime: v.Start_time,
  121. EndTime: v.End_time,
  122. })
  123. }
  124. }
  125. return result, nil
  126. }
  127. /*
  128. * 获取已有的权益
  129. * @param appid
  130. * @param account_id 账户id
  131. * @param ent_account_id 企业账户id
  132. * @param ent_id 企业id
  133. * @param ent_user_id 企业用户id
  134. * @return 所有已开通的权益
  135. */
  136. func HasPowers(appid string, account_id, ent_account_id, ent_id, ent_user_id int64) ([]string, error) {
  137. result := []string{}
  138. if appid == "" {
  139. return result, errors.New("无效的参数appid")
  140. } else if account_id == 0 && ent_account_id == 0 {
  141. return result, errors.New("无效的参数account_id、ent_account_id")
  142. }
  143. m := map[string]bool{}
  144. bps := Base_power.FindMyPowers(appid, account_id, ent_account_id)
  145. if bps != nil {
  146. for _, v := range *bps {
  147. if v.Power_type == 1 || (v.Power_type == 2 && Base_ent_empower.HasEmpower(appid, v.Function_code, ent_id, ent_user_id)) {
  148. if m[v.Function_code] {
  149. continue
  150. }
  151. m[v.Function_code] = true
  152. result = append(result, v.Function_code)
  153. }
  154. }
  155. }
  156. return result, nil
  157. }
  158. /*
  159. * 重新授权
  160. * @param appid
  161. * @param function_code 功能代码
  162. * @param ent_id 企业id
  163. * @param ent_user_id 企业用户id
  164. * @return 0:失败 1:成功 -2:数量不足
  165. */
  166. func ReEmpower(appid string, function_code []string, ent_id int64, ent_user_id []int64) (int64, error) {
  167. if appid == "" {
  168. return 0, errors.New("无效的参数appid")
  169. } else if len(function_code) == 0 {
  170. return 0, errors.New("无效的参数function_code")
  171. } else if ent_id == 0 {
  172. return 0, errors.New("无效的参数ent_id")
  173. } else if len(ent_user_id) == 0 {
  174. return 0, errors.New("无效的参数ent_user_id")
  175. } else if status, err := Base_ent_wait_empower.CheckBeforeEmpower(appid, function_code, ent_id, ent_user_id); err != nil {
  176. return status, err
  177. }
  178. if Base_ent_empower.ReEmpower(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 ent_user_id 企业用户id
  189. * @return 0:失败 1:成功
  190. */
  191. func Empower(appid string, function_code []string, ent_id int64, ent_user_id []int64) (int64, error) {
  192. if appid == "" {
  193. return 0, errors.New("无效的参数appid")
  194. } else if len(function_code) == 0 {
  195. return 0, errors.New("无效的参数function_code")
  196. } else if ent_id == 0 {
  197. return 0, errors.New("无效的参数ent_id")
  198. } else if len(ent_user_id) == 0 {
  199. return 0, errors.New("无效的参数ent_user_id")
  200. } else if status, err := Base_ent_wait_empower.CheckBeforeEmpower(appid, function_code, ent_id, ent_user_id); err != nil {
  201. return status, err
  202. }
  203. if Base_ent_empower.Empower(appid, function_code, ent_id, ent_user_id) {
  204. return 1, nil
  205. }
  206. return 0, nil
  207. }
  208. /*
  209. * 根据功能代码取消授权
  210. * @param appid
  211. * @param function_code 功能代码
  212. * @param ent_id 企业id
  213. * @param ent_user_id 企业用户id
  214. * @return 0:失败 1:成功
  215. */
  216. func CancelEmpower(appid string, function_code []string, ent_id int64, ent_user_id []int64) (int64, error) {
  217. if appid == "" {
  218. return 0, errors.New("无效的参数appid")
  219. } else if len(function_code) == 0 {
  220. return 0, errors.New("无效的参数function_code")
  221. } else if ent_id == 0 {
  222. return 0, errors.New("无效的参数ent_id")
  223. } else if len(ent_user_id) == 0 {
  224. return 0, errors.New("无效的参数ent_user_id")
  225. }
  226. if Mysql_BaseService.ExecTx("根据功能代码取消授权", func(tx *sql.Tx) bool {
  227. return Base_ent_empower.CancelEmpower(tx, appid, function_code, ent_id, ent_user_id)
  228. }) {
  229. return 1, nil
  230. }
  231. return 0, nil
  232. }
  233. /*
  234. * 根据功能分类取消所有授权
  235. * @param appid
  236. * @param function_code 功能代码
  237. * @param ent_id 企业id
  238. * @param ent_user_id 企业用户id
  239. * @return 0:失败 1:成功
  240. */
  241. func CancelAllEmpower(appid string, function_module []string, ent_id int64, ent_user_id []int64) (int64, error) {
  242. if appid == "" {
  243. return 0, errors.New("无效的参数appid")
  244. } else if len(function_module) == 0 {
  245. return 0, errors.New("无效的参数function_module")
  246. } else if ent_id == 0 {
  247. return 0, errors.New("无效的参数ent_id")
  248. } else if len(ent_user_id) == 0 {
  249. return 0, errors.New("无效的参数ent_user_id")
  250. }
  251. if Mysql_BaseService.ExecTx("根据功能分类取消所有授权", func(tx *sql.Tx) bool {
  252. return Base_ent_empower.CancelAllEmpower(tx, appid, function_module, ent_id, ent_user_id)
  253. }) {
  254. return 1, nil
  255. }
  256. return 0, nil
  257. }
  258. /*
  259. * 授权列表
  260. * @param appid
  261. * @param function_code 功能代码
  262. * @param ent_id 企业id
  263. * @param page_num 开始页码,小于1的话,返回所有数据,不进行分页
  264. * @param page_size 每页大小
  265. * @return 总条数,只有第一页的时候才返回, 如果企业下所有人都有权限返回-1
  266. * @return 列表,如果企业下所有人都有权限,返回空数组
  267. */
  268. func EmpowerList(appid, function_code string, ent_id, page_num, page_size int64) (int64, []*pb.Empower, error) {
  269. var count int64
  270. result := []*pb.Empower{}
  271. if appid == "" {
  272. return count, result, errors.New("无效的参数appid")
  273. } else if function_code == "" {
  274. return count, result, errors.New("无效的参数function_code")
  275. } else if ent_id == 0 {
  276. return count, result, errors.New("无效的参数ent_id")
  277. }
  278. if Base_ent_wait_empower.IsAllEmpower(appid, function_code, ent_id) {
  279. count = -1
  280. } else {
  281. list := Base_ent_empower.List(appid, function_code, ent_id, page_num, page_size)
  282. if list != nil {
  283. if len(*list) == 1 && (*list)[0].Ent_user_id == 0 {
  284. count = -1
  285. } else {
  286. for _, v := range *list {
  287. result = append(result, &pb.Empower{
  288. EntUserId: v.Ent_user_id,
  289. })
  290. }
  291. if page_num == 1 {
  292. count = Base_ent_empower.Count(appid, function_code, ent_id)
  293. }
  294. }
  295. }
  296. }
  297. return count, result, nil
  298. }