business_apply.go 28 KB

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