power.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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.CheckFunctionCode(appid, function_code, ent_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_module 功能分类
  187. * @param function_code 功能代码
  188. * @param ent_id 企业id
  189. * @param ent_user_id 企业用户id
  190. * @return 0:失败 1:成功 -2:数量不足
  191. */
  192. func CrReEmpower(appid string, function_module, function_code []string, ent_id int64, ent_user_id []int64) (int64, error) {
  193. if appid == "" {
  194. return 0, errors.New("无效的参数appid")
  195. } else if len(function_module) == 0 {
  196. return 0, errors.New("无效的参数function_module")
  197. } else if len(function_code) == 0 {
  198. return 0, errors.New("无效的参数function_code")
  199. } else if ent_id == 0 {
  200. return 0, errors.New("无效的参数ent_id")
  201. } else if len(ent_user_id) == 0 {
  202. return 0, errors.New("无效的参数ent_user_id")
  203. } else if status, err := Base_ent_wait_empower.CheckFunctionCode(appid, function_code, ent_id); err != nil {
  204. return status, err
  205. }
  206. var err error
  207. var status int64
  208. if Mysql_BaseService.ExecTx("根据功能分类取消所有授权", func(tx *sql.Tx) bool {
  209. ok1 := Base_ent_empower.CancelAllEmpower(tx, appid, function_module, ent_id, ent_user_id)
  210. ok2 := Base_ent_empower.Empower(tx, appid, function_code, ent_id, ent_user_id)
  211. if status, err = Base_ent_empower.CheckEmpowerCount(tx, appid, function_code, ent_id); err != nil {
  212. return false
  213. }
  214. return ok1 && ok2
  215. }) {
  216. status = 1
  217. }
  218. return status, err
  219. }
  220. /*
  221. * 授权
  222. * @param appid
  223. * @param function_code 功能代码
  224. * @param ent_id 企业id
  225. * @param ent_user_id 企业用户id
  226. * @return 0:失败 1:成功
  227. */
  228. func Empower(appid string, function_code []string, ent_id int64, ent_user_id []int64) (int64, error) {
  229. if appid == "" {
  230. return 0, errors.New("无效的参数appid")
  231. } else if len(function_code) == 0 {
  232. return 0, errors.New("无效的参数function_code")
  233. } else if ent_id == 0 {
  234. return 0, errors.New("无效的参数ent_id")
  235. } else if len(ent_user_id) == 0 {
  236. return 0, errors.New("无效的参数ent_user_id")
  237. } else if status, err := Base_ent_wait_empower.CheckFunctionCode(appid, function_code, ent_id); err != nil {
  238. return status, err
  239. }
  240. if Mysql_BaseService.ExecTx("授权", func(tx *sql.Tx) bool {
  241. Base_ent_empower.CancelEmpower(tx, appid, function_code, ent_id, ent_user_id)
  242. return Base_ent_empower.Empower(tx, appid, function_code, ent_id, ent_user_id)
  243. }) {
  244. return 1, nil
  245. }
  246. return 0, nil
  247. }
  248. /*
  249. * 根据功能代码取消授权
  250. * @param appid
  251. * @param function_code 功能代码
  252. * @param ent_id 企业id
  253. * @param ent_user_id 企业用户id
  254. * @return 0:失败 1:成功
  255. */
  256. func CancelEmpower(appid string, function_code []string, ent_id int64, ent_user_id []int64) (int64, error) {
  257. if appid == "" {
  258. return 0, errors.New("无效的参数appid")
  259. } else if len(function_code) == 0 {
  260. return 0, errors.New("无效的参数function_code")
  261. } else if ent_id == 0 {
  262. return 0, errors.New("无效的参数ent_id")
  263. } else if len(ent_user_id) == 0 {
  264. return 0, errors.New("无效的参数ent_user_id")
  265. }
  266. if Mysql_BaseService.ExecTx("根据功能代码取消授权", func(tx *sql.Tx) bool {
  267. return Base_ent_empower.CancelEmpower(tx, appid, function_code, ent_id, ent_user_id)
  268. }) {
  269. return 1, nil
  270. }
  271. return 0, nil
  272. }
  273. /*
  274. * 根据功能分类取消所有授权
  275. * @param appid
  276. * @param function_module 功能分类
  277. * @param ent_id 企业id
  278. * @param ent_user_id 企业用户id
  279. * @return 0:失败 1:成功
  280. */
  281. func CancelAllEmpower(appid string, function_module []string, ent_id int64, ent_user_id []int64) (int64, error) {
  282. if appid == "" {
  283. return 0, errors.New("无效的参数appid")
  284. } else if len(function_module) == 0 {
  285. return 0, errors.New("无效的参数function_module")
  286. } else if ent_id == 0 {
  287. return 0, errors.New("无效的参数ent_id")
  288. } else if len(ent_user_id) == 0 {
  289. return 0, errors.New("无效的参数ent_user_id")
  290. }
  291. if Mysql_BaseService.ExecTx("根据功能分类取消所有授权", func(tx *sql.Tx) bool {
  292. return Base_ent_empower.CancelAllEmpower(tx, appid, function_module, ent_id, ent_user_id)
  293. }) {
  294. return 1, nil
  295. }
  296. return 0, nil
  297. }
  298. /*
  299. * 授权列表
  300. * @param appid
  301. * @param function_code 功能代码
  302. * @param ent_id 企业id
  303. * @param page_num 开始页码,小于1的话,返回所有数据,不进行分页
  304. * @param page_size 每页大小
  305. * @return 总条数,只有第一页的时候才返回, 如果企业下所有人都有权限返回-1
  306. * @return 列表,如果企业下所有人都有权限,返回空数组
  307. */
  308. func EmpowerList(appid, function_code string, ent_id, page_num, page_size int64) (int64, []*pb.Empower, error) {
  309. var count int64
  310. result := []*pb.Empower{}
  311. if appid == "" {
  312. return count, result, errors.New("无效的参数appid")
  313. } else if function_code == "" {
  314. return count, result, errors.New("无效的参数function_code")
  315. } else if ent_id == 0 {
  316. return count, result, errors.New("无效的参数ent_id")
  317. }
  318. if Base_ent_wait_empower.IsAllEmpower(appid, function_code, ent_id) {
  319. count = -1
  320. } else {
  321. list := Base_ent_empower.List(appid, function_code, ent_id, page_num, page_size)
  322. if list != nil {
  323. if len(*list) == 1 && (*list)[0].Ent_user_id == 0 {
  324. count = -1
  325. } else {
  326. for _, v := range *list {
  327. result = append(result, &pb.Empower{
  328. EntUserId: v.Ent_user_id,
  329. })
  330. }
  331. if page_num == 1 {
  332. count = Base_ent_empower.Count(appid, function_code, ent_id)
  333. }
  334. }
  335. }
  336. }
  337. return count, result, nil
  338. }