company_permission.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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=? AND a.status IN ('正式','试用','永续') `
  405. _, err = o.Raw(sql, companyId, productId).QueryRows(&items)
  406. return
  407. }
  408. // 根据企业用户id和产品id获取所有正式的权限
  409. func GetCompanyProductReportPermissionListInit16_0(companyIds []int, productId int) (items []*CompanyReportPermissionAndName, err error) {
  410. o := orm.NewOrm()
  411. 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 (` + utils.GetOrmInReplace(len(companyIds)) + `) and a.product_id=? AND a.status IN ('正式','试用','永续') `
  412. _, err = o.Raw(sql, companyIds, productId).QueryRows(&items)
  413. return
  414. }
  415. // GetChartPermissionListById 根据权限id获取产品权限详情
  416. func GetChartPermissionListById(chartPermissionId int) (item *ChartPermission, err error) {
  417. o := orm.NewOrm()
  418. sql := `SELECT * FROM chart_permission WHERE chart_permission_id =? `
  419. err = o.Raw(sql, chartPermissionId).QueryRow(&item)
  420. return
  421. }
  422. func GetPermissionLookItemsSandO(permissionIds string) (items []*PermissionLookItem, err error) {
  423. o := orm.NewOrm()
  424. sql := ` SELECT * FROM chart_permission WHERE enabled=1 AND chart_permission_id IN (` + permissionIds + `) ORDER BY sort ASC `
  425. _, err = o.Raw(sql).QueryRows(&items)
  426. return
  427. }
  428. func GetPermissionLookItemsSandOByName(permissionName string) (items []*PermissionLookItem, err error) {
  429. o := orm.NewOrm()
  430. sql := ` SELECT * FROM chart_permission WHERE enabled=1 AND product_id = 2 AND remark IN (` + permissionName + `) AND parent_id > 0 ORDER BY sort ASC `
  431. _, err = o.Raw(sql).QueryRows(&items)
  432. return
  433. }
  434. func GetParentIdFromGroup(gid int) (items *int, err error) {
  435. o := orm.NewOrm()
  436. sql := `SELECT parent_id FROM sys_group WHERE group_id=? `
  437. err = o.Raw(sql, gid).QueryRow(&items)
  438. return
  439. }
  440. func GetGroupIdsByParentId(gid int) (items []*string, err error) {
  441. o := orm.NewOrm()
  442. sql := `SELECT group_id FROM sys_group WHERE parent_id=? `
  443. _, err = o.Raw(sql, gid).QueryRows(&items)
  444. return
  445. }
  446. func GetGroupNamesById(gid int) (items *string, err error) {
  447. o := orm.NewOrm()
  448. sql := `SELECT group_name FROM sys_group WHERE group_id=? `
  449. err = o.Raw(sql, gid).QueryRow(&items)
  450. return
  451. }
  452. // GetChartPermissionFirst 获取排序后的权限分类
  453. func GetChartPermissionFirst() (list []*ChartPermissionFirst, err error) {
  454. o := orm.NewOrm()
  455. 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 `
  456. _, err = o.Raw(sql).QueryRows(&list)
  457. return
  458. }
  459. // GetCompanyReportPermissionUpgrade 获取升级的权限类型
  460. func GetCompanyReportPermissionUpgrade(companyId, productId int) (items []*CompanyReportPermission, err error) {
  461. o := orm.NewOrm()
  462. sql := `SELECT
  463. re.*
  464. FROM
  465. company_report_permission as re
  466. INNER JOIN chart_permission as c ON c.chart_permission_id = re.chart_permission_id
  467. WHERE
  468. re.company_id = ?
  469. AND re.product_id = ?
  470. AND re.is_upgrade = 1
  471. AND c.permission_type = 1
  472. AND STATUS = '正式' `
  473. _, err = o.Raw(sql, companyId, productId).QueryRows(&items)
  474. return
  475. }
  476. // GetCompanyPermissionNameCheck 通过名称判断客户是否开了某一类型的权限
  477. func GetCompanyPermissionNameCheck(companyId, productId int, permissionName string) (count int, err error) {
  478. o := orm.NewOrm()
  479. sql := `SELECT
  480. COUNT( 1 ) AS count
  481. FROM
  482. company_report_permission AS a
  483. INNER JOIN chart_permission as c ON c.chart_permission_id = a.chart_permission_id
  484. WHERE
  485. a.company_id = ?
  486. AND a.STATUS = '正式'
  487. AND a.product_id = ?
  488. AND c.chart_permission_name = ? `
  489. err = o.Raw(sql, companyId, productId, permissionName).QueryRow(&count)
  490. return
  491. }
  492. // GetCompanyReportPermissionUpgradeByChartPermissionId 通过行业权限获取具备该行业升级权限的公司
  493. func GetCompanyReportPermissionUpgradeByChartPermissionId(chartPermissionId int) (items []*CompanyReportPermission, err error) {
  494. o := orm.NewOrm()
  495. sql := `SELECT
  496. company_id
  497. FROM
  498. company_report_permission AS p
  499. WHERE
  500. 1 = 1
  501. AND p.is_upgrade = 1
  502. AND p.chart_permission_id = ?
  503. AND p.STATUS = '正式'
  504. OR (
  505. p.STATUS = '永续'
  506. AND p.chart_permission_id = ?
  507. )`
  508. _, err = o.Raw(sql, chartPermissionId, chartPermissionId).QueryRows(&items)
  509. return
  510. }
  511. func GetPermissionNameByPermissionIds(permissionIds []int) (names []string, err error) {
  512. if len(permissionIds) == 0 {
  513. return
  514. }
  515. o := orm.NewOrm()
  516. sql := `SELECT
  517. DISTINCT chart_permission_name
  518. FROM
  519. chart_permission
  520. WHERE
  521. chart_permission_id IN (` + utils.GetOrmInReplace(len(permissionIds)) + `)`
  522. _, err = o.Raw(sql, permissionIds).QueryRows(&names)
  523. return
  524. }
  525. func GetPermissionIdsByPermissionNames(names []string) (ids []int, err error) {
  526. if len(names) == 0 {
  527. return
  528. }
  529. o := orm.NewOrm()
  530. sql := `SELECT
  531. DISTINCT chart_permission_id
  532. FROM
  533. chart_permission
  534. WHERE
  535. chart_permission_name IN (` + utils.GetOrmInReplace(len(names)) + `) AND parent_id > 0 `
  536. _, err = o.Raw(sql, names).QueryRows(&ids)
  537. return
  538. }
  539. // GetCompanyIdsStrByReportPermissionIds 根据有效阅读权限IDs获取客户IDs
  540. func GetCompanyIdsStrByReportPermissionIds(permissionIds []int) (companyIds []int, err error) {
  541. if len(permissionIds) == 0 {
  542. return
  543. }
  544. o := orm.NewOrm()
  545. sql := `SELECT
  546. DISTINCT company_id
  547. FROM
  548. company_report_permission
  549. WHERE
  550. report_permission_id IN (` + utils.GetOrmInReplace(len(permissionIds)) + `) AND (DATE(NOW()) BETWEEN start_date AND end_date) AND status IN ('试用', '正式', '永续')`
  551. _, err = o.Raw(sql, permissionIds).QueryRows(&companyIds)
  552. return
  553. }
  554. // GetCompanyIdsStrByContractPermissionIds 根据合同权限IDs获取客户IDs
  555. func GetCompanyIdsStrByContractPermissionIds(permissionIds []int) (companyIds []int, err error) {
  556. if len(permissionIds) == 0 {
  557. return
  558. }
  559. o := orm.NewOrm()
  560. sql := `SELECT
  561. DISTINCT company_id
  562. FROM
  563. company_contract_permission
  564. WHERE
  565. chart_permission_id IN (` + utils.GetOrmInReplace(len(permissionIds)) + `) AND (DATE(NOW()) BETWEEN start_date AND end_date)`
  566. _, err = o.Raw(sql, permissionIds).QueryRows(&companyIds)
  567. return
  568. }
  569. // CompanyListPermissionIds 客户列表权限IDs
  570. type CompanyListPermissionIds struct {
  571. CompanyId int `description:"客户ID"`
  572. ChartPermissionIds string `description:"权限IDs"`
  573. }
  574. // GetContractPermissionIdsByCompanyIds 根据客户ID获取合同权限IDs
  575. func GetContractPermissionIdsByCompanyIds(companyIds []int) (items []*CompanyListPermissionIds, err error) {
  576. if len(companyIds) == 0 {
  577. return
  578. }
  579. o := orm.NewOrm()
  580. sql := `SELECT
  581. company_id,
  582. GROUP_CONCAT(
  583. DISTINCT chart_permission_id
  584. ) AS chart_permission_ids
  585. FROM
  586. company_contract_permission
  587. WHERE
  588. (DATE(NOW()) BETWEEN start_date AND end_date) AND company_id IN (` + utils.GetOrmInReplace(len(companyIds)) + `)`
  589. sql += `GROUP BY company_id`
  590. _, err = o.Raw(sql, companyIds).QueryRows(&items)
  591. return
  592. }
  593. // GetReportPermissionIdsByCompanyIds 根据客户ID获取有效阅读权限IDs
  594. func GetReportPermissionIdsByCompanyIds(companyIds []int) (items []*CompanyListPermissionIds, err error) {
  595. if len(companyIds) == 0 {
  596. return
  597. }
  598. o := orm.NewOrm()
  599. sql := `SELECT
  600. company_id,
  601. GROUP_CONCAT(
  602. DISTINCT chart_permission_id
  603. ) AS chart_permission_ids
  604. FROM
  605. company_report_permission
  606. WHERE
  607. (DATE(NOW()) BETWEEN start_date AND end_date) AND status IN ('试用', '正式', '永续') AND company_id IN (` + utils.GetOrmInReplace(len(companyIds)) + `)`
  608. sql += `GROUP BY company_id`
  609. _, err = o.Raw(sql, companyIds).QueryRows(&items)
  610. return
  611. }
  612. // GetCompanyReportPermissionByCompanyIdAndProductId 获取权限类型
  613. func GetCompanyReportPermissionByCompanyIdAndProductId(companyId, productId int) (items []*CompanyReportPermission, err error) {
  614. o := orm.NewOrm()
  615. sql := `SELECT
  616. re.*
  617. FROM
  618. company_report_permission as re
  619. INNER JOIN chart_permission as c ON c.chart_permission_id = re.chart_permission_id
  620. WHERE
  621. re.company_id = ?
  622. AND re.product_id = ?
  623. AND STATUS = '正式' `
  624. _, err = o.Raw(sql, companyId, productId).QueryRows(&items)
  625. return
  626. }
  627. // 列表
  628. func GetCompanyReportPermissionList(condition string, pars []interface{}, startSize, pageSize int) (items []*CompanyReportPermission, err error) {
  629. o := orm.NewOrm()
  630. sql := `SELECT * FROM company_report_permission WHERE 1= 1 `
  631. if condition != "" {
  632. sql += condition
  633. }
  634. if startSize+pageSize > 0 {
  635. sql += ` LIMIT ?,? `
  636. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  637. } else {
  638. _, err = o.Raw(sql, pars).QueryRows(&items)
  639. }
  640. return
  641. }