company_permission.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. package company
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hz_crm_api/utils"
  6. "strings"
  7. "time"
  8. )
  9. type ChartPermission struct {
  10. ChartPermissionId int `description:"权限id"`
  11. ChartPermissionName string `description:"权限名称(旧)"`
  12. PermissionName string `description:"权限名称"`
  13. Sort int `description:"排序"`
  14. Enabled int `description:"是否可用"`
  15. CreatedTime time.Time `description:"创建时间"`
  16. LastUpdatedTime time.Time `description:"最后更新时间"`
  17. TeleconferenceSort int `description:"电话会排序"`
  18. Remark string `description:"备注"`
  19. ClassifyName string `description:"分类"`
  20. PermissionType int `description:"1主观,2客观"`
  21. Checked bool `description:"选中状态"`
  22. ParentId int `description:"父级权限id"`
  23. IsPublic int `description:"是否是公有权限1:公有权限,0私有权限"`
  24. }
  25. type PermissionSetItem struct {
  26. ChartPermissionId int `description:"权限id"`
  27. PermissionName string `description:"权限名称"`
  28. PermissionType int `description:"1主观,2客观"`
  29. ParentId int `description:"父级权限id"`
  30. IsPublic int `description:"是否是公有权限1:公有权限,0私有权限"`
  31. Checked bool `description:"选中状态"`
  32. Child []*PermissionSetItem `description:"具体的主客观-方便前端的排版用的"`
  33. }
  34. type PermissionSetItemType struct {
  35. PermissionName string `description:"权限名称"`
  36. Checked bool `description:"选中状态"`
  37. CheckedMinate bool `description:"不确定状态"`
  38. NoClicking bool `description:"是否禁止点击"`
  39. Items []*PermissionLookItem
  40. }
  41. type PermissionSetList struct {
  42. ClassifyName string `description:"分类"`
  43. Items []*PermissionSetItem
  44. CheckList []int
  45. }
  46. type PermissionSetListType struct {
  47. ClassifyName string `description:"分类"`
  48. Items []*PermissionSetItemType
  49. CheckList []int
  50. }
  51. type PermissionSetResp struct {
  52. List []*PermissionSetList
  53. //ListUpgrade []*PermissionSetList `description:"升级权限列表"`
  54. ListType []*PermissionSetListType `description:"主观客观列表"`
  55. }
  56. type ChartPermissionFirst struct {
  57. ChartPermissionId int `description:"权限id"`
  58. ClassifyName string `description:"分类"`
  59. }
  60. func GetPermissionSetItems(productId int, classifyName string) (items []*PermissionSetItem, err error) {
  61. o := orm.NewOrm()
  62. sql := ` SELECT * FROM chart_permission WHERE enabled=1 AND product_id=? AND classify_name=? AND parent_id>0 AND permission_type=0 ORDER BY sort ASC `
  63. _, err = o.Raw(sql, productId, classifyName).QueryRows(&items)
  64. return
  65. }
  66. func GetPermissionByProductId(productId int) (items []*PermissionSetItem, err error) {
  67. o := orm.NewOrm()
  68. sql := ` SELECT * FROM chart_permission WHERE enabled=1 AND product_id=? AND permission_type=0 ORDER BY sort ASC `
  69. _, err = o.Raw(sql, productId).QueryRows(&items)
  70. return
  71. }
  72. func GetPermissionSetItemsByType(productId int, classifyName string) (items []*PermissionSetItem, err error) {
  73. o := orm.NewOrm()
  74. sql := ` SELECT * FROM chart_permission WHERE enabled=1 AND product_id=? AND classify_name=? AND parent_id>0 AND permission_type!=2 ORDER BY sort ASC `
  75. _, err = o.Raw(sql, productId, classifyName).QueryRows(&items)
  76. return
  77. }
  78. func GetPermissionSetSandoItems(productId int, classifyName string) (items []*PermissionLookItem, err error) {
  79. o := orm.NewOrm()
  80. sql := ` SELECT * FROM chart_permission WHERE enabled=1 AND product_id=? AND classify_name=? AND parent_id>0 ORDER BY sort ASC `
  81. _, err = o.Raw(sql, productId, classifyName).QueryRows(&items)
  82. return
  83. }
  84. func GetPermissionSetItemsType(productId int, classifyName string) (items []*PermissionLookItem, err error) {
  85. o := orm.NewOrm()
  86. sql := ` SELECT * FROM chart_permission WHERE enabled=1 AND product_id=? AND classify_name=? AND permission_type!=0 ORDER BY sort ASC `
  87. _, err = o.Raw(sql, productId, classifyName).QueryRows(&items)
  88. return
  89. }
  90. type CompanyReportPermission struct {
  91. CompanyReportPermissionId int64 `orm:"column(company_report_permission_id);pk"`
  92. CompanyId int
  93. ReportPermissionId int
  94. CreatedTime time.Time
  95. LastUpdatedTime time.Time
  96. ChartPermissionId int
  97. StartDate string `description:"权限开始日期"`
  98. EndDate string `description:"权限结束日期"`
  99. ProductId int `description:"产品id"`
  100. ProductName string `description:"产品名称"`
  101. CompanyContractId int `description:"合同id"`
  102. Status string `description:"'正式','试用','关闭'"`
  103. ModifyTime time.Time `description:"修改时间"`
  104. IsUpgrade int `description:"是否升级,1是,0否"`
  105. ExpensiveYx int `description:"权益研选: 0-3w; 1-5w ,2: 10W"`
  106. }
  107. // 新增客户权限
  108. func AddCompanyReportPermission(item *CompanyReportPermission) (err error) {
  109. o := orm.NewOrm()
  110. lastId, err := o.Insert(item)
  111. item.CompanyReportPermissionId = lastId
  112. return
  113. }
  114. func GetCompanyReportPermissionCount(companyId, productId int) (count int, err error) {
  115. o := orm.NewOrm()
  116. sql := `SELECT COUNT(1) AS count FROM company_report_permission WHERE company_id=? AND product_id=? `
  117. err = o.Raw(sql, companyId, productId).QueryRow(&count)
  118. return
  119. }
  120. func DeleteCompanyReportPermission(companyId, productId int) (err error) {
  121. o := orm.NewOrm()
  122. sql := ` DELETE FROM company_report_permission WHERE company_id=? AND product_id=? `
  123. _, err = o.Raw(sql, companyId, productId).Exec()
  124. return
  125. }
  126. type PermissionLookResp struct {
  127. List []*PermissionLookList
  128. ListRai []*PermissionLookList `description:"权益"`
  129. }
  130. type PermissionLookSandoResp struct {
  131. List []*PermissionLookList
  132. ListType []*PermissionSetListType `description:"主观客观列表"`
  133. }
  134. type PermissionLookList struct {
  135. ClassifyName string `description:"分类"`
  136. Items []*PermissionLookItem
  137. ItemsType []*PermissionSetItemType `description:"主观客观列表"`
  138. CheckList []int
  139. RaiMerge int `description:"权益-用于前端判断展示形式:1-全部合并 2-全部拆分"`
  140. }
  141. type PermissionLookItem struct {
  142. ChartPermissionId int `description:"权限id"`
  143. PermissionName string `description:"权限名称"`
  144. StartDate string `description:"权限开始日期"`
  145. EndDate string `description:"权限结束日期"`
  146. Status string `description:"'正式','试用','关闭'"`
  147. ExpireDay string `description:"到期天数"`
  148. ClassifyName string `description:"分类"`
  149. PermissionType int `description:"1主观,2客观"`
  150. PermissionTypeName string `description:"主观、客观"`
  151. Checked bool `description:"选中状态"`
  152. Remark string `description:"备注"`
  153. IsMerge bool `description:"是否合并行业, 给前端的标识, 暂时仅权益使用"`
  154. RaiBothHas bool `description:"权益-是否主客观都有"`
  155. IsUpgrade int `description:"是否升级,1是,0否"`
  156. ExpensiveYx int `description:"权益研选: 0-3w; 1-5w ,2: 10W"`
  157. Points float64 `description:"研选扣点包点数"`
  158. ParentId int `description:"父级权限id"`
  159. IsPublic int `description:"是否是公有权限1:公有权限,0私有权限"`
  160. Child []*PermissionLookItem `description:"子权限"`
  161. }
  162. // todo 确认是否需要删除
  163. func GetPermissionLookItems(productId int, classifyName string) (items []*PermissionLookItem, err error) {
  164. o := orm.NewOrm()
  165. sql := ` SELECT * FROM chart_permission WHERE enabled=1 AND product_id=? AND classify_name=? AND parent_id > 0 ORDER BY sort ASC `
  166. _, err = o.Raw(sql, productId, classifyName).QueryRows(&items)
  167. return
  168. }
  169. func GetPermissionLookItemsByProductId(productId int) (items []*PermissionLookItem, err error) {
  170. o := orm.NewOrm()
  171. sql := ` SELECT * FROM chart_permission WHERE enabled=1 AND product_id=? ORDER BY sort ASC `
  172. _, err = o.Raw(sql, productId).QueryRows(&items)
  173. return
  174. }
  175. func GetPermissionLookItemsByParentId(productId int, parentId int) (items []*PermissionLookItem, err error) {
  176. o := orm.NewOrm()
  177. sql := ` SELECT * FROM chart_permission WHERE enabled=1 AND product_id=? AND parent_id=? ORDER BY sort ASC `
  178. _, err = o.Raw(sql, productId, parentId).QueryRows(&items)
  179. return
  180. }
  181. func GetCompanyPermissionCheck(companyId, permissionId int) (count int, err error) {
  182. o := orm.NewOrm()
  183. sql := `SELECT COUNT(1) AS count FROM company_report_permission AS a WHERE a.company_id=? AND a.chart_permission_id=? `
  184. err = o.Raw(sql, companyId, permissionId).QueryRow(&count)
  185. return
  186. }
  187. // GetCompanyPermission 根据客户id和品种id获取对应的数据
  188. func GetCompanyPermission(companyId, permissionId int) (item *CompanyReportPermission, err error) {
  189. o := orm.NewOrm()
  190. sql := `SELECT * FROM company_report_permission AS a WHERE a.company_id=? AND a.chart_permission_id=? `
  191. err = o.Raw(sql, companyId, permissionId).QueryRow(&item)
  192. return
  193. }
  194. func GetCompanyPermissionCheckItem(companyId, permissionId int) (item *PermissionLookItem, err error) {
  195. o := orm.NewOrm()
  196. sql := `SELECT * FROM company_report_permission AS a WHERE a.company_id=? AND a.chart_permission_id=? `
  197. err = o.Raw(sql, companyId, permissionId).QueryRow(&item)
  198. return
  199. }
  200. // 通过主观ID获取对应的客观ID
  201. func GetPermissionIdById(permissionIds string) (allpermissionId string, err error) {
  202. sql := ` SELECT
  203. GROUP_CONCAT( DISTINCT chart_permission_id SEPARATOR ',' ) AS permission
  204. FROM
  205. chart_permission
  206. WHERE
  207. parent_id > 0 and
  208. product_permission_name IN (
  209. SELECT
  210. product_permission_name
  211. FROM
  212. chart_permission
  213. WHERE
  214. chart_permission_id IN ( ` + permissionIds + ` )) `
  215. o := orm.NewOrm()
  216. err = o.Raw(sql).QueryRow(&allpermissionId)
  217. return
  218. }
  219. // 通过主观ID获取对应的客观ID
  220. func GetCompanyPermissionIdById(companyId int) (allpermissionId string, err error) {
  221. sql := `SELECT
  222. GROUP_CONCAT( DISTINCT chart_permission_id SEPARATOR ',' ) AS permission
  223. FROM
  224. company_report_permission AS a
  225. WHERE
  226. a.company_id = ?
  227. AND product_id = 2`
  228. o := orm.NewOrm()
  229. err = o.Raw(sql, companyId).QueryRow(&allpermissionId)
  230. return
  231. }
  232. type ContractPermissionList struct {
  233. ClassifyName string `description:"分类"`
  234. Items []*PermissionLookItem
  235. CheckList []int
  236. CheckAll bool
  237. }
  238. func GetCompanyIdStr(permissionStr string) (company_id_str string, err error) {
  239. o := orm.NewOrm()
  240. sql := `SELECT GROUP_CONCAT(DISTINCT company_id SEPARATOR ',') AS company_id_str
  241. FROM company_report_permission AS a
  242. WHERE a.chart_permission_id IN( ` + permissionStr + `) `
  243. err = o.Raw(sql).QueryRow(&company_id_str)
  244. return
  245. }
  246. type PermissionVarietyResp struct {
  247. List []*PermissionVarietyList
  248. }
  249. type PermissionVarietyItem struct {
  250. ChartPermissionId int `description:"权限id"`
  251. ParentId int `description:"父级权限id"`
  252. ClassifyName string `orm:"column(permission_name)" description:"权限名称"`
  253. }
  254. type PermissionVarietyList struct {
  255. ChartPermissionId int `description:"父级id"`
  256. ClassifyName string `description:"分类"`
  257. Items []*PermissionVarietyItem
  258. }
  259. // todo 删除品种列表
  260. func GetPermissionVarietyItems(productId int, classifyName string) (items []*PermissionVarietyItem, err error) {
  261. o := orm.NewOrm()
  262. sql := ` SELECT * FROM chart_permission WHERE enabled=1 AND product_id=? AND classify_name=? AND parent_id > 0 GROUP BY permission_name ORDER BY sort ASC `
  263. _, err = o.Raw(sql, productId, classifyName).QueryRows(&items)
  264. return
  265. }
  266. func GetPermissionVarietyItemsByProductId(productId int) (items []*PermissionVarietyItem, err error) {
  267. o := orm.NewOrm()
  268. sql := ` SELECT * FROM chart_permission WHERE enabled=1 AND product_id=? and parent_id > 0 GROUP BY permission_name ORDER BY sort ASC `
  269. _, err = o.Raw(sql, productId).QueryRows(&items)
  270. return
  271. }
  272. func GetCompanyReportPermission(companyId, productId int) (items []*CompanyReportPermission, err error) {
  273. o := orm.NewOrm()
  274. sql := `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
  275. _, err = o.Raw(sql, companyId, productId).QueryRows(&items)
  276. return
  277. }
  278. func GetCompanyReportPermissionByStatus(companyId, productId int, status string) (items []*CompanyReportPermission, err error) {
  279. o := orm.NewOrm()
  280. sql := `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? AND status = ? `
  281. _, err = o.Raw(sql, companyId, productId, status).QueryRows(&items)
  282. return
  283. }
  284. type PermissionCompany struct {
  285. CompanyId int
  286. }
  287. //
  288. //func GetCompanyListCompanyIdByPermissionIdStr(permissionStr string, permissionCount int) (items []*PermissionCompany, err error) {
  289. // o := orm.NewOrm()
  290. // sql := `
  291. // SELECT company_id, COUNT(company_id) ks
  292. // FROM company_report_permission
  293. // WHERE report_permission_id IN (` + permissionStr + `)
  294. //
  295. // GROUP BY company_id
  296. // HAVING company_id IN
  297. //
  298. // (SELECT company_id
  299. // FROM company_report_permission
  300. // GROUP BY company_id
  301. // HAVING COUNT(company_id) = ?)
  302. //
  303. // AND COUNT(company_id) = ?
  304. //`
  305. // _, err = o.Raw(sql, permissionCount, permissionCount).QueryRows(&items)
  306. // return
  307. //}
  308. func GetCompanyListCompanyIdByPermissionIdStr(permissionStr string) (company_id string, err error) {
  309. o := orm.NewOrm()
  310. sql := `
  311. SELECT
  312. GROUP_CONCAT(DISTINCT company_id) AS company_id
  313. FROM company_report_permission
  314. WHERE report_permission_id IN (?)
  315. #GROUP BY company_id
  316. `
  317. err = o.Raw(sql, permissionStr).QueryRow(&company_id)
  318. return
  319. }
  320. func GetPermissionNameByPermissionIdStr(permissionStr string) (chart_permission_name string, err error) {
  321. o := orm.NewOrm()
  322. sql := `SELECT GROUP_CONCAT(DISTINCT chart_permission_name SEPARATOR ',') AS chart_permission_name FROM chart_permission WHERE chart_permission_id IN (` + permissionStr + `)`
  323. err = o.Raw(sql).QueryRow(&chart_permission_name)
  324. return
  325. }
  326. func GetPermissionIdsByPermissionNameStr(chartPermissionName string) (chart_permission_id string, err error) {
  327. o := orm.NewOrm()
  328. sql := `SELECT GROUP_CONCAT(DISTINCT chart_permission_id SEPARATOR ',') AS chart_permission_id FROM chart_permission WHERE chart_permission_name IN(` + chartPermissionName + `)`
  329. err = o.Raw(sql).QueryRow(&chart_permission_id)
  330. return
  331. }
  332. // 新增试用权限
  333. type PermissionAddTryOutReq struct {
  334. CompanyId int `description:"客户id"`
  335. ChartPermissionId string `description:"权限id,多个用英文逗号隔开"`
  336. }
  337. func FixPermissionStatus(companyId, productId int, startDate, endDate, status string) (err error) {
  338. o := orm.NewOrm()
  339. sql := `UPDATE company_report_permission SET start_date=?,end_date=?,status=?,modify_time=NOW() WHERE company_id=? AND product_id=? AND status IS NULL `
  340. _, err = o.Raw(sql, startDate, endDate, status, companyId, productId).Exec()
  341. return
  342. }
  343. func GetPermissionLookItemsExt(productId string, classifyName string) (items []*PermissionLookItem, err error) {
  344. o := orm.NewOrm()
  345. sql := ` SELECT * FROM chart_permission WHERE enabled=1 AND product_id IN(` + productId + `) AND classify_name=? AND parent_id > 0 ORDER BY sort ASC `
  346. _, err = o.Raw(sql, classifyName).QueryRows(&items)
  347. return
  348. }
  349. func GetPermissionLookItemsExtByParentId(productId string, parentId int) (items []*PermissionLookItem, err error) {
  350. o := orm.NewOrm()
  351. sql := ` SELECT * FROM chart_permission WHERE enabled=1 AND product_id IN(` + productId + `) AND parent_id=? ORDER BY sort ASC `
  352. _, err = o.Raw(sql, parentId).QueryRows(&items)
  353. return
  354. }
  355. // 客户授权产品结构体(包含产品名称)
  356. type CompanyReportPermissionAndName struct {
  357. CompanyReportPermissionId int `description:"客户授权产品id"`
  358. CompanyId int
  359. ReportPermissionId int
  360. CreatedTime time.Time
  361. LastUpdatedTime time.Time
  362. ChartPermissionId int
  363. StartDate string `description:"权限开始日期"`
  364. EndDate string `description:"权限结束日期"`
  365. ProductId int `description:"产品id"`
  366. ProductName string `description:"产品名称"`
  367. CompanyContractId int `description:"合同id"`
  368. PermissionName string `description:"客户授权产品的名称"`
  369. ClassifyName string `description:"客户授权产品的分类名称"`
  370. Status string `description:"'正式','试用','关闭'"`
  371. ModifyTime time.Time `description:"修改时间"`
  372. }
  373. // 根据企业用户id字符串集合
  374. func GetCompanyReportPermissionListByCompanyIds(companyIds string) (items []*CompanyReportPermissionAndName, err error) {
  375. o := orm.NewOrm()
  376. sql := `SELECT a.*,b.permission_name,b.classify_name FROM company_report_permission a left join chart_permission b on a.chart_permission_id=b.chart_permission_id WHERE a.company_id in (` + companyIds + `)`
  377. _, err = o.Raw(sql).QueryRows(&items)
  378. return
  379. }
  380. // 根据企业用户id和产品id获取所有正式的权限
  381. func GetCompanyReportPermissionListByProductId(companyId, productId int) (items []*CompanyReportPermissionAndName, err error) {
  382. o := orm.NewOrm()
  383. sql := `SELECT a.*,b.permission_name,b.classify_name FROM company_report_permission a left join chart_permission b on a.chart_permission_id=b.chart_permission_id WHERE a.company_id = ? and a.product_id=? and a.status="正式"`
  384. _, err = o.Raw(sql, companyId, productId).QueryRows(&items)
  385. return
  386. }
  387. // GetChartPermissionListByIds 根据权限id集合获取所有的权限列表
  388. func GetChartPermissionListByIds(chartPermissionIds []int) (list []*ChartPermission, err error) {
  389. o := orm.NewOrm()
  390. //o.Using("rddp")
  391. //sNums:= make([],len(nums))
  392. idStrList := make([]string, len(chartPermissionIds))
  393. for key, id := range chartPermissionIds {
  394. idStrList[key] = fmt.Sprint(id)
  395. }
  396. idStr := strings.Join(idStrList, ",")
  397. sql := `SELECT * FROM chart_permission WHERE chart_permission_id in (` + idStr + `) `
  398. _, err = o.Raw(sql).QueryRows(&list)
  399. return
  400. }
  401. // 根据企业用户id和产品id获取所有正式的权限
  402. func GetCompanyProductReportPermissionList(companyId, productId int) (items []*CompanyReportPermissionAndName, err error) {
  403. o := orm.NewOrm()
  404. sql := `SELECT a.*,b.permission_name,b.classify_name FROM company_report_permission a left join chart_permission b on a.chart_permission_id=b.chart_permission_id WHERE a.company_id = ? and a.product_id=? `
  405. _, err = o.Raw(sql, companyId, productId).QueryRows(&items)
  406. return
  407. }
  408. // GetChartPermissionListById 根据权限id获取产品权限详情
  409. func GetChartPermissionListById(chartPermissionId int) (item *ChartPermission, err error) {
  410. o := orm.NewOrm()
  411. sql := `SELECT * FROM chart_permission WHERE chart_permission_id =? `
  412. err = o.Raw(sql, chartPermissionId).QueryRow(&item)
  413. return
  414. }
  415. func GetPermissionLookItemsSandO(permissionIds string) (items []*PermissionLookItem, err error) {
  416. o := orm.NewOrm()
  417. sql := ` SELECT * FROM chart_permission WHERE enabled=1 AND chart_permission_id IN (` + permissionIds + `) ORDER BY sort ASC `
  418. _, err = o.Raw(sql).QueryRows(&items)
  419. return
  420. }
  421. func GetPermissionLookItemsSandOByName(permissionName string) (items []*PermissionLookItem, err error) {
  422. o := orm.NewOrm()
  423. sql := ` SELECT * FROM chart_permission WHERE enabled=1 AND product_id = 2 AND remark IN (` + permissionName + `) AND parent_id > 0 ORDER BY sort ASC `
  424. _, err = o.Raw(sql).QueryRows(&items)
  425. return
  426. }
  427. func GetParentIdFromGroup(gid int) (items *int, err error) {
  428. o := orm.NewOrm()
  429. sql := `SELECT parent_id FROM sys_group WHERE group_id=? `
  430. err = o.Raw(sql, gid).QueryRow(&items)
  431. return
  432. }
  433. func GetGroupIdsByParentId(gid int) (items []*string, err error) {
  434. o := orm.NewOrm()
  435. sql := `SELECT group_id FROM sys_group WHERE parent_id=? `
  436. _, err = o.Raw(sql, gid).QueryRows(&items)
  437. return
  438. }
  439. func GetGroupNamesById(gid int) (items *string, err error) {
  440. o := orm.NewOrm()
  441. sql := `SELECT group_name FROM sys_group WHERE group_id=? `
  442. err = o.Raw(sql, gid).QueryRow(&items)
  443. return
  444. }
  445. // GetChartPermissionFirst 获取排序后的权限分类
  446. func GetChartPermissionFirst() (list []*ChartPermissionFirst, err error) {
  447. o := orm.NewOrm()
  448. sql := ` SELECT chart_permission_id, classify_name FROM chart_permission where enabled =1 and product_id=1 and parent_id=0 ORDER BY sort ASC `
  449. _, err = o.Raw(sql).QueryRows(&list)
  450. return
  451. }
  452. // GetCompanyReportPermissionUpgrade 获取升级的权限类型
  453. func GetCompanyReportPermissionUpgrade(companyId, productId int) (items []*CompanyReportPermission, err error) {
  454. o := orm.NewOrm()
  455. sql := `SELECT
  456. re.*
  457. FROM
  458. company_report_permission as re
  459. INNER JOIN chart_permission as c ON c.chart_permission_id = re.chart_permission_id
  460. WHERE
  461. re.company_id = ?
  462. AND re.product_id = ?
  463. AND re.is_upgrade = 1
  464. AND c.permission_type = 1
  465. AND STATUS = '正式' `
  466. _, err = o.Raw(sql, companyId, productId).QueryRows(&items)
  467. return
  468. }
  469. // GetCompanyPermissionNameCheck 通过名称判断客户是否开了某一类型的权限
  470. func GetCompanyPermissionNameCheck(companyId, productId int, permissionName string) (count int, err error) {
  471. o := orm.NewOrm()
  472. sql := `SELECT
  473. COUNT( 1 ) AS count
  474. FROM
  475. company_report_permission AS a
  476. INNER JOIN chart_permission as c ON c.chart_permission_id = a.chart_permission_id
  477. WHERE
  478. a.company_id = ?
  479. AND a.STATUS = '正式'
  480. AND a.product_id = ?
  481. AND c.chart_permission_name = ? `
  482. err = o.Raw(sql, companyId, productId, permissionName).QueryRow(&count)
  483. return
  484. }
  485. // GetCompanyReportPermissionUpgradeByChartPermissionId 通过行业权限获取具备该行业升级权限的公司
  486. func GetCompanyReportPermissionUpgradeByChartPermissionId(chartPermissionId int) (items []*CompanyReportPermission, err error) {
  487. o := orm.NewOrm()
  488. sql := `SELECT
  489. company_id
  490. FROM
  491. company_report_permission AS p
  492. WHERE
  493. 1 = 1
  494. AND p.is_upgrade = 1
  495. AND p.chart_permission_id = ?
  496. AND p.STATUS = '正式'
  497. OR (
  498. p.STATUS = '永续'
  499. AND p.chart_permission_id = ?
  500. )`
  501. _, err = o.Raw(sql, chartPermissionId, chartPermissionId).QueryRows(&items)
  502. return
  503. }
  504. func GetPermissionNameByPermissionIds(permissionIds []int) (names []string, err error) {
  505. if len(permissionIds) == 0 {
  506. return
  507. }
  508. o := orm.NewOrm()
  509. sql := `SELECT
  510. DISTINCT chart_permission_name
  511. FROM
  512. chart_permission
  513. WHERE
  514. chart_permission_id IN (` + utils.GetOrmInReplace(len(permissionIds)) + `)`
  515. _, err = o.Raw(sql, permissionIds).QueryRows(&names)
  516. return
  517. }
  518. func GetPermissionIdsByPermissionNames(names []string) (ids []int, err error) {
  519. if len(names) == 0 {
  520. return
  521. }
  522. o := orm.NewOrm()
  523. sql := `SELECT
  524. DISTINCT chart_permission_id
  525. FROM
  526. chart_permission
  527. WHERE
  528. chart_permission_name IN (` + utils.GetOrmInReplace(len(names)) + `) AND parent_id > 0 `
  529. _, err = o.Raw(sql, names).QueryRows(&ids)
  530. return
  531. }
  532. // GetCompanyIdsStrByReportPermissionIds 根据有效阅读权限IDs获取客户IDs
  533. func GetCompanyIdsStrByReportPermissionIds(permissionIds []int) (companyIds []int, err error) {
  534. if len(permissionIds) == 0 {
  535. return
  536. }
  537. o := orm.NewOrm()
  538. sql := `SELECT
  539. DISTINCT company_id
  540. FROM
  541. company_report_permission
  542. WHERE
  543. report_permission_id IN (` + utils.GetOrmInReplace(len(permissionIds)) + `) AND (DATE(NOW()) BETWEEN start_date AND end_date) AND status IN ('试用', '正式', '永续')`
  544. _, err = o.Raw(sql, permissionIds).QueryRows(&companyIds)
  545. return
  546. }
  547. // GetCompanyIdsStrByContractPermissionIds 根据合同权限IDs获取客户IDs
  548. func GetCompanyIdsStrByContractPermissionIds(permissionIds []int) (companyIds []int, err error) {
  549. if len(permissionIds) == 0 {
  550. return
  551. }
  552. o := orm.NewOrm()
  553. sql := `SELECT
  554. DISTINCT company_id
  555. FROM
  556. company_contract_permission
  557. WHERE
  558. chart_permission_id IN (` + utils.GetOrmInReplace(len(permissionIds)) + `) AND (DATE(NOW()) BETWEEN start_date AND end_date)`
  559. _, err = o.Raw(sql, permissionIds).QueryRows(&companyIds)
  560. return
  561. }
  562. // CompanyListPermissionIds 客户列表权限IDs
  563. type CompanyListPermissionIds struct {
  564. CompanyId int `description:"客户ID"`
  565. ChartPermissionIds string `description:"权限IDs"`
  566. }
  567. // GetContractPermissionIdsByCompanyIds 根据客户ID获取合同权限IDs
  568. func GetContractPermissionIdsByCompanyIds(companyIds []int) (items []*CompanyListPermissionIds, err error) {
  569. if len(companyIds) == 0 {
  570. return
  571. }
  572. o := orm.NewOrm()
  573. sql := `SELECT
  574. company_id,
  575. GROUP_CONCAT(
  576. DISTINCT chart_permission_id
  577. ) AS chart_permission_ids
  578. FROM
  579. company_contract_permission
  580. WHERE
  581. (DATE(NOW()) BETWEEN start_date AND end_date) AND company_id IN (` + utils.GetOrmInReplace(len(companyIds)) + `)`
  582. sql += `GROUP BY company_id`
  583. _, err = o.Raw(sql, companyIds).QueryRows(&items)
  584. return
  585. }
  586. // GetReportPermissionIdsByCompanyIds 根据客户ID获取有效阅读权限IDs
  587. func GetReportPermissionIdsByCompanyIds(companyIds []int) (items []*CompanyListPermissionIds, err error) {
  588. if len(companyIds) == 0 {
  589. return
  590. }
  591. o := orm.NewOrm()
  592. sql := `SELECT
  593. company_id,
  594. GROUP_CONCAT(
  595. DISTINCT chart_permission_id
  596. ) AS chart_permission_ids
  597. FROM
  598. company_report_permission
  599. WHERE
  600. (DATE(NOW()) BETWEEN start_date AND end_date) AND status IN ('试用', '正式', '永续') AND company_id IN (` + utils.GetOrmInReplace(len(companyIds)) + `)`
  601. sql += `GROUP BY company_id`
  602. _, err = o.Raw(sql, companyIds).QueryRows(&items)
  603. return
  604. }
  605. // GetCompanyReportPermissionByCompanyIdAndProductId 获取权限类型
  606. func GetCompanyReportPermissionByCompanyIdAndProductId(companyId, productId int) (items []*CompanyReportPermission, err error) {
  607. o := orm.NewOrm()
  608. sql := `SELECT
  609. re.*
  610. FROM
  611. company_report_permission as re
  612. INNER JOIN chart_permission as c ON c.chart_permission_id = re.chart_permission_id
  613. WHERE
  614. re.company_id = ?
  615. AND re.product_id = ?
  616. AND STATUS = '正式' `
  617. _, err = o.Raw(sql, companyId, productId).QueryRows(&items)
  618. return
  619. }
  620. // 列表
  621. func GetCompanyReportPermissionList(condition string, pars []interface{}, startSize, pageSize int) (items []*CompanyReportPermission, err error) {
  622. o := orm.NewOrm()
  623. sql := `SELECT * FROM company_report_permission WHERE 1= 1 `
  624. if condition != "" {
  625. sql += condition
  626. }
  627. if startSize+pageSize > 0 {
  628. sql += ` LIMIT ?,? `
  629. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  630. } else {
  631. _, err = o.Raw(sql, pars).QueryRows(&items)
  632. }
  633. return
  634. }