worksite.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. package work
  2. import (
  3. "time"
  4. "zhiyuan/models"
  5. "zhiyuan/pkg/app"
  6. "zhiyuan/pkg/db"
  7. "zhiyuan/pkg/utils"
  8. "zhiyuan/services/form"
  9. "zhiyuan/services/work/worksite"
  10. "github.com/gin-gonic/gin"
  11. )
  12. func WorkSiteList(c *gin.Context) {
  13. page := app.HandlePageNums(c)
  14. where := map[string]string{
  15. "where": "`zy_work_site`.`id`>0 AND `zy_work_site`.`deleted_at`=0",
  16. "_group_by": "`zy_work_site`.`id`",
  17. "_order_by": "`zy_work_site`.`id` desc",
  18. }
  19. if page.PageSize != 0 {
  20. where["_page_size"] = utils.ToStr(page.PageSize)
  21. where["_page_num"] = utils.ToStr(page.PageNum)
  22. }
  23. param := make(map[string]interface{})
  24. username := c.Query("username")
  25. if username != "" {
  26. where["where"] = where["where"] + " AND `zy_work_site`.`username` LIKE {{username}}"
  27. param["username"] = "%" + username + "%"
  28. }
  29. phone := c.Query("phone")
  30. if phone != "" {
  31. where["where"] = where["where"] + " AND `zy_work_site`.`phone` LIKE {{phone}}"
  32. param["phone"] = "%" + phone + "%"
  33. }
  34. village := c.Query("village")
  35. if village != "" {
  36. where["where"] = where["where"] + " AND `zy_work_site`.`village` LIKE {{village}}"
  37. param["village"] = "%" + village + "%"
  38. }
  39. address := c.Query("address")
  40. if address != "" {
  41. where["where"] = where["where"] + " AND `zy_work_site`.`address` LIKE {{address}}"
  42. param["address"] = "%" + address + "%"
  43. }
  44. room_no := c.Query("room_no")
  45. if room_no != "" {
  46. where["where"] = where["where"] + " AND `zy_work_site`.`room_no` LIKE {{room_no}}"
  47. param["room_no"] = "%" + room_no + "%"
  48. }
  49. area := c.Query("area")
  50. if area != "" {
  51. where["where"] = where["where"] + " AND `zy_work_site`.`area` LIKE {{area}}"
  52. param["area"] = "%" + area + "%"
  53. }
  54. search := c.Query("search")
  55. if search != "" {
  56. where["where"] = where["where"] + " AND (`zy_work_site`.`username` LIKE {{search}} OR `zy_work_site`.`phone` LIKE {{search}} OR `zy_work_site`.`village` LIKE {{search}} OR `zy_work_site`.`address` LIKE {{search}} OR `zy_work_site`.`room_no` LIKE {{search}} OR `zy_work_site`.`area` LIKE {{search}})"
  57. param["search"] = "%" + search + "%"
  58. }
  59. state := utils.ToInt(c.Query("state"))
  60. if state != 0 {
  61. where["where"] = where["where"] + " AND `zy_work_site`.`state`={{state}}"
  62. param["state"] = state - 1
  63. }
  64. if utils.ToInt(c.Query("my")) != 0 {
  65. where["where"] = where["where"] + " AND (`zy_work_site`.`manager_id`={{adminID}} OR `zy_work_site`.`designer_id`={{adminID}} OR `zy_work_site`.`salesman_id`={{adminID}} OR `zy_work_site`.`project_manager_id`={{adminID}} OR `zy_work_site`.`project_leader_id`={{adminID}} OR `zy_work_site`.`servicer_id`={{adminID}} OR `zy_work_site`.`quality_id`={{adminID}} OR `zy_work_site`.`assistant_id`={{adminID}} OR `zy_work_site`.`created_id`={{adminID}} OR FIND_IN_SET({{adminID}}, `zy_work_site`.`admin_ids`))"
  66. param["adminID"] = c.GetInt("adminID")
  67. }
  68. total, err := worksite.CountRaw(where["where"], param)
  69. if err != nil {
  70. app.Error(c, err.Error())
  71. return
  72. }
  73. type WorksiteList struct {
  74. ID int `json:"id"`
  75. PkgId int `json:"pkg_id"`
  76. Username string `json:"username"`
  77. Phone string `json:"phone"`
  78. Village string `json:"village"`
  79. Address string `json:"address"`
  80. RoomNo string `json:"room_no"`
  81. Area string `json:"area"`
  82. Longitude string `json:"longitude"`
  83. Latitude string `json:"latitude"`
  84. ManagerId int `json:"manager_id"`
  85. DesignerId int `json:"designer_id"`
  86. CreatedId int `json:"created_id"`
  87. AdminIds string `json:"admin_ids"`
  88. StartTime int `json:"starttime"`
  89. EndTime int `json:"endtime"`
  90. StopTime int `json:"stoptime"`
  91. State int `json:"state"`
  92. CreatedAt int `json:"created_at"`
  93. UpdatedAt int `json:"updated_at"`
  94. ManagerName string `json:"manager_name"`
  95. ManagerPhone string `json:"manager_phone"`
  96. DesignerName string `json:"designer_name"`
  97. DesignerPhone string `json:"designer_phone"`
  98. CreatedName string `json:"created_name"`
  99. AdminNames string `json:"admin_names"`
  100. }
  101. worksiteList := make([]WorksiteList, 0)
  102. if err = worksite.GetWorkSitesRaw(where, param, &worksiteList); err != nil {
  103. app.Error(c, err.Error())
  104. return
  105. }
  106. data := gin.H{
  107. "list": worksiteList,
  108. "total": total,
  109. "limit": page.PageSize,
  110. }
  111. app.Success(c, data)
  112. }
  113. func WorkSiteInfo(c *gin.Context) {
  114. id := utils.StrTo(c.Param("id")).MustInt()
  115. if id <= 0 {
  116. app.ErrorMsg(c, "department id must be a number", nil)
  117. return
  118. }
  119. type WorksiteInfo struct {
  120. ID int `json:"id"`
  121. PkgId int `json:"pkg_id"`
  122. Username string `json:"username"`
  123. Phone string `json:"phone"`
  124. Village string `json:"village"`
  125. Address string `json:"address"`
  126. RoomNo string `json:"room_no"`
  127. Area string `json:"area"`
  128. Longitude string `json:"longitude"`
  129. Latitude string `json:"latitude"`
  130. ManagerId int `json:"manager_id"`
  131. DesignerId int `json:"designer_id"`
  132. CreatedId int `json:"created_id"`
  133. AdminIds string `json:"admin_ids"`
  134. StartTime int `json:"starttime"`
  135. EndTime int `json:"endtime"`
  136. StopTime int `json:"stoptime"`
  137. State int `json:"state"`
  138. CreatedAt int `json:"created_at"`
  139. UpdatedAt int `json:"updated_at"`
  140. ManagerName string `json:"manager_name"`
  141. ManagerPhone string `json:"manager_phone"`
  142. DesignerName string `json:"designer_name"`
  143. DesignerPhone string `json:"designer_phone"`
  144. CreatedName string `json:"created_name"`
  145. AdminNames string `json:"admin_names"`
  146. Discount string `json:"discount"`
  147. Promotion string `json:"promotion"`
  148. Gifts string `json:"gifts"`
  149. Other string `json:"other"`
  150. Hose string `json:"hose"`
  151. Bear string `json:"bear"`
  152. }
  153. var worksiteInfo WorksiteInfo
  154. err := worksite.GetWorkSiteByID(id, &worksiteInfo)
  155. if err != nil {
  156. app.Error(c, err.Error())
  157. return
  158. }
  159. app.Success(c, worksiteInfo)
  160. }
  161. func WorkSiteAdd(c *gin.Context) {
  162. var addForm form.WorkSiteAdd
  163. if app.Bind(c, &addForm) != nil {
  164. return
  165. }
  166. id, err := worksite.Add(addForm, c.GetInt("adminID"))
  167. if err != nil {
  168. app.Error(c, err.Error())
  169. return
  170. }
  171. app.Success(c, gin.H{"id": id})
  172. }
  173. func WorkSiteEdit(c *gin.Context) {
  174. id := utils.ToInt(c.Param("id"))
  175. if id <= 0 {
  176. app.ErrorMsg(c, "id must be a number", nil)
  177. return
  178. }
  179. var addForm form.WorkSiteEdit
  180. if app.Bind(c, &addForm) != nil {
  181. return
  182. }
  183. err := worksite.EditByID(addForm, id)
  184. if err != nil {
  185. app.ErrorMsg(c, err.Error(), nil)
  186. return
  187. }
  188. app.Success(c, nil)
  189. }
  190. func WorkSiteDispatch(c *gin.Context) {
  191. id := utils.ToInt(c.Param("id"))
  192. if id <= 0 {
  193. app.ErrorMsg(c, "id must be a number", nil)
  194. return
  195. }
  196. var adminForm form.WorkSiteAdminID
  197. if app.Bind(c, &adminForm) != nil {
  198. return
  199. }
  200. err := worksite.DispatchByID(adminForm, id)
  201. if err != nil {
  202. app.ErrorMsg(c, err.Error(), nil)
  203. return
  204. }
  205. app.Success(c, nil)
  206. }
  207. func WorkSiteStart(c *gin.Context) {
  208. id := utils.ToInt(c.Param("id"))
  209. if id <= 0 {
  210. app.ErrorMsg(c, "id must be a number", nil)
  211. return
  212. }
  213. err := worksite.StartByID(id, c.GetInt("adminID"))
  214. if err != nil {
  215. app.ErrorMsg(c, err.Error(), nil)
  216. return
  217. }
  218. app.Success(c, nil)
  219. }
  220. func WorkSiteEditTime(c *gin.Context) {
  221. id := utils.ToInt(c.Param("id"))
  222. if id <= 0 {
  223. app.ErrorMsg(c, "id must be a number", nil)
  224. return
  225. }
  226. var timeForm form.WorkSiteTime
  227. if app.Bind(c, &timeForm) != nil {
  228. return
  229. }
  230. err := worksite.EditTime(timeForm, id, c.GetInt("adminID"))
  231. if err != nil {
  232. app.ErrorMsg(c, err.Error(), nil)
  233. return
  234. }
  235. app.Success(c, nil)
  236. }
  237. func WorkSiteDel(c *gin.Context) {
  238. id := utils.ToInt(c.Param("id"))
  239. if id <= 0 {
  240. app.ErrorMsg(c, "id must be a number", nil)
  241. return
  242. }
  243. err := worksite.DeleteByID(id)
  244. if err != nil {
  245. app.Error(c, err.Error())
  246. return
  247. }
  248. app.Success(c, nil)
  249. }
  250. type WorkSiteReturnVisitModel struct {
  251. ID int `json:"id"`
  252. SiteId int `json:"site_id"`
  253. Cycle int `json:"cycle"`
  254. PlanTime int `json:"plan_time"`
  255. ReturnVisitID int `json:"return_visit_id"`
  256. ReturnVisitContent string `json:"return_visit_content"`
  257. ReturnVisitAdminId int `json:"return_visit_admin_id"`
  258. ReturnVisitTime int `json:"return_visit_time"`
  259. ReturnVisitName string `json:"return_visit_name" prop:"select:admin.username"`
  260. ReturnVisitPhone string `json:"return_visit_phone" prop:"select:admin.phone"`
  261. ReturnVisitHeadimgurl string `json:"return_visit_headimgurl" prop:"select:admin.headimgurl"`
  262. ReturnVisitFrom map[int]form.ReturnVisitItem `json:"return_visit_from" prop:"select:false"`
  263. ReturnVisit *models.WorkReturnVisit `json:"return_visit" prop:"select:false"`
  264. models.WorkSiteReturnVisit
  265. }
  266. func (model WorkSiteReturnVisitModel) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel {
  267. return []db.JoinModel{
  268. {
  269. Model: models.Admin{},
  270. As: "admin",
  271. On: []string{
  272. "`admin`.`id` = " + model.TableName() + ".`return_visit_admin_id`",
  273. },
  274. },
  275. }
  276. }
  277. func WorkSiteReturnVisitInfo(c *gin.Context) {
  278. id := utils.StrTo(c.Param("id")).MustInt()
  279. if id <= 0 {
  280. app.ErrorMsg(c, "id must be a number", nil)
  281. return
  282. }
  283. var workSiteReturnVisitInfo *WorkSiteReturnVisitModel
  284. db.GetModel(map[string]interface{}{
  285. "id": id,
  286. }, &workSiteReturnVisitInfo)
  287. if workSiteReturnVisitInfo == nil {
  288. app.ErrorMsg(c, "id must be a number", nil)
  289. return
  290. }
  291. if workSiteReturnVisitInfo.ReturnVisitID != 0 {
  292. workSiteReturnVisitInfo.ReturnVisit = models.GetWorkReturnVisitModel(int64(workSiteReturnVisitInfo.ReturnVisitID), false)
  293. }
  294. utils.JsonDecode(workSiteReturnVisitInfo.ReturnVisitContent).To(&workSiteReturnVisitInfo.ReturnVisitFrom)
  295. app.Success(c, workSiteReturnVisitInfo)
  296. }
  297. func WorkSiteReturnVisit(c *gin.Context) {
  298. id := utils.ToInt(c.Param("id"))
  299. if id <= 0 {
  300. app.ErrorMsg(c, "id must be a number", nil)
  301. return
  302. }
  303. var returnForm form.ReturnVisitFrom
  304. if app.Bind(c, &returnForm) != nil {
  305. return
  306. }
  307. var workSiteReturnVisit *models.WorkSiteReturnVisit
  308. db.GetModel(map[string]interface{}{
  309. "id": id,
  310. }, &workSiteReturnVisit)
  311. if workSiteReturnVisit == nil {
  312. app.ErrorMsg(c, "invalid id", nil)
  313. return
  314. }
  315. if workSiteReturnVisit.ReturnVisitID == 0 {
  316. app.ErrorMsg(c, "无需回访", nil)
  317. return
  318. }
  319. data := map[string]interface{}{
  320. "return_visit_content": utils.JsonEncode(returnForm.ReturnVisit),
  321. }
  322. if workSiteReturnVisit.ReturnVisitContent == "" {
  323. data["return_visit_admin_id"] = c.GetInt("adminID")
  324. data["return_visit_time"] = time.Now().Unix()
  325. }
  326. err := db.UpdateModel(db.Type(workSiteReturnVisit), int64(id), data)
  327. if err != nil {
  328. app.ErrorMsg(c, err.Error(), nil)
  329. return
  330. }
  331. app.Success(c, nil)
  332. }