order.go 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  1. package aftersale
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "zhiyuan/pkg/app"
  7. "zhiyuan/pkg/config"
  8. "zhiyuan/pkg/db"
  9. "zhiyuan/pkg/param"
  10. orderParam "zhiyuan/pkg/param/order"
  11. "zhiyuan/pkg/utils"
  12. "zhiyuan/services/admin"
  13. "zhiyuan/services/aftersale"
  14. "zhiyuan/services/aftersale/order"
  15. "zhiyuan/services/form"
  16. "zhiyuan/services/material/brand"
  17. "zhiyuan/services/region"
  18. "zhiyuan/services/user"
  19. "github.com/gin-gonic/gin"
  20. "github.com/tealeg/xlsx/v3"
  21. )
  22. func OrderAdd(c *gin.Context) {
  23. var form form.OrderAdd
  24. if app.Bind(c, &form) != nil {
  25. return
  26. }
  27. form.Typ = 2
  28. id, err := order.Add(form)
  29. if err != nil {
  30. app.Error(c, err.Error())
  31. return
  32. }
  33. app.Success(c, gin.H{"id": id})
  34. }
  35. func OrderEventList(c *gin.Context) {
  36. page := app.HandlePageNum(c)
  37. params := make(map[string]interface{})
  38. where := map[string]string{
  39. "where": "o.deleted_at=0 AND e.event_type=1",
  40. "_order_by": "id desc",
  41. }
  42. if page.PageSize != 0 {
  43. where["_page_size"] = utils.ToStr(page.PageSize)
  44. where["_page_num"] = utils.ToStr(page.PageNum)
  45. }
  46. // state
  47. state := utils.ToInt(c.Query("state"))
  48. if state != 0 {
  49. where["where"] += " AND e.order_state={{state}}"
  50. params["state"] = state
  51. }
  52. // created_date
  53. createdDate := c.QueryArray("created_date[]")
  54. if createdDate != nil && len(createdDate) == 2 {
  55. where["where"] += " AND e.created_at BETWEEN {{created_at_min}} AND {{created_at_max}}"
  56. params["created_at_min"] = utils.DateParseUnix(createdDate[0], "Y-m-d")
  57. params["created_at_max"] = utils.DateParseUnix(createdDate[1], "Y-m-d") + 86399
  58. }
  59. // site_id
  60. if adminInfo := admin.GetAdminCache(c.GetInt("adminID")); adminInfo.SiteID > 0 {
  61. where["where"] += " AND (site_id={{site_id}} or site_id=0)"
  62. params["site_id"] = adminInfo.SiteID
  63. }
  64. total, err := order.GetCountJoinOrder(where["where"], params)
  65. if err != nil {
  66. app.Error(c, err.Error())
  67. return
  68. }
  69. type Auth struct {
  70. Revoke bool `json:"revoke"`
  71. Check bool `json:"check"`
  72. Allot bool `json:"allot"`
  73. ReAllot bool `json:"re_allot"`
  74. }
  75. type OrderList struct {
  76. ID int `json:"id"`
  77. OrderNo string `json:"order_no"`
  78. LinkName string `json:"link_name"`
  79. LinkPhone string `json:"link_phone"`
  80. Content string `json:"content"`
  81. State int `json:"state"`
  82. StateName string `json:"state_name"`
  83. StateColor string `json:"state_color"`
  84. Title string `json:"title"`
  85. InWarranty string `json:"in_warranty"`
  86. Auth Auth `json:"auth"`
  87. Leader int `json:"leader"`
  88. CreatedAt string `json:"created_at"`
  89. }
  90. orderList := make([]OrderList, 0)
  91. err = order.GetListJoinOrder(where, params, &orderList)
  92. if err != nil {
  93. app.Error(c, err.Error())
  94. return
  95. }
  96. orderStateList := utils.ParseSliceMap(orderParam.Params.State, "id")
  97. for k, v := range orderList {
  98. v.StateName = utils.ToStr(orderStateList[utils.ToStr(v.State)]["name"])
  99. v.StateColor = utils.ToStr(orderStateList[utils.ToStr(v.State)]["color"])
  100. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
  101. v.Auth = Auth{
  102. Check: utils.IsContain(orderParam.Allow.Check, v.State),
  103. Revoke: utils.IsContain(orderParam.Allow.Revoke, v.State),
  104. ReAllot: utils.IsContain(orderParam.Allow.ReAllot, v.State),
  105. Allot: utils.IsContain(orderParam.Allow.Allot, v.State),
  106. }
  107. if v.InWarranty == "0" {
  108. v.InWarranty = "否"
  109. } else {
  110. v.InWarranty = "是"
  111. }
  112. orderList[k] = v
  113. }
  114. data := gin.H{
  115. "list": orderList,
  116. "total": total,
  117. "limit": page.PageSize,
  118. }
  119. app.Success(c, data)
  120. }
  121. func OrderList(c *gin.Context) {
  122. page := app.HandlePageNum(c)
  123. params := make(map[string]interface{})
  124. where := map[string]string{
  125. "where": "deleted_at=0",
  126. "_order_by": "id desc",
  127. }
  128. if page.PageSize != 0 {
  129. where["_page_size"] = utils.ToStr(page.PageSize)
  130. where["_page_num"] = utils.ToStr(page.PageNum)
  131. }
  132. // leader
  133. leaderID := utils.ToInt(c.Query("leader_id"))
  134. if leaderID > 0 {
  135. where["where"] += " AND leader={{leader_id}}"
  136. params["leader_id"] = leaderID
  137. }
  138. // state
  139. state := utils.ToInt(c.Query("state"))
  140. if state != 0 {
  141. where["where"] += " AND if(state=90,90,if(is_force=0,state,100))={{state}}"
  142. params["state"] = state
  143. }
  144. // is_force
  145. isForce := utils.ToInt(c.Query("is_force"))
  146. if isForce != 0 {
  147. if isForce == 2 {
  148. where["where"] += " AND is_force!=0"
  149. } else {
  150. where["where"] += " AND is_force={{isForce}}"
  151. params["isForce"] = isForce - 1
  152. }
  153. }
  154. // type
  155. typ := utils.ToInt(c.Query("type"))
  156. if typ != 0 {
  157. where["where"] += " AND type={{type}}"
  158. params["type"] = typ - 1
  159. }
  160. // address
  161. address := c.Query("address")
  162. if address != "" {
  163. where["where"] += " AND address LIKE {{address}}"
  164. params["address"] = "%" + address + "%"
  165. }
  166. // inWarranty
  167. inWarranty := utils.ToInt(c.Query("in_warranty"))
  168. if utils.IsContain([]int{0, 1}, inWarranty) {
  169. where["where"] += " AND in_warranty={{in_warranty}}"
  170. params["in_warranty"] = inWarranty
  171. }
  172. // user
  173. user := c.Query("user")
  174. if user != "" {
  175. where["where"] += " AND (link_name LIKE {{user}} OR link_phone LIKE {{user}})"
  176. params["user"] = "%" + user + "%"
  177. }
  178. // site_id
  179. if adminInfo := admin.GetAdminCache(c.GetInt("adminID")); adminInfo.SiteID > 0 {
  180. where["where"] += " AND (site_id={{site_id}} or site_id=0)"
  181. params["site_id"] = adminInfo.SiteID
  182. }
  183. // state
  184. finish_state := utils.ToInt(c.Query("finish_state"))
  185. if finish_state != 0 {
  186. switch finish_state {
  187. case 1:
  188. where["where"] += " AND (o.finish_state=1)"
  189. break
  190. case 2:
  191. where["where"] += " AND (o.finish_state=2)"
  192. break
  193. case 3:
  194. where["where"] += " AND (o.finish_state=1 AND UNIX_TIMESTAMP()<=o.end_time)"
  195. break
  196. case 4:
  197. where["where"] += " AND (o.finish_state=1 AND UNIX_TIMESTAMP()>o.end_time)"
  198. break
  199. case 5:
  200. where["_order_by"] = "o.incomplete_count desc"
  201. break
  202. }
  203. }
  204. createdDate := c.QueryArray("created_date[]")
  205. if createdDate != nil && len(createdDate) == 2 {
  206. where["where"] += " AND o.created_at BETWEEN {{created_at_min}} AND {{created_at_max}}"
  207. params["created_at_min"] = utils.DateParseUnix(createdDate[0], "Y-m-d")
  208. params["created_at_max"] = utils.DateParseUnix(createdDate[1], "Y-m-d") + 86399
  209. }
  210. total, err := order.GetCountWidthDetail(where["where"], params)
  211. if err != nil {
  212. app.Error(c, err.Error())
  213. return
  214. }
  215. type Auth struct {
  216. Revoke bool `json:"revoke"`
  217. Check bool `json:"check"`
  218. Allot bool `json:"allot"`
  219. ReAllot bool `json:"re_allot"`
  220. ForceComplete bool `json:"force_complete"`
  221. }
  222. // 回访信息
  223. type Visit struct {
  224. OrderID int `json:"order_id"`
  225. Content string `json:"content"`
  226. FinishedAt string `json:"finished_at"`
  227. }
  228. type OrderList struct {
  229. ID int `json:"id"`
  230. OrderNo string `json:"order_no"`
  231. LinkName string `json:"link_name"`
  232. LinkPhone string `json:"link_phone"`
  233. Content string `json:"content"`
  234. State int `json:"state"`
  235. CState int `json:"cstate"`
  236. StateName string `json:"state_name"`
  237. StateColor string `json:"state_color"`
  238. InWarranty int `json:"in_warranty"`
  239. Remarks string `json:"remarks"`
  240. WarrantyStart string `json:"warranty_start"`
  241. WarrantyEnd string `json:"warranty_end"`
  242. WarrantyPeriod string `json:"warranty_period"`
  243. VisitList []*Visit `json:"visit_list"`
  244. Address string `json:"address"`
  245. IsIssue int `json:"is_issue"`
  246. IsForce int `json:"is_force"`
  247. Type int `json:"type"`
  248. Leader int `json:"leader"`
  249. LeaderName string `json:"leader_name"`
  250. Auth Auth `json:"auth"`
  251. CreatedAt string `json:"created_at"`
  252. MaintenanceRemark string `json:"maintenance_remark"`
  253. StartTime string `json:"start_time"`
  254. EndTime string `json:"end_time"`
  255. FinishTime string `json:"finish_time"`
  256. IncompleteCount int `json:"incomplete_count"`
  257. FinishState int `json:"finish_state"`
  258. RepairTimeout int `json:"repair_timeout"`
  259. }
  260. orderList := make([]OrderList, 0)
  261. err = order.GetListWidthDetail(where, params, &orderList)
  262. if err != nil {
  263. app.Error(c, err.Error())
  264. return
  265. }
  266. orderIds := make([]int, 0)
  267. adminIds := make([]int, 0)
  268. for _, v := range orderList {
  269. orderIds = append(orderIds, v.ID)
  270. if v.Leader > 0 {
  271. adminIds = append(adminIds, v.Leader)
  272. }
  273. }
  274. adminMap := make(map[int]string, 0)
  275. if len(adminIds) > 0 {
  276. adminList, err := admin.GetAdmins(map[string]interface{}{"id in": adminIds}, []string{"id, username"}, app.Page{}, nil)
  277. if err == nil {
  278. for _, v := range adminList {
  279. adminMap[v.ID] = v.Username
  280. }
  281. }
  282. }
  283. visitList := make([]*Visit, 0)
  284. visitListMap := make(map[int][]*Visit, 0)
  285. if len(orderIds) > 0 {
  286. if _, err := order.GetVisits(map[string]interface{}{"order_id in": orderIds, "_orderby": "created_at desc", "deleted_at": 0, "state": 2}, nil, &visitList); err == nil {
  287. for _, v := range visitList {
  288. v.FinishedAt = utils.DateS(v.FinishedAt, "YYYY-MM-DD")
  289. visitListMap[v.OrderID] = append(visitListMap[v.OrderID], v)
  290. }
  291. }
  292. }
  293. orderStateList := utils.ParseSliceMap(orderParam.Params.State, "id")
  294. for k, v := range orderList {
  295. v.StateName = utils.ToStr(orderStateList[utils.ToStr(v.CState)]["name"])
  296. v.StateColor = utils.ToStr(orderStateList[utils.ToStr(v.CState)]["color"])
  297. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
  298. v.StartTime = utils.DateS(v.StartTime, "YYYY-MM-DD HH:mm")
  299. v.EndTime = utils.DateS(v.EndTime, "YYYY-MM-DD HH:mm")
  300. v.FinishTime = utils.DateS(v.FinishTime, "YYYY-MM-DD HH:mm")
  301. v.Auth = Auth{
  302. Check: utils.IsContain(orderParam.Allow.Check, v.CState),
  303. Revoke: utils.IsContain(orderParam.Allow.Revoke, v.CState),
  304. Allot: utils.IsContain(orderParam.Allow.Allot, v.CState),
  305. ReAllot: utils.IsContain(orderParam.Allow.ReAllot, v.CState),
  306. ForceComplete: utils.IsContain(orderParam.Allow.ForceComplete, v.CState),
  307. }
  308. if visitListMap[v.ID] == nil {
  309. v.VisitList = make([]*Visit, 0)
  310. } else {
  311. v.VisitList = visitListMap[v.ID]
  312. }
  313. v.LeaderName = adminMap[v.Leader]
  314. if v.WarrantyStart != "0" {
  315. v.WarrantyPeriod = utils.DateS(v.WarrantyStart, "YYYY-MM-DD") + " 至 " + utils.DateS(v.WarrantyEnd, "YYYY-MM-DD")
  316. } else {
  317. v.WarrantyPeriod = ""
  318. }
  319. orderList[k] = v
  320. }
  321. data := gin.H{
  322. "list": orderList,
  323. "total": total,
  324. "limit": page.PageSize,
  325. }
  326. app.Success(c, data)
  327. }
  328. func OrderLists(c *gin.Context) {
  329. page := app.HandlePageNum(c)
  330. params := make(map[string]interface{})
  331. where := map[string]string{
  332. "where": "deleted_at=0",
  333. "_order_by": "id desc",
  334. }
  335. if page.PageSize != 0 {
  336. where["_page_size"] = utils.ToStr(page.PageSize)
  337. where["_page_num"] = utils.ToStr(page.PageNum)
  338. }
  339. // leader
  340. leaderID := utils.ToInt(c.Query("leader_id"))
  341. if leaderID > 0 {
  342. where["where"] += " AND leader={{leader_id}}"
  343. params["leader_id"] = leaderID
  344. }
  345. // state
  346. state := utils.ToInt(c.Query("state"))
  347. if state != 0 {
  348. where["where"] += " AND if(state=90,90,if(is_force=0,state,100))={{state}}"
  349. params["state"] = state
  350. }
  351. // is_force
  352. isForce := utils.ToInt(c.Query("is_force"))
  353. if isForce != 0 {
  354. if isForce == 2 {
  355. where["where"] += " AND is_force!=0"
  356. } else {
  357. where["where"] += " AND is_force={{isForce}}"
  358. params["isForce"] = isForce - 1
  359. }
  360. }
  361. // type
  362. typ := utils.ToInt(c.Query("type"))
  363. if typ != 0 {
  364. where["where"] += " AND type={{type}}"
  365. params["type"] = typ - 1
  366. }
  367. // address
  368. address := c.Query("address")
  369. if address != "" {
  370. where["where"] += " AND address LIKE {{address}}"
  371. params["address"] = "%" + address + "%"
  372. }
  373. // inWarranty
  374. inWarranty := utils.ToInt(c.Query("in_warranty"))
  375. if utils.IsContain([]int{0, 1}, inWarranty) {
  376. where["where"] += " AND in_warranty={{in_warranty}}"
  377. params["in_warranty"] = inWarranty
  378. }
  379. // user
  380. user := c.Query("user")
  381. if user != "" {
  382. where["where"] += " AND (link_name LIKE {{user}} OR link_phone LIKE {{user}})"
  383. params["user"] = "%" + user + "%"
  384. }
  385. createdDate := c.QueryArray("created_date[]")
  386. if createdDate != nil && len(createdDate) == 2 {
  387. where["where"] += " AND o.created_at BETWEEN {{created_at_min}} AND {{created_at_max}}"
  388. params["created_at_min"] = utils.DateParseUnix(createdDate[0], "Y-m-d")
  389. params["created_at_max"] = utils.DateParseUnix(createdDate[1], "Y-m-d") + 86399
  390. }
  391. total, err := order.GetCountWidthDetail(where["where"], params)
  392. if err != nil {
  393. app.Error(c, err.Error())
  394. return
  395. }
  396. type Auth struct {
  397. Revoke bool `json:"revoke"`
  398. Check bool `json:"check"`
  399. Allot bool `json:"allot"`
  400. ReAllot bool `json:"re_allot"`
  401. ForceComplete bool `json:"force_complete"`
  402. }
  403. // 回访信息
  404. type Visit struct {
  405. OrderID int `json:"order_id"`
  406. Content string `json:"content"`
  407. FinishedAt string `json:"finished_at"`
  408. }
  409. type Comment struct {
  410. ID int `json:"id"`
  411. OrderID int `json:"order_id"`
  412. UserID int `json:"user_id"`
  413. Star int `json:"star"`
  414. Content string `json:"content"`
  415. Tags string `json:"tags"`
  416. }
  417. type OrderList struct {
  418. ID int `json:"id"`
  419. OrderNo string `json:"order_no"`
  420. LinkName string `json:"link_name"`
  421. LinkPhone string `json:"link_phone"`
  422. Content string `json:"content"`
  423. State int `json:"state"`
  424. CState int `json:"cstate"`
  425. StateName string `json:"state_name"`
  426. StateColor string `json:"state_color"`
  427. InWarranty int `json:"in_warranty"`
  428. Remarks string `json:"remarks"`
  429. WarrantyStart string `json:"warranty_start"`
  430. WarrantyEnd string `json:"warranty_end"`
  431. WarrantyPeriod string `json:"warranty_period"`
  432. VisitList []*Visit `json:"visit_list"`
  433. Address string `json:"address"`
  434. IsIssue int `json:"is_issue"`
  435. IsForce int `json:"is_force"`
  436. Type int `json:"type"`
  437. Leader int `json:"leader"`
  438. LeaderName string `json:"leader_name"`
  439. Auth Auth `json:"auth"`
  440. CreatedAt string `json:"created_at"`
  441. Comment *Comment `json:"comment"`
  442. }
  443. orderList := make([]OrderList, 0)
  444. err = order.GetListWidthDetail(where, params, &orderList)
  445. if err != nil {
  446. app.Error(c, err.Error())
  447. return
  448. }
  449. orderIds := make([]int, 0)
  450. adminIds := make([]int, 0)
  451. for _, v := range orderList {
  452. orderIds = append(orderIds, v.ID)
  453. if v.Leader > 0 {
  454. adminIds = append(adminIds, v.Leader)
  455. }
  456. }
  457. adminMap := make(map[int]string, 0)
  458. if len(adminIds) > 0 {
  459. adminList, err := admin.GetAdmins(map[string]interface{}{"id in": adminIds}, []string{"id, username"}, app.Page{}, nil)
  460. if err == nil {
  461. for _, v := range adminList {
  462. adminMap[v.ID] = v.Username
  463. }
  464. }
  465. }
  466. visitList := make([]*Visit, 0)
  467. visitListMap := make(map[int][]*Visit, 0)
  468. if len(orderIds) > 0 {
  469. if _, err := order.GetVisits(map[string]interface{}{"order_id in": orderIds, "_orderby": "created_at desc", "deleted_at": 0, "state": 2}, nil, &visitList); err == nil {
  470. for _, v := range visitList {
  471. v.FinishedAt = utils.DateS(v.FinishedAt, "YYYY-MM-DD")
  472. visitListMap[v.OrderID] = append(visitListMap[v.OrderID], v)
  473. }
  474. }
  475. }
  476. commentList := make([]Comment, 0)
  477. commentListMap := make(map[int]Comment, 0)
  478. if len(orderIds) > 0 {
  479. if _, err := order.GetComment(map[string]interface{}{"order_id in": orderIds}, nil, &commentList); err == nil {
  480. for _, v := range commentList {
  481. commentListMap[v.OrderID] = v
  482. }
  483. }
  484. }
  485. orderStateList := utils.ParseSliceMap(orderParam.Params.State, "id")
  486. for k, v := range orderList {
  487. v.StateName = utils.ToStr(orderStateList[utils.ToStr(v.CState)]["name"])
  488. v.StateColor = utils.ToStr(orderStateList[utils.ToStr(v.CState)]["color"])
  489. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
  490. v.Auth = Auth{
  491. Check: utils.IsContain(orderParam.Allow.Check, v.CState),
  492. Revoke: utils.IsContain(orderParam.Allow.Revoke, v.CState),
  493. Allot: utils.IsContain(orderParam.Allow.Allot, v.CState),
  494. ReAllot: utils.IsContain(orderParam.Allow.ReAllot, v.CState),
  495. ForceComplete: utils.IsContain(orderParam.Allow.ForceComplete, v.CState),
  496. }
  497. if visitListMap[v.ID] == nil {
  498. v.VisitList = make([]*Visit, 0)
  499. } else {
  500. v.VisitList = visitListMap[v.ID]
  501. }
  502. if comment, ok := commentListMap[v.ID]; ok {
  503. v.Comment = &comment
  504. }
  505. v.LeaderName = adminMap[v.Leader]
  506. if v.WarrantyStart != "0" {
  507. v.WarrantyPeriod = utils.DateS(v.WarrantyStart, "YYYY-MM-DD") + " 至 " + utils.DateS(v.WarrantyEnd, "YYYY-MM-DD")
  508. } else {
  509. v.WarrantyPeriod = ""
  510. }
  511. orderList[k] = v
  512. }
  513. data := gin.H{
  514. "list": orderList,
  515. "total": total,
  516. "limit": page.PageSize,
  517. }
  518. app.Success(c, data)
  519. }
  520. func OrderCheck(c *gin.Context) {
  521. id := utils.StrTo(c.Param("id")).MustInt()
  522. if id <= 0 {
  523. app.ErrorMsg(c, "无效的订单ID", nil)
  524. return
  525. }
  526. var form = form.OrderCheck{
  527. Servicer: c.GetInt("adminID"),
  528. }
  529. if err := order.Check(form, id); err != nil {
  530. app.Error(c, err.Error())
  531. return
  532. }
  533. app.Success(c, nil)
  534. }
  535. func OrderRemark(c *gin.Context) {
  536. id := utils.StrTo(c.Param("id")).MustInt()
  537. if id <= 0 {
  538. app.ErrorMsg(c, "无效的订单ID", nil)
  539. return
  540. }
  541. var form form.OrderRemark
  542. if app.Bind(c, &form) != nil {
  543. return
  544. }
  545. if err := order.Remark(form, id); err != nil {
  546. app.Error(c, err.Error())
  547. return
  548. }
  549. app.Success(c, nil)
  550. }
  551. func OrderAllot(c *gin.Context) {
  552. id := utils.StrTo(c.Param("id")).MustInt()
  553. if id <= 0 {
  554. app.ErrorMsg(c, "无效的订单ID", nil)
  555. return
  556. }
  557. var form form.OrderAllot
  558. if app.Bind(c, &form) != nil {
  559. return
  560. }
  561. form.Servicer = c.GetInt("adminID")
  562. if err := order.Allot(form, id); err != nil {
  563. app.Error(c, err.Error())
  564. return
  565. }
  566. app.Success(c, nil)
  567. }
  568. func OrderInfo(c *gin.Context) {
  569. id := utils.StrTo(c.Param("id")).MustInt()
  570. if id <= 0 {
  571. app.Error(c, "工单 id 有误")
  572. return
  573. }
  574. type OrderInfo struct {
  575. ID int `json:"id"`
  576. OrderNo string `json:"order_no"`
  577. MainType int `json:"main_type"`
  578. SubType int `json:"sub_type"`
  579. Type string `json:"type"`
  580. LinkName string `json:"link_name"`
  581. LinkPhone string `json:"link_phone"`
  582. Province int `json:"province"`
  583. City int `json:"city"`
  584. Region int `json:"region"`
  585. Address string `json:"address"`
  586. Content string `json:"content"`
  587. Pics string `json:"pics"`
  588. PicList []string `json:"pic_list"`
  589. State int `json:"state"`
  590. StateName string `json:"state_name"`
  591. RepairID int `json:"repair_id"`
  592. InWarranty int `json:"in_warranty"`
  593. StateColor string `json:"state_color"`
  594. CreatedAt string `json:"created_at"`
  595. ScheduleTime string `json:"schedule_time"`
  596. }
  597. var orderInfo OrderInfo
  598. err := order.GetOneWithDetail("o.id={{id}}", map[string]interface{}{"id": id}, &orderInfo)
  599. if err != nil {
  600. app.Error(c, err.Error())
  601. return
  602. }
  603. orderTypeMap := aftersale.GetTypeMapByCache()
  604. orderStatusList := utils.ParseSliceMap(orderParam.Params.State, "id")
  605. orderInfo.PicList = utils.ParseImgStr(orderInfo.Pics)
  606. orderInfo.Address = region.GetFullAddressByCodes(orderInfo.Province, orderInfo.City, orderInfo.Region, orderInfo.Address, "", nil)
  607. orderInfo.StateName = utils.ToStr(orderStatusList[utils.ToStr(orderInfo.State)]["name"])
  608. orderInfo.StateColor = utils.ToStr(orderStatusList[utils.ToStr(orderInfo.State)]["color"])
  609. orderInfo.CreatedAt = utils.DateS(orderInfo.CreatedAt, "YYYY-MM-DD")
  610. orderInfo.Type = orderTypeMap[orderInfo.MainType] + "/" + orderTypeMap[orderInfo.SubType]
  611. type OrderEvent struct {
  612. Title string `json:"title"`
  613. Content string `json:"content"`
  614. Pics string `json:"pics"`
  615. PicList []string `json:"pic_list"`
  616. CreatedAt string `json:"created_at"`
  617. }
  618. eventList := make([]*OrderEvent, 0)
  619. _, err = order.GetEventList(map[string]interface{}{"order_id": id, "_orderby": "created_at desc", "event_type": 1}, nil, &eventList)
  620. if err != nil {
  621. app.Error(c, err.Error())
  622. return
  623. }
  624. for k, v := range eventList {
  625. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD hh:mm")
  626. v.PicList = utils.ParseImgStr(v.Pics)
  627. eventList[k] = v
  628. }
  629. // repair
  630. type Repair struct {
  631. ID int `json:"id"`
  632. WorkerName string `json:"worker_name"`
  633. WorkerPhone string `json:"worker_phone"`
  634. ScheduleTime string `json:"schedule_time"`
  635. FinishedAt string `json:"finished_at"`
  636. Content string `json:"content"`
  637. Duration float64 `json:"duration"`
  638. Pics string `json:"pics"`
  639. PicList []string `json:"pic_list"`
  640. State int `json:"state"`
  641. StateName string `json:"state_name"`
  642. CreatedAt string `json:"created_at"`
  643. AuditType int `json:"audit_type"`
  644. AuditState int `json:"audit_state"`
  645. AuditAt int `json:"audit_at"`
  646. }
  647. repairList := make([]*Repair, 0)
  648. if _, err = order.GetRepairList(map[string]interface{}{"order_id": id, "_orderby": "created_at desc"}, nil, &repairList); err != nil {
  649. app.Error(c, err.Error())
  650. return
  651. }
  652. repairStatusList := utils.ParseSliceMap(orderParam.Params.RepairState, "id")
  653. for _, v := range repairList {
  654. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD hh:mm")
  655. v.ScheduleTime = utils.DateS(v.ScheduleTime, "YYYY-MM-DD hh:mm")
  656. v.FinishedAt = utils.DateS(v.FinishedAt, "YYYY-MM-DD hh:mm")
  657. v.PicList = utils.ParseImgStr(v.Pics)
  658. v.StateName = utils.ToStr(repairStatusList[utils.ToStr(v.State)]["name"])
  659. }
  660. // 回访信息
  661. type Visit struct {
  662. ID int `json:"id"`
  663. OrderID int `json:"order_id"`
  664. State int `json:"state"`
  665. StateName string `json:"state_name"`
  666. VisitTime string `json:"visit_time"`
  667. Content string `json:"content"`
  668. FinishedAt string `json:"finished_at"`
  669. }
  670. visitList := make([]*Visit, 0)
  671. if _, err := order.GetVisits(map[string]interface{}{"order_id": id, "state": 2, "_orderby": "created_at desc", "deleted_at": 0}, nil, &visitList); err != nil {
  672. app.Error(c, err.Error())
  673. }
  674. for _, v := range visitList {
  675. v.FinishedAt = utils.DateS(v.FinishedAt, "YYYY-MM-DD")
  676. }
  677. // 完结信息
  678. type Issue struct {
  679. ID int `json:"id"`
  680. Director int `json:"director"`
  681. DirectorName string `json:"director_name"`
  682. IssueID int `json:"issue_id"`
  683. IssueName string `json:"issue_name"`
  684. IssueDesc string `json:"issue_desc"`
  685. Brand int `json:"brand"`
  686. BrandName string `json:"brand_name"`
  687. CreatedAt string `json:"created_at"`
  688. }
  689. issueList := make([]*Issue, 0)
  690. if _, err := order.GetIssues(map[string]interface{}{"order_id": id, "_orderby": "created_at desc"}, nil, &issueList); err != nil {
  691. app.Error(c, err.Error())
  692. }
  693. issueMap := aftersale.GetIssueMapByCache()
  694. directorList := utils.ParseSliceMap(orderParam.Params.IssueDirector, "id")
  695. brandIds := make([]int, 0)
  696. for _, v := range issueList {
  697. brandIds = utils.AppendUniqueInt(brandIds, v.Brand)
  698. }
  699. brandMap := make(map[int]string)
  700. if len(brandIds) > 0 {
  701. if brandList, err := brand.GetList(map[string]interface{}{"id in": brandIds}, nil, app.Page{}, nil); err == nil {
  702. for _, v := range brandList {
  703. brandMap[v.ID] = v.BrandName
  704. }
  705. }
  706. }
  707. for _, v := range issueList {
  708. if issueName, ok := issueMap[v.IssueID]; ok {
  709. v.IssueName = issueName
  710. }
  711. v.DirectorName = utils.ToStr(directorList[utils.ToStr(v.Director)]["name"])
  712. if brandName, ok := brandMap[v.Brand]; ok {
  713. v.BrandName = brandName
  714. }
  715. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD")
  716. }
  717. res := gin.H{
  718. "info": orderInfo,
  719. "event": eventList,
  720. "repair": repairList,
  721. "visit": visitList,
  722. "issue": issueList,
  723. }
  724. type Comment struct {
  725. ID int `json:"id"`
  726. OrderID int `json:"order_id"`
  727. UserID int `json:"user_id"`
  728. Star int `json:"star"`
  729. Content string `json:"content"`
  730. Tags string `json:"tags"`
  731. }
  732. var comment Comment
  733. if _, err := order.GetComment(map[string]interface{}{"order_id": id}, nil, &comment); err != nil {
  734. app.Error(c, err.Error())
  735. }
  736. if comment.ID != 0 {
  737. res["comment"] = comment
  738. }
  739. app.Success(c, res)
  740. }
  741. func OrderDel(c *gin.Context) {
  742. id := utils.StrTo(c.Param("id")).MustInt()
  743. if id <= 0 {
  744. app.Error(c, "工单 id 有误")
  745. return
  746. }
  747. err := order.Del(id)
  748. if err != nil {
  749. app.Error(c, err.Error())
  750. return
  751. }
  752. app.Success(c, nil)
  753. }
  754. func OrderAddIssue(c *gin.Context) {
  755. id := utils.StrTo(c.Param("id")).MustInt()
  756. if id <= 0 {
  757. app.Error(c, "工单 id 有误")
  758. return
  759. }
  760. var form form.OrderIssue
  761. if app.Bind(c, &form) != nil {
  762. return
  763. }
  764. err := order.Issue(form, id)
  765. if err != nil {
  766. app.Error(c, err.Error())
  767. return
  768. }
  769. app.Success(c, nil)
  770. }
  771. func OrderComplete(c *gin.Context) {
  772. id := utils.StrTo(c.Param("id")).MustInt()
  773. if id <= 0 {
  774. app.Error(c, "工单 id 有误")
  775. return
  776. }
  777. var form form.OrderComplete
  778. if app.Bind(c, &form) != nil {
  779. return
  780. }
  781. form.Servicer = c.GetInt("adminID")
  782. form.IsForce = utils.ToInt(c.Query("is_force"))
  783. //if isForce := utils.ToInt(c.Query("is_force")); isForce == 1 {
  784. // form.IsForce = 1
  785. //}
  786. err := order.Complete(form, id)
  787. if err != nil {
  788. app.Error(c, err.Error())
  789. return
  790. }
  791. app.Success(c, nil)
  792. }
  793. func OrderExport(c *gin.Context) {
  794. params := make(map[string]interface{})
  795. where := map[string]string{
  796. "where": "deleted_at=0",
  797. "_order_by": "id desc",
  798. }
  799. // leader
  800. leaderID := utils.ToInt(c.Query("leader_id"))
  801. if leaderID > 0 {
  802. where["where"] += " AND leader={{leader_id}}"
  803. params["leader_id"] = leaderID
  804. }
  805. // state
  806. state := utils.ToInt(c.Query("state"))
  807. if state != 0 {
  808. where["where"] += " AND if(state=90,90,if(is_force=0,state,100))={{state}}"
  809. params["state"] = state
  810. }
  811. // is_force
  812. isForce := utils.ToInt(c.Query("is_force"))
  813. if isForce != 0 {
  814. if isForce == 2 {
  815. where["where"] += " AND is_force!=0"
  816. } else {
  817. where["where"] += " AND is_force={{isForce}}"
  818. params["isForce"] = isForce - 1
  819. }
  820. }
  821. // type
  822. typ := utils.ToInt(c.Query("type"))
  823. if typ != 0 {
  824. where["where"] += " AND type={{type}}"
  825. params["type"] = typ - 1
  826. }
  827. // address
  828. address := c.Query("address")
  829. if address != "" {
  830. where["where"] += " AND address LIKE {{address}}"
  831. params["address"] = "%" + address + "%"
  832. }
  833. // inWarranty
  834. inWarranty := utils.ToInt(c.Query("in_warranty"))
  835. if utils.IsContain([]int{0, 1}, inWarranty) {
  836. where["where"] += " AND in_warranty={{in_warranty}}"
  837. params["in_warranty"] = inWarranty
  838. }
  839. // user
  840. user1 := c.Query("user")
  841. if user1 != "" {
  842. where["where"] += " AND (link_name LIKE {{user}} OR link_phone LIKE {{user}})"
  843. params["user"] = "%" + user1 + "%"
  844. }
  845. // state
  846. finish_state := utils.ToInt(c.Query("finish_state"))
  847. if finish_state != 0 {
  848. switch finish_state {
  849. case 1:
  850. where["where"] += " AND (o.finish_state=1)"
  851. break
  852. case 2:
  853. where["where"] += " AND (o.finish_state=2)"
  854. break
  855. case 3:
  856. where["where"] += " AND (o.finish_state=1 AND UNIX_TIMESTAMP()<=o.end_time)"
  857. break
  858. case 4:
  859. where["where"] += " AND (o.finish_state=1 AND UNIX_TIMESTAMP()>o.end_time)"
  860. break
  861. case 5:
  862. where["_order_by"] = "o.incomplete_count desc"
  863. break
  864. }
  865. }
  866. createdDate := c.QueryArray("created_date[]")
  867. if createdDate != nil && len(createdDate) == 2 {
  868. where["where"] += " AND o.created_at BETWEEN {{created_at_min}} AND {{created_at_max}}"
  869. params["created_at_min"] = utils.DateParseUnix(createdDate[0], "Y-m-d")
  870. params["created_at_max"] = utils.DateParseUnix(createdDate[1], "Y-m-d") + 86399
  871. }
  872. if adminInfo := admin.GetAdminCache(c.GetInt("adminID")); adminInfo.SiteID > 0 {
  873. where["where"] += " AND site_id = {{site_id}}"
  874. params["site_id"] = adminInfo.SiteID
  875. }
  876. type House struct {
  877. ID int `json:"id"`
  878. Address string `json:"address"`
  879. Designer int `json:"designer"`
  880. DesignerName string `json:"designer_name"`
  881. District int `json:"district"`
  882. DistrictName string `json:"district_name"`
  883. Supervisor int `json:"supervisor"`
  884. SupervisorName string `json:"supervisor_name"`
  885. ProjectManager int `json:"project_manager"`
  886. ProjectManagerName string `json:"project_manager_name"`
  887. ProjectLeader int `json:"project_leader"`
  888. ProjectLeaderName string `json:"project_leader_name"`
  889. ProjectStart string `json:"project_start"`
  890. ProjectEnd string `json:"project_end"`
  891. }
  892. type Visit struct {
  893. OrderID int `json:"order_id"`
  894. Content string `json:"content"`
  895. FinishedAt string `json:"finished_at"`
  896. }
  897. type Detail struct {
  898. OrderID int `json:"order_id"`
  899. Address string `json:"address"`
  900. Content string `json:"content"`
  901. }
  902. type Issue struct {
  903. OrderID int `json:"order_id"`
  904. IssueDesc string `json:"issue_desc"`
  905. Director int `json:"director"`
  906. }
  907. type Repair struct {
  908. OrderID int `json:"order_id"`
  909. WorkerName string `json:"worker_name"`
  910. }
  911. type Orders struct {
  912. ID int `json:"id"`
  913. CreatedAt string `json:"created_at"`
  914. LinkName string `json:"link_name"`
  915. LinkPhone string `json:"link_phone"`
  916. UserID int `json:"user_id"`
  917. InWarranty int `json:"in_warranty"`
  918. Leader int `json:"leader"`
  919. LeaderName string `json:"leader_name"`
  920. HouseID int `json:"house_id"`
  921. House *House `json:"house"`
  922. Repair *Repair `json:"repair"`
  923. Issue *Issue `json:"issue"`
  924. Detail *Detail `json:"detial"`
  925. Visit string `json:"visit"`
  926. }
  927. orderList := make([]*Orders, 0)
  928. err := order.GetLists(where, params, app.Page{}, &orderList)
  929. houseIds := make([]int, 0)
  930. orderIds := make([]int, 0)
  931. userIds := make([]int, 0)
  932. adminIds := make([]int, 0)
  933. for _, v := range orderList {
  934. houseIds = append(houseIds, v.HouseID)
  935. orderIds = append(orderIds, v.ID)
  936. userIds = append(userIds, v.UserID)
  937. adminIds = append(adminIds, v.Leader)
  938. }
  939. houseMap := make(map[int]*House, 0)
  940. if len(houseIds) > 0 {
  941. houseList := make([]*House, 0)
  942. user.GetHouseList(map[string]interface{}{"id in": houseIds}, nil, app.Page{}, &houseList)
  943. for _, v := range houseList {
  944. adminIds = append(adminIds, v.Designer, v.Supervisor, v.ProjectManager, v.ProjectLeader)
  945. houseMap[v.ID] = v
  946. }
  947. }
  948. userMap := make(map[int]string, 0)
  949. if len(userIds) > 0 {
  950. userList, _ := user.GetList(map[string]interface{}{"id in": userIds}, nil, app.Page{}, nil)
  951. for _, v := range userList {
  952. userMap[v.ID] = v.Name
  953. }
  954. }
  955. type AdminInfo struct {
  956. Username string `json:"username"`
  957. State int `json:"state"`
  958. }
  959. adminMap := make(map[int]AdminInfo, 0)
  960. if len(adminIds) > 0 {
  961. adminList, _ := admin.GetAdmins(map[string]interface{}{"id in": adminIds}, nil, app.Page{}, nil)
  962. for _, v := range adminList {
  963. adminMap[v.ID] = AdminInfo{Username: v.Username, State: v.State}
  964. }
  965. }
  966. districtMap := make(map[int]string)
  967. for _, v := range param.Params.District {
  968. districtMap[v.ID] = v.Name
  969. }
  970. for k, v := range houseMap {
  971. if v.District > 0 {
  972. v.DistrictName = districtMap[v.District]
  973. }
  974. if v.Designer > 0 {
  975. v.DesignerName = adminMap[v.Designer].Username
  976. }
  977. if v.Supervisor > 0 {
  978. v.SupervisorName = adminMap[v.Supervisor].Username
  979. }
  980. if v.ProjectLeader > 0 {
  981. v.ProjectLeaderName = adminMap[v.ProjectLeader].Username
  982. }
  983. if v.ProjectManager > 0 {
  984. v.ProjectManagerName = adminMap[v.ProjectManager].Username
  985. }
  986. if v.Supervisor > 0 {
  987. if adminMap[v.Supervisor].State == -1 {
  988. v.SupervisorName = "离职项目经理"
  989. } else {
  990. v.SupervisorName = adminMap[v.Supervisor].Username
  991. }
  992. }
  993. houseMap[k] = v
  994. }
  995. detialMap := make(map[int]*Detail, 0)
  996. if len(orderIds) > 0 {
  997. detailList := make([]*Detail, 0)
  998. order.GetDetailList(map[string]interface{}{"order_id in": orderIds}, nil, app.Page{}, &detailList)
  999. for _, v := range detailList {
  1000. detialMap[v.OrderID] = v
  1001. }
  1002. }
  1003. issueMap := make(map[int]*Issue, 0)
  1004. if len(orderIds) > 0 {
  1005. issueList := make([]*Issue, 0)
  1006. order.GetIssues(map[string]interface{}{"order_id in": orderIds}, nil, &issueList)
  1007. for _, v := range issueList {
  1008. issueMap[v.OrderID] = v
  1009. }
  1010. }
  1011. issueDirectorMap := orderParam.GetIssueDirectorMap()
  1012. visitList := make([]*Visit, 0)
  1013. visitListMap := make(map[int][]string, 0)
  1014. if len(orderIds) > 0 {
  1015. if _, err := order.GetVisits(map[string]interface{}{"order_id in": orderIds, "_orderby": "created_at desc", "deleted_at": 0, "state": 2}, nil, &visitList); err == nil {
  1016. for _, v := range visitList {
  1017. visitListMap[v.OrderID] = append(visitListMap[v.OrderID], v.Content)
  1018. }
  1019. }
  1020. }
  1021. for _, v := range orderList {
  1022. if v.Leader > 0 {
  1023. v.LeaderName = adminMap[v.Leader].Username
  1024. }
  1025. if visitListMap[v.ID] != nil {
  1026. v.Visit = strings.Join(visitListMap[v.ID], "\n")
  1027. }
  1028. v.House = houseMap[v.HouseID]
  1029. }
  1030. wb := xlsx.NewFile()
  1031. filename := "售后维修表-" + db.ToString(time.Now().Unix()) + ".xlsx"
  1032. fullPath := config.Cfg.App.ExportPath + filename
  1033. styleBold := utils.GetCommonStyle(utils.StyleCfg{IsBold: true, FontSize: 10})
  1034. styleCommon := utils.GetCommonStyle(utils.StyleCfg{FontSize: 9})
  1035. sh, err := wb.AddSheet("Sheet1")
  1036. if err != nil {
  1037. app.Error(c, err.Error())
  1038. return
  1039. }
  1040. var row *xlsx.Row
  1041. height := float64(30)
  1042. row = utils.AddRow(sh, utils.Row{Height: height})
  1043. utils.AddCell(row, utils.Cell{Value: "售后维修交接表", HMerge: 22, Style: styleBold})
  1044. sh = utils.SetColWidth(sh, []float64{5, 5, 10, 10, 10, 10, 20, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 30, 15, 15, 15, 10, 10})
  1045. row = utils.AddRow(sh, utils.Row{Height: height})
  1046. utils.AddCell(row, utils.Cell{Value: "序号", Style: styleBold})
  1047. utils.AddCell(row, utils.Cell{Value: "级别", Style: styleBold})
  1048. utils.AddCell(row, utils.Cell{Value: "工地性质", Style: styleBold})
  1049. utils.AddCell(row, utils.Cell{Value: "开工日期", Style: styleBold})
  1050. utils.AddCell(row, utils.Cell{Value: "竣工", Style: styleBold})
  1051. utils.AddCell(row, utils.Cell{Value: "报修时间", Style: styleBold})
  1052. utils.AddCell(row, utils.Cell{Value: "工地名称", Style: styleBold})
  1053. utils.AddCell(row, utils.Cell{Value: "业主姓名", Style: styleBold})
  1054. utils.AddCell(row, utils.Cell{Value: "联系电话", Style: styleBold})
  1055. utils.AddCell(row, utils.Cell{Value: "设计师", Style: styleBold})
  1056. utils.AddCell(row, utils.Cell{Value: "区域", Style: styleBold})
  1057. utils.AddCell(row, utils.Cell{Value: "售后报修人", Style: styleBold})
  1058. utils.AddCell(row, utils.Cell{Value: "工程主管", Style: styleBold})
  1059. utils.AddCell(row, utils.Cell{Value: "队长", Style: styleBold})
  1060. utils.AddCell(row, utils.Cell{Value: "项目经理", Style: styleBold})
  1061. utils.AddCell(row, utils.Cell{Value: "指派项目经理", Style: styleBold})
  1062. utils.AddCell(row, utils.Cell{Value: "是否过保修", Style: styleBold})
  1063. utils.AddCell(row, utils.Cell{Value: "责任人", Style: styleBold})
  1064. utils.AddCell(row, utils.Cell{Value: "报修内容", Style: styleBold})
  1065. utils.AddCell(row, utils.Cell{Value: "反馈结果", Style: styleBold})
  1066. utils.AddCell(row, utils.Cell{Value: "客服回访情况", Style: styleBold})
  1067. utils.AddCell(row, utils.Cell{Value: "解决结果", Style: styleBold})
  1068. utils.AddCell(row, utils.Cell{Value: "报销情况", Style: styleBold})
  1069. for k, v := range orderList {
  1070. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY/MM/DD")
  1071. if v.House.ProjectStart == "0" {
  1072. v.House.ProjectStart = ""
  1073. } else {
  1074. v.House.ProjectStart = utils.DateS(v.House.ProjectStart, "YYYY/MM/DD")
  1075. }
  1076. if v.House.ProjectEnd == "0" {
  1077. v.House.ProjectEnd = ""
  1078. } else {
  1079. v.House.ProjectEnd = utils.DateS(v.House.ProjectEnd, "YYYY/MM/DD")
  1080. }
  1081. row = utils.AddRow(sh, utils.Row{Height: height})
  1082. utils.AddCell(row, utils.Cell{Value: utils.ToStr(k + 1), Style: styleCommon})
  1083. utils.AddCell(row, utils.Cell{Value: "一级", Style: styleCommon})
  1084. utils.AddCell(row, utils.Cell{Value: "全包", Style: styleCommon})
  1085. utils.AddCell(row, utils.Cell{Value: v.House.ProjectStart, Style: styleCommon})
  1086. utils.AddCell(row, utils.Cell{Value: v.House.ProjectEnd, Style: styleCommon})
  1087. utils.AddCell(row, utils.Cell{Value: v.CreatedAt, Style: styleCommon})
  1088. utils.AddCell(row, utils.Cell{Value: v.House.Address, Style: styleCommon})
  1089. utils.AddCell(row, utils.Cell{Value: v.LinkName, Style: styleCommon})
  1090. utils.AddCell(row, utils.Cell{Value: v.LinkPhone, Style: styleCommon})
  1091. utils.AddCell(row, utils.Cell{Value: v.House.DesignerName, Style: styleCommon})
  1092. utils.AddCell(row, utils.Cell{Value: v.House.DistrictName, Style: styleCommon})
  1093. utils.AddCell(row, utils.Cell{Value: userMap[v.UserID], Style: styleCommon})
  1094. utils.AddCell(row, utils.Cell{Value: v.House.ProjectManagerName, Style: styleCommon})
  1095. utils.AddCell(row, utils.Cell{Value: v.House.ProjectLeaderName, Style: styleCommon})
  1096. utils.AddCell(row, utils.Cell{Value: v.House.SupervisorName, Style: styleCommon})
  1097. utils.AddCell(row, utils.Cell{Value: v.LeaderName, Style: styleCommon})
  1098. inWarranty := "是"
  1099. if v.InWarranty > 0 {
  1100. inWarranty = "否"
  1101. }
  1102. utils.AddCell(row, utils.Cell{Value: inWarranty, Style: styleCommon})
  1103. issueDirectorName := ""
  1104. issueDesc := ""
  1105. if issue, ok := issueMap[v.ID]; ok {
  1106. issueDirectorName = issueDirectorMap[issue.Director]
  1107. issueDesc = issue.IssueDesc
  1108. }
  1109. utils.AddCell(row, utils.Cell{Value: issueDirectorName, Style: styleCommon})
  1110. utils.AddCell(row, utils.Cell{Value: detialMap[v.ID].Content, Style: styleCommon})
  1111. utils.AddCell(row, utils.Cell{Value: issueDesc, Style: styleCommon})
  1112. utils.AddCell(row, utils.Cell{Value: v.Visit, Style: styleCommon})
  1113. utils.AddCell(row, utils.Cell{Value: "", Style: styleCommon})
  1114. utils.AddCell(row, utils.Cell{Value: "", Style: styleCommon})
  1115. }
  1116. if err := wb.Save(fullPath); err != nil {
  1117. app.Error(c, err.Error())
  1118. return
  1119. }
  1120. app.Success(c, gin.H{"path": "export/" + filename, "filename": filename})
  1121. }
  1122. func OrderVisitStatist(c *gin.Context) {
  1123. page := app.HandlePageNums(c)
  1124. var s db.Select
  1125. s.TableName = "`zy_as_order_visit`"
  1126. s.Select = map[string]string{
  1127. "date": "`zy_as_order_visit`.`visit_time`",
  1128. "total": "COUNT(1)",
  1129. "finished_visit": "COUNT(`zy_as_order_visit`.`state` = 2 OR NULL)",
  1130. "ufinished_visit": "COUNT(`zy_as_order_visit`.`state` = 1 OR NULL)",
  1131. "overtime_visit": "COUNT(`zy_as_order_visit`.`finished_at` > `zy_as_order_visit`.`visit_time` OR NULL)",
  1132. }
  1133. s.GroupBy = "`zy_as_order_visit`.`visit_time`"
  1134. s.OrderBy = "`zy_as_order_visit`.`visit_time` DESC"
  1135. if page.PageSize != 0 {
  1136. s.Limit = int64(page.PageSize)
  1137. s.Offset = int64(page.PageSize * (page.PageNum - 1))
  1138. }
  1139. date := utils.ToInt(c.Query("date"))
  1140. if date != 0 {
  1141. s.Where = append(s.Where, fmt.Sprintf("`zy_as_order_visit`.`visit_time` = %s", s.Param(date)))
  1142. }
  1143. startdate := utils.ToInt(c.Query("startdate"))
  1144. if startdate != 0 {
  1145. s.Where = append(s.Where, fmt.Sprintf("`zy_as_order_visit`.`visit_time` >= %s", s.Param(startdate)))
  1146. }
  1147. enddate := utils.ToInt(c.Query("enddate"))
  1148. if enddate != 0 {
  1149. s.Where = append(s.Where, fmt.Sprintf("`zy_as_order_visit`.`visit_time` <= %s", s.Param(enddate)))
  1150. }
  1151. s.Where = append(s.Where, fmt.Sprintf("`zy_as_order_visit`.`deleted_at` = %s", s.Param(0)))
  1152. query, params := s.Query()
  1153. list, err := db.QueryMap(query, params, nil)
  1154. if err != nil {
  1155. app.ErrorMsg(c, err.Error(), nil)
  1156. return
  1157. }
  1158. if list == nil {
  1159. list = make([]map[string]interface{}, 0)
  1160. }
  1161. count, err := db.GetCount(s, nil)
  1162. if err != nil {
  1163. app.ErrorMsg(c, err.Error(), nil)
  1164. return
  1165. }
  1166. app.Success(c, gin.H{
  1167. "list": list,
  1168. "count": count,
  1169. })
  1170. }
  1171. func RepairCheckList(c *gin.Context) {
  1172. page := app.HandlePageNum(c)
  1173. where := map[string]string{
  1174. "where": " o.deleted_at=0 and r.audit_type != 0",
  1175. "_group_by": "r.id",
  1176. "_order_by": "r.id desc",
  1177. }
  1178. param := make(map[string]interface{})
  1179. if page.PageSize != 0 {
  1180. where["_page_size"] = utils.ToStr(page.PageSize)
  1181. where["_page_num"] = utils.ToStr(page.PageNum)
  1182. }
  1183. auditState := utils.ToInt(c.Query("audit_state"))
  1184. if auditState > 0 {
  1185. where["where"] = where["where"] + " AND r.audit_state = {{auditState}}"
  1186. param["auditState"] = auditState - 1
  1187. }
  1188. auditType := utils.ToInt(c.Query("audit_type"))
  1189. if auditType > 0 {
  1190. where["where"] = where["where"] + " AND r.audit_type = {{auditType}}"
  1191. param["auditType"] = auditType
  1192. }
  1193. keyword := c.Query("keyword")
  1194. if keyword != "" {
  1195. where["where"] = where["where"] + " AND (o.link_name like {{keyword}} or o.link_phone like {{keyword}})"
  1196. param["keyword"] = "%" + keyword + "%"
  1197. }
  1198. // leader
  1199. leaderID := utils.ToInt(c.Query("leader_id"))
  1200. if leaderID > 0 {
  1201. where["where"] += " AND leader={{leader_id}}"
  1202. param["leader_id"] = leaderID
  1203. }
  1204. // address
  1205. address := c.Query("address")
  1206. if address != "" {
  1207. where["where"] += " AND address LIKE {{address}}"
  1208. param["address"] = "%" + address + "%"
  1209. }
  1210. createdDate := c.QueryArray("created_date[]")
  1211. if createdDate != nil && len(createdDate) == 2 {
  1212. where["where"] += " AND o.created_at BETWEEN {{created_at_min}} AND {{created_at_max}}"
  1213. param["created_at_min"] = utils.DateParseUnix(createdDate[0], "Y-m-d")
  1214. param["created_at_max"] = utils.DateParseUnix(createdDate[1], "Y-m-d") + 86399
  1215. }
  1216. total, err := order.GetCountWidthOrder(where["where"], param)
  1217. if err != nil {
  1218. app.Error(c, err.Error())
  1219. return
  1220. }
  1221. type Admin struct {
  1222. ID int `json:"id"`
  1223. UserName string `json:"username"`
  1224. Phone string `json:"phone"`
  1225. }
  1226. type RepairList struct {
  1227. ID int `json:"id"`
  1228. OrderId int `json:"order_id"`
  1229. WorkerName string `json:"worker_name"`
  1230. WorkerPhone string `json:"worker_phone"`
  1231. ScheduleTime string `json:"schedule_time"`
  1232. FinishedAt string `json:"finished_at"`
  1233. Content string `json:"content"`
  1234. Duration float64 `json:"duration"`
  1235. Pics string `json:"pics"`
  1236. PicList []string `json:"pic_list"`
  1237. State int `json:"state"`
  1238. StateName string `json:"state_name"`
  1239. CreatedAt string `json:"created_at"`
  1240. AuditType int `json:"audit_type"`
  1241. AuditState int `json:"audit_state"`
  1242. AuditAt string `json:"audit_at"`
  1243. OrderNo string `json:"order_no"`
  1244. MainType int `json:"main_type"`
  1245. SubType int `json:"sub_type"`
  1246. Type string `json:"type"`
  1247. LinkName string `json:"link_name"`
  1248. LinkPhone string `json:"link_phone"`
  1249. Address string `json:"address"`
  1250. DetailContent string `json:"detail_content"`
  1251. DetailPics string `json:"detail_pics"`
  1252. OrderCreatedAt string `json:"order_created_at"`
  1253. Leader int `json:"leader"`
  1254. LeaderInfo *Admin `json:"leader_info"`
  1255. WarrantyStart string `json:"warranty_start"`
  1256. WarrantyEnd string `json:"warranty_end"`
  1257. WarrantyPeriod string `json:"warranty_period"`
  1258. IsForce int `json:"is_force"`
  1259. EndTime string `json:"end_time"`
  1260. }
  1261. repairList := make([]RepairList, 0)
  1262. err = order.GetListWidthOrder(where, param, page, &repairList)
  1263. if err != nil {
  1264. app.Error(c, err.Error())
  1265. return
  1266. }
  1267. adminIds := make([]int, 0)
  1268. for _, v := range repairList {
  1269. if v.Leader > 0 {
  1270. adminIds = append(adminIds, v.Leader)
  1271. }
  1272. }
  1273. orderTypeMap := aftersale.GetTypeMapByCache()
  1274. adminMap := make(map[int]*Admin, 0)
  1275. if len(adminIds) > 0 {
  1276. adminList := make([]*Admin, 0)
  1277. if _, err := admin.GetAdmins(map[string]interface{}{"id in": adminIds}, []string{"id, username, phone"}, app.Page{}, &adminList); err == nil {
  1278. for _, v := range adminList {
  1279. adminMap[v.ID] = v
  1280. }
  1281. }
  1282. }
  1283. repairStatusList := utils.ParseSliceMap(orderParam.Params.RepairState, "id")
  1284. for k, v := range repairList {
  1285. v.PicList = utils.ParseImgStr(v.Pics)
  1286. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD")
  1287. v.Type = orderTypeMap[v.MainType] + "/" + orderTypeMap[v.SubType]
  1288. if v.Leader > 0 && adminMap[v.Leader] != nil {
  1289. v.LeaderInfo = adminMap[v.Leader]
  1290. } else {
  1291. v.LeaderInfo = &Admin{}
  1292. }
  1293. if v.WarrantyStart != "0" {
  1294. v.WarrantyPeriod = utils.DateS(v.WarrantyStart, "YYYY-MM-DD") + " 至 " + utils.DateS(v.WarrantyEnd, "YYYY-MM-DD")
  1295. } else {
  1296. v.WarrantyPeriod = "保修期外"
  1297. }
  1298. v.ScheduleTime = utils.DateS(v.ScheduleTime, "YYYY-MM-DD")
  1299. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD")
  1300. v.OrderCreatedAt = utils.DateS(v.OrderCreatedAt, "YYYY-MM-DD")
  1301. v.EndTime = utils.DateS(v.EndTime, "YYYY-MM-DD HH:mm")
  1302. v.FinishedAt = utils.DateS(v.FinishedAt, "YYYY-MM-DD HH:mm")
  1303. v.AuditAt = utils.DateS(v.AuditAt, "YYYY-MM-DD HH:mm")
  1304. v.StateName = utils.ToStr(repairStatusList[utils.ToStr(v.State)]["name"])
  1305. repairList[k] = v
  1306. }
  1307. data := gin.H{
  1308. "list": repairList,
  1309. "total": total,
  1310. "limit": page.PageSize,
  1311. }
  1312. app.Success(c, data)
  1313. }
  1314. func RepairCheck(c *gin.Context) {
  1315. id := utils.StrTo(c.Param("id")).MustInt()
  1316. if id <= 0 {
  1317. app.Error(c, "工单 id 有误")
  1318. return
  1319. }
  1320. var form form.RepairCheck
  1321. if app.Bind(c, &form) != nil {
  1322. return
  1323. }
  1324. err := order.RepairCheck(id, form.State, c.GetInt("adminID"), form.Remark)
  1325. if err != nil {
  1326. app.Error(c, err.Error())
  1327. return
  1328. }
  1329. app.Success(c, nil)
  1330. }