business_apply.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. package business_trip
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "hongze/hongze_mobile_admin/controllers"
  7. "hongze/hongze_mobile_admin/models/tables/admin"
  8. "hongze/hongze_mobile_admin/models/tables/business_trip"
  9. "hongze/hongze_mobile_admin/models/tables/company_approval_message"
  10. "hongze/hongze_mobile_admin/services"
  11. "hongze/hongze_mobile_admin/utils"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // 出差
  17. type BusinessTrip struct {
  18. controllers.BaseAuth
  19. }
  20. // @Title 出差申请接口
  21. // @Description 出差申请接口
  22. // @Param request body business_trip.BusinessApplyReq true "type json string"
  23. // @Success Ret=200 保存成功
  24. // @router /apply/add [post]
  25. func (this *BusinessTrip) ApplyAdd() {
  26. sysUser := this.AdminWx
  27. sysUserId := sysUser.AdminId
  28. var req business_trip.BusinessApplyReq
  29. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  30. if err != nil {
  31. this.FailWithMessage("解析参数失败", "解析参数失败,Err:"+err.Error())
  32. return
  33. }
  34. if req.ArriveDate == "" {
  35. this.FailWithMessage("请填写到达日期", "请填写到达日期")
  36. return
  37. }
  38. if req.ReturnDate == "" {
  39. this.FailWithMessage("请填写返程日期", "请填写返程日期")
  40. return
  41. }
  42. if req.Province == "" {
  43. this.FailWithMessage("请填写目的地省", "请填写目的地省")
  44. return
  45. }
  46. if req.City == "" {
  47. this.FailWithMessage("请填写目的地市", "请填写目的地市")
  48. return
  49. }
  50. if req.Reason == "" {
  51. this.FailWithMessage("请填写出差事由", "请填写出差事由")
  52. return
  53. }
  54. if req.Transportation == "" {
  55. this.FailWithMessage("请填写交通工具", "请填写交通工具")
  56. return
  57. }
  58. if req.Itinerary == "" {
  59. this.FailWithMessage("请填写行程说明", "请填写行程说明")
  60. return
  61. }
  62. startP, _ := time.ParseInLocation(utils.FormatDate, req.ArriveDate, time.Local)
  63. endP, _ := time.ParseInLocation(utils.FormatDate, req.ReturnDate, time.Local)
  64. nowDate, _ := time.ParseInLocation(utils.FormatDate, time.Now().Format(utils.FormatDate), time.Local)
  65. if startP.Before(nowDate) {
  66. this.FailWithMessage("出差开始日期不能小于当前日期", "出差开始日期不能小于当前日期")
  67. return
  68. }
  69. if !endP.Equal(nowDate) && endP.Before(nowDate) {
  70. this.FailWithMessage("出差结束时间不能小于当前时间", "出差结束时间不能小于当前时间")
  71. return
  72. }
  73. if !startP.Equal(endP) && (startP.After(endP) || startP.Equal(endP)) {
  74. this.FailWithMessage("出差开始日期应小于结束日期", "出差开始日期应小于结束日期")
  75. return
  76. }
  77. //校验出差日期冲突
  78. {
  79. businessApplyCount, err := business_trip.CheckBusinessApplyDate(req.ArriveDate, req.ReturnDate, "'待审批','已通过'", sysUser.AdminId, 0)
  80. if err != nil {
  81. this.FailWithMessage("时间冲突检测失败", "时间冲突检测失败-CheckBusinessApplyDate!Err:"+err.Error())
  82. return
  83. }
  84. if businessApplyCount > 0 {
  85. this.FailWithMessage("日期已被占用", "日期已被占用-CheckBusinessApplyDate!")
  86. return
  87. }
  88. }
  89. //校验申请人,是否被邀请为同行人,并且出差日期冲突
  90. {
  91. peerCount, err := business_trip.CheckBusinessApplyPeerDate(req.ArriveDate, req.ReturnDate, "'待审批','已通过'", sysUser.AdminId, 0)
  92. if err != nil {
  93. this.FailWithMessage("时间冲突检测失败", "时间冲突检测失败-CheckBusinessApplyPeerDate!Err:"+err.Error())
  94. return
  95. }
  96. if peerCount > 0 {
  97. this.FailWithMessage("所选日期您被设置为同行人,请重新选择!", "所选日期您被设置为同行人,请重新选择!")
  98. return
  99. }
  100. }
  101. //校验同行人,出差日期冲突
  102. {
  103. if req.PeerPeopleId != "" {
  104. var existPeerNameArr []string
  105. peerIdArr := strings.Split(req.PeerPeopleId, ",")
  106. peerNameArr := strings.Split(req.PeerPeopleName, ",")
  107. for k, v := range peerIdArr {
  108. peerId, err := strconv.Atoi(v)
  109. if err != nil {
  110. this.FailWithMessage("申请失败", "同行人id失败,Err:"+err.Error())
  111. return
  112. }
  113. peerCount, err := business_trip.CheckBusinessApplyPeerDate(req.ArriveDate, req.ReturnDate, "'待审批','已通过'", peerId, 0)
  114. if err != nil {
  115. this.FailWithMessage("时间冲突检测失败", "时间冲突检测失败-CheckBusinessApplyPeerDate!Err:"+err.Error())
  116. return
  117. }
  118. if peerCount > 0 {
  119. existPeerNameArr = append(existPeerNameArr, peerNameArr[k])
  120. }
  121. }
  122. if len(existPeerNameArr) > 0 {
  123. this.FailWithMessage(strings.Join(existPeerNameArr, ",")+"日期已被占用", "日期已被占用-CheckBusinessApplyDate!")
  124. return
  125. }
  126. }
  127. }
  128. approveItem, err := admin.GetAdminById(utils.ApproveUserId)
  129. if err != nil {
  130. this.FailWithMessage("获取审批人信息失败", "获取审批人信息失败,Err:"+err.Error())
  131. return
  132. }
  133. item := new(business_trip.BusinessApply)
  134. item.ApplyAdminId = sysUser.AdminId
  135. item.ApplyRealName = sysUser.RealName
  136. item.ArriveDate = req.ArriveDate
  137. item.ReturnDate = req.ReturnDate
  138. item.Province = req.Province
  139. item.City = req.City
  140. item.Reason = req.Reason
  141. item.Transportation = req.Transportation
  142. item.PeerPeopleId = req.PeerPeopleId
  143. item.PeerPeopleName = req.PeerPeopleName
  144. item.Status = "待审批"
  145. item.ApproveId = approveItem.AdminId
  146. item.ApproveName = approveItem.RealName
  147. item.CreateTime = time.Now()
  148. item.ModifyTime = time.Now()
  149. item.Itinerary = req.Itinerary
  150. applyId, err := business_trip.AddBusinessApply(item)
  151. if err != nil {
  152. this.FailWithMessage("申请失败", "申请失败,Err:"+err.Error())
  153. return
  154. }
  155. //新增随行人
  156. {
  157. if req.PeerPeopleId != "" {
  158. peerList := make([]*business_trip.BusinessApplyPeer, 0)
  159. peerIdArr := strings.Split(req.PeerPeopleId, ",")
  160. peerNameArr := strings.Split(req.PeerPeopleName, ",")
  161. for k, v := range peerIdArr {
  162. peerId, err := strconv.Atoi(v)
  163. if err != nil {
  164. this.FailWithMessage("申请失败", "同行人id失败,Err:"+err.Error())
  165. return
  166. }
  167. peerItem := new(business_trip.BusinessApplyPeer)
  168. peerItem.BusinessApplyId = int(applyId)
  169. peerItem.ArriveDate = req.ArriveDate
  170. peerItem.ReturnDate = req.ReturnDate
  171. peerItem.Province = req.Province
  172. peerItem.City = req.City
  173. peerItem.Status = "待审批"
  174. peerItem.PeerPeopleId = peerId
  175. peerItem.PeerPeopleName = peerNameArr[k]
  176. peerItem.CreateTime = time.Now()
  177. peerItem.ModifyTime = time.Now()
  178. peerList = append(peerList, peerItem)
  179. }
  180. err = business_trip.AddBusinessApplyPeer(peerList)
  181. if err != nil {
  182. this.FailWithMessage("申请失败", "新增同行人信息失败,Err:"+err.Error())
  183. return
  184. }
  185. }
  186. }
  187. {
  188. //系统消息
  189. sourceType := 10
  190. content := sysUser.RealName + " " + req.Province + req.City + req.Reason + "出差申请"
  191. go services.AddCompanyApprovalMessage(sysUserId, approveItem.AdminId, 0, int(applyId), 1, sourceType, 1, "", content, content, "", "")
  192. }
  193. //模板消息
  194. {
  195. var wxAppPath string
  196. if utils.RunMode == "debug" {
  197. wxAppPath = "pages/index/index"
  198. } else {
  199. wxAppPath = "pages-approve/businessTrip/detail?id=" + strconv.Itoa(int(applyId))
  200. }
  201. first := "您好,有新的申请待处理"
  202. keyword1 := sysUser.RealName
  203. keyword2 := "--" //sysUser.Mobile
  204. //keyword3 := time.Now().Format(utils.FormatDateTime)
  205. keyword3 := req.ArriveDate + "至" + req.ReturnDate
  206. var keyword4 string
  207. if req.PeerPeopleId != "" {
  208. keyword4 = req.Transportation + "前往" + req.Province + req.City + "," + "同行人:" + req.PeerPeopleName
  209. } else {
  210. keyword4 = req.Transportation + "前往" + req.Province + req.City
  211. }
  212. remark := "请尽快完成审批"
  213. go services.SendWxMsgWithRoadshowPending(first, keyword1, keyword2, keyword3, keyword4, remark, wxAppPath, approveItem.Mobile)
  214. }
  215. this.OkWithMessage("保存成功")
  216. }
  217. // @Title 出差重新申请接口
  218. // @Description 出差重新申请接口
  219. // @Param request body business_trip.BusinessApplyEditReq true "type json string"
  220. // @Success Ret=200 保存成功
  221. // @router /apply/edit [post]
  222. func (this *BusinessTrip) ApplyEdit() {
  223. sysUser := this.AdminWx
  224. sysUserId := sysUser.AdminId
  225. var req business_trip.BusinessApplyEditReq
  226. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  227. if err != nil {
  228. this.FailWithMessage("解析参数失败", "解析参数失败,Err:"+err.Error())
  229. return
  230. }
  231. if req.ArriveDate == "" {
  232. this.FailWithMessage("请填写到达日期", "请填写到达日期")
  233. return
  234. }
  235. if req.ReturnDate == "" {
  236. this.FailWithMessage("请填写返程日期", "请填写返程日期")
  237. return
  238. }
  239. if req.Province == "" {
  240. this.FailWithMessage("请填写目的地省", "请填写目的地省")
  241. return
  242. }
  243. if req.City == "" {
  244. this.FailWithMessage("请填写目的地市", "请填写目的地市")
  245. return
  246. }
  247. if req.Reason == "" {
  248. this.FailWithMessage("请填写出差事由", "请填写出差事由")
  249. return
  250. }
  251. if req.Transportation == "" {
  252. this.FailWithMessage("请填写交通工具", "请填写交通工具")
  253. return
  254. }
  255. if req.Itinerary == "" {
  256. this.FailWithMessage("请填写行程说明", "请填写行程说明")
  257. return
  258. }
  259. startP, _ := time.ParseInLocation(utils.FormatDate, req.ArriveDate, time.Local)
  260. endP, _ := time.ParseInLocation(utils.FormatDate, req.ReturnDate, time.Local)
  261. nowDate, _ := time.ParseInLocation(utils.FormatDate, time.Now().Format(utils.FormatDate), time.Local)
  262. if startP.Before(nowDate) {
  263. fmt.Println("出差开始日期不能小于当前日期!")
  264. return
  265. }
  266. if !endP.Equal(nowDate) && endP.Before(nowDate) {
  267. fmt.Println("出差结束时间不能小于当前时间!")
  268. return
  269. }
  270. if !startP.Equal(endP) && (startP.After(endP) || startP.Equal(endP)) {
  271. fmt.Println("出差开始日期应小于结束日期!")
  272. return
  273. }
  274. //校验出差日期冲突
  275. {
  276. businessApplyCount, err := business_trip.CheckBusinessApplyDate(req.ArriveDate, req.ReturnDate, "'待审批','已通过'", sysUser.AdminId, req.BusinessApplyId)
  277. if err != nil {
  278. this.FailWithMessage("时间冲突检测失败", "时间冲突检测失败-CheckBusinessApplyDate!Err:"+err.Error())
  279. return
  280. }
  281. if businessApplyCount > 0 {
  282. this.FailWithMessage("日期已被占用", "日期已被占用-CheckBusinessApplyDate!")
  283. return
  284. }
  285. }
  286. //校验申请人,是否被邀请为同行人,并且出差日期冲突
  287. {
  288. peerCount, err := business_trip.CheckBusinessApplyPeerDate(req.ArriveDate, req.ReturnDate, "'待审批','已通过'", sysUser.AdminId, 0)
  289. if err != nil {
  290. this.FailWithMessage("时间冲突检测失败", "时间冲突检测失败-CheckBusinessApplyPeerDate!Err:"+err.Error())
  291. return
  292. }
  293. if peerCount > 0 {
  294. this.FailWithMessage("所选日期您被设置为同行人,请重新选择!", "所选日期您被设置为同行人,请重新选择!")
  295. return
  296. }
  297. }
  298. //校验同行人,出差日期冲突
  299. {
  300. if req.PeerPeopleId != "" {
  301. var existPeerNameArr []string
  302. peerIdArr := strings.Split(req.PeerPeopleId, ",")
  303. peerNameArr := strings.Split(req.PeerPeopleName, ",")
  304. for k, v := range peerIdArr {
  305. peerId, err := strconv.Atoi(v)
  306. if err != nil {
  307. this.FailWithMessage("申请失败", "同行人id失败,Err:"+err.Error())
  308. return
  309. }
  310. peerCount, err := business_trip.CheckBusinessApplyPeerDate(req.ArriveDate, req.ReturnDate, "'待审批','已通过'", peerId, req.BusinessApplyId)
  311. if err != nil {
  312. this.FailWithMessage("时间冲突检测失败", "时间冲突检测失败-CheckBusinessApplyPeerDate!Err:"+err.Error())
  313. return
  314. }
  315. if peerCount > 0 {
  316. existPeerNameArr = append(existPeerNameArr, peerNameArr[k])
  317. }
  318. }
  319. if len(existPeerNameArr) > 0 {
  320. this.FailWithMessage(strings.Join(existPeerNameArr, ",")+"日期已被占用", "日期已被占用-CheckBusinessApplyDate!")
  321. return
  322. }
  323. }
  324. }
  325. if req.BusinessApplyId <= 0 {
  326. this.FailWithMessage("参数错误", "参数错误!BusinessApplyId:"+strconv.Itoa(req.BusinessApplyId))
  327. return
  328. }
  329. businessApplyItem, err := business_trip.GetBusinessApplyById(req.BusinessApplyId)
  330. if err != nil {
  331. if err.Error() == utils.ErrNoRow() {
  332. this.FailWithMessage("出差申请已被删除,请刷新页面", "出差申请已被删除,请刷新页面")
  333. return
  334. }
  335. this.FailWithMessage("获取数据失败!", "获取数据失败!GetBusinessApplyById:"+err.Error())
  336. return
  337. }
  338. if businessApplyItem.Status == "已通过" {
  339. this.FailWithMessage("已通过,不可进行重新申请操作!", "已通过,不可进行重新申请操作!")
  340. return
  341. } else if businessApplyItem.Status == "已过期" {
  342. this.FailWithMessage("已过期,不可进行重新申请操作!", "已过期,不可进行重新申请操作!")
  343. return
  344. }
  345. whereParams := make(map[string]interface{})
  346. updateParams := make(map[string]interface{})
  347. whereParams["business_apply_id"] = req.BusinessApplyId
  348. updateParams["arrive_date"] = req.ArriveDate
  349. updateParams["return_date"] = req.ReturnDate
  350. updateParams["province"] = req.Province
  351. updateParams["city"] = req.City
  352. updateParams["reason"] = req.Reason
  353. updateParams["transportation"] = req.Transportation
  354. updateParams["peer_people_id"] = req.PeerPeopleId
  355. updateParams["peer_people_name"] = req.PeerPeopleName
  356. updateParams["status"] = "待审批"
  357. updateParams["refuse_reason"] = ""
  358. updateParams["create_time"] = time.Now()
  359. updateParams["modify_time"] = time.Now()
  360. updateParams["itinerary"] = req.Itinerary
  361. err = business_trip.UpdateBusinessApply(whereParams, updateParams)
  362. if err != nil {
  363. this.FailWithMessage("重新申请失败", "重新申请失败,Err:"+err.Error())
  364. return
  365. }
  366. //新增随行人
  367. {
  368. if req.PeerPeopleId != "" {
  369. err = business_trip.DeleteBusinessApplyPeer(req.BusinessApplyId)
  370. if err != nil {
  371. this.FailWithMessage("申请失败", "删除同行人失败,Err:"+err.Error())
  372. return
  373. }
  374. peerList := make([]*business_trip.BusinessApplyPeer, 0)
  375. peerIdArr := strings.Split(req.PeerPeopleId, ",")
  376. peerNameArr := strings.Split(req.PeerPeopleName, ",")
  377. for k, v := range peerIdArr {
  378. peerId, err := strconv.Atoi(v)
  379. if err != nil {
  380. this.FailWithMessage("申请失败", "同行人id失败,Err:"+err.Error())
  381. return
  382. }
  383. peerItem := new(business_trip.BusinessApplyPeer)
  384. peerItem.BusinessApplyId = req.BusinessApplyId
  385. peerItem.ArriveDate = req.ArriveDate
  386. peerItem.ReturnDate = req.ReturnDate
  387. peerItem.Province = req.Province
  388. peerItem.City = req.City
  389. peerItem.Status = "待审批"
  390. peerItem.PeerPeopleId = peerId
  391. peerItem.PeerPeopleName = peerNameArr[k]
  392. peerItem.CreateTime = time.Now()
  393. peerItem.ModifyTime = time.Now()
  394. peerList = append(peerList, peerItem)
  395. }
  396. err = business_trip.AddBusinessApplyPeer(peerList)
  397. if err != nil {
  398. this.FailWithMessage("申请失败", "新增同行人信息失败,Err:"+err.Error())
  399. return
  400. }
  401. }
  402. }
  403. approveItem, err := admin.GetAdminById(utils.ApproveUserId)
  404. if err != nil {
  405. this.FailWithMessage("获取审批人信息失败!", "获取审批人信息失败,Err:"+err.Error())
  406. return
  407. }
  408. {
  409. //系统消息
  410. sourceType := 10
  411. content := sysUser.RealName + " " + req.Province + req.City + req.Reason + "出差申请"
  412. go services.AddCompanyApprovalMessage(sysUserId, approveItem.AdminId, 0, req.BusinessApplyId, 1, sourceType, 1, "", content, content, "", "")
  413. }
  414. //模板消息
  415. {
  416. var wxAppPath string
  417. if utils.RunMode == "debug" {
  418. wxAppPath = "pages/index/index"
  419. } else {
  420. wxAppPath = "pages-approve/businessTrip/detail?id=" + strconv.Itoa(req.BusinessApplyId)
  421. }
  422. first := "您好,有新的申请待处理"
  423. keyword1 := sysUser.RealName
  424. keyword2 := "--" //sysUser.Mobile
  425. //keyword3 := time.Now().Format(utils.FormatDateTime)
  426. keyword3 := req.ArriveDate + "至" + req.ReturnDate
  427. var keyword4 string
  428. if req.PeerPeopleId != "" {
  429. keyword4 = req.Transportation + "前往" + req.Province + req.City + "," + "同行人:" + req.PeerPeopleName
  430. } else {
  431. keyword4 = req.Transportation + "前往" + req.Province + req.City
  432. }
  433. remark := "请尽快完成审批"
  434. go services.SendWxMsgWithRoadshowPending(first, keyword1, keyword2, keyword3, keyword4, remark, wxAppPath, approveItem.Mobile)
  435. }
  436. this.OkWithMessage("申请成功")
  437. }
  438. // @Title 出差申请列表
  439. // @Description 出差申请列表接口
  440. // @Param PageSize query int true "每页数据条数"
  441. // @Param CurrentIndex query int true "当前页页码,从1开始"
  442. // @Param Reason query string true "出差事由"
  443. // @Param Status query string true "状态"
  444. // @Success 200 {object} business_trip.BusinessApplyListResp
  445. // @router /apply/list [get]
  446. func (this *BusinessTrip) ApplyList() {
  447. sysUser := this.AdminWx
  448. sysUserId := sysUser.AdminId
  449. pageSize, _ := this.GetInt("PageSize")
  450. currentIndex, _ := this.GetInt("CurrentIndex")
  451. var total int
  452. page := paging.GetPaging(currentIndex, pageSize, total)
  453. var startSize int
  454. if pageSize <= 0 {
  455. pageSize = utils.PageSize10
  456. }
  457. if currentIndex <= 0 {
  458. currentIndex = 1
  459. }
  460. startSize = paging.StartIndex(currentIndex, pageSize)
  461. reason := this.GetString("Reason")
  462. status := this.GetString("Status")
  463. var condition string
  464. var pars []interface{}
  465. condition += ` AND apply_admin_id=? `
  466. pars = append(pars, sysUserId)
  467. if reason != "" {
  468. condition += ` AND reason=? `
  469. pars = append(pars, reason)
  470. }
  471. if status != "" {
  472. if status == "已处理" {
  473. condition += ` AND status <> '待审批' `
  474. } else {
  475. condition += ` AND status=? `
  476. pars = append(pars, status)
  477. }
  478. }
  479. resp := new(business_trip.BusinessApplyListResp)
  480. total, err := business_trip.GetBusinessApplyListCount(condition, pars)
  481. if err != nil && err.Error() != utils.ErrNoRow() {
  482. this.FailWithMessage("获取信息失败!", "获取数据总数失败,GetCalendarListCount,Err:"+err.Error())
  483. return
  484. }
  485. dataList, err := business_trip.GetBusinessApplyList(condition, pars, startSize, pageSize)
  486. if err != nil {
  487. this.FailWithMessage("获取信息失败!", "获取数据总数失败,GetBusinessApplyList,Err:"+err.Error())
  488. return
  489. }
  490. page = paging.GetPaging(currentIndex, pageSize, total)
  491. resp.Paging = page
  492. resp.List = dataList
  493. this.OkDetailed(resp, "获取成功")
  494. }
  495. // @Title 删除出差申请接口
  496. // @Description 删除出差申请接口
  497. // @Param request body business_trip.BusinessApplyDeleteReq true "type json string"
  498. // @Success Ret=200 删除成功
  499. // @router /apply/delete [post]
  500. func (this *BusinessTrip) Delete() {
  501. //sysUser := this.AdminWx
  502. //sysUserId := sysUser.AdminId
  503. var req business_trip.BusinessApplyDeleteReq
  504. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  505. if err != nil {
  506. this.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error())
  507. return
  508. }
  509. if req.BusinessApplyId <= 0 {
  510. this.FailWithMessage("参数错误!", "参数错误!BusinessApplyId:"+strconv.Itoa(req.BusinessApplyId))
  511. return
  512. }
  513. businessApplyItem, err := business_trip.GetBusinessApplyById(req.BusinessApplyId)
  514. if err != nil {
  515. if err.Error() == utils.ErrNoRow() {
  516. this.FailWithMessage("出差申请信息已被删除,请刷新页面!", "出差申请信息已被删除,请刷新页面!")
  517. return
  518. }
  519. this.FailWithMessage("获取数据失败!", "获取数据失败!GetBusinessApplyById:"+err.Error())
  520. return
  521. }
  522. err = business_trip.DeleteBusinessApply(businessApplyItem.BusinessApplyId)
  523. if err != nil {
  524. this.FailWithMessage("删除失败!", "删除失败!DeleteBusinessApply:"+err.Error())
  525. return
  526. }
  527. if businessApplyItem.PeerPeopleId != "" {
  528. err = business_trip.DeleteBusinessApplyPeer(businessApplyItem.BusinessApplyId)
  529. if err != nil {
  530. this.FailWithMessage("删除失败!", "删除失败!DeleteBusinessApplyPeer:"+err.Error())
  531. return
  532. }
  533. }
  534. //删除系统消息
  535. go company_approval_message.DeleteCompanyApprovalMessage(req.BusinessApplyId, 10)
  536. this.OkWithMessage("删除成功")
  537. }
  538. // @Title 撤回出差申请接口
  539. // @Description 撤回出差申请接口
  540. // @Param request body business_trip.BusinessApplyBackReq true "type json string"
  541. // @Success Ret=200 保存成功
  542. // @router /apply/back [post]
  543. func (this *BusinessTrip) Back() {
  544. //sysUser := this.AdminWx
  545. //sysUserId := sysUser.AdminId
  546. var req business_trip.BusinessApplyBackReq
  547. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  548. if err != nil {
  549. this.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error())
  550. return
  551. }
  552. if req.BusinessApplyId <= 0 {
  553. this.FailWithMessage("参数错误!", "参数错误!BusinessApplyId:"+strconv.Itoa(req.BusinessApplyId))
  554. return
  555. }
  556. businessApplyItem, err := business_trip.GetBusinessApplyById(req.BusinessApplyId)
  557. if err != nil {
  558. if err.Error() == utils.ErrNoRow() {
  559. this.FailWithMessage("出差申请已被删除,请刷新页面", "出差申请已被删除,请刷新页面")
  560. return
  561. }
  562. this.FailWithMessage("获取数据失败!", "获取数据失败!GetBusinessApplyById:"+err.Error())
  563. return
  564. }
  565. if businessApplyItem.Status == "已通过" {
  566. this.FailWithMessage("已通过,不可进行撤回操作!", "已通过,不可进行撤回操作!")
  567. return
  568. } else if businessApplyItem.Status == "已驳回" {
  569. this.FailWithMessage("已驳回,不可进行撤回操作!", "已驳回,不可进行撤回操作!")
  570. return
  571. } else if businessApplyItem.Status == "已撤回" {
  572. this.FailWithMessage("已撤回,不可重复操作!", "已撤回,不可重复操作!")
  573. return
  574. } else if businessApplyItem.Status == "已过期" {
  575. this.FailWithMessage("已过期,不可进行重复操作!", "已过期,不可进行重复操作!")
  576. return
  577. }
  578. whereParams := make(map[string]interface{})
  579. updateParams := make(map[string]interface{})
  580. whereParams["business_apply_id"] = req.BusinessApplyId
  581. updateParams["status"] = "已撤回"
  582. updateParams["modify_time"] = time.Now()
  583. err = business_trip.UpdateBusinessApply(whereParams, updateParams)
  584. if err != nil {
  585. this.FailWithMessage("撤回失败", "撤回失败!UpdateBusinessApply:"+err.Error())
  586. return
  587. }
  588. if businessApplyItem.PeerPeopleId != "" {
  589. peerWhereParams := make(map[string]interface{})
  590. peerUpdateParams := make(map[string]interface{})
  591. whereParams["business_apply_id"] = req.BusinessApplyId
  592. peerUpdateParams["status"] = "已撤回"
  593. peerUpdateParams["modify_time"] = time.Now()
  594. err = business_trip.UpdateBusinessApplyPeer(peerWhereParams, peerUpdateParams)
  595. if err != nil {
  596. this.FailWithMessage("撤回失败", "撤回失败!UpdateBusinessApplyPeer:"+err.Error())
  597. return
  598. }
  599. }
  600. //删除系统消息
  601. go company_approval_message.DeleteCompanyApprovalMessage(req.BusinessApplyId, 10)
  602. this.OkWithMessage("撤回成功")
  603. }
  604. // @Title 出差申请详情
  605. // @Description 出差申请详情接口
  606. // @Param BusinessApplyId query int true "出差申请id"
  607. // @Success 200 {object} business_trip.BusinessApplyView
  608. // @router /apply/detail [get]
  609. func (this *BusinessTrip) ApplyDetail() {
  610. sysUser := this.AdminWx
  611. sysUserId := sysUser.AdminId
  612. businessApplyId, _ := this.GetInt("BusinessApplyId")
  613. if businessApplyId <= 0 {
  614. this.FailWithMessage("参数错误", "出差申请id错误:"+strconv.Itoa(businessApplyId))
  615. return
  616. }
  617. item, err := business_trip.GetBusinessApplyById(businessApplyId)
  618. if err != nil {
  619. if err.Error() == utils.ErrNoRow() {
  620. this.FailWithMessage("该出差申请已被删除", "该出差申请已被删除")
  621. return
  622. }
  623. this.FailWithMessage("获取数据失败", "获取数据失败:"+err.Error())
  624. return
  625. }
  626. if item.ApproveId == sysUserId {
  627. item.IsApprove = true
  628. }
  629. // 审批已通过且在出差日期之前时, 审批人/申请人均可关闭
  630. nowDate, _ := time.ParseInLocation(utils.FormatDate, time.Now().Format(utils.FormatDate), time.Local)
  631. arriveDate, _ := time.ParseInLocation(utils.FormatDate, item.ArriveDate, time.Local)
  632. if item.Status == "已通过" && arriveDate.After(nowDate) && (sysUserId == item.ApproveId || sysUserId == item.ApplyAdminId) {
  633. item.IsClose = true
  634. }
  635. this.OkDetailed(item, "获取成功")
  636. }
  637. // @Title 关闭接口
  638. // @Description 关闭接口
  639. // @Param request body business_trip.BusinessApplyCloseReq true "type json string"
  640. // @Success Ret=200 保存成功
  641. // @router /apply/close [post]
  642. func (this *BusinessTrip) Close() {
  643. sysUser := this.AdminWx
  644. sysUserId := sysUser.AdminId
  645. var req business_trip.BusinessApplyCloseReq
  646. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  647. if err != nil {
  648. this.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error())
  649. return
  650. }
  651. if req.BusinessApplyId <= 0 {
  652. this.FailWithMessage("参数解析异常!", "参数错误!BusinessApplyId:"+strconv.Itoa(req.BusinessApplyId))
  653. return
  654. }
  655. businessApplyItem, err := business_trip.GetBusinessApplyById(req.BusinessApplyId)
  656. if err != nil {
  657. if err.Error() == utils.ErrNoRow() {
  658. this.FailWithMessage("出差申请已被删除,请刷新页面!", "出差申请已被删除,请刷新页面:"+strconv.Itoa(req.BusinessApplyId))
  659. return
  660. }
  661. this.FailWithMessage("获取数据失败!", "获取数据失败!GetBusinessApplyById:"+err.Error())
  662. return
  663. }
  664. if sysUser.AdminId != businessApplyItem.ApplyAdminId && sysUser.AdminId != businessApplyItem.ApproveId {
  665. this.FailWithMessage("无权操作", "无权操作")
  666. return
  667. }
  668. // 判断是申请人还是审批人操作的关闭
  669. isApply := false
  670. if sysUser.AdminId == businessApplyItem.ApplyAdminId {
  671. isApply = true
  672. }
  673. //fmt.Println(req.BusinessApplyId)
  674. //fmt.Println(req.CloseReason)
  675. whereParams := make(map[string]interface{})
  676. updateParams := make(map[string]interface{})
  677. whereParams["business_apply_id"] = req.BusinessApplyId
  678. updateParams["status"] = "已关闭"
  679. updateParams["close_reason"] = req.CloseReason
  680. updateParams["modify_time"] = time.Now()
  681. updateParams["close_time"] = time.Now()
  682. err = business_trip.UpdateBusinessApply(whereParams, updateParams)
  683. if err != nil {
  684. this.FailWithMessage("关闭失败!", "关闭失败!UpdateBusinessApply:"+err.Error())
  685. return
  686. }
  687. peerWhereParams := make(map[string]interface{})
  688. peerUpdateParams := make(map[string]interface{})
  689. peerWhereParams["business_apply_id"] = req.BusinessApplyId
  690. peerUpdateParams["status"] = "已关闭"
  691. peerUpdateParams["modify_time"] = time.Now()
  692. err = business_trip.UpdateBusinessApplyPeer(peerWhereParams, peerUpdateParams)
  693. if err != nil {
  694. this.FailWithMessage("关闭失败!", "关闭失败!UpdateBusinessApplyPeer:"+err.Error())
  695. return
  696. }
  697. // 审批人关闭时, 发送消息给申请人
  698. // 若为申请人自己关闭, 则消息发送给审批人
  699. receiveUserId := businessApplyItem.ApplyAdminId
  700. if isApply {
  701. receiveUserId = businessApplyItem.ApproveId
  702. }
  703. {
  704. //系统消息
  705. sourceType := 10
  706. content := businessApplyItem.ApplyRealName + " " + businessApplyItem.Province + businessApplyItem.City + businessApplyItem.Reason + "出差申请已关闭"
  707. go services.AddCompanyApprovalMessage(sysUserId, receiveUserId, 0, businessApplyItem.BusinessApplyId, 1, sourceType, 1, "", content, content, "", "")
  708. }
  709. //模板消息
  710. {
  711. status := "已关闭"
  712. receiver, _ := admin.GetAdminById(receiveUserId)
  713. //applyItem, _ := admin.GetAdminById(businessApplyItem.ApplyAdminId)
  714. wxAppPath := "pages-approve/businessTrip/detail?id=" + strconv.Itoa(req.BusinessApplyId)
  715. first := "您的出差申请已被关闭"
  716. if isApply {
  717. first = fmt.Sprintf("%s的出差申请已关闭", businessApplyItem.ApplyRealName)
  718. }
  719. keyword1 := businessApplyItem.Reason + "出差申请" + status
  720. keyword2 := status
  721. var remark string
  722. go services.SendWxMsgWithRoadshowDeleteNotice(first, keyword1, keyword2, remark, wxAppPath, receiver.Mobile)
  723. }
  724. this.OkWithMessage("关闭成功")
  725. }