seal_approval.go 16 KB

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