contract_approval.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. contractReq "hongze/hongze_mobile_admin/models/request/contract"
  7. contractResp "hongze/hongze_mobile_admin/models/response/contract"
  8. "hongze/hongze_mobile_admin/models/tables/contract_approval"
  9. "hongze/hongze_mobile_admin/models/tables/contract_approval_record"
  10. contractService "hongze/hongze_mobile_admin/services/contract"
  11. "hongze/hongze_mobile_admin/utils"
  12. "strconv"
  13. "strings"
  14. )
  15. //合同审批模块
  16. type ContractApprovalCommon struct {
  17. BaseAuth
  18. }
  19. // List
  20. // @Title 审批单列表
  21. // @Description 审批单列表接口
  22. // @Param ContractType query string false "合同类型,枚举值:'新签合同','续约合同','补充协议'"
  23. // @Param Status query string false "合同状态,枚举值:'待审批','已审批','已驳回','已撤回'"
  24. // @Param ProductId query int false "客户类型:传0或者不传为当前账号权限,1 代表是:ficc;2 代表是:权益"
  25. // @Param SellerId query string false "选择的销售id"
  26. // @Param Keyword query string false "搜索关键字"
  27. // @Param KeywordEq query string false "搜索关键字(全等)"
  28. // @Param ModifyStartTime query string false "服务更新时间的选择开始时间,格式:2021-05-23 00:00:00"
  29. // @Param ModifyEndTime query string false "服务更新时间的选择结束时间,格式:2021-05-26 23:59:59"
  30. // @Success 200 {object} contract.ContractApprovalListResp
  31. // @router /list [get]
  32. func (c *ContractApprovalCommon) List() {
  33. sysUser := c.AdminWx
  34. //合同类型、产品类型、合同状态、更新时间、所选销售
  35. //关键字:合同编号、客户名称,社会信用码
  36. contractType := c.GetString("ContractType")
  37. status := c.GetString("Status")
  38. productId, _ := c.GetInt("ProductId")
  39. sellerIds := c.GetString("SellerId")
  40. keyword := c.GetString("Keyword")
  41. keywordEq := c.GetString("KeywordEq")
  42. modifyStartTime := c.GetString("ModifyStartTime")
  43. modifyEndTime := c.GetString("ModifyEndTime")
  44. childCondition := ""
  45. condition := ""
  46. childPars := make([]interface{}, 0)
  47. pars := make([]interface{}, 0)
  48. ////如果不是超管或者合规,那么只能查看自己的合同
  49. //if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN { //权益管理员
  50. // condition += ` AND c.product_id = ? `
  51. // pars = append(pars, 2)
  52. //} else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN { //ficc管理员
  53. // condition += ` AND c.product_id = ? `
  54. // pars = append(pars, 1)
  55. //} else {
  56. // if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  57. // condition += ` AND c.seller_id = ? `
  58. // pars = append(pars, sysUser.AdminId)
  59. // }
  60. //}
  61. //join表字段
  62. joinCondition := " and a.curr_node_id=d.node_id"
  63. //归属
  64. if status == "待审批" {
  65. condition += ` AND ( (((c.seller_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") ) `
  66. joinCondition = ``
  67. pars = append(pars, sysUser.AdminId, sysUser.AdminId, sysUser.AdminId)
  68. } else if status == "处理中" {
  69. condition += ` AND ((c.seller_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="待审批" `
  70. joinCondition = " and a.curr_node_id!=d.node_id"
  71. pars = append(pars, sysUser.AdminId, sysUser.AdminId)
  72. } else {
  73. condition += ` AND (c.seller_id = ? or (d.approve_user_id = ? and d.node_id <= a.curr_node_id))`
  74. pars = append(pars, sysUser.AdminId, sysUser.AdminId)
  75. }
  76. //合同类型、、更新时间、所选销售
  77. //关键字:合同编号、客户名称,社会信用码
  78. if contractType != "" {
  79. condition += ` AND c.contract_type = ? `
  80. pars = append(pars, contractType)
  81. }
  82. //审批状态
  83. fmt.Println(status)
  84. if status != "" {
  85. if status == "已审批" {
  86. condition += ` AND c.status in ("已审批","已驳回") `
  87. } else if status == "其它" {
  88. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_SELLER || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  89. condition += ` AND c.status in ("已撤回","已作废","已解约") `
  90. } else {
  91. condition += ` AND c.status in ("已作废","已解约") `
  92. }
  93. } else {
  94. condition += ` AND c.status = ? `
  95. if status == "处理中" {
  96. pars = append(pars, "待审批")
  97. } else {
  98. pars = append(pars, status)
  99. }
  100. }
  101. } else {
  102. //childCondition += ` AND status != "已撤回" `
  103. condition += ` AND c.status not in ("已撤回","待提交") `
  104. }
  105. //产品类型
  106. if productId > 0 {
  107. condition += ` AND c.product_id = ? `
  108. pars = append(pars, productId)
  109. }
  110. //所选销售
  111. if sellerIds != "" {
  112. condition += ` AND c.seller_id IN (` + sellerIds + `) `
  113. }
  114. //更新开始时间
  115. //更新开始时间
  116. if modifyStartTime != "" {
  117. condition += ` AND a.modify_time >= ? `
  118. pars = append(pars, modifyStartTime)
  119. }
  120. //更新结束时间
  121. if modifyEndTime != "" {
  122. condition += ` AND a.modify_time <= ? `
  123. pars = append(pars, modifyEndTime)
  124. }
  125. //关键字
  126. if keyword != "" {
  127. condition += ` AND (c.contract_code LIKE '%` + keyword + `%' OR c.company_name LIKE '%` + keyword + `%' OR c.credit_code LIKE '%` + keyword + `%' ) `
  128. }
  129. //关键字(全等)
  130. if keywordEq != "" {
  131. condition += ` AND c.company_name =? `
  132. pars = append(pars, keywordEq)
  133. }
  134. pageSize, _ := c.GetInt("PageSize")
  135. currentIndex, _ := c.GetInt("CurrentIndex")
  136. var startSize int
  137. if pageSize <= 0 {
  138. pageSize = utils.PageSize20
  139. }
  140. if currentIndex <= 0 {
  141. currentIndex = 1
  142. }
  143. startSize = paging.StartIndex(currentIndex, pageSize)
  144. var total int
  145. var list []*contract_approval.ContractApprovalList
  146. if status != "待审批" && status != "处理中" {
  147. tmpTotal, err := contract_approval.GetContractApprovalListCountV2(childCondition, condition, childPars, pars)
  148. if err != nil {
  149. c.FailWithMessage("获取失败", "获取数据总数失败,Err:"+err.Error())
  150. return
  151. }
  152. orderBy := ` modify_time desc `
  153. switch status {
  154. case "已签回":
  155. orderBy = ` check_back_file_time desc `
  156. case "已撤回":
  157. orderBy = ` c.modify_time desc `
  158. case "已作废":
  159. orderBy = ` c.invalid_time desc `
  160. }
  161. tmpList, err := contract_approval.GetContractApprovalListV2(childCondition, condition, orderBy, childPars, pars, startSize, pageSize)
  162. if err != nil {
  163. c.FailWithMessage("获取审批列表失败!", "获取审批列表失败,Err:"+err.Error())
  164. return
  165. }
  166. total = tmpTotal
  167. list = tmpList
  168. } else {
  169. tmpTotal, err := contract_approval.GetContractApprovalListCount(childCondition, condition, joinCondition, childPars, pars)
  170. if err != nil {
  171. c.FailWithMessage("获取失败", "获取数据总数失败,Err:"+err.Error())
  172. return
  173. }
  174. tmpList, err := contract_approval.GetContractApprovalList(childCondition, condition, joinCondition, childPars, pars, startSize, pageSize)
  175. if err != nil {
  176. c.FailWithMessage("获取审批列表失败!", "获取审批列表失败,Err:"+err.Error())
  177. return
  178. }
  179. total = tmpTotal
  180. list = tmpList
  181. }
  182. if len(list) > 0 {
  183. contractApprovalIdSlice := make([]string, 0)
  184. for i := 0; i < len(list); i++ {
  185. contractApprovalIdSlice = append(contractApprovalIdSlice, strconv.Itoa(list[i].ContractApprovalId))
  186. }
  187. contractApprovalIdStr := strings.Join(contractApprovalIdSlice, ",")
  188. //获取审批流列表数据
  189. contractApprovalRecordList, err := contract_approval_record.GetContractApprovalRecordList(contractApprovalIdStr, sysUser.AdminId)
  190. if err != nil {
  191. c.FailWithMessage("获取审批列表失败!", "获取审批列表失败,Err:"+err.Error())
  192. return
  193. }
  194. contractApprovalRecordMap := make(map[int]*contract_approval_record.ContractApprovalRecord)
  195. for i := 0; i < len(contractApprovalRecordList); i++ {
  196. contractApprovalRecordMap[contractApprovalRecordList[i].ContractApprovalId] = contractApprovalRecordList[i]
  197. }
  198. for i := 0; i < len(list); i++ {
  199. item := list[i]
  200. list[i].StartDateStr = item.StartDate.Format(utils.FormatDate)
  201. list[i].EndDateStr = item.EndDate.Format(utils.FormatDate)
  202. list[i].CreateTimeStr = item.CreateTime.Format(utils.FormatDateTime)
  203. list[i].ModifyTimeStr = item.ModifyTime.Format(utils.FormatDateTime)
  204. if item.ApproveStatus == "已审批" || item.ApproveStatus == "已驳回" {
  205. list[i].ApproveTime = item.ModifyTime
  206. list[i].ApproveTimeStr = item.ModifyTime.Format(utils.FormatDateTime)
  207. }
  208. //list[i].ContractInfo = contractDetail
  209. //校验最新审批流数据
  210. //if contractApprovalRecord, has := contractApprovalRecordMap[item.ContractApprovalId]; has {
  211. // //审批流id
  212. // list[i].ContractApprovalRecordId = contractApprovalRecord.ContractApprovalRecordId
  213. // //审批时间
  214. // list[i].ApproveTime = contractApprovalRecord.ApproveTime
  215. // list[i].ApproveTimeStr = contractApprovalRecord.ApproveTime.Format(utils.FormatDateTime)
  216. //
  217. // ////判断是否处于待审批状态
  218. // //if contractApprovalRecord.Status == "待审批" {
  219. // // //如果不是审批者,那么不能审批操作
  220. // // if contractApprovalRecord.NodeType != "check" {
  221. // // continue
  222. // // }
  223. // // //如果没有指定审批人的话
  224. // // if contractApprovalRecord.ApproveUserId <= 0 {
  225. // // //判断是否与审批角色一致,如果一致,那么允许操作审批
  226. // // if contractApprovalRecord.ApproveRoleTypeCode == sysUser.RoleTypeCode {
  227. // // list[i].CanApprovalButton = true
  228. // // }
  229. // // } else if contractApprovalRecord.ApproveUserId == sysUser.AdminId {
  230. // // //如果指定审批人就是自己的话,那么操作审批
  231. // // list[i].CanApprovalButton = true
  232. // // }
  233. // //}
  234. //}
  235. //合同状态(突然说要改成展示合同状态,那就根据业务需求,改成返回:合同状态咯)
  236. //list[i].Status = item.ContractStatus
  237. if item.Status == "待审批" && status == "处理中" {
  238. list[i].Status = "处理中"
  239. }
  240. //如果是代付合同,那么就展示代付合同的审核内容
  241. if item.ContractBusinessType == "代付合同" {
  242. item.ApplyContent = "代付合同"
  243. }
  244. }
  245. }
  246. page := paging.GetPaging(currentIndex, pageSize, total)
  247. c.OkDetailed(contractResp.ContractApprovalListResp{
  248. List: list,
  249. Paging: page,
  250. }, "获取成功")
  251. }
  252. // @Title 审批单详情
  253. // @Description 审批单详情接口
  254. // @Param ContractApprovalId query int false "审批单id"
  255. // @Param ContractApprovalRecordId query int false "审批流程单id"
  256. // @Success 200 {object} contract.ContractDetailResp
  257. // @router /detail [get]
  258. func (c *ContractApprovalCommon) Detail() {
  259. //审批流程单id
  260. contractApprovalRecordId, _ := c.GetInt("ContractApprovalRecordId")
  261. if contractApprovalRecordId > 0 {
  262. c.detailByApprovalRecordId(contractApprovalRecordId)
  263. return
  264. }
  265. //审批单id
  266. contractApprovalId, _ := c.GetInt("ContractApprovalId")
  267. //审批单详情
  268. contractApprovalInfo, err := contract_approval.GetContractApprovalById(contractApprovalId)
  269. if err != nil {
  270. c.FailWithMessage("获取审批单失败!", "获取审批单失败,Err:"+err.Error())
  271. return
  272. }
  273. //合同详情
  274. contractInfo, flowNodeListResp, contractOpButton, err := contractService.GetContractDetailByContractId(contractApprovalInfo.ContractId, c.AdminWx)
  275. if err != nil {
  276. c.FailWithMessage("获取合同详情失败!", "获取合同详情失败,ERR:"+err.Error())
  277. return
  278. }
  279. resp := contractResp.ContractDetailResp{
  280. ContractDetail: contractInfo,
  281. FlowNodeList: flowNodeListResp,
  282. OpButton: contractOpButton,
  283. }
  284. c.OkDetailed(resp, "获取成功")
  285. }
  286. func (c *ContractApprovalCommon) detailByApprovalRecordId(approvalRecordId int) {
  287. approvalRecordInfo, err := contract_approval_record.GetContractApprovalRecordById(approvalRecordId)
  288. if err != nil {
  289. c.FailWithMessage("获取审批流程单失败", "获取审批流程单失败,Err:"+err.Error())
  290. return
  291. }
  292. //审批单详情
  293. contractApprovalInfo, err := contract_approval.GetContractApprovalById(approvalRecordInfo.ContractApprovalId)
  294. if err != nil {
  295. c.FailWithMessage("获取审批单失败", "获取审批单失败,Err:"+err.Error())
  296. return
  297. }
  298. //合同详情
  299. contractInfo, flowNodeListResp, contractOpButton, err := contractService.GetContractDetailByContractId(contractApprovalInfo.ContractId, c.AdminWx)
  300. if err != nil {
  301. c.FailWithMessage("获取合同详情失败!", "获取合同详情失败,ERR:"+err.Error())
  302. return
  303. }
  304. resp := contractResp.ContractDetailResp{
  305. ContractDetail: contractInfo,
  306. FlowNodeList: flowNodeListResp,
  307. OpButton: contractOpButton,
  308. }
  309. c.OkDetailed(resp, "获取成功")
  310. return
  311. }
  312. // @Title 驳回审批
  313. // @Description 驳回审批接口
  314. // @Param request body contract.RejectReq true "type json string"
  315. // @Success Ret=200 驳回成功
  316. // @router /reject [post]
  317. func (c *ContractApprovalCommon) Reject() {
  318. var req contractReq.RejectReq
  319. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  320. if err != nil {
  321. c.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error())
  322. return
  323. }
  324. //合同id编号
  325. contractId := req.ContractId
  326. if contractId <= 0 {
  327. c.FailWithMessage("请传入合同编号!", "请传入合同编号!")
  328. return
  329. }
  330. //数据校验
  331. contractInfo, approvalInfo, approvalRecord, err := contractService.CheckApproveAuth(contractId, c.AdminWx)
  332. if err != nil {
  333. c.FailWithMessage("驳回审批失败!", "驳回审批失败,Err:"+err.Error())
  334. return
  335. }
  336. err = contractService.Reject(contractInfo, approvalInfo, approvalRecord, c.AdminWx, req.Remark)
  337. if err != nil {
  338. c.FailWithMessage("驳回审批失败!", "驳回审批失败,Err:"+err.Error())
  339. return
  340. }
  341. c.OkWithMessage("驳回成功")
  342. }
  343. // @Title 处理审批单
  344. // @Description 处理审批单
  345. // @Param request body contract.ApprovedReq true "type json string"
  346. // @Success Ret=200 申请成功
  347. // @router /approved [post]
  348. func (c *ContractApprovalCommon) Approved() {
  349. var req contractReq.ApprovedReq
  350. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  351. if err != nil {
  352. c.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error())
  353. return
  354. }
  355. //合同id编号
  356. contractId := req.ContractId
  357. if contractId <= 0 {
  358. c.FailWithMessage("请传入合同编号!", "请传入合同编号!")
  359. return
  360. }
  361. //数据校验
  362. contractInfo, approvalInfo, approvalRecord, err := contractService.CheckApproveAuth(contractId, c.AdminWx)
  363. if err != nil {
  364. c.FailWithMessage("驳回审批失败!", "驳回审批失败,Err:"+err.Error())
  365. return
  366. }
  367. err = contractService.Approved(contractInfo, approvalInfo, approvalRecord, c.AdminWx, req.Remark)
  368. if err != nil {
  369. c.FailWithMessage("审核失败!", "审核失败,Err:"+err.Error())
  370. return
  371. }
  372. c.OkWithMessage("审核通过")
  373. }