seal_approval.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. sealReq "hongze/hongze_mobile_admin/models/request/seal"
  6. sealResp "hongze/hongze_mobile_admin/models/response/seal"
  7. "hongze/hongze_mobile_admin/models/tables/contract_approval"
  8. "hongze/hongze_mobile_admin/models/tables/contract_approval_record"
  9. "hongze/hongze_mobile_admin/models/tables/seal"
  10. sealService "hongze/hongze_mobile_admin/services/seal"
  11. "hongze/hongze_mobile_admin/utils"
  12. "strconv"
  13. "strings"
  14. )
  15. //SealApprovalCommon 用印审批模块
  16. type SealApprovalCommon struct {
  17. BaseAuth
  18. }
  19. // List
  20. // @Title 审批单列表
  21. // @Description 审批单列表接口
  22. // @Param Use query string false "用途,枚举值:销售合同, 渠道合同, 付款通知函, 招投标, 战略合作协议"
  23. // @Param ServiceType query string false "业务类型,枚举值:"新签合同, 续约合同, 补充协议"
  24. // @Param SealType query string false "公章类型,枚举值:合同章, 公章, 法人章"
  25. // @Param Status query string false "合同状态,枚举值:'待审批','已审批','已驳回','已撤回','已签回'"
  26. // @Param UserId query string false "选择的申请人id"
  27. // @Param Keyword query string false "搜索关键字"
  28. // @Param KeywordEq query string false "搜索关键字(全等)"
  29. // @Param ModifyStartTime query string false "服务更新时间的选择开始时间,格式:2021-05-23 00:00:00"
  30. // @Param ModifyEndTime query string false "服务更新时间的选择结束时间,格式:2021-05-26 23:59:59"
  31. // @Success 200 {object} seal.SealApprovalListResp
  32. // @router /list [get]
  33. func (c *SealApprovalCommon) List() {
  34. sysUser := c.AdminWx
  35. //合同类型、产品类型、合同状态、更新时间、所选销售
  36. //关键字:合同编号、客户名称,社会信用码
  37. serviceType := c.GetString("ServiceType")
  38. use := c.GetString("Use")
  39. sealType := c.GetString("SealType")
  40. status := c.GetString("Status")
  41. productId, _ := c.GetInt("ProductId")
  42. sellerIds := c.GetString("SellerId")
  43. keyword := c.GetString("Keyword")
  44. keywordEq := c.GetString("KeywordEq")
  45. modifyStartTime := c.GetString("ModifyStartTime")
  46. modifyEndTime := c.GetString("ModifyEndTime")
  47. childCondition := ""
  48. condition := ""
  49. childPars := make([]interface{}, 0)
  50. pars := make([]interface{}, 0)
  51. ////如果不是超管或者合规,那么只能查看自己的合同
  52. //if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN { //权益管理员
  53. // condition += ` AND c.product_id = ? `
  54. // pars = append(pars, 2)
  55. //} else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN { //ficc管理员
  56. // condition += ` AND c.product_id = ? `
  57. // pars = append(pars, 1)
  58. //} else {
  59. // if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  60. // condition += ` AND c.seller_id = ? `
  61. // pars = append(pars, sysUser.AdminId)
  62. // }
  63. //}
  64. //join表字段
  65. joinCondition := " and a.curr_node_id=d.node_id"
  66. //归属
  67. if status == "待审批" {
  68. condition += ` AND ( (((c.user_id = ? and a.start_node_id = a.curr_node_id) OR (d.approve_user_id = ? and a.curr_node_id=d.node_id)) and d.status="待审批") or (d.approve_user_id = ? and a.curr_node_id > d.node_id and d.node_type="cc") )`
  69. //condition += ` AND (c.user_id = ? OR d.approve_user_id = ?) and (d.approve_user_id is null or d.approve_user_id = c.user_id)`
  70. joinCondition = ``
  71. pars = append(pars, sysUser.AdminId, sysUser.AdminId, sysUser.AdminId)
  72. } else if status == "处理中" {
  73. condition += ` AND ((c.user_id = ? and a.start_node_id != a.curr_node_id) OR (d.approve_user_id = ? and a.curr_node_id > d.node_id and d.node_type!="cc")) and c.status="待审批" `
  74. joinCondition = " and a.curr_node_id!=d.node_id"
  75. pars = append(pars, sysUser.AdminId, sysUser.AdminId)
  76. } else if status == "其它" {
  77. condition += ` AND ( (c.user_id = ? and c.status in ("已撤回","已作废") ) or (d.approve_user_id = ? and d.node_id <= a.curr_node_id and c.status = "已作废" ))`
  78. pars = append(pars, sysUser.AdminId, sysUser.AdminId)
  79. } else {
  80. condition += ` AND (c.user_id = ? or (d.approve_user_id = ? and d.node_id <= a.curr_node_id))`
  81. pars = append(pars, sysUser.AdminId, sysUser.AdminId)
  82. }
  83. //合同类型、、更新时间、所选销售
  84. //关键字:合同编号、客户名称,社会信用码
  85. if use != "" {
  86. condition += ` AND c.use = ? `
  87. pars = append(pars, use)
  88. }
  89. if serviceType != "" {
  90. condition += ` AND c.service_type = ? `
  91. pars = append(pars, serviceType)
  92. }
  93. if sealType != "" {
  94. condition += ` AND c.seal_type = ? `
  95. pars = append(pars, sealType)
  96. }
  97. //审批状态
  98. if status != "" {
  99. if status == "已审批" {
  100. condition += ` AND c.status in ("已审批","已驳回") `
  101. } else if status != "其它" { //其它状态额外处理,这边不处理
  102. condition += ` AND c.status = ? `
  103. if status == "处理中" {
  104. pars = append(pars, "待审批")
  105. } else {
  106. pars = append(pars, status)
  107. }
  108. //if status == "待审批" {
  109. // condition += ` AND a.status = ? `
  110. // pars = append(pars, status)
  111. //}
  112. }
  113. } else {
  114. //childCondition += ` AND status != "已撤回" `
  115. condition += ` AND c.status not in ("已撤回","待提交") `
  116. }
  117. //产品类型
  118. if productId > 0 {
  119. condition += ` AND c.product_id = ? `
  120. pars = append(pars, productId)
  121. }
  122. //所选销售
  123. if sellerIds != "" {
  124. condition += ` AND c.user_id IN (` + sellerIds + `) `
  125. }
  126. //更新开始时间
  127. //更新开始时间
  128. if modifyStartTime != "" {
  129. condition += ` AND a.modify_time >= ? `
  130. pars = append(pars, modifyStartTime)
  131. }
  132. //更新结束时间
  133. if modifyEndTime != "" {
  134. condition += ` AND a.modify_time <= ? `
  135. pars = append(pars, modifyEndTime)
  136. }
  137. //关键字
  138. if keyword != "" {
  139. condition += ` AND (c.code LIKE '%` + keyword + `%' OR c.use_company_name LIKE '%` + keyword + `%' OR c.company_name LIKE '%` + keyword + `%' OR c.credit_code LIKE '%` + keyword + `%' ) `
  140. }
  141. //关键字(全等)
  142. if keywordEq != "" {
  143. condition += ` AND (c.use_company_name =? OR c.company_name =?) `
  144. pars = append(pars, keywordEq, keywordEq)
  145. }
  146. pageSize, _ := c.GetInt("PageSize")
  147. currentIndex, _ := c.GetInt("CurrentIndex")
  148. var startSize int
  149. if pageSize <= 0 {
  150. pageSize = utils.PageSize20
  151. }
  152. if currentIndex <= 0 {
  153. currentIndex = 1
  154. }
  155. startSize = paging.StartIndex(currentIndex, pageSize)
  156. var total int
  157. var list []*contract_approval.SealApprovalList
  158. if status != "待审批" && status != "处理中" {
  159. tmpTotal, err := contract_approval.GetSealApprovalListCountV2(childCondition, condition, childPars, pars)
  160. if err != nil {
  161. c.FailWithMessage("获取失败", "获取数据总数失败,Err:"+err.Error())
  162. return
  163. }
  164. orderBy := ` modify_time desc `
  165. switch status {
  166. case "已撤回":
  167. orderBy = ` c.modify_time desc `
  168. case "已作废":
  169. orderBy = ` c.invalid_time desc `
  170. }
  171. tmpList, err := contract_approval.GetSealApprovalListV2(childCondition, condition, orderBy, childPars, pars, startSize, pageSize)
  172. if err != nil {
  173. c.FailWithMessage("获取审批列表失败!", "获取审批列表失败,Err:"+err.Error())
  174. return
  175. }
  176. total = tmpTotal
  177. list = tmpList
  178. } else {
  179. tmpTotal, err := contract_approval.GetSealApprovalListCount(childCondition, condition, joinCondition, childPars, pars)
  180. if err != nil {
  181. c.FailWithMessage("获取失败", "获取数据总数失败,Err:"+err.Error())
  182. return
  183. }
  184. tmpList, err := contract_approval.GetSealApprovalList(childCondition, condition, joinCondition, childPars, pars, startSize, pageSize)
  185. if err != nil {
  186. c.FailWithMessage("获取审批列表失败!", "获取审批列表失败,Err:"+err.Error())
  187. return
  188. }
  189. total = tmpTotal
  190. list = tmpList
  191. }
  192. if len(list) > 0 {
  193. contractApprovalIdSlice := make([]string, 0)
  194. for i := 0; i < len(list); i++ {
  195. contractApprovalIdSlice = append(contractApprovalIdSlice, strconv.Itoa(list[i].ContractApprovalId))
  196. }
  197. contractApprovalIdStr := strings.Join(contractApprovalIdSlice, ",")
  198. //获取当前审批流列表数据
  199. contractApprovalRecordList, err := contract_approval_record.GetContractApprovalRecordList(contractApprovalIdStr, sysUser.AdminId)
  200. if err != nil {
  201. c.FailWithMessage("获取审批列表失败!", "获取审批列表失败,Err:"+err.Error())
  202. return
  203. }
  204. contractApprovalRecordMap := make(map[int]*contract_approval_record.ContractApprovalRecord)
  205. for i := 0; i < len(contractApprovalRecordList); i++ {
  206. contractApprovalRecordMap[contractApprovalRecordList[i].ContractApprovalId] = contractApprovalRecordList[i]
  207. }
  208. //获取自己的审批流列表数据
  209. selfContractApprovalRecordList, err := contract_approval_record.GetSelfContractApprovalRecordList(contractApprovalIdStr, sysUser.AdminId)
  210. if err != nil {
  211. c.FailWithMessage("获取审批列表失败!", "获取审批列表失败,Err:"+err.Error())
  212. return
  213. }
  214. selfContractApprovalRecordMap := make(map[int]*contract_approval_record.ContractApprovalRecord)
  215. for i := 0; i < len(selfContractApprovalRecordList); i++ {
  216. selfContractApprovalRecordMap[selfContractApprovalRecordList[i].ContractApprovalId] = selfContractApprovalRecordList[i]
  217. }
  218. for i := 0; i < len(list); i++ {
  219. item := list[i]
  220. list[i].CreateTimeStr = item.CreateTime.Format(utils.FormatDateTime)
  221. list[i].ModifyTimeStr = item.ModifyTime.Format(utils.FormatDateTime)
  222. if item.ApproveStatus == "已审批" || item.ApproveStatus == "已驳回" {
  223. list[i].ApproveTime = item.ModifyTime
  224. list[i].ApproveTimeStr = item.ModifyTime.Format(utils.FormatDateTime)
  225. }
  226. //list[i].ContractInfo = contractDetail
  227. //如果当前状态是待审批的话,那么校验自己的审批流数据,然后去返回对应的状态
  228. if item.Status == "待审批" {
  229. //如果是自己发起的审批单,同时已经经过了一轮审批,那么标记为处理中
  230. if item.ApplyUserId == c.AdminWx.AdminId && item.CurrNodeId > item.StartNodeId {
  231. list[i].Status = "处理中"
  232. } else {
  233. if contractApprovalRecord, has := selfContractApprovalRecordMap[item.ContractApprovalId]; has {
  234. if contractApprovalRecord.NodeId < item.CurrNodeId && contractApprovalRecord.NodeType != "cc" {
  235. list[i].Status = "处理中"
  236. }
  237. }
  238. }
  239. }
  240. }
  241. }
  242. page := paging.GetPaging(currentIndex, pageSize, total)
  243. c.OkDetailed(sealResp.SealApprovalListResp{
  244. List: list,
  245. Paging: page,
  246. }, "获取成功")
  247. }
  248. // Detail
  249. // @Title 审批单详情
  250. // @Description 审批单详情接口
  251. // @Param SealId query int false "用印id"
  252. // @Param ContractApprovalId query int false "审批单id"
  253. // @Param ContractApprovalRecordId query int false "审批流程单id"
  254. // @Success 200 {object} seal.SealDetailResp
  255. // @router /detail [get]
  256. func (c *SealApprovalCommon) Detail() {
  257. //审批流程单id
  258. contractApprovalRecordId, _ := c.GetInt("ContractApprovalRecordId")
  259. if contractApprovalRecordId > 0 {
  260. c.detailByApprovalRecordId(contractApprovalRecordId)
  261. return
  262. }
  263. //用印id
  264. sealId, _ := c.GetInt("SealId")
  265. if sealId <= 0 {
  266. //审批单id
  267. contractApprovalId, _ := c.GetInt("ContractApprovalId")
  268. if contractApprovalId <= 0 {
  269. c.FailWithMessage("获取审批单失败!审批单id未传", "获取审批单失败!审批单id未传")
  270. return
  271. }
  272. //审批单详情
  273. contractApprovalInfo, err := contract_approval.GetContractApprovalById(contractApprovalId)
  274. if err != nil {
  275. c.FailWithMessage("获取审批单失败!", "获取审批单失败,Err:"+err.Error())
  276. return
  277. }
  278. sealId = contractApprovalInfo.ContractId
  279. }
  280. //用印详情
  281. sealInfo, flowNodeListResp, opButton, err := sealService.GetSealDetailBySealId(sealId, c.AdminWx)
  282. if err != nil {
  283. c.FailWithMessage("获取详情失败", "获取详情失败,Err:"+err.Error())
  284. return
  285. }
  286. resp := sealResp.SealDetailResp{
  287. SealDetail: sealInfo,
  288. FlowNodeList: flowNodeListResp,
  289. OpButton: opButton,
  290. }
  291. c.OkDetailed(resp, "获取成功")
  292. }
  293. func (c *SealApprovalCommon) detailByApprovalRecordId(approvalRecordId int) {
  294. approvalRecordInfo, err := contract_approval_record.GetContractApprovalRecordById(approvalRecordId)
  295. if err != nil {
  296. c.FailWithMessage("获取审批流程单失败", "获取审批流程单失败,Err:"+err.Error())
  297. return
  298. }
  299. //审批单详情
  300. contractApprovalInfo, err := contract_approval.GetContractApprovalById(approvalRecordInfo.ContractApprovalId)
  301. if err != nil {
  302. c.FailWithMessage("获取审批单失败", "获取审批单失败,Err:"+err.Error())
  303. return
  304. }
  305. //用印详情
  306. sealInfo, flowNodeListResp, opButton, err := sealService.GetSealDetailBySealId(contractApprovalInfo.ContractId, c.AdminWx)
  307. if err != nil {
  308. c.FailWithMessage("获取详情失败", "获取详情失败,Err:"+err.Error())
  309. return
  310. }
  311. resp := sealResp.SealDetailResp{
  312. SealDetail: sealInfo,
  313. FlowNodeList: flowNodeListResp,
  314. OpButton: opButton,
  315. }
  316. c.OkDetailed(resp, "获取成功")
  317. return
  318. }
  319. // Reject
  320. // @Title 驳回审批
  321. // @Description 驳回审批接口
  322. // @Param request body seal.RejectReq true "type json string"
  323. // @Success Ret=200 驳回成功
  324. // @router /reject [post]
  325. func (c *SealApprovalCommon) Reject() {
  326. var req sealReq.RejectReq
  327. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  328. if err != nil {
  329. c.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error())
  330. return
  331. }
  332. //用印id编号
  333. sealId := req.SealId
  334. if sealId <= 0 {
  335. c.FailWithMessage("请传入用印单编号!", "请传入用印单编号!")
  336. return
  337. }
  338. remark := strings.Trim(req.Remark, " ")
  339. if remark == "" {
  340. c.FailWithMessage("请输入驳回理由!", "请输入驳回理由!")
  341. return
  342. }
  343. //数据校验
  344. sealInfo, approvalInfo, approvalRecord, err := sealService.CheckApproveAuth(sealId, c.AdminWx)
  345. if err != nil {
  346. c.FailWithMessage("驳回审批失败!", "驳回审批失败,Err:"+err.Error())
  347. return
  348. }
  349. err = sealService.Reject(sealInfo, approvalInfo, approvalRecord, c.AdminWx, remark)
  350. if err != nil {
  351. c.FailWithMessage("驳回审批失败!", "驳回审批失败,Err:"+err.Error())
  352. return
  353. }
  354. c.OkWithMessage("驳回成功")
  355. }
  356. // Approved
  357. // @Title 处理审批单
  358. // @Description 处理审批单
  359. // @Param request body seal.ApprovedReq true "type json string"
  360. // @Success Ret=200 申请成功
  361. // @router /approved [post]
  362. func (c *SealApprovalCommon) Approved() {
  363. var req sealReq.ApprovedReq
  364. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  365. if err != nil {
  366. c.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error())
  367. return
  368. }
  369. //用印编号id编号
  370. sealId := req.SealId
  371. if sealId <= 0 {
  372. c.FailWithMessage("请传入用印单编号!", "请传入用印单编号!")
  373. return
  374. }
  375. //数据校验
  376. sealInfo, approvalInfo, approvalRecord, err := sealService.CheckApproveAuth(sealId, c.AdminWx)
  377. if err != nil {
  378. c.FailWithMessage("驳回审批失败!", "驳回审批失败,Err:"+err.Error())
  379. return
  380. }
  381. err = sealService.Approved(sealInfo, approvalInfo, approvalRecord, c.AdminWx, req.Remark)
  382. if err != nil {
  383. c.FailWithMessage("审核失败!", "审核失败,Err:"+err.Error())
  384. return
  385. }
  386. c.OkWithMessage("审核通过")
  387. }
  388. // CancelApply
  389. // @Title 撤回审批申请
  390. // @Description 撤回审批申请接口
  391. // @Param SealId query int true "用印id编号"
  392. // @Success Ret=200 撤回申请成功
  393. // @router /cancelApply [get]
  394. func (c *SealApprovalCommon) CancelApply() {
  395. //用印id编号
  396. sealId, err := c.GetInt("SealId")
  397. if err != nil {
  398. c.FailWithMessage("获取审批流编号失败", "获取审批流编号失败,err:"+err.Error())
  399. return
  400. }
  401. //获取用印信息
  402. sealInfo, err := seal.GetSealInfoById(sealId)
  403. if err != nil {
  404. c.FailWithMessage("获取审批流信息失败", "获取审批流信息失败,err:"+err.Error())
  405. return
  406. }
  407. err = sealService.CancelApply(sealInfo, c.AdminWx.AdminId, c.AdminWx.RealName)
  408. if err != nil {
  409. c.FailWithMessage("撤回申请失败!", "撤回申请失败,Err:"+err.Error())
  410. return
  411. }
  412. c.OkWithMessage("撤回申请成功")
  413. }