business_apply.go 28 KB

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