approval_new.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. approval2 "hongze/hongze_mobile_admin/models/request/approval"
  6. approvalResp "hongze/hongze_mobile_admin/models/response/approval"
  7. "hongze/hongze_mobile_admin/models/tables/chart_permission"
  8. "hongze/hongze_mobile_admin/models/tables/company_approval"
  9. "hongze/hongze_mobile_admin/models/tables/company_contract"
  10. "hongze/hongze_mobile_admin/models/tables/company_delay_permission"
  11. "hongze/hongze_mobile_admin/models/tables/company_product"
  12. "hongze/hongze_mobile_admin/models/tables/company_report_permission"
  13. "hongze/hongze_mobile_admin/models/tables/contract"
  14. "hongze/hongze_mobile_admin/models/tables/contract_approval"
  15. "hongze/hongze_mobile_admin/models/tables/contract_approval_record"
  16. company_apply "hongze/hongze_mobile_admin/services/company_approval"
  17. contractService "hongze/hongze_mobile_admin/services/contract"
  18. "hongze/hongze_mobile_admin/utils"
  19. "strconv"
  20. "strings"
  21. "time"
  22. )
  23. // ListV2
  24. // @Title 获取审批列表接口
  25. // @Description 获取审批列表接口
  26. // @Param PageSize query int true "每页数据条数"
  27. // @Param CurrentIndex query int true "当前页页码,从1开始"
  28. // @Param Status query string true "状态:'待审批','已审批','驳回','已撤回'"
  29. // @Param KeyWord query string true "搜索关键词"
  30. // @Param Keyword query string true "搜索关键词"
  31. // @Param KeywordEq query string true "搜索关键词(全等)"
  32. // @Success 200 {object} approval.CompanyApprovalListResp
  33. // @router /list [get]
  34. func (c *ApprovalCommon) ListV2() {
  35. sysUser := c.AdminWx
  36. pageSize, _ := c.GetInt("PageSize")
  37. currentIndex, _ := c.GetInt("CurrentIndex")
  38. status := c.GetString("Status")
  39. keyword := c.GetString("Keyword")
  40. keywordEq := c.GetString("KeywordEq")
  41. condition := ""
  42. pars := make([]interface{}, 0)
  43. ////如果不是超管或者合规,那么只能查看自己的合同
  44. //join表字段
  45. //归属
  46. if status == "待审批" {
  47. condition += ` AND ( (((c.apply_user_id = ? and a.start_node_id = a.curr_node_id) OR (f.approve_user_id = ? and a.curr_node_id=f.node_id ) ) and c.approve_status="待审批") or (f.approve_user_id = ? and a.curr_node_id > f.node_id and f.node_type="cc") )`
  48. pars = append(pars, sysUser.AdminId, sysUser.AdminId, sysUser.AdminId)
  49. } else {
  50. condition += ` AND (c.apply_user_id = ? or (f.approve_user_id = ? and f.node_id <= a.curr_node_id) )`
  51. pars = append(pars, sysUser.AdminId, sysUser.AdminId)
  52. }
  53. //审批状态
  54. if status != "" {
  55. if status == "已审批" {
  56. condition += ` AND c.approve_status in ("已审批","驳回") `
  57. } else {
  58. condition += ` AND c.approve_status = ? `
  59. if status == "处理中" {
  60. pars = append(pars, "待审批")
  61. } else {
  62. pars = append(pars, status)
  63. }
  64. }
  65. } else {
  66. condition += ` AND c.approve_status != "已撤回" `
  67. }
  68. //关键字
  69. if keyword != "" {
  70. condition += ` AND ( e.company_name LIKE '%` + keyword + `%' OR e.credit_code LIKE '%` + keyword + `%' ) `
  71. }
  72. //关键字(全等)
  73. if keywordEq != "" {
  74. condition += ` AND e.company_name =? `
  75. pars = append(pars, keywordEq)
  76. }
  77. var startSize int
  78. if pageSize <= 0 {
  79. pageSize = utils.PageSize20
  80. }
  81. if currentIndex <= 0 {
  82. currentIndex = 1
  83. }
  84. startSize = paging.StartIndex(currentIndex, pageSize)
  85. total, err := contract_approval.GetCompanyApprovalCount(condition, pars)
  86. if err != nil {
  87. c.FailWithMessage("获取失败", "获取数据总数失败,Err:"+err.Error())
  88. return
  89. }
  90. list, err := contract_approval.GetCompanyApprovalList(condition, pars, startSize, pageSize)
  91. if err != nil {
  92. c.FailWithMessage("获取审批列表失败!", "获取审批列表失败,Err:"+err.Error())
  93. return
  94. }
  95. respList := make([]*company_approval.CompanyApprovalList, 0)
  96. listLen := len(list)
  97. if listLen > 0 {
  98. contractApprovalIdSlice := make([]string, 0)
  99. companyContractIdSlice := make([]string, 0)
  100. for i := 0; i < len(list); i++ {
  101. contractApprovalIdSlice = append(contractApprovalIdSlice, strconv.Itoa(list[i].ContractApprovalId))
  102. companyContractIdSlice = append(companyContractIdSlice, strconv.Itoa(list[i].CompanyContractId))
  103. }
  104. contractApprovalIdStr := strings.Join(contractApprovalIdSlice, ",")
  105. companyContractMap := make(map[int]*company_contract.CompanyContract)
  106. if len(companyContractIdSlice) > 0 {
  107. companyContractIdStr := strings.Join(companyContractIdSlice, ",")
  108. companyContractList, e := company_contract.GetCompanyContractDetailByContractIds(companyContractIdStr)
  109. if e == nil {
  110. for _, v := range companyContractList {
  111. companyContractMap[v.CompanyContractId] = v
  112. }
  113. }
  114. }
  115. //获取审批流列表数据
  116. contractApprovalRecordList, err := contract_approval_record.GetContractApprovalRecordList(contractApprovalIdStr, sysUser.AdminId)
  117. if err != nil {
  118. c.FailWithMessage("获取审批流列表失败!", "获取审批流列表失败,Err:"+err.Error())
  119. return
  120. }
  121. contractApprovalRecordMap := make(map[int]*contract_approval_record.ContractApprovalRecord)
  122. for i := 0; i < len(contractApprovalRecordList); i++ {
  123. contractApprovalRecordMap[contractApprovalRecordList[i].ContractApprovalId] = contractApprovalRecordList[i]
  124. }
  125. for i := 0; i < len(list); i++ {
  126. item := list[i]
  127. list[i].StartDateStr = item.StartDate.Format(utils.FormatDate)
  128. list[i].EndDateStr = item.EndDate.Format(utils.FormatDate)
  129. list[i].CreateTimeStr = item.CreateTime.Format(utils.FormatDateTime)
  130. list[i].ModifyTimeStr = item.ModifyTime.Format(utils.FormatDateTime)
  131. list[i].ApproveTimeStr = item.ApproveTime.Format(utils.FormatDateTime)
  132. //合同审批权限
  133. ApprovalButton := false
  134. var contractDetail contract.ContractDetail
  135. jsonErr := json.Unmarshal([]byte(list[i].ContractDetail), &contractDetail)
  136. if jsonErr != nil {
  137. c.FailWithMessage("审批合同数据转换失败!", "审批合同数据转换失败,Err:"+jsonErr.Error())
  138. return
  139. }
  140. list[i].ContractInfo = contractDetail
  141. //申请转正时合同相关信息
  142. if item.CompanyContractId > 0 {
  143. if com, ok := companyContractMap[item.CompanyContractId]; ok {
  144. if com.Source == "系统合同" {
  145. item.ContractSourceTag = "标准合同"
  146. } else {
  147. item.ContractSourceTag = "非标合同"
  148. }
  149. }
  150. }
  151. //校验最新审批流数据
  152. if contractApprovalRecord, has := contractApprovalRecordMap[item.ContractApprovalId]; has {
  153. //审批流id
  154. list[i].ContractApprovalRecordId = contractApprovalRecord.ContractApprovalRecordId
  155. //审批时间
  156. list[i].ApproveTime = contractApprovalRecord.ApproveTime
  157. list[i].ApproveTimeStr = contractApprovalRecord.ApproveTime.Format(utils.FormatDateTime)
  158. //判断是否有操作权限
  159. list[i].Status = contractApprovalRecord.Status
  160. //判断是否处于待审批状态
  161. if contractApprovalRecord.Status == "待审批" {
  162. //如果不是审批者,那么不能审批操作
  163. if contractApprovalRecord.NodeType != "check" {
  164. continue
  165. }
  166. //如果没有指定审批人的话
  167. if contractApprovalRecord.ApproveUserId <= 0 {
  168. //判断是否与审批角色一致,如果一致,那么允许操作审批
  169. if contractApprovalRecord.ApproveRoleTypeCode == sysUser.RoleTypeCode {
  170. ApprovalButton = true
  171. }
  172. } else if contractApprovalRecord.ApproveUserId == sysUser.AdminId {
  173. //如果指定审批人就是自己的话,那么操作审批
  174. ApprovalButton = true
  175. }
  176. }
  177. }
  178. //撤销权限(本人才有,然后没有审核操作过的才允许撤销)
  179. //if item.Status == "待审批" && item.StartNodeId == item.CurrNodeId && item.ApplyUserId == sysUser.AdminId {
  180. // opButton.Cancel = true
  181. //}
  182. respTmp := &company_approval.CompanyApprovalList{
  183. CompanyId: item.CompanyId,
  184. ProductId: item.ProductId,
  185. CompanyName: item.CompanyName,
  186. CreditCode: item.CreditCode,
  187. CompanyType: item.CompanyType,
  188. IndustryName: item.IndustryName,
  189. SellerName: item.SellerName,
  190. CreateTime: item.CreateTimeStr,
  191. ApprovalTime: item.CreateTime.Format(utils.FormatDateTime),
  192. ApproveTime: item.ApproveTimeStr,
  193. Status: item.CompanyProductStatus,
  194. ApproveContent: item.ApplyContent,
  195. ApproveStatus: item.ApproveStatus,
  196. ApplyMethod: item.ApplyMethod,
  197. ApplyReasons: item.ApplyReasons,
  198. FreezeStartDate: item.FreezeStartDate,
  199. FreezeEndDate: item.FreezeEndDate,
  200. FreezeExpireDays: item.FreezeExpireDays,
  201. Address: item.Address,
  202. Source: item.Source,
  203. ApproveRemark: item.ApproveRemark,
  204. StartDate: item.StartDate.Format(utils.FormatDate),
  205. EndDate: item.EndDate.Format(utils.FormatDate),
  206. ExpireDay: item.ExpireDay,
  207. CompanyApprovalId: item.CompanyApprovalId,
  208. //ApprovalCount int `description:"已延期审批次数"`
  209. CompanyContractId: item.CompanyContractId,
  210. ContractSourceTag: item.ContractSourceTag,
  211. //DelayPermission :
  212. ApplyRealName: item.ApplyRealName,
  213. ApplyUserId: item.ApplyUserId,
  214. OpButton: ApprovalButton,
  215. }
  216. if respTmp.FreezeStartDate != "" && respTmp.FreezeEndDate != "" && respTmp.Status == utils.COMPANY_STATUS_FREEZE {
  217. freezeEndDate, _ := time.Parse(utils.FormatDate, item.FreezeEndDate)
  218. if time.Now().Before(freezeEndDate) {
  219. expireDays := freezeEndDate.Sub(time.Now()).Hours() / 24
  220. respTmp.FreezeExpireDays = int(expireDays)
  221. }
  222. }
  223. if respTmp.StartDate != "" && respTmp.EndDate != "" {
  224. endDate, _ := time.Parse(utils.FormatDate, respTmp.EndDate)
  225. if time.Now().Before(endDate) {
  226. expireDays := endDate.Sub(time.Now()).Hours() / 24
  227. list[i].ExpireDay = int(expireDays)
  228. }
  229. }
  230. switch item.ApplyMethod {
  231. case 2:
  232. //客户冻结转试用次数
  233. approvalCount, err := company_approval.GetFreezeToTryCompanyApprovalCount(respTmp.CompanyId, respTmp.ProductId, respTmp.ApplyUserId)
  234. if err != nil && err.Error() != utils.ErrNoRow() {
  235. c.FailWithMessage("获取失败", "获取冻结转试用审批次数失败,Err:"+err.Error())
  236. return
  237. }
  238. respTmp.ApprovalCount = approvalCount
  239. case 3:
  240. delayPermission, err := company_delay_permission.GetApplyDelayPermission(respTmp.CompanyApprovalId)
  241. if err != nil && err.Error() != utils.ErrNoRow() {
  242. c.FailWithMessage("获取失败", "获取延期审批权限失败,Err:"+err.Error())
  243. return
  244. }
  245. respTmp.DelayPermission = delayPermission
  246. //客户审批试用延期次数
  247. approvalCount, err := company_approval.GetCompanyTryDelayApprovalCount(respTmp.CompanyId, respTmp.ProductId)
  248. if err != nil && err.Error() != utils.ErrNoRow() {
  249. c.FailWithMessage("获取失败", "获取延期审批次数失败,Err:"+err.Error())
  250. return
  251. }
  252. respTmp.ApprovalCount = approvalCount
  253. }
  254. respList = append(respList, respTmp)
  255. }
  256. }
  257. page := paging.GetPaging(currentIndex, pageSize, total)
  258. c.OkDetailed(approvalResp.CompanyApprovalListResp{
  259. List: respList,
  260. Paging: page,
  261. }, "获取成功")
  262. }
  263. // @Title 获取审批单详情接口
  264. // @Description 获取审批单详情接口
  265. // @Param ApprovalId query int true "审批单id"
  266. // @Success 200 {object} approval.CompanyApprovalDetailResp
  267. // @router /detail [get]
  268. func (c *ApprovalCommon) Detail() {
  269. companyApprovalId, _ := c.GetInt("ApprovalId")
  270. //if roleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || roleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN { //审批
  271. // condition += ` AND c.approve_role_type_code=? `
  272. // pars = append(pars, roleTypeCode)
  273. //} else if roleTypeCode == utils.ROLE_TYPE_CODE_FICC_SELLER || roleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER { //销售
  274. // condition += ` AND c.apply_user_id=? `
  275. // pars = append(pars, sysUser.AdminId)
  276. //}
  277. item, flowNodeListResp, err := company_apply.GetApprovalDetailByCompanyApprovalId(companyApprovalId, c.AdminWx)
  278. if err != nil {
  279. c.FailWithMessage("获取失败", "获取数据失败,Err:"+err.Error())
  280. return
  281. }
  282. resp := approvalResp.CompanyApprovalDetailResp{
  283. CompanyApprovalDetail: item,
  284. FlowNodeList: flowNodeListResp,
  285. }
  286. //获取权限列表
  287. //子权限切片集合
  288. var permissionClassifyArr []*chart_permission.ChartPermission
  289. if item.ProductId == 1 {
  290. // 查询一级分类名称
  291. permissionClassifyArr, err = chart_permission.GetPermissionFirstByProductId(item.ProductId)
  292. if err != nil {
  293. c.FailWithMessage("获取失败", "获取审批权限失败,Err:"+err.Error())
  294. return
  295. }
  296. } else {
  297. permissionClassifyArr, err = chart_permission.GetPermissionFirstByProductId(item.ProductId)
  298. if err != nil {
  299. c.FailWithMessage("获取失败", "获取审批权限失败,Err:"+err.Error())
  300. return
  301. }
  302. }
  303. //获取需要审批的权限
  304. delayPermissionList, err := company_delay_permission.GetDelayPermissionItems(item.CompanyId, item.CompanyApprovalId)
  305. if err != nil && err.Error() != utils.ErrNoRow() {
  306. c.FailWithMessage("获取失败", "获取审批权限失败,Err:"+err.Error())
  307. return
  308. }
  309. delayPermissionIdMap := make(map[int]int)
  310. for _, delayPermission := range delayPermissionList {
  311. delayPermissionIdMap[delayPermission.ChartPermissionId] = 0
  312. }
  313. //遍历获取
  314. for _, v := range permissionClassifyArr {
  315. checkList := make([]int, 0)
  316. plist := new(company_report_permission.PermissionLookList)
  317. items, err := company_report_permission.GetPermissionLookByParentId(item.ProductId, v.ChartPermissionId)
  318. if err != nil {
  319. c.FailWithMessage("获取失败", "获取权限信息失败,Err:"+err.Error())
  320. return
  321. }
  322. // 权益主客观均选择的情况下进行合并
  323. //equityPermissionNameCount := make(map[string]int, 0)
  324. for _, n := range items {
  325. if _, ok := delayPermissionIdMap[n.ChartPermissionId]; ok {
  326. checkList = append(checkList, n.ChartPermissionId)
  327. }
  328. //equityPermissionNameCount[n.PermissionName] += 1
  329. }
  330. //newItems := make([]*company_report_permission.PermissionLookItem, 0)
  331. //mergedMap := make(map[string]bool, 0)
  332. //for _, n := range items {
  333. // if equityPermissionNameCount[n.PermissionName] == 2 && !mergedMap[n.PermissionName] {
  334. // newItems = append(newItems, n)
  335. // mergedMap[n.PermissionName] = true
  336. // }
  337. // if equityPermissionNameCount[n.PermissionName] == 1 {
  338. // n.PermissionName = n.Remark
  339. // newItems = append(newItems, n)
  340. // }
  341. //}
  342. //plist.Items = newItems
  343. plist.Items = items
  344. plist.ClassifyName = v.ChartPermissionName
  345. plist.CheckList = checkList
  346. if item.ProductId == 1 {
  347. resp.FiccPermissionList = append(resp.FiccPermissionList, plist)
  348. } else {
  349. // CRM8.8-权限主客观合并
  350. rp := make([]*company_report_permission.PermissionLookList, 0)
  351. rp = append(rp, plist)
  352. newPermissionLookList := contractService.HandleEquityPermissionLookList(rp)
  353. //resp.PermissionList = append(resp.PermissionList, plist)
  354. resp.PermissionList = newPermissionLookList
  355. }
  356. }
  357. c.OkDetailed(resp, "获取成功")
  358. }
  359. // ApplyApprove
  360. // @Title 审批
  361. // @Description 审批接口
  362. // @Param request body approval.CompanyApplyApproveReq true "type json string"
  363. // @Success Ret=200 审批成功
  364. // @router /approve [post]
  365. func (c *ApprovalCommon) ApplyApprove() {
  366. sysUser := c.AdminWx
  367. var req approval2.CompanyApplyApproveReq
  368. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  369. if err != nil {
  370. c.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error())
  371. return
  372. }
  373. //roleTypeCode := sysUser.RoleTypeCode
  374. //productId := services.GetProductId(roleTypeCode)
  375. //if productId == 0 {
  376. // c.FailWithMessage("参数错误", "参数解析失败,Err:"+"客户类型为:"+roleTypeCode)
  377. // return
  378. //}
  379. if req.CompanyId <= 0 {
  380. c.FailWithMessage("参数错误", "客户Id小于等于0")
  381. return
  382. }
  383. var condition string
  384. var pars []interface{}
  385. approvalItem, err := company_approval.GetApprovalInfoByApprovalId(condition, pars, req.CompanyApprovalId)
  386. if err != nil {
  387. c.FailWithMessage("获取审批信息失败", "获取信息失败,Err:"+err.Error())
  388. }
  389. if req.Status != 1 && req.Status != 2 {
  390. c.FailWithMessage("参数错误", "审批状态Id小于等于0")
  391. return
  392. }
  393. companyProduct, err := company_product.GetCompanyProductByCompanyIdAndProductId(req.CompanyId, approvalItem.ProductId)
  394. if err != nil {
  395. c.FailWithMessage("获取信息失败", "获取信息失败,Err:"+err.Error())
  396. return
  397. }
  398. if companyProduct == nil {
  399. c.FailWithMessage("获取审批信息失败", "获取信息失败,companyProduct is null")
  400. return
  401. }
  402. //CompanyApprovalId
  403. //数据校验(校验是否具有审批权限)
  404. _, _, approvalRecord, err := company_apply.CheckApproveAuth(req.CompanyId, approvalItem.ProductId, sysUser)
  405. if err != nil {
  406. c.FailWithMessage("审核失败!", "审核失败,Err:"+err.Error())
  407. return
  408. }
  409. //真实审核操作
  410. if req.Status == 1 {
  411. err = company_apply.Approved(approvalRecord, sysUser, req.Remark)
  412. if err != nil {
  413. c.FailWithMessage("审核失败!", "审核失败,Err:"+err.Error())
  414. return
  415. }
  416. } else {
  417. err = company_apply.Reject(approvalRecord, sysUser, req.Remark)
  418. if err != nil {
  419. c.FailWithMessage("审核失败!", "审核失败,Err:"+err.Error())
  420. return
  421. }
  422. }
  423. c.OkWithMessage("审批操作成功")
  424. }