business_apply.go 27 KB

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