123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652 |
- package admin
- import (
- "fmt"
- "zhiyuan/pkg/app"
- "zhiyuan/pkg/db"
- "zhiyuan/pkg/param"
- adminParam "zhiyuan/pkg/param/admin"
- "zhiyuan/pkg/utils"
- "zhiyuan/services/admin"
- "zhiyuan/services/aftersale"
- "zhiyuan/services/form"
- "zhiyuan/services/material/pkg"
- "zhiyuan/services/structs"
- "zhiyuan/services/user"
- "github.com/gin-gonic/gin"
- "github.com/tealeg/xlsx/v3"
- )
- func UserInfo(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.Error(c, "user id must be a number")
- return
- }
- auth, err := user.GetInfoByID(id, nil, nil)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, auth)
- }
- func UserList(c *gin.Context) {
- isSelect := utils.ToInt(c.Query("is_select"))
- if isSelect == 1 {
- UserListSelect(c)
- return
- }
- page := app.HandlePageNum(c)
- where := map[string]string{
- "where": "`zy_user`.`id`>0",
- "_order_by": "`zy_user`.`id` desc",
- "_group_by": "`zy_user`.`id`",
- }
- if page.PageSize != 0 {
- where["_page_size"] = utils.ToStr(page.PageSize)
- where["_page_num"] = utils.ToStr(page.PageNum)
- }
- params := make(map[string]interface{})
- // house
- filterUserIds := make([]int, 0)
- address := c.Query("address")
- if address != "" {
- if houseList, err := user.GetHouseList(map[string]interface{}{"address LIKE ": "%" + address + "%"}, []string{"address", "user_id"}, app.Page{1, 10}, nil); err == nil {
- for _, v := range houseList {
- filterUserIds = append(filterUserIds, v.UserID)
- }
- fmt.Println(houseList)
- }
- if len(filterUserIds) == 0 {
- filterUserIds = []int{-1}
- }
- where["where"] = where["where"] + " AND `zy_user`.`id` in {{filterUserIds}}"
- params["filterUserIds"] = filterUserIds
- }
- // user
- userKeyword := c.Query("user")
- if userKeyword != "" {
- where["where"] = where["where"] + " AND (`zy_user`.`name` like {{userKeyword}} OR `zy_user`.`phone` like {{userKeyword}})"
- params["userKeyword"] = userKeyword
- }
- // site_id
- //if adminInfo := admin.GetAdminCache(c.GetInt("adminID")); adminInfo.SiteID > 0 {
- // where["where"] = where["where"] + " AND `zy_user`.`site_id` = {{site_id}}"
- // params["site_id"] = adminInfo.SiteID
- //}
- if !admin.CheckAuth([]string{"user:show_all"}, c.GetInt("adminID")) {
- wheres := "`zy_user`.`created_id` = {{created_id}} OR FIND_IN_SET({{created_id}}, `zy_user`.`show_ids`)"
- if admin.CheckAuth([]string{"user:show_shop"}, c.GetInt("adminID")) && (address != "" || userKeyword != "") {
- /*type Admin struct {
- ID int `json:"id"`
- ShopIds string `json:"shop_ids"`
- }
- var my Admin
- if _, err := admin.GetOne(map[string]interface{}{
- "id": c.GetInt("adminID"),
- }, nil, &my); err != nil {
- app.Error(c, err.Error())
- return
- }
- shopids := strings.Split(my.ShopIds, ",")
- wherea := "1=2"
- paramsa := make(map[string]interface{})
- for i, shopid := range shopids {
- id, _ := db.ToInt64(shopid)
- wherea = wherea + " OR FIND_IN_SET({{shop" + db.ToString(i) + "}}, `shop_ids`)"
- paramsa["shop"+db.ToString(i)] = id
- }
- adminList := make([]Admin, 0)
- if _, err := admin.GetAdminsRaw(map[string]string{
- "where": wherea,
- }, paramsa, &adminList); err != nil {
- app.Error(c, err.Error())
- return
- }
- adminids := make([]int, 0)
- for _, v := range adminList {
- adminids = append(adminids, v.ID)
- }
- wheres = wheres + " OR `zy_user`.`created_id` in {{created_ids}}"
- params["created_ids"] = adminids*/
- wheres = wheres + " OR 1=1"
- }
- where["where"] = where["where"] + " AND (" + wheres + ")"
- params["created_id"] = c.GetInt("adminID")
- }
- state := utils.ToInt(c.Query("state"))
- if state != 0 {
- switch state {
- case 1:
- where["_having"] = "count(distinct `zy_user_house`.`id`) != 0"
- where["_order_by"] = "count(distinct `zy_user_house`.`id`) desc"
- case 2:
- where["_having"] = "count(distinct if(!ISNULL(`zy_user_house`.`id`) AND `zy_user_house`.`warranty_start` != 0, `zy_user_house`.`id`, NULL)) != 0"
- where["_order_by"] = "count(distinct if(!ISNULL(`zy_user_house`.`id`) AND `zy_user_house`.`warranty_start` != 0, `zy_user_house`.`id`, NULL)) desc"
- case 3:
- 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"
- 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"
- case 4:
- 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"
- 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"
- case 5:
- 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"
- 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"
- case 6:
- where["_having"] = "count(distinct `zy_mat_pick_order`.`id`) != 0"
- where["_order_by"] = "count(distinct `zy_mat_pick_order`.`id`) desc"
- case 7:
- where["_having"] = "count(distinct `zy_mat_bid_order`.`id`) != 0"
- where["_order_by"] = "count(distinct `zy_mat_bid_order`.`id`) desc"
- case 8:
- where["_having"] = "count(distinct `zy_as_order`.`id`) != 0"
- where["_order_by"] = "count(distinct `zy_as_order`.`id`) desc"
- case 9:
- where["_having"] = "`zy_user`.`book` = 1"
- case 10:
- where["_order_by"] = "`integral`.`num` desc"
- }
- }
- 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`"
- if _, ok := where["_having"]; ok {
- query = query + " HAVING " + where["_having"]
- }
- total, err := db.GetQueryCount(query, params, nil)
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- type UserList struct {
- ID int `json:"id"`
- Name string `json:"name"`
- NickName string `json:"nickname"`
- Phone string `json:"phone"`
- House []*structs.HouseList `json:"house"`
- HasIssue int `json:"has_issue"`
- Book int `json:"book"`
- Issue string `json:"issue"`
- SiteID int `json:"site_id"`
- SiteName string `json:"site_name"`
- CreatedAt string `json:"created_at"`
- HouseCount int `json:"house_count"`
- WarrantyHouseCount int `json:"warranty_house_count"`
- WarrantyPeriodMaterialHouseCount int `json:"warranty_period_material_house_count"`
- WarrantyPeriodBaseHouseCount int `json:"warranty_period_base_house_count"`
- WarrantyPeriodElectricHouseCount int `json:"warranty_period_electric_house_count"`
- PickOrderCount int `json:"pick_order_count"`
- BidOrderCount int `json:"bid_order_count"`
- AsOrderCount int `json:"as_order_count"`
- Integral int `json:"integral"`
- }
- userList := make([]UserList, 0)
- err = user.GetUsersRaw(where, params, &userList)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- userIds := make([]int, 0)
- for _, v := range userList {
- userIds = append(userIds, v.ID)
- }
- houseList := make([]*structs.HouseList, 0)
- houseMap := make(map[int][]*structs.HouseList, 0)
- distrctMap := make(map[int]string)
- for _, v := range param.Params.District {
- distrctMap[v.ID] = v.Name
- }
- if len(userIds) > 0 {
- typeList := make([]*structs.WarrantyType, 0)
- aftersale.GetTypeList(map[string]interface{}{"pid": 0, "`show`": 1}, nil, app.Page{}, &typeList)
- _, err = user.GetHouseList(map[string]interface{}{"user_id in": userIds, "deleted_at": 0}, nil, app.Page{}, &houseList)
- adminIds := make([]int, 0)
- adminMap := make(map[int]string)
- for _, v := range houseList {
- if v.ProjectLeader > 0 {
- adminIds = append(adminIds, v.ProjectLeader)
- }
- if v.ProjectManager > 0 {
- adminIds = append(adminIds, v.ProjectManager)
- }
- if v.Supervisor > 0 {
- adminIds = append(adminIds, v.Supervisor)
- }
- }
- if len(adminIds) > 0 {
- if adminList, err := admin.GetAdmins(map[string]interface{}{"id in": adminIds}, nil, app.Page{}, nil); err == nil {
- for _, v := range adminList {
- adminMap[v.ID] = v.Username
- }
- }
- }
- for _, v := range houseList {
- warrantyStart := utils.ToInt64(v.WarrantyStart)
- v.DistrictName = distrctMap[v.District]
- v.WarrantyStart = utils.DateS(v.WarrantyStart, "YYYY-MM-DD")
- v.ProjectStart = utils.DateS(v.ProjectStart, "YYYY-MM-DD")
- v.ProjectEnd = utils.DateS(v.ProjectEnd, "YYYY-MM-DD")
- if v.Supervisor > 0 {
- v.SupervisorName = adminMap[v.Supervisor]
- }
- if v.ProjectLeader > 0 {
- v.ProjectLeaderName = adminMap[v.ProjectLeader]
- }
- if v.ProjectManager > 0 {
- v.ProjectManagerName = adminMap[v.ProjectManager]
- }
- warrantyType := make([]*structs.WarrantyType, 0)
- utils.DeepCopy(&warrantyType, &typeList)
- if warrantyStart > 0 {
- for _, t := range warrantyType {
- user.GetWarrantyPeriod(warrantyStart, t, v)
- }
- }
- v.WarrantyType = warrantyType
- houseMap[v.UserID] = append(houseMap[v.UserID], v)
- }
- }
- siteList := utils.ParseSliceMap(adminParam.Params.DataSite, "id")
- for k, v := range userList {
- v.SiteName = utils.ToStr(siteList[utils.ToStr(v.SiteID)]["name"])
- v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD")
- if houseMap[v.ID] == nil {
- houseMap[v.ID] = make([]*structs.HouseList, 0)
- }
- v.House = houseMap[v.ID]
- userList[k] = v
- }
- data := gin.H{
- "list": userList,
- "total": total,
- "limit": page.PageSize,
- }
- app.Success(c, data)
- }
- func UserListSelect(c *gin.Context) {
- type UserList struct {
- ID int `json:"id"`
- Name string `json:"name"`
- Phone string `json:"phone"`
- }
- userList := make([]UserList, 0)
- where := map[string]interface{}{"id >": 1}
- phone := c.Query("phone")
- if phone != "" {
- where["phone like"] = "%" + phone + "%"
- }
- _, err := user.GetList(where, []string{"id", "name", "phone"}, app.Page{1, 10}, &userList)
- if err != nil {
- app.Error(c, err.Error())
- }
- app.Success(c, userList)
- }
- func UserAdd(c *gin.Context) {
- var form form.UserAdd
- if app.Bind(c, &form) != nil {
- return
- }
- form.AdminID = c.GetInt("adminID")
- id, err := user.Add(form)
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- app.Success(c, gin.H{"id": id})
- }
- func UserEdit(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.Error(c, "user id must be a number")
- return
- }
- var form form.UserAdd
- if app.Bind(c, &form) != nil {
- return
- }
- form.AdminID = c.GetInt("adminID")
- err := user.EditByID(form, id)
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- app.Success(c, nil)
- }
- func UserDel(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.Error(c, "user id must be a number")
- return
- }
- err := user.DeleteByID(id)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, nil)
- }
- func UserImport(c *gin.Context) {
- file, err := c.FormFile("file")
- if err != nil {
- app.Error(c, fmt.Sprintf("get form err: %s", err.Error()))
- return
- }
- fileExt := utils.FileExt(file.Filename)
- attachKey := utils.RandomStr() + fileExt
- file.Filename = "data/" + attachKey
- if err := c.SaveUploadedFile(file, file.Filename); err != nil {
- app.Error(c, fmt.Sprintf("上传文件失败%s", err.Error()))
- return
- }
- wb, err := xlsx.OpenFile(file.Filename)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- sh, ok := wb.Sheet["Sheet1"]
- if !ok {
- fmt.Println("Sheet does not exist")
- return
- }
- adminMap := make(map[string]int, 0)
- if adminList, _ := admin.GetAdmins(nil, nil, app.Page{}, nil); adminList != nil {
- for _, v := range adminList {
- adminMap[v.Username] = v.ID
- }
- }
- districtMap := make(map[string]int)
- for _, v := range param.Params.District {
- districtMap[v.Name] = v.ID
- }
- shopMap := map[string]int{
- "安欣家": 1,
- "昇吉家": 2,
- "德配": 3,
- "玖悦会": 4,
- }
- pkgMap := make(map[string]int)
- if pkgList, err := pkg.GetPkgs(nil, nil, app.Page{}, nil); err == nil {
- for _, v := range pkgList {
- pkgMap[v.PkgName] = v.ID
- }
- }
- siteID := 1
- sh.ForEachRow(func(r *xlsx.Row) error {
- if r.GetCoordinate() <= 1 {
- return nil
- }
- if projectStart, err := r.GetCell(21).GetTime(false); err == nil {
- projectStart = projectStart.AddDate(0, 1, 0)
- fmt.Println(utils.ToStr(utils.DateParseUnix(utils.DateT(projectStart, "YYYY-MM-DD"), "Y-m-d")))
- }
- userMap := map[string]interface{}{
- "name": r.GetCell(4).String(),
- "phone": r.GetCell(5).String(),
- "site_id": siteID,
- }
- userID, err := user.InsertOne(userMap)
- if err != nil {
- return nil
- }
- houseMap := map[string]interface{}{
- "user_id": userID,
- "province": 360000,
- "city": 360100,
- "pkg_id": pkgMap[r.GetCell(2).String()],
- "address": r.GetCell(3).String(),
- "link_name": r.GetCell(4).String(),
- "link_phone": r.GetCell(5).String(),
- "shop_id": shopMap[r.GetCell(17).String()],
- }
- if r.GetCell(1).String() != "" {
- houseMap["district"] = districtMap[r.GetCell(1).String()]
- }
- designer := r.GetCell(6).String()
- if designer != "" {
- if adminMap[designer] > 0 {
- houseMap["designer"] = adminMap[designer]
- } else {
- if adminID, err := admin.InsertOne(map[string]interface{}{"username": designer, "role_ids": 2, "deleted_at": 1}); err == nil {
- //admin.AddRoleByAdmin(utils.ToInt(adminID), []int{2})
- adminMap[designer] = utils.ToInt(adminID)
- houseMap["designer"] = utils.ToInt(adminID)
- }
- }
- }
- salesman := r.GetCell(7).String()
- if salesman != "" {
- if adminMap[salesman] > 0 {
- houseMap["salesman"] = adminMap[salesman]
- } else {
- if adminID, err := admin.InsertOne(map[string]interface{}{"username": salesman, "role_ids": 3, "deleted_at": 1}); err == nil {
- //admin.AddRoleByAdmin(utils.ToInt(adminID), []int{3})
- adminMap[salesman] = utils.ToInt(adminID)
- houseMap["salesman"] = utils.ToInt(adminID)
- }
- }
- }
- projectManager := r.GetCell(12).String()
- if projectManager != "" {
- if adminMap[projectManager] > 0 {
- houseMap["project_manager"] = adminMap[projectManager]
- } else {
- if adminID, err := admin.InsertOne(map[string]interface{}{"username": projectManager, "role_ids": 4, "deleted_at": 1}); err == nil {
- //admin.AddRoleByAdmin(utils.ToInt(adminID), []int{4})
- adminMap[projectManager] = utils.ToInt(adminID)
- houseMap["project_manager"] = utils.ToInt(adminID)
- }
- }
- }
- projectLeader := r.GetCell(13).String()
- if projectLeader != "" {
- if adminMap[projectLeader] > 0 {
- houseMap["project_leader"] = adminMap[projectLeader]
- } else {
- if adminID, err := admin.InsertOne(map[string]interface{}{"username": projectLeader, "role_ids": 5, "deleted_at": 1}); err == nil {
- //admin.AddRoleByAdmin(utils.ToInt(adminID), []int{5})
- adminMap[projectLeader] = utils.ToInt(adminID)
- houseMap["project_leader"] = utils.ToInt(adminID)
- }
- }
- }
- supervisor := r.GetCell(14).String()
- if supervisor != "" {
- if adminMap[supervisor] > 0 {
- houseMap["supervisor"] = adminMap[supervisor]
- } else {
- if adminID, err := admin.InsertOne(map[string]interface{}{"username": supervisor, "role_ids": 6, "deleted_at": 1}); err == nil {
- //admin.AddRoleByAdmin(utils.ToInt(adminID), []int{6})
- adminMap[supervisor] = utils.ToInt(adminID)
- houseMap["supervisor"] = utils.ToInt(adminID)
- }
- }
- }
- if projectStart, err := r.GetCell(15).GetTime(false); err == nil {
- houseMap["project_start"] = utils.ToStr(utils.DateParseUnix(utils.DateT(projectStart, "YYYY-MM-DD"), "Y-m-d"))
- } else {
- houseMap["project_start"] = "0"
- }
- if projectEnd, err := r.GetCell(20).GetTime(false); err == nil {
- houseMap["project_end"] = utils.ToStr(utils.DateParseUnix(utils.DateT(projectEnd, "YYYY-MM-DD"), "Y-m-d"))
- } else {
- houseMap["project_end"] = "0"
- }
- if warrantyStart, err := r.GetCell(21).GetTime(false); err == nil {
- if siteID == 2 {
- warrantyStart = warrantyStart.AddDate(0, 1, 0)
- }
- houseMap["warranty_start"] = utils.ToStr(utils.DateParseUnix(utils.DateT(warrantyStart, "YYYY-MM-DD"), "Y-m-d"))
- } else {
- houseMap["warranty_start"] = "0"
- }
- user.InsertOneHouse(houseMap)
- fmt.Println(houseMap)
- return nil
- })
- app.Success(c, nil)
- }
- func HouseInfo(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.Error(c, "house id must be a number")
- return
- }
- type House struct {
- ID int `json:"id"`
- UserID int `json:"user_id"`
- District int `json:"district"`
- Address string `json:"address"`
- PkgID int `json:"pkg_id"`
- ShopID int `json:"shop_id"`
- Designer int `json:"designer"`
- Supervisor int `json:"supervisor"`
- Salesman int `json:"salesman"`
- ProjectManager int `json:"project_manager"`
- ProjectLeader int `json:"project_leader"`
- ProjectStart string `json:"project_start"`
- ProjectEnd string `json:"project_end"`
- WarrantyStart string `json:"warranty_start"`
- WarrantyMark string `json:"warranty_mark"`
- WarrantyPeriodBase int `json:"warranty_period_base"`
- WarrantyPeriodMaterial int `json:"warranty_period_material"`
- WarrantyPeriodElectric int `json:"warranty_period_electric"`
- WarrantyPeriodBaseAddDate int `json:"warranty_period_base_adddate"`
- WarrantyPeriodMaterialAddDate int `json:"warranty_period_material_adddate"`
- WarrantyPeriodElectricAddDate int `json:"warranty_period_electric_adddate"`
- Mark string `json:"mark"`
- Pictures string `json:"pictures"`
- }
- var houseInfo *House
- _, err := user.GetHouseInfoByID(id, nil, &houseInfo)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- houseInfo.ProjectStart = utils.DateS(houseInfo.ProjectStart, "YYYY-MM-DD")
- houseInfo.ProjectEnd = utils.DateS(houseInfo.ProjectEnd, "YYYY-MM-DD")
- houseInfo.WarrantyStart = utils.DateS(houseInfo.WarrantyStart, "YYYY-MM-DD")
- app.Success(c, houseInfo)
- }
- func HouseList(c *gin.Context) {
- userID := utils.StrTo(c.Query("user_id")).MustInt()
- if userID <= 0 {
- app.Error(c, "user id must be a number")
- return
- }
- houseList := make([]*structs.HouseList, 0)
- distrctMap := make(map[int]string)
- for _, v := range param.Params.District {
- distrctMap[v.ID] = v.Name
- }
- if _, err := user.GetHouseList(map[string]interface{}{"user_id": userID, "deleted_at": 0}, nil, app.Page{}, &houseList); err != nil {
- app.Error(c, err.Error())
- return
- }
- typeList := make([]*structs.WarrantyType, 0)
- aftersale.GetTypeList(map[string]interface{}{"pid": 0, "`show`": 1}, nil, app.Page{}, &typeList)
- for k, v := range houseList {
- warrantyStart := utils.ToInt64(v.WarrantyStart)
- v.DistrictName = distrctMap[v.District]
- v.WarrantyStart = utils.DateS(v.WarrantyStart, "YYYY-MM-DD")
- v.ProjectStart = utils.DateS(v.ProjectStart, "YYYY-MM-DD")
- v.ProjectEnd = utils.DateS(v.ProjectEnd, "YYYY-MM-DD")
- v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD")
- warrantyType := make([]*structs.WarrantyType, 0)
- utils.DeepCopy(&warrantyType, &typeList)
- if warrantyStart > 0 {
- for _, t := range warrantyType {
- user.GetWarrantyPeriod(warrantyStart, t, v)
- }
- }
- v.WarrantyType = warrantyType
- houseList[k] = v
- }
- type User struct {
- ID int `json:"id"`
- Name string `json:"name"`
- Phone string `json:"phone"`
- }
- userInfo := &User{}
- if _, err := user.GetOne(map[string]interface{}{"id": userID}, nil, &userInfo); err != nil {
- app.Error(c, err.Error())
- return
- }
- data := gin.H{
- "list": houseList,
- "user_info": userInfo,
- }
- app.Success(c, data)
- }
- func HouseAdd(c *gin.Context) {
- var form form.HouseAdd
- if app.Bind(c, &form) != nil {
- return
- }
- form.AdminID = c.GetInt("adminID")
- houseID, err := user.HouseAdd(form)
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- app.Success(c, gin.H{"house_id": houseID})
- }
- func HouseEdit(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.Error(c, "house id must be a number")
- return
- }
- var form form.HouseAdd
- if app.Bind(c, &form) != nil {
- return
- }
- form.AdminID = c.GetInt("adminID")
- if _, err := user.HouseEdit(form, id); err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- app.Success(c, nil)
- }
- func HouseDel(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.Error(c, "house id must be a number")
- return
- }
- if _, err := user.HouseDelByID(id); err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, nil)
- }
|