admin.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. package admin
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "zhiyuan/models"
  9. "zhiyuan/models/raffle"
  10. "zhiyuan/pkg/app"
  11. "zhiyuan/pkg/config"
  12. "zhiyuan/pkg/db"
  13. "zhiyuan/pkg/errcode"
  14. adminParam "zhiyuan/pkg/param/admin"
  15. orderParam "zhiyuan/pkg/param/order"
  16. "zhiyuan/pkg/utils"
  17. "zhiyuan/services/admin"
  18. "zhiyuan/services/aftersale/order"
  19. "zhiyuan/services/dept"
  20. "zhiyuan/services/form"
  21. "zhiyuan/services/role"
  22. "github.com/gin-gonic/gin"
  23. "github.com/tealeg/xlsx/v3"
  24. )
  25. func Dashboard(c *gin.Context) {
  26. // 是否有缓存
  27. //cacheKey := "dashborad_data"
  28. //cacheData, err := redis.Get(cacheKey)
  29. //if err == nil {
  30. // app.Success(c, utils.JsonDecode(cacheData).ToMap())
  31. // return
  32. //}
  33. // 本月报修
  34. currentTime := time.Now()
  35. monthDate := utils.GetFirstDateOfMonth(currentTime).Unix()
  36. dayDate := utils.GetZeroTime(currentTime).Unix()
  37. orderBaseMap := map[string]interface{}{
  38. "deleted_at": 0,
  39. }
  40. eventBaseWhere := "o.deleted_at=0 AND e.event_type=1 AND e.created_at>" + utils.ToStr(dayDate)
  41. if adminInfo := admin.GetAdminCache(c.GetInt("adminID")); adminInfo.SiteID > 0 {
  42. orderBaseMap["site_id"] = adminInfo.SiteID
  43. eventBaseWhere = eventBaseWhere + " AND o.site_id=" + utils.ToStr(adminInfo.SiteID)
  44. }
  45. statisData := make(map[string]int64)
  46. statisData["month_num"], _ = order.Count(utils.MergeMap(map[string]interface{}{"created_at >": monthDate}, orderBaseMap))
  47. statisData["day_num"], _ = order.Count(utils.MergeMap(map[string]interface{}{"created_at >": dayDate}, orderBaseMap))
  48. statisData["day_repair_num"], _ = order.GetCountJoinOrder(eventBaseWhere+" AND order_state="+utils.ToStr(orderParam.State.Repaired.ID), nil)
  49. statisData["day_finish_num"], _ = order.GetCountJoinOrder(eventBaseWhere+" AND order_state="+utils.ToStr(orderParam.State.Confirmed.ID), nil)
  50. // 待办事项
  51. todoData := make(map[string]int64)
  52. // 待审核
  53. todoData["created"], _ = order.Count(utils.MergeMap(map[string]interface{}{"state": orderParam.State.Created.ID}, orderBaseMap))
  54. // 待分配
  55. todoData["checked"], _ = order.Count(utils.MergeMap(map[string]interface{}{"state": orderParam.State.Checked.ID}, orderBaseMap))
  56. // 待维修
  57. todoData["alloted"], _ = order.Count(utils.MergeMap(map[string]interface{}{"state": orderParam.State.Allotted.ID}, orderBaseMap))
  58. res := gin.H{
  59. "todo": todoData,
  60. "statis": statisData,
  61. }
  62. //redis.Set(cacheKey, res, 60)
  63. app.Success(c, res)
  64. }
  65. func Login(c *gin.Context) {
  66. var form form.AdminLogin
  67. if app.Bind(c, &form) != nil {
  68. return
  69. }
  70. token, err := admin.Login(form.Phone, form.Password)
  71. if err != nil {
  72. app.ErrorMsg(c, err.Error(), nil)
  73. return
  74. }
  75. app.Success(c, map[string]string{
  76. "token": token,
  77. })
  78. }
  79. func Info(c *gin.Context) {
  80. type SettingInfo struct {
  81. ImgHost string `json:"img_host"`
  82. }
  83. type AdminInfo struct {
  84. ID int `json:"id"`
  85. UserName string `json:"username"`
  86. RoleIds string `json:"role_ids"`
  87. ShopIds string `json:"shop_ids"`
  88. Auths []string `json:"auths"`
  89. Setting SettingInfo `json:"setting"`
  90. }
  91. var adminInfo *AdminInfo
  92. _, err := admin.GetInfoByID(c.GetInt("adminID"), []string{"id", "username", "shop_ids"}, &adminInfo)
  93. if adminInfo == nil {
  94. app.Response(c, http.StatusUnauthorized, errcode.TokenInvalid, nil)
  95. return
  96. }
  97. adminInfo.Auths, err = admin.GetAuth(adminInfo.ID)
  98. if err != nil {
  99. app.ErrorMsg(c, err.Error(), nil)
  100. return
  101. }
  102. adminInfo.Setting = SettingInfo{
  103. ImgHost: config.Cfg.App.ImgHost,
  104. }
  105. app.Success(c, adminInfo)
  106. }
  107. func Logout(c *gin.Context) {
  108. err := admin.Logout(c.GetInt("adminID"))
  109. if err != nil {
  110. app.ErrorMsg(c, err.Error(), nil)
  111. return
  112. }
  113. app.Success(c, nil)
  114. }
  115. func AdminAdd(c *gin.Context) {
  116. var addForm form.AdminAdd
  117. if app.Bind(c, &addForm) != nil {
  118. return
  119. }
  120. id, err := admin.Add(addForm)
  121. if err != nil {
  122. app.Error(c, err.Error())
  123. return
  124. }
  125. app.Success(c, gin.H{"id": id})
  126. }
  127. func AdminEdit(c *gin.Context) {
  128. id := utils.ToInt(c.Param("id"))
  129. if id <= 0 {
  130. app.ErrorMsg(c, "admin id must be a number", nil)
  131. return
  132. }
  133. var addForm form.AdminAdd
  134. if app.Bind(c, &addForm) != nil {
  135. return
  136. }
  137. err := admin.EditByID(addForm, id)
  138. if err != nil {
  139. app.ErrorMsg(c, err.Error(), nil)
  140. return
  141. }
  142. app.Success(c, nil)
  143. }
  144. func AdminEditState(c *gin.Context) {
  145. id := utils.ToInt(c.Param("id"))
  146. if id <= 0 {
  147. app.ErrorMsg(c, "admin id must be a number", nil)
  148. return
  149. }
  150. state := utils.ToInt(c.Param("state"))
  151. if state != 1 && state != -1 {
  152. app.ErrorMsg(c, "状态有误", nil)
  153. return
  154. }
  155. if err := admin.EditStateByID(id, state); err != nil {
  156. app.ErrorMsg(c, err.Error(), nil)
  157. return
  158. }
  159. app.Success(c, nil)
  160. }
  161. func AdminList(c *gin.Context) {
  162. isSelect := utils.ToInt(c.Query("is_select"))
  163. if isSelect == 1 {
  164. AdminListSelect(c)
  165. return
  166. }
  167. page := app.HandlePageNums(c)
  168. where := map[string]string{
  169. "where": "id>1 AND deleted_at=0",
  170. "_order_by": "id desc",
  171. }
  172. if page.PageSize != 0 {
  173. where["_page_size"] = utils.ToStr(page.PageSize)
  174. where["_page_num"] = utils.ToStr(page.PageNum)
  175. }
  176. param := make(map[string]interface{})
  177. // deptID
  178. deptID := utils.ToInt(c.Query("dept_id"))
  179. if deptID > 0 {
  180. where["where"] = where["where"] + " AND dept_id IN {{dept_id}}"
  181. param["dept_id"] = dept.GetSubDeptIds(deptID, []int{deptID})
  182. }
  183. // deptID
  184. ids := c.Query("ids")
  185. if ids != "" {
  186. Ids := make([]int, 0)
  187. for _, v := range strings.Split(ids, ",") {
  188. i, err := strconv.Atoi(v)
  189. if err == nil {
  190. Ids = append(Ids, i)
  191. }
  192. }
  193. where["where"] = where["where"] + " AND id IN {{ids}}"
  194. param["ids"] = Ids
  195. }
  196. // state
  197. state := utils.ToInt(c.Query("state"))
  198. if state != 0 {
  199. where["where"] = where["where"] + " AND state={{state}}"
  200. param["state"] = state
  201. }
  202. roleId := c.Query("role_id")
  203. if roleId != "" {
  204. where["where"] = where["where"] + " AND FIND_IN_SET({{role_id}}, role_ids)"
  205. param["role_id"] = roleId
  206. }
  207. shopId := c.Query("shop_id")
  208. if shopId != "" {
  209. where["where"] = where["where"] + " AND FIND_IN_SET({{shop_id}}, shop_ids)"
  210. param["shop_id"] = shopId
  211. }
  212. // user
  213. user := c.Query("user")
  214. if user != "" {
  215. where["where"] = where["where"] + " AND (username LIKE {{user}} OR phone LIKE {{user}})"
  216. param["user"] = "%" + user + "%"
  217. }
  218. total, err := admin.CountRaw(where["where"], param)
  219. if err != nil {
  220. app.Error(c, err.Error())
  221. return
  222. }
  223. type AdminList struct {
  224. ID int `json:"id"`
  225. UserName string `json:"username"`
  226. Phone string `json:"phone"`
  227. RoleIds string `json:"role_ids"`
  228. RoleNames string `json:"role_names"`
  229. DeptID int `json:"dept_id"`
  230. DeptName string `json:"dept_name"`
  231. Headimgurl string `json:"headimgurl"`
  232. Nickname string `json:"nickname"`
  233. SiteID int `json:"site_id"`
  234. State int `json:"state"`
  235. StateName string `json:"state_name"`
  236. StateColor string `json:"state_color"`
  237. AccountName string `json:"account_name"`
  238. AccountBank string `json:"account_bank"`
  239. AccountNo string `json:"account_no"`
  240. CreatedAt string `json:"created_at"`
  241. ShopIds string `json:"shop_ids"`
  242. EntryDate int `json:"entry_date"`
  243. RestDay string `json:"rest_day"`
  244. LogIds string `json:"log_ids"`
  245. }
  246. adminList := make([]AdminList, 0)
  247. if _, err = admin.GetAdminsRaw(where, param, &adminList); err != nil {
  248. app.Error(c, err.Error())
  249. return
  250. }
  251. adminIds := make([]int, 0)
  252. for _, v := range adminList {
  253. adminIds = append(adminIds, v.ID)
  254. }
  255. roleListMap := make(map[int]string)
  256. if roleList, err := role.GetList(nil, []string{"id, name"}, app.Page{}, nil); err == nil {
  257. for _, v := range roleList {
  258. roleListMap[v.ID] = v.Name
  259. }
  260. }
  261. stateMap := adminParam.GetStateMap()
  262. for k, v := range adminList {
  263. roleNames := ""
  264. roleSlice := strings.Split(v.RoleIds, ",")
  265. for _, vv := range roleSlice {
  266. roleNames += "," + roleListMap[utils.ToInt(vv)]
  267. }
  268. if stateObj, ok := stateMap[v.State]; ok {
  269. v.StateName = stateObj.Name
  270. v.StateColor = stateObj.Color
  271. }
  272. v.RoleNames = strings.TrimLeft(roleNames, ",")
  273. v.DeptName = dept.GetDeptFullName(v.DeptID, "-")
  274. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
  275. adminList[k] = v
  276. }
  277. data := gin.H{
  278. "list": adminList,
  279. "total": total,
  280. "limit": page.PageSize,
  281. }
  282. app.Success(c, data)
  283. }
  284. func AdminExport(c *gin.Context) {
  285. wb := xlsx.NewFile()
  286. time := utils.ToStr(time.Now().UnixNano())
  287. filename := "员工信息-" + time + ".xlsx"
  288. fullPath := config.Cfg.App.ExportPath + filename
  289. sh, err := wb.AddSheet("Sheet1")
  290. if err != nil {
  291. app.Error(c, err.Error())
  292. return
  293. }
  294. sh.SetColWidth(1, 1, 20)
  295. sh.SetColWidth(2, 2, 30)
  296. sh.SetColWidth(3, 3, 20)
  297. var row *xlsx.Row
  298. var cell *xlsx.Cell
  299. rowHeader := sh.AddRow()
  300. rowHeader.SetHeight(48)
  301. cell = rowHeader.AddCell()
  302. cell.Value = "名称"
  303. cell = rowHeader.AddCell()
  304. cell.Value = "活动链接"
  305. adminList, err := admin.GetAdmins(map[string]interface{}{"deleted_at": 0}, nil, app.Page{}, nil)
  306. for _, v := range adminList {
  307. row = sh.AddRow()
  308. row.SetHeight(30)
  309. cell = row.AddCell()
  310. cell.Value = v.Username
  311. cell = row.AddCell()
  312. cell.Value = "https://jz.nczyzs.com/activity/one#?ref_id=" + utils.ToStr(v.ID)
  313. }
  314. if err := wb.Save(fullPath); err != nil {
  315. app.Error(c, err.Error())
  316. return
  317. }
  318. app.Success(c, gin.H{"path": "export/" + filename, "filename": filename})
  319. }
  320. func AdminListSelect(c *gin.Context) {
  321. type AdminList struct {
  322. ID int `json:"id"`
  323. UserName string `json:"username"`
  324. }
  325. adminList := make([]AdminList, 0)
  326. where := map[string]string{
  327. "where": "id>1",
  328. "_order_by": "id desc",
  329. }
  330. if c.Query("show_all") == "" {
  331. where["where"] = where["where"] + " AND deleted_at=0"
  332. }
  333. if c.Query("limit") != "" {
  334. page := app.HandlePageNum(c)
  335. where["_page_size"] = utils.ToStr(page.PageSize)
  336. where["_page_num"] = utils.ToStr(page.PageNum)
  337. }
  338. param := make(map[string]interface{})
  339. // site_id
  340. //if adminInfo := admin.GetAdminCache(c.GetInt("adminID")); adminInfo.SiteID > 0 {
  341. // where["where"] += " AND (site_id={{site_id}} or site_id=0)"
  342. // param["site_id"] = adminInfo.SiteID
  343. //}
  344. username := c.Query("username")
  345. if username != "" {
  346. where["where"] = where["where"] + " AND username LIKE {{username}}"
  347. param["username"] = "%" + username + "%"
  348. }
  349. adminIds := c.Query("admin_ids")
  350. if adminIds != "" {
  351. where["where"] = where["where"] + " AND id IN {{ids}}"
  352. param["ids"] = strings.Split(adminIds, ",")
  353. }
  354. roleId := c.Query("role_id")
  355. if roleId != "" {
  356. where["where"] = where["where"] + " AND FIND_IN_SET({{role_id}}, role_ids)"
  357. param["role_id"] = roleId
  358. }
  359. if _, err := admin.GetAdminsRaw(where, param, &adminList); err != nil {
  360. app.Error(c, err.Error())
  361. }
  362. app.Success(c, adminList)
  363. }
  364. func AdminDel(c *gin.Context) {
  365. id := utils.ToInt(c.Param("id"))
  366. if id <= 0 {
  367. app.ErrorMsg(c, "admin id must be a number", nil)
  368. return
  369. }
  370. err := admin.DeleteByID(id)
  371. if err != nil {
  372. app.Error(c, err.Error())
  373. return
  374. }
  375. app.Success(c, nil)
  376. }
  377. func AdminInfo(c *gin.Context) {
  378. id := utils.StrTo(c.Param("id")).MustInt()
  379. if id <= 0 {
  380. app.ErrorMsg(c, "department id must be a number", nil)
  381. return
  382. }
  383. type AdminInfo struct {
  384. ID int `json:"id"`
  385. UserName string `json:"username"`
  386. Phone string `json:"phone"`
  387. RoleIds string `json:"role_ids"`
  388. DeptID int `json:"dept_id"`
  389. SiteID int `json:"site_id"`
  390. State int `json:"state"`
  391. CreatedAt string `json:"created_at"`
  392. }
  393. var admininfo AdminInfo
  394. _, err := admin.GetInfoByID(id, nil, &admininfo)
  395. if err != nil {
  396. app.Error(c, err.Error())
  397. return
  398. }
  399. app.Success(c, admininfo)
  400. }
  401. func SalesmanAdd(c *gin.Context) {
  402. var addForm form.SalesmanAdd
  403. if app.Bind(c, &addForm) != nil {
  404. return
  405. }
  406. id, err := admin.AddSalesman(addForm)
  407. if err != nil {
  408. app.Error(c, err.Error())
  409. return
  410. }
  411. app.Success(c, gin.H{"id": id})
  412. }
  413. func Password(c *gin.Context) {
  414. var form form.Password
  415. if app.Bind(c, &form) != nil {
  416. return
  417. }
  418. if err := admin.Password(c.GetInt("adminID"), form); err != nil {
  419. app.ErrorMsg(c, err.Error(), nil)
  420. return
  421. }
  422. app.Success(c, nil)
  423. }
  424. func GetRaffleInfo(c *gin.Context) {
  425. id := utils.StrTo(c.Param("id")).MustInt()
  426. if id <= 0 {
  427. app.ErrorMsg(c, "抽奖id有误", nil)
  428. return
  429. }
  430. var raffles *raffle.Raffle
  431. db.GetModel(map[string]interface{}{
  432. "id": id,
  433. "deleted_at": 0,
  434. }, &raffles)
  435. if raffles == nil || raffles.DeletedAt != 0 {
  436. app.ErrorMsg(c, "抽奖活动不存在", nil)
  437. return
  438. }
  439. raffleItems := make([]raffle.RaffleItem, 0)
  440. db.GetModel(map[string]interface{}{
  441. "raffle_id": raffles.ID,
  442. "deleted_at": 0,
  443. }, &raffleItems)
  444. items := make([]gin.H, 0)
  445. itemMap := map[int64]raffle.RaffleItem{}
  446. for _, item := range raffleItems {
  447. items = append(items, gin.H{
  448. "id": item.ID,
  449. "name": item.Name,
  450. "picture": item.Picture,
  451. "weight": item.Weight,
  452. "winning": item.Winning,
  453. })
  454. itemMap[item.ID] = item
  455. }
  456. ret := gin.H{
  457. "state": 0,
  458. "items": items,
  459. "raffle": gin.H{
  460. "name": raffles.Name,
  461. },
  462. }
  463. app.Success(c, ret)
  464. }
  465. func WorkArrangeItemImport(c *gin.Context) {
  466. file, err := c.FormFile("file")
  467. if err != nil {
  468. app.Error(c, fmt.Sprintf("get form err: %s", err.Error()))
  469. return
  470. }
  471. fileExt := utils.FileExt(file.Filename)
  472. attachKey := utils.RandomStr() + fileExt
  473. file.Filename = "data/" + attachKey
  474. if err := c.SaveUploadedFile(file, file.Filename); err != nil {
  475. app.Error(c, fmt.Sprintf("上传文件失败%s", err.Error()))
  476. return
  477. }
  478. wb, err := xlsx.OpenFile(file.Filename)
  479. if err != nil {
  480. app.Error(c, err.Error())
  481. return
  482. }
  483. arrangeId, _ := db.ToInt64(c.PostForm("arrange_id"))
  484. if arrangeId <= 0 {
  485. app.ErrorMsg(c, "导入错误", nil)
  486. return
  487. }
  488. for _, sh := range wb.Sheet {
  489. datas := make([]map[string]interface{}, 0)
  490. headers := make([]string, 0)
  491. err := sh.ForEachRow(func(r *xlsx.Row) error {
  492. if r.GetCoordinate() <= 0 {
  493. err := r.ForEachCell(func(c *xlsx.Cell) error {
  494. headers = append(headers, c.String())
  495. return nil
  496. })
  497. if err != nil {
  498. return err
  499. }
  500. } else {
  501. var data models.WorkArrangeItem
  502. for i, header := range headers {
  503. value := r.GetCell(i).String()
  504. switch header {
  505. case "天数":
  506. data.Day, _ = db.ToInt64(value)
  507. case "时间":
  508. if value == "下午" {
  509. data.Type = 1
  510. }
  511. case "内容":
  512. data.Content = value
  513. case "培训老师":
  514. data.Teacher = value
  515. case "考核内容":
  516. data.Examine = value
  517. default:
  518. }
  519. }
  520. if data.Day < 0 || data.Content == "" && data.Teacher == "" && data.Examine == "" {
  521. return nil
  522. }
  523. var item models.WorkArrangeItem
  524. db.GetModel(map[string]interface{}{
  525. "arrange_id": arrangeId,
  526. "day": data.Day,
  527. "type": data.Type,
  528. "deleted_at": 0,
  529. }, &item)
  530. da := map[string]interface{}{
  531. "arrange_id": arrangeId,
  532. "day": data.Day,
  533. "type": data.Type,
  534. "content": data.Content,
  535. "teacher": data.Teacher,
  536. "examine": data.Examine,
  537. }
  538. if item.ID == 0 {
  539. datas = append(datas, da)
  540. } else {
  541. err := db.UpdateModel(db.Type(item), item.ID, da)
  542. if err != nil {
  543. return err
  544. }
  545. }
  546. }
  547. return nil
  548. })
  549. if err != nil {
  550. app.Error(c, err.Error())
  551. return
  552. }
  553. db.InsertModels(db.Type(models.WorkArrangeItem{}), datas)
  554. }
  555. app.Success(c, nil)
  556. }