business_apply.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. "time"
  14. )
  15. // 出差
  16. type BusinessTrip struct {
  17. controllers.BaseAuth
  18. }
  19. // @Title 出差申请接口
  20. // @Description 出差申请接口
  21. // @Param request body business_trip.BusinessApplyReq true "type json string"
  22. // @Success Ret=200 保存成功
  23. // @router /apply/add [post]
  24. func (this *BusinessTrip) ApplyAdd() {
  25. sysUser := this.AdminWx
  26. sysUserId := sysUser.AdminId
  27. var req business_trip.BusinessApplyReq
  28. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  29. if err != nil {
  30. this.FailWithMessage("解析参数失败", "解析参数失败,Err:"+err.Error())
  31. return
  32. }
  33. if req.ArriveDate == "" {
  34. this.FailWithMessage("请填写到达日期", "请填写到达日期")
  35. return
  36. }
  37. if req.ReturnDate == "" {
  38. this.FailWithMessage("请填写返程日期", "请填写返程日期")
  39. return
  40. }
  41. if req.Province == "" {
  42. this.FailWithMessage("请填写目的地省", "请填写目的地省")
  43. return
  44. }
  45. if req.City == "" {
  46. this.FailWithMessage("请填写目的地市", "请填写目的地市")
  47. return
  48. }
  49. if req.Reason == "" {
  50. this.FailWithMessage("请填写出差事由", "请填写出差事由")
  51. return
  52. }
  53. if req.Transportation == "" {
  54. this.FailWithMessage("请填写交通工具", "请填写交通工具")
  55. return
  56. }
  57. startP, _ := time.ParseInLocation(utils.FormatDate, req.ArriveDate, time.Local)
  58. endP, _ := time.ParseInLocation(utils.FormatDate, req.ReturnDate, time.Local)
  59. nowDate, _ := time.ParseInLocation(utils.FormatDate, time.Now().Format(utils.FormatDate), time.Local)
  60. if startP.Before(nowDate) {
  61. fmt.Println("出差开始日期不能小于当前日期!")
  62. return
  63. }
  64. if !endP.Equal(nowDate) && endP.Before(nowDate) {
  65. fmt.Println("出差结束时间不能小于当前时间!")
  66. return
  67. }
  68. if !startP.Equal(endP) && (startP.After(endP) || startP.Equal(endP)) {
  69. fmt.Println("出差开始日期应小于结束日期!")
  70. return
  71. }
  72. approveItem, err := admin.GetAdminById(66)
  73. if err != nil {
  74. this.FailWithMessage("获取审批人信息失败", "获取审批人信息失败,Err:"+err.Error())
  75. return
  76. }
  77. item := new(business_trip.BusinessApply)
  78. item.ApplyAdminId = sysUser.AdminId
  79. item.ApplyRealName = sysUser.RealName
  80. item.ArriveDate = req.ArriveDate
  81. item.ReturnDate = req.ReturnDate
  82. item.Province = req.Province
  83. item.City = req.City
  84. item.Reason = req.Reason
  85. item.Transportation = req.Transportation
  86. item.PeerPeopleId = req.PeerPeopleId
  87. item.PeerPeopleName = req.PeerPeopleName
  88. item.Status = "待审批"
  89. item.ApproveId = approveItem.AdminId
  90. item.ApproveName = approveItem.RealName
  91. item.CreateTime = time.Now()
  92. item.ModifyTime = time.Now()
  93. applyId, err := business_trip.AddBusinessApply(item)
  94. if err != nil {
  95. this.FailWithMessage("申请失败", "申请失败,Err:"+err.Error())
  96. return
  97. }
  98. {
  99. //系统消息
  100. sourceType := 10
  101. content := sysUser.RealName + " " + req.Province + req.City + req.Reason + "出差申请"
  102. go services.AddCompanyApprovalMessage(sysUserId, approveItem.AdminId, 0, int(applyId), 1, sourceType, 2, "", content, content, "", "")
  103. }
  104. //模板消息
  105. {
  106. wxAppPath := ""
  107. first := "您好,有新的申请待处理"
  108. keyword1 := sysUser.RealName
  109. keyword2 := sysUser.Mobile
  110. keyword3 := time.Now().Format(utils.FormatDateTime)
  111. var keyword4 string
  112. if req.PeerPeopleId != "" {
  113. keyword4 = req.ArriveDate + "----" + req.ReturnDate + "," + req.Transportation + "前往" + req.Province + req.City + "," + "同行人" + req.PeerPeopleName
  114. } else {
  115. keyword4 = req.ArriveDate + "----" + req.ReturnDate + "," + req.Transportation + "前往" + req.Province + req.City
  116. }
  117. remark := "请尽快完成审批"
  118. go services.SendWxMsgWithRoadshowPending(first, keyword1, keyword2, keyword3, keyword4, remark, wxAppPath, approveItem.Mobile)
  119. }
  120. this.OkWithMessage("保存成功")
  121. }
  122. // @Title 出差重新申请接口
  123. // @Description 出差重新申请接口
  124. // @Param request body business_trip.BusinessApplyEditReq true "type json string"
  125. // @Success Ret=200 保存成功
  126. // @router /apply/edit [post]
  127. func (this *BusinessTrip) ApplyEdit() {
  128. sysUser := this.AdminWx
  129. sysUserId := sysUser.AdminId
  130. var req business_trip.BusinessApplyEditReq
  131. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  132. if err != nil {
  133. this.FailWithMessage("解析参数失败", "解析参数失败,Err:"+err.Error())
  134. return
  135. }
  136. if req.ArriveDate == "" {
  137. this.FailWithMessage("请填写到达日期", "请填写到达日期")
  138. return
  139. }
  140. if req.ReturnDate == "" {
  141. this.FailWithMessage("请填写返程日期", "请填写返程日期")
  142. return
  143. }
  144. if req.Province == "" {
  145. this.FailWithMessage("请填写目的地省", "请填写目的地省")
  146. return
  147. }
  148. if req.City == "" {
  149. this.FailWithMessage("请填写目的地市", "请填写目的地市")
  150. return
  151. }
  152. if req.Reason == "" {
  153. this.FailWithMessage("请填写出差事由", "请填写出差事由")
  154. return
  155. }
  156. if req.Transportation == "" {
  157. this.FailWithMessage("请填写交通工具", "请填写交通工具")
  158. return
  159. }
  160. startP, _ := time.ParseInLocation(utils.FormatDate, req.ArriveDate, time.Local)
  161. endP, _ := time.ParseInLocation(utils.FormatDate, req.ReturnDate, time.Local)
  162. nowDate, _ := time.ParseInLocation(utils.FormatDate, time.Now().Format(utils.FormatDate), time.Local)
  163. if startP.Before(nowDate) {
  164. fmt.Println("出差开始日期不能小于当前日期!")
  165. return
  166. }
  167. if !endP.Equal(nowDate) && endP.Before(nowDate) {
  168. fmt.Println("出差结束时间不能小于当前时间!")
  169. return
  170. }
  171. if !startP.Equal(endP) && (startP.After(endP) || startP.Equal(endP)) {
  172. fmt.Println("出差开始日期应小于结束日期!")
  173. return
  174. }
  175. if req.BusinessApplyId <= 0 {
  176. this.FailWithMessage("参数错误", "参数错误!BusinessApplyId:"+strconv.Itoa(req.BusinessApplyId))
  177. return
  178. }
  179. businessApplyItem, err := business_trip.GetBusinessApplyById(req.BusinessApplyId)
  180. if err != nil {
  181. if err.Error() == utils.ErrNoRow() {
  182. this.FailWithMessage("出差申请已被删除,请刷新页面", "出差申请已被删除,请刷新页面")
  183. return
  184. }
  185. this.FailWithMessage("获取数据失败!", "获取数据失败!GetBusinessApplyById:"+err.Error())
  186. return
  187. }
  188. if businessApplyItem.Status == "已审批" {
  189. this.FailWithMessage("已审批,不可进行重新申请操作!", "已审批,不可进行重新申请操作!")
  190. return
  191. } else if businessApplyItem.Status == "已过期" {
  192. this.FailWithMessage("已过期,不可进行重新申请操作!", "已过期,不可进行重新申请操作!")
  193. return
  194. }
  195. whereParams := make(map[string]interface{})
  196. updateParams := make(map[string]interface{})
  197. whereParams["business_apply_id"] = req.BusinessApplyId
  198. updateParams["arrive_date"] = req.ArriveDate
  199. updateParams["return_date"] = req.ReturnDate
  200. updateParams["province"] = req.Province
  201. updateParams["city"] = req.City
  202. updateParams["reason"] = req.Reason
  203. updateParams["transportation"] = req.Transportation
  204. updateParams["peer_people_id"] = req.PeerPeopleId
  205. updateParams["peer_people_name"] = req.PeerPeopleName
  206. updateParams["status"] = "待审批"
  207. updateParams["refuse_reason"] = ""
  208. updateParams["create_time"] = time.Now()
  209. updateParams["modify_time"] = time.Now()
  210. err = business_trip.UpdateBusinessApply(whereParams, updateParams)
  211. if err != nil {
  212. this.FailWithMessage("重新申请失败", "重新申请失败,Err:"+err.Error())
  213. return
  214. }
  215. approveItem, err := admin.GetAdminById(66)
  216. if err != nil {
  217. this.FailWithMessage("获取审批人信息失败!", "获取审批人信息失败,Err:"+err.Error())
  218. return
  219. }
  220. {
  221. //系统消息
  222. sourceType := 10
  223. content := sysUser.RealName + " " + req.Province + req.City + req.Reason + "出差申请"
  224. go services.AddCompanyApprovalMessage(sysUserId, approveItem.AdminId, 0, req.BusinessApplyId, 1, sourceType, 2, "", content, content, "", "")
  225. }
  226. //模板消息
  227. {
  228. wxAppPath := ""
  229. first := "您好,有新的申请待处理"
  230. keyword1 := sysUser.RealName
  231. keyword2 := sysUser.Mobile
  232. keyword3 := time.Now().Format(utils.FormatDateTime)
  233. var keyword4 string
  234. if req.PeerPeopleId != "" {
  235. keyword4 = req.ArriveDate + "----" + req.ReturnDate + "," + req.Transportation + "前往" + req.Province + req.City + "," + "同行人" + req.PeerPeopleName
  236. } else {
  237. keyword4 = req.ArriveDate + "----" + req.ReturnDate + "," + req.Transportation + "前往" + req.Province + req.City
  238. }
  239. remark := "请尽快完成审批"
  240. go services.SendWxMsgWithRoadshowPending(first, keyword1, keyword2, keyword3, keyword4, remark, wxAppPath, approveItem.Mobile)
  241. }
  242. this.OkWithMessage("申请成功")
  243. }
  244. // @Title 出差申请列表
  245. // @Description 出差申请列表接口
  246. // @Param PageSize query int true "每页数据条数"
  247. // @Param CurrentIndex query int true "当前页页码,从1开始"
  248. // @Param Reason query string true "出差事由"
  249. // @Param Status query string true "状态"
  250. // @Success 200 {object} business_trip.BusinessApplyListResp
  251. // @router /apply/list [get]
  252. func (this *BusinessTrip) ApplyList() {
  253. sysUser := this.AdminWx
  254. sysUserId := sysUser.AdminId
  255. pageSize, _ := this.GetInt("PageSize")
  256. currentIndex, _ := this.GetInt("CurrentIndex")
  257. var total int
  258. page := paging.GetPaging(currentIndex, pageSize, total)
  259. var startSize int
  260. if pageSize <= 0 {
  261. pageSize = utils.PageSize10
  262. }
  263. if currentIndex <= 0 {
  264. currentIndex = 1
  265. }
  266. startSize = paging.StartIndex(currentIndex, pageSize)
  267. reason := this.GetString("Reason")
  268. status := this.GetString("Status")
  269. var condition string
  270. var pars []interface{}
  271. condition += ` AND apply_admin_id=? `
  272. pars = append(pars, sysUserId)
  273. if reason != "" {
  274. condition += ` AND reason=? `
  275. pars = append(pars, reason)
  276. }
  277. if status != "" {
  278. condition += ` AND status=? `
  279. pars = append(pars, status)
  280. }
  281. resp := new(business_trip.BusinessApplyListResp)
  282. total, err := business_trip.GetBusinessApplyListCount(condition, pars)
  283. if err != nil && err.Error() != utils.ErrNoRow() {
  284. this.FailWithMessage("获取信息失败!", "获取数据总数失败,GetCalendarListCount,Err:"+err.Error())
  285. return
  286. }
  287. dataList, err := business_trip.GetBusinessApplyList(condition, pars, startSize, pageSize)
  288. if err != nil {
  289. this.FailWithMessage("获取信息失败!", "获取数据总数失败,GetBusinessApplyList,Err:"+err.Error())
  290. return
  291. }
  292. page = paging.GetPaging(currentIndex, pageSize, total)
  293. resp.Paging = page
  294. resp.List = dataList
  295. this.OkDetailed(resp, "获取成功")
  296. }
  297. // @Title 删除出差申请接口
  298. // @Description 删除出差申请接口
  299. // @Param request body business_trip.BusinessApplyDeleteReq true "type json string"
  300. // @Success Ret=200 删除成功
  301. // @router /apply/delete [post]
  302. func (this *BusinessTrip) Delete() {
  303. //sysUser := this.AdminWx
  304. //sysUserId := sysUser.AdminId
  305. var req business_trip.BusinessApplyDeleteReq
  306. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  307. if err != nil {
  308. this.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error())
  309. return
  310. }
  311. if req.BusinessApplyId <= 0 {
  312. this.FailWithMessage("参数错误!", "参数错误!BusinessApplyId:"+strconv.Itoa(req.BusinessApplyId))
  313. return
  314. }
  315. businessApplyItem, err := business_trip.GetBusinessApplyById(req.BusinessApplyId)
  316. if err != nil {
  317. if err.Error() == utils.ErrNoRow() {
  318. this.FailWithMessage("出差申请信息已被删除,请刷新页面!", "出差申请信息已被删除,请刷新页面!")
  319. return
  320. }
  321. this.FailWithMessage("获取数据失败!", "获取数据失败!GetBusinessApplyById:"+err.Error())
  322. return
  323. }
  324. err = business_trip.DeleteBusinessApply(businessApplyItem.BusinessApplyId)
  325. if err != nil {
  326. this.FailWithMessage("删除失败!", "删除失败!DeleteBusinessApply:"+err.Error())
  327. return
  328. }
  329. //删除系统消息
  330. go company_approval_message.DeleteCompanyApprovalMessage(req.BusinessApplyId, 10)
  331. this.OkWithMessage("删除成功")
  332. }
  333. // @Title 撤回出差申请接口
  334. // @Description 撤回出差申请接口
  335. // @Param request body business_trip.BusinessApplyBackReq true "type json string"
  336. // @Success Ret=200 保存成功
  337. // @router /apply/back [post]
  338. func (this *BusinessTrip) Back() {
  339. //sysUser := this.AdminWx
  340. //sysUserId := sysUser.AdminId
  341. var req business_trip.BusinessApplyBackReq
  342. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  343. if err != nil {
  344. this.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error())
  345. return
  346. }
  347. if req.BusinessApplyId <= 0 {
  348. this.FailWithMessage("参数错误!", "参数错误!BusinessApplyId:"+strconv.Itoa(req.BusinessApplyId))
  349. return
  350. }
  351. businessApplyItem, err := business_trip.GetBusinessApplyById(req.BusinessApplyId)
  352. if err != nil {
  353. if err.Error() == utils.ErrNoRow() {
  354. this.FailWithMessage("出差申请已被删除,请刷新页面", "出差申请已被删除,请刷新页面")
  355. return
  356. }
  357. this.FailWithMessage("获取数据失败!", "获取数据失败!GetBusinessApplyById:"+err.Error())
  358. return
  359. }
  360. if businessApplyItem.Status == "已审批" {
  361. this.FailWithMessage("已审批,不可进行撤回操作!", "已审批,不可进行撤回操作!")
  362. return
  363. } else if businessApplyItem.Status == "已驳回" {
  364. this.FailWithMessage("已驳回,不可进行撤回操作!", "已驳回,不可进行撤回操作!")
  365. return
  366. } else if businessApplyItem.Status == "已撤回" {
  367. this.FailWithMessage("已撤回,不可重复操作!", "已撤回,不可重复操作!")
  368. return
  369. } else if businessApplyItem.Status == "已过期" {
  370. this.FailWithMessage("已过期,不可进行重复操作!", "已过期,不可进行重复操作!")
  371. return
  372. }
  373. whereParams := make(map[string]interface{})
  374. updateParams := make(map[string]interface{})
  375. whereParams["business_apply_id"] = req.BusinessApplyId
  376. updateParams["status"] = "已撤回"
  377. updateParams["modify_time"] = time.Now()
  378. err = business_trip.UpdateBusinessApply(whereParams, updateParams)
  379. if err != nil {
  380. this.FailWithMessage("撤回失败", "撤回失败!UpdateBusinessApply:"+err.Error())
  381. return
  382. }
  383. //删除系统消息
  384. go company_approval_message.DeleteCompanyApprovalMessage(req.BusinessApplyId, 10)
  385. this.OkWithMessage("撤回成功")
  386. }
  387. // @Title 出差申请详情
  388. // @Description 出差申请详情接口
  389. // @Param BusinessApplyId query int true "出差申请id"
  390. // @Success 200 {object} business_trip.BusinessApplyView
  391. // @router /apply/detail [get]
  392. func (this *BusinessTrip) ApplyDetail() {
  393. sysUser := this.AdminWx
  394. sysUserId := sysUser.AdminId
  395. businessApplyId, _ := this.GetInt("BusinessApplyId")
  396. if businessApplyId <= 0 {
  397. this.FailWithMessage("参数错误", "出差申请id错误:"+strconv.Itoa(businessApplyId))
  398. return
  399. }
  400. item, err := business_trip.GetBusinessApplyById(businessApplyId)
  401. if err != nil {
  402. if err.Error() == utils.ErrNoRow() {
  403. this.FailWithMessage("该出差申请已被删除", "该出差申请已被删除")
  404. return
  405. }
  406. this.FailWithMessage("获取数据失败", "获取数据失败:"+err.Error())
  407. return
  408. }
  409. if item.ApproveId == sysUserId {
  410. item.IsApply = true
  411. }
  412. this.OkDetailed(item, "获取成功")
  413. }