user.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. package admin
  2. import (
  3. "fmt"
  4. "zhiyuan/pkg/app"
  5. "zhiyuan/pkg/db"
  6. "zhiyuan/pkg/param"
  7. adminParam "zhiyuan/pkg/param/admin"
  8. "zhiyuan/pkg/utils"
  9. "zhiyuan/services/admin"
  10. "zhiyuan/services/aftersale"
  11. "zhiyuan/services/form"
  12. "zhiyuan/services/material/pkg"
  13. "zhiyuan/services/structs"
  14. "zhiyuan/services/user"
  15. "github.com/gin-gonic/gin"
  16. "github.com/tealeg/xlsx/v3"
  17. )
  18. func UserInfo(c *gin.Context) {
  19. id := utils.StrTo(c.Param("id")).MustInt()
  20. if id <= 0 {
  21. app.Error(c, "user id must be a number")
  22. return
  23. }
  24. auth, err := user.GetInfoByID(id, nil, nil)
  25. if err != nil {
  26. app.Error(c, err.Error())
  27. return
  28. }
  29. app.Success(c, auth)
  30. }
  31. func UserList(c *gin.Context) {
  32. isSelect := utils.ToInt(c.Query("is_select"))
  33. if isSelect == 1 {
  34. UserListSelect(c)
  35. return
  36. }
  37. page := app.HandlePageNum(c)
  38. where := map[string]string{
  39. "where": "`zy_user`.`id`>0",
  40. "_order_by": "`zy_user`.`id` desc",
  41. "_group_by": "`zy_user`.`id`",
  42. }
  43. if page.PageSize != 0 {
  44. where["_page_size"] = utils.ToStr(page.PageSize)
  45. where["_page_num"] = utils.ToStr(page.PageNum)
  46. }
  47. params := make(map[string]interface{})
  48. // house
  49. filterUserIds := make([]int, 0)
  50. address := c.Query("address")
  51. if address != "" {
  52. if houseList, err := user.GetHouseList(map[string]interface{}{"address LIKE ": "%" + address + "%"}, []string{"address", "user_id"}, app.Page{1, 10}, nil); err == nil {
  53. for _, v := range houseList {
  54. filterUserIds = append(filterUserIds, v.UserID)
  55. }
  56. fmt.Println(houseList)
  57. }
  58. if len(filterUserIds) == 0 {
  59. filterUserIds = []int{-1}
  60. }
  61. where["where"] = where["where"] + " AND `zy_user`.`id` in {{filterUserIds}}"
  62. params["filterUserIds"] = filterUserIds
  63. }
  64. // user
  65. userKeyword := c.Query("user")
  66. if userKeyword != "" {
  67. where["where"] = where["where"] + " AND (`zy_user`.`name` like {{userKeyword}} OR `zy_user`.`phone` like {{userKeyword}})"
  68. params["userKeyword"] = userKeyword
  69. }
  70. // site_id
  71. //if adminInfo := admin.GetAdminCache(c.GetInt("adminID")); adminInfo.SiteID > 0 {
  72. // where["where"] = where["where"] + " AND `zy_user`.`site_id` = {{site_id}}"
  73. // params["site_id"] = adminInfo.SiteID
  74. //}
  75. if !admin.CheckAuth([]string{"user:show_all"}, c.GetInt("adminID")) {
  76. wheres := "`zy_user`.`created_id` = {{created_id}} OR FIND_IN_SET({{created_id}}, `zy_user`.`show_ids`)"
  77. if admin.CheckAuth([]string{"user:show_shop"}, c.GetInt("adminID")) && (address != "" || userKeyword != "") {
  78. /*type Admin struct {
  79. ID int `json:"id"`
  80. ShopIds string `json:"shop_ids"`
  81. }
  82. var my Admin
  83. if _, err := admin.GetOne(map[string]interface{}{
  84. "id": c.GetInt("adminID"),
  85. }, nil, &my); err != nil {
  86. app.Error(c, err.Error())
  87. return
  88. }
  89. shopids := strings.Split(my.ShopIds, ",")
  90. wherea := "1=2"
  91. paramsa := make(map[string]interface{})
  92. for i, shopid := range shopids {
  93. id, _ := db.ToInt64(shopid)
  94. wherea = wherea + " OR FIND_IN_SET({{shop" + db.ToString(i) + "}}, `shop_ids`)"
  95. paramsa["shop"+db.ToString(i)] = id
  96. }
  97. adminList := make([]Admin, 0)
  98. if _, err := admin.GetAdminsRaw(map[string]string{
  99. "where": wherea,
  100. }, paramsa, &adminList); err != nil {
  101. app.Error(c, err.Error())
  102. return
  103. }
  104. adminids := make([]int, 0)
  105. for _, v := range adminList {
  106. adminids = append(adminids, v.ID)
  107. }
  108. wheres = wheres + " OR `zy_user`.`created_id` in {{created_ids}}"
  109. params["created_ids"] = adminids*/
  110. wheres = wheres + " OR 1=1"
  111. }
  112. where["where"] = where["where"] + " AND (" + wheres + ")"
  113. params["created_id"] = c.GetInt("adminID")
  114. }
  115. state := utils.ToInt(c.Query("state"))
  116. if state != 0 {
  117. switch state {
  118. case 1:
  119. where["_having"] = "count(distinct `zy_user_house`.`id`) != 0"
  120. where["_order_by"] = "count(distinct `zy_user_house`.`id`) desc"
  121. case 2:
  122. where["_having"] = "count(distinct if(!ISNULL(`zy_user_house`.`id`) AND `zy_user_house`.`warranty_start` != 0, `zy_user_house`.`id`, NULL)) != 0"
  123. where["_order_by"] = "count(distinct if(!ISNULL(`zy_user_house`.`id`) AND `zy_user_house`.`warranty_start` != 0, `zy_user_house`.`id`, NULL)) desc"
  124. case 3:
  125. where["_having"] = "count(distinct if(!ISNULL(`zy_user_house`.`id`) AND `zy_user_house`.`warranty_start` != 0 AND date_add(from_unixtime(`zy_user_house`.`warranty_start`), interval `zy_user_house`.`warranty_period_material` year) >= now(), `zy_user_house`.`id`, NULL)) != 0"
  126. where["_order_by"] = "count(distinct if(!ISNULL(`zy_user_house`.`id`) AND `zy_user_house`.`warranty_start` != 0 AND date_add(from_unixtime(`zy_user_house`.`warranty_start`), interval `zy_user_house`.`warranty_period_material` year) >= now(), `zy_user_house`.`id`, NULL)) desc"
  127. case 4:
  128. where["_having"] = "count(distinct if(!ISNULL(`zy_user_house`.`id`) AND `zy_user_house`.`warranty_start` != 0 AND date_add(from_unixtime(`zy_user_house`.`warranty_start`), interval `zy_user_house`.`warranty_period_base` year) >= now(), `zy_user_house`.`id`, NULL)) != 0"
  129. where["_order_by"] = "count(distinct if(!ISNULL(`zy_user_house`.`id`) AND `zy_user_house`.`warranty_start` != 0 AND date_add(from_unixtime(`zy_user_house`.`warranty_start`), interval `zy_user_house`.`warranty_period_base` year) >= now(), `zy_user_house`.`id`, NULL)) desc"
  130. case 5:
  131. where["_having"] = "count(distinct if(!ISNULL(`zy_user_house`.`id`) AND `zy_user_house`.`warranty_start` != 0 AND date_add(from_unixtime(`zy_user_house`.`warranty_start`), interval `zy_user_house`.`warranty_period_electric` year) >= now(), `zy_user_house`.`id`, NULL)) != 0"
  132. where["_order_by"] = "count(distinct if(!ISNULL(`zy_user_house`.`id`) AND `zy_user_house`.`warranty_start` != 0 AND date_add(from_unixtime(`zy_user_house`.`warranty_start`), interval `zy_user_house`.`warranty_period_electric` year) >= now(), `zy_user_house`.`id`, NULL)) desc"
  133. case 6:
  134. where["_having"] = "count(distinct `zy_mat_pick_order`.`id`) != 0"
  135. where["_order_by"] = "count(distinct `zy_mat_pick_order`.`id`) desc"
  136. case 7:
  137. where["_having"] = "count(distinct `zy_mat_bid_order`.`id`) != 0"
  138. where["_order_by"] = "count(distinct `zy_mat_bid_order`.`id`) desc"
  139. case 8:
  140. where["_having"] = "count(distinct `zy_as_order`.`id`) != 0"
  141. where["_order_by"] = "count(distinct `zy_as_order`.`id`) desc"
  142. case 9:
  143. where["_having"] = "`zy_user`.`book` = 1"
  144. case 10:
  145. where["_order_by"] = "`integral`.`num` desc"
  146. }
  147. }
  148. query := "SELECT `zy_user`.* from `zy_user` left join `zy_user_house` on `zy_user_house`.`user_id` = `zy_user`.`id` AND `zy_user_house`.`deleted_at` = 0 left join `zy_mat_pick_order` on `zy_mat_pick_order`.`customer_phone` = `zy_user`.`phone` left join `zy_mat_bid_order` on `zy_mat_bid_order`.`customer_phone` = `zy_user`.`phone` AND `zy_mat_bid_order`.`deleted_at` = 0 left join `zy_as_order` on `zy_as_order`.`user_id` = `zy_user`.`id` AND `zy_as_order`.`deleted_at` = 0 WHERE " + where["where"] + " GROUP BY `zy_user`.`id`"
  149. if _, ok := where["_having"]; ok {
  150. query = query + " HAVING " + where["_having"]
  151. }
  152. total, err := db.GetQueryCount(query, params, nil)
  153. if err != nil {
  154. app.ErrorMsg(c, err.Error(), nil)
  155. return
  156. }
  157. type UserList struct {
  158. ID int `json:"id"`
  159. Name string `json:"name"`
  160. NickName string `json:"nickname"`
  161. Phone string `json:"phone"`
  162. House []*structs.HouseList `json:"house"`
  163. HasIssue int `json:"has_issue"`
  164. Book int `json:"book"`
  165. Issue string `json:"issue"`
  166. SiteID int `json:"site_id"`
  167. SiteName string `json:"site_name"`
  168. CreatedAt string `json:"created_at"`
  169. HouseCount int `json:"house_count"`
  170. WarrantyHouseCount int `json:"warranty_house_count"`
  171. WarrantyPeriodMaterialHouseCount int `json:"warranty_period_material_house_count"`
  172. WarrantyPeriodBaseHouseCount int `json:"warranty_period_base_house_count"`
  173. WarrantyPeriodElectricHouseCount int `json:"warranty_period_electric_house_count"`
  174. PickOrderCount int `json:"pick_order_count"`
  175. BidOrderCount int `json:"bid_order_count"`
  176. AsOrderCount int `json:"as_order_count"`
  177. Integral int `json:"integral"`
  178. }
  179. userList := make([]UserList, 0)
  180. err = user.GetUsersRaw(where, params, &userList)
  181. if err != nil {
  182. app.Error(c, err.Error())
  183. return
  184. }
  185. userIds := make([]int, 0)
  186. for _, v := range userList {
  187. userIds = append(userIds, v.ID)
  188. }
  189. houseList := make([]*structs.HouseList, 0)
  190. houseMap := make(map[int][]*structs.HouseList, 0)
  191. distrctMap := make(map[int]string)
  192. for _, v := range param.Params.District {
  193. distrctMap[v.ID] = v.Name
  194. }
  195. if len(userIds) > 0 {
  196. typeList := make([]*structs.WarrantyType, 0)
  197. aftersale.GetTypeList(map[string]interface{}{"pid": 0, "`show`": 1}, nil, app.Page{}, &typeList)
  198. _, err = user.GetHouseList(map[string]interface{}{"user_id in": userIds, "deleted_at": 0}, nil, app.Page{}, &houseList)
  199. adminIds := make([]int, 0)
  200. adminMap := make(map[int]string)
  201. for _, v := range houseList {
  202. if v.ProjectLeader > 0 {
  203. adminIds = append(adminIds, v.ProjectLeader)
  204. }
  205. if v.ProjectManager > 0 {
  206. adminIds = append(adminIds, v.ProjectManager)
  207. }
  208. if v.Supervisor > 0 {
  209. adminIds = append(adminIds, v.Supervisor)
  210. }
  211. }
  212. if len(adminIds) > 0 {
  213. if adminList, err := admin.GetAdmins(map[string]interface{}{"id in": adminIds}, nil, app.Page{}, nil); err == nil {
  214. for _, v := range adminList {
  215. adminMap[v.ID] = v.Username
  216. }
  217. }
  218. }
  219. for _, v := range houseList {
  220. warrantyStart := utils.ToInt64(v.WarrantyStart)
  221. v.DistrictName = distrctMap[v.District]
  222. v.WarrantyStart = utils.DateS(v.WarrantyStart, "YYYY-MM-DD")
  223. v.ProjectStart = utils.DateS(v.ProjectStart, "YYYY-MM-DD")
  224. v.ProjectEnd = utils.DateS(v.ProjectEnd, "YYYY-MM-DD")
  225. if v.Supervisor > 0 {
  226. v.SupervisorName = adminMap[v.Supervisor]
  227. }
  228. if v.ProjectLeader > 0 {
  229. v.ProjectLeaderName = adminMap[v.ProjectLeader]
  230. }
  231. if v.ProjectManager > 0 {
  232. v.ProjectManagerName = adminMap[v.ProjectManager]
  233. }
  234. warrantyType := make([]*structs.WarrantyType, 0)
  235. utils.DeepCopy(&warrantyType, &typeList)
  236. if warrantyStart > 0 {
  237. for _, t := range warrantyType {
  238. user.GetWarrantyPeriod(warrantyStart, t, v)
  239. }
  240. }
  241. v.WarrantyType = warrantyType
  242. houseMap[v.UserID] = append(houseMap[v.UserID], v)
  243. }
  244. }
  245. siteList := utils.ParseSliceMap(adminParam.Params.DataSite, "id")
  246. for k, v := range userList {
  247. v.SiteName = utils.ToStr(siteList[utils.ToStr(v.SiteID)]["name"])
  248. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD")
  249. if houseMap[v.ID] == nil {
  250. houseMap[v.ID] = make([]*structs.HouseList, 0)
  251. }
  252. v.House = houseMap[v.ID]
  253. userList[k] = v
  254. }
  255. data := gin.H{
  256. "list": userList,
  257. "total": total,
  258. "limit": page.PageSize,
  259. }
  260. app.Success(c, data)
  261. }
  262. func UserListSelect(c *gin.Context) {
  263. type UserList struct {
  264. ID int `json:"id"`
  265. Name string `json:"name"`
  266. Phone string `json:"phone"`
  267. }
  268. userList := make([]UserList, 0)
  269. where := map[string]interface{}{"id >": 1}
  270. phone := c.Query("phone")
  271. if phone != "" {
  272. where["phone like"] = "%" + phone + "%"
  273. }
  274. _, err := user.GetList(where, []string{"id", "name", "phone"}, app.Page{1, 10}, &userList)
  275. if err != nil {
  276. app.Error(c, err.Error())
  277. }
  278. app.Success(c, userList)
  279. }
  280. func UserAdd(c *gin.Context) {
  281. var form form.UserAdd
  282. if app.Bind(c, &form) != nil {
  283. return
  284. }
  285. form.AdminID = c.GetInt("adminID")
  286. id, err := user.Add(form)
  287. if err != nil {
  288. app.ErrorMsg(c, err.Error(), nil)
  289. return
  290. }
  291. app.Success(c, gin.H{"id": id})
  292. }
  293. func UserEdit(c *gin.Context) {
  294. id := utils.StrTo(c.Param("id")).MustInt()
  295. if id <= 0 {
  296. app.Error(c, "user id must be a number")
  297. return
  298. }
  299. var form form.UserAdd
  300. if app.Bind(c, &form) != nil {
  301. return
  302. }
  303. form.AdminID = c.GetInt("adminID")
  304. err := user.EditByID(form, id)
  305. if err != nil {
  306. app.ErrorMsg(c, err.Error(), nil)
  307. return
  308. }
  309. app.Success(c, nil)
  310. }
  311. func UserDel(c *gin.Context) {
  312. id := utils.StrTo(c.Param("id")).MustInt()
  313. if id <= 0 {
  314. app.Error(c, "user id must be a number")
  315. return
  316. }
  317. err := user.DeleteByID(id)
  318. if err != nil {
  319. app.Error(c, err.Error())
  320. return
  321. }
  322. app.Success(c, nil)
  323. }
  324. func UserImport(c *gin.Context) {
  325. file, err := c.FormFile("file")
  326. if err != nil {
  327. app.Error(c, fmt.Sprintf("get form err: %s", err.Error()))
  328. return
  329. }
  330. fileExt := utils.FileExt(file.Filename)
  331. attachKey := utils.RandomStr() + fileExt
  332. file.Filename = "data/" + attachKey
  333. if err := c.SaveUploadedFile(file, file.Filename); err != nil {
  334. app.Error(c, fmt.Sprintf("上传文件失败%s", err.Error()))
  335. return
  336. }
  337. wb, err := xlsx.OpenFile(file.Filename)
  338. if err != nil {
  339. app.Error(c, err.Error())
  340. return
  341. }
  342. sh, ok := wb.Sheet["Sheet1"]
  343. if !ok {
  344. fmt.Println("Sheet does not exist")
  345. return
  346. }
  347. adminMap := make(map[string]int, 0)
  348. if adminList, _ := admin.GetAdmins(nil, nil, app.Page{}, nil); adminList != nil {
  349. for _, v := range adminList {
  350. adminMap[v.Username] = v.ID
  351. }
  352. }
  353. districtMap := make(map[string]int)
  354. for _, v := range param.Params.District {
  355. districtMap[v.Name] = v.ID
  356. }
  357. shopMap := map[string]int{
  358. "安欣家": 1,
  359. "昇吉家": 2,
  360. "德配": 3,
  361. "玖悦会": 4,
  362. }
  363. pkgMap := make(map[string]int)
  364. if pkgList, err := pkg.GetPkgs(nil, nil, app.Page{}, nil); err == nil {
  365. for _, v := range pkgList {
  366. pkgMap[v.PkgName] = v.ID
  367. }
  368. }
  369. siteID := 1
  370. sh.ForEachRow(func(r *xlsx.Row) error {
  371. if r.GetCoordinate() <= 1 {
  372. return nil
  373. }
  374. if projectStart, err := r.GetCell(21).GetTime(false); err == nil {
  375. projectStart = projectStart.AddDate(0, 1, 0)
  376. fmt.Println(utils.ToStr(utils.DateParseUnix(utils.DateT(projectStart, "YYYY-MM-DD"), "Y-m-d")))
  377. }
  378. userMap := map[string]interface{}{
  379. "name": r.GetCell(4).String(),
  380. "phone": r.GetCell(5).String(),
  381. "site_id": siteID,
  382. }
  383. userID, err := user.InsertOne(userMap)
  384. if err != nil {
  385. return nil
  386. }
  387. houseMap := map[string]interface{}{
  388. "user_id": userID,
  389. "province": 360000,
  390. "city": 360100,
  391. "pkg_id": pkgMap[r.GetCell(2).String()],
  392. "address": r.GetCell(3).String(),
  393. "link_name": r.GetCell(4).String(),
  394. "link_phone": r.GetCell(5).String(),
  395. "shop_id": shopMap[r.GetCell(17).String()],
  396. }
  397. if r.GetCell(1).String() != "" {
  398. houseMap["district"] = districtMap[r.GetCell(1).String()]
  399. }
  400. designer := r.GetCell(6).String()
  401. if designer != "" {
  402. if adminMap[designer] > 0 {
  403. houseMap["designer"] = adminMap[designer]
  404. } else {
  405. if adminID, err := admin.InsertOne(map[string]interface{}{"username": designer, "role_ids": 2, "deleted_at": 1}); err == nil {
  406. //admin.AddRoleByAdmin(utils.ToInt(adminID), []int{2})
  407. adminMap[designer] = utils.ToInt(adminID)
  408. houseMap["designer"] = utils.ToInt(adminID)
  409. }
  410. }
  411. }
  412. salesman := r.GetCell(7).String()
  413. if salesman != "" {
  414. if adminMap[salesman] > 0 {
  415. houseMap["salesman"] = adminMap[salesman]
  416. } else {
  417. if adminID, err := admin.InsertOne(map[string]interface{}{"username": salesman, "role_ids": 3, "deleted_at": 1}); err == nil {
  418. //admin.AddRoleByAdmin(utils.ToInt(adminID), []int{3})
  419. adminMap[salesman] = utils.ToInt(adminID)
  420. houseMap["salesman"] = utils.ToInt(adminID)
  421. }
  422. }
  423. }
  424. projectManager := r.GetCell(12).String()
  425. if projectManager != "" {
  426. if adminMap[projectManager] > 0 {
  427. houseMap["project_manager"] = adminMap[projectManager]
  428. } else {
  429. if adminID, err := admin.InsertOne(map[string]interface{}{"username": projectManager, "role_ids": 4, "deleted_at": 1}); err == nil {
  430. //admin.AddRoleByAdmin(utils.ToInt(adminID), []int{4})
  431. adminMap[projectManager] = utils.ToInt(adminID)
  432. houseMap["project_manager"] = utils.ToInt(adminID)
  433. }
  434. }
  435. }
  436. projectLeader := r.GetCell(13).String()
  437. if projectLeader != "" {
  438. if adminMap[projectLeader] > 0 {
  439. houseMap["project_leader"] = adminMap[projectLeader]
  440. } else {
  441. if adminID, err := admin.InsertOne(map[string]interface{}{"username": projectLeader, "role_ids": 5, "deleted_at": 1}); err == nil {
  442. //admin.AddRoleByAdmin(utils.ToInt(adminID), []int{5})
  443. adminMap[projectLeader] = utils.ToInt(adminID)
  444. houseMap["project_leader"] = utils.ToInt(adminID)
  445. }
  446. }
  447. }
  448. supervisor := r.GetCell(14).String()
  449. if supervisor != "" {
  450. if adminMap[supervisor] > 0 {
  451. houseMap["supervisor"] = adminMap[supervisor]
  452. } else {
  453. if adminID, err := admin.InsertOne(map[string]interface{}{"username": supervisor, "role_ids": 6, "deleted_at": 1}); err == nil {
  454. //admin.AddRoleByAdmin(utils.ToInt(adminID), []int{6})
  455. adminMap[supervisor] = utils.ToInt(adminID)
  456. houseMap["supervisor"] = utils.ToInt(adminID)
  457. }
  458. }
  459. }
  460. if projectStart, err := r.GetCell(15).GetTime(false); err == nil {
  461. houseMap["project_start"] = utils.ToStr(utils.DateParseUnix(utils.DateT(projectStart, "YYYY-MM-DD"), "Y-m-d"))
  462. } else {
  463. houseMap["project_start"] = "0"
  464. }
  465. if projectEnd, err := r.GetCell(20).GetTime(false); err == nil {
  466. houseMap["project_end"] = utils.ToStr(utils.DateParseUnix(utils.DateT(projectEnd, "YYYY-MM-DD"), "Y-m-d"))
  467. } else {
  468. houseMap["project_end"] = "0"
  469. }
  470. if warrantyStart, err := r.GetCell(21).GetTime(false); err == nil {
  471. if siteID == 2 {
  472. warrantyStart = warrantyStart.AddDate(0, 1, 0)
  473. }
  474. houseMap["warranty_start"] = utils.ToStr(utils.DateParseUnix(utils.DateT(warrantyStart, "YYYY-MM-DD"), "Y-m-d"))
  475. } else {
  476. houseMap["warranty_start"] = "0"
  477. }
  478. user.InsertOneHouse(houseMap)
  479. fmt.Println(houseMap)
  480. return nil
  481. })
  482. app.Success(c, nil)
  483. }
  484. func HouseInfo(c *gin.Context) {
  485. id := utils.StrTo(c.Param("id")).MustInt()
  486. if id <= 0 {
  487. app.Error(c, "house id must be a number")
  488. return
  489. }
  490. type House struct {
  491. ID int `json:"id"`
  492. UserID int `json:"user_id"`
  493. District int `json:"district"`
  494. Address string `json:"address"`
  495. PkgID int `json:"pkg_id"`
  496. ShopID int `json:"shop_id"`
  497. Designer int `json:"designer"`
  498. Supervisor int `json:"supervisor"`
  499. Salesman int `json:"salesman"`
  500. ProjectManager int `json:"project_manager"`
  501. ProjectLeader int `json:"project_leader"`
  502. ProjectStart string `json:"project_start"`
  503. ProjectEnd string `json:"project_end"`
  504. WarrantyStart string `json:"warranty_start"`
  505. WarrantyMark string `json:"warranty_mark"`
  506. WarrantyPeriodBase int `json:"warranty_period_base"`
  507. WarrantyPeriodMaterial int `json:"warranty_period_material"`
  508. WarrantyPeriodElectric int `json:"warranty_period_electric"`
  509. WarrantyPeriodBaseAddDate int `json:"warranty_period_base_adddate"`
  510. WarrantyPeriodMaterialAddDate int `json:"warranty_period_material_adddate"`
  511. WarrantyPeriodElectricAddDate int `json:"warranty_period_electric_adddate"`
  512. Mark string `json:"mark"`
  513. Pictures string `json:"pictures"`
  514. }
  515. var houseInfo *House
  516. _, err := user.GetHouseInfoByID(id, nil, &houseInfo)
  517. if err != nil {
  518. app.Error(c, err.Error())
  519. return
  520. }
  521. houseInfo.ProjectStart = utils.DateS(houseInfo.ProjectStart, "YYYY-MM-DD")
  522. houseInfo.ProjectEnd = utils.DateS(houseInfo.ProjectEnd, "YYYY-MM-DD")
  523. houseInfo.WarrantyStart = utils.DateS(houseInfo.WarrantyStart, "YYYY-MM-DD")
  524. app.Success(c, houseInfo)
  525. }
  526. func HouseList(c *gin.Context) {
  527. userID := utils.StrTo(c.Query("user_id")).MustInt()
  528. if userID <= 0 {
  529. app.Error(c, "user id must be a number")
  530. return
  531. }
  532. houseList := make([]*structs.HouseList, 0)
  533. distrctMap := make(map[int]string)
  534. for _, v := range param.Params.District {
  535. distrctMap[v.ID] = v.Name
  536. }
  537. if _, err := user.GetHouseList(map[string]interface{}{"user_id": userID, "deleted_at": 0}, nil, app.Page{}, &houseList); err != nil {
  538. app.Error(c, err.Error())
  539. return
  540. }
  541. typeList := make([]*structs.WarrantyType, 0)
  542. aftersale.GetTypeList(map[string]interface{}{"pid": 0, "`show`": 1}, nil, app.Page{}, &typeList)
  543. for k, v := range houseList {
  544. warrantyStart := utils.ToInt64(v.WarrantyStart)
  545. v.DistrictName = distrctMap[v.District]
  546. v.WarrantyStart = utils.DateS(v.WarrantyStart, "YYYY-MM-DD")
  547. v.ProjectStart = utils.DateS(v.ProjectStart, "YYYY-MM-DD")
  548. v.ProjectEnd = utils.DateS(v.ProjectEnd, "YYYY-MM-DD")
  549. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD")
  550. warrantyType := make([]*structs.WarrantyType, 0)
  551. utils.DeepCopy(&warrantyType, &typeList)
  552. if warrantyStart > 0 {
  553. for _, t := range warrantyType {
  554. user.GetWarrantyPeriod(warrantyStart, t, v)
  555. }
  556. }
  557. v.WarrantyType = warrantyType
  558. houseList[k] = v
  559. }
  560. type User struct {
  561. ID int `json:"id"`
  562. Name string `json:"name"`
  563. Phone string `json:"phone"`
  564. }
  565. userInfo := &User{}
  566. if _, err := user.GetOne(map[string]interface{}{"id": userID}, nil, &userInfo); err != nil {
  567. app.Error(c, err.Error())
  568. return
  569. }
  570. data := gin.H{
  571. "list": houseList,
  572. "user_info": userInfo,
  573. }
  574. app.Success(c, data)
  575. }
  576. func HouseAdd(c *gin.Context) {
  577. var form form.HouseAdd
  578. if app.Bind(c, &form) != nil {
  579. return
  580. }
  581. form.AdminID = c.GetInt("adminID")
  582. houseID, err := user.HouseAdd(form)
  583. if err != nil {
  584. app.ErrorMsg(c, err.Error(), nil)
  585. return
  586. }
  587. app.Success(c, gin.H{"house_id": houseID})
  588. }
  589. func HouseEdit(c *gin.Context) {
  590. id := utils.StrTo(c.Param("id")).MustInt()
  591. if id <= 0 {
  592. app.Error(c, "house id must be a number")
  593. return
  594. }
  595. var form form.HouseAdd
  596. if app.Bind(c, &form) != nil {
  597. return
  598. }
  599. form.AdminID = c.GetInt("adminID")
  600. if _, err := user.HouseEdit(form, id); err != nil {
  601. app.ErrorMsg(c, err.Error(), nil)
  602. return
  603. }
  604. app.Success(c, nil)
  605. }
  606. func HouseDel(c *gin.Context) {
  607. id := utils.StrTo(c.Param("id")).MustInt()
  608. if id <= 0 {
  609. app.Error(c, "house id must be a number")
  610. return
  611. }
  612. if _, err := user.HouseDelByID(id); err != nil {
  613. app.Error(c, err.Error())
  614. return
  615. }
  616. app.Success(c, nil)
  617. }