power.go 12 KB

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