package customer import ( "time" "zhiyuan/models" "zhiyuan/models/budget" "zhiyuan/pkg/app" "zhiyuan/pkg/config" "zhiyuan/pkg/db" "zhiyuan/pkg/utils" "zhiyuan/services/form" "zhiyuan/services/user" "zhiyuan/services/user/check" "zhiyuan/services/user/checks" "github.com/gin-gonic/gin" ) func Login(c *gin.Context) { var form form.UserLogin if app.Bind(c, &form) != nil { return } token, err := user.Login(form) if err != nil { app.Error(c, err.Error()) return } app.Success(c, map[string]string{ "token": token, }) } func Logout(c *gin.Context) { err := user.Logout(c.GetInt("userID")) if err != nil { app.ErrorMsg(c, err.Error(), nil) return } app.Success(c, nil) } func Info(c *gin.Context) { type House struct { ID int `json:"id"` Address string `json:"address"` Province int `json:"province"` City int `json:"city"` Region int `json:"region"` RegionName string `json:"region_name"` LinkName string `json:"link_name"` LinkPhone string `json:"link_phone"` WarrantyStart string `json:"warranty_start"` } type SettingInfo struct { ImgHost string `json:"img_host"` ServiceTel string `json:"service_tel"` PageSize int `json:"page_size"` } type UserInfo struct { ID int `json:"id"` Name string `json:"name"` NickName string `json:"nickname"` Phone string `json:"phone"` HeadImgUrl string `json:"headimgurl"` Book int `json:"book"` House []*House `json:"house"` Setting SettingInfo `json:"setting"` Integral int `json:"integral"` } var userInfo UserInfo _, err := user.GetInfoByID(c.GetInt("userID"), nil, &userInfo) if err != nil { app.ErrorMsg(c, err.Error(), nil) return } houseList := make([]*House, 0) if _, err = user.GetHouseList(map[string]interface{}{"user_id": c.GetInt("userID"), "deleted_at": 0}, nil, app.Page{}, &houseList); err != nil { app.ErrorMsg(c, err.Error(), nil) return } for k, v := range houseList { v.WarrantyStart = utils.DateS(v.WarrantyStart, "YYYY-MM-DD") houseList[k] = v } userInfo.House = houseList userInfo.Setting = SettingInfo{ ImgHost: config.Cfg.App.ImgHost, ServiceTel: config.Cfg.App.ServiceTel, PageSize: config.Cfg.App.PageLimitMin, } var integral models.UserIntegralStatist db.GetModel(map[string]interface{}{ "user_id": userInfo.ID, }, &integral) userInfo.Integral = int(integral.Integral) app.Success(c, userInfo) } func InfoEdit(c *gin.Context) { var form user.EditInfoForm if app.Bind(c, &form) != nil { return } err := user.EditInfoByID(form, c.GetInt("userID")) if err != nil { app.ErrorMsg(c, err.Error(), nil) return } app.Success(c, nil) } func UnBindWeixin(c *gin.Context) { if err := user.UnBindWeixin(c.GetInt("userID")); err != nil { app.ErrorMsg(c, err.Error(), nil) return } app.Success(c, nil) } func CheckBook(c *gin.Context) { userMap := map[string]interface{}{ "book": 1, } if _, err := db.Update(models.User{}.TableName(), map[string]interface{}{"id": c.GetInt("userID")}, userMap); err != nil { app.ErrorMsg(c, err.Error(), nil) } app.Success(c, nil) } func CheckCourse(c *gin.Context) { id, err := check.CheckCourse(c.GetInt("userID")) if err != nil { app.Error(c, err.Error()) return } app.Success(c, gin.H{"id": id}) } func CheckList(c *gin.Context) { page := app.HandlePageNums(c) where := map[string]string{ "where": "`zy_user_check`.`id`>0 AND `zy_user_check`.`state` < 2", "_group_by": "`zy_user_check`.`id`", "_order_by": "`zy_user_check`.`id` desc", } if page.PageSize != 0 { where["_page_size"] = utils.ToStr(page.PageSize) where["_page_num"] = utils.ToStr(page.PageNum) } param := make(map[string]interface{}) now := time.Now() today0 := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Unix() where["where"] = where["where"] + " AND `zy_user_check`.`user_id` = {{user_id}}" param["user_id"] = c.GetInt("userID") where["where"] = where["where"] + " AND `zy_user_check`.`created_at` >= {{today}}" param["today"] = today0 total, err := check.CountRaw(where["where"], param) if err != nil { app.Error(c, err.Error()) return } type CheckList struct { ID int `json:"id"` UserId int `json:"user_id"` Total int `json:"total"` Progress int `json:"progress"` Right int `json:"right"` State int `json:"state"` CreatedAt string `json:"created_at"` UpdatedAt int `json:"updated_at"` } checkList := make([]CheckList, 0) if err = check.GetChecksRaw(where, param, &checkList); err != nil { app.Error(c, err.Error()) return } for k, v := range checkList { v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm") checkList[k] = v } data := gin.H{ "list": checkList, "total": total, "limit": page.PageSize, } app.Success(c, data) } func CheckInfo(c *gin.Context) { check_id := utils.ToInt(c.Param("id")) if check_id <= 0 { app.Error(c, "考核id有误") return } checkInfo, err := check.GetOne(map[string]interface{}{"id": check_id}, nil, nil) if err != nil { app.Error(c, err.Error()) return } where := map[string]string{ "where": "`zy_user_checks`.`id`>0", "_group_by": "`zy_user_checks`.`id`", "_order_by": "`zy_user_checks`.`id` asc", } param := make(map[string]interface{}) where["where"] = where["where"] + " AND `zy_user_checks`.`check_id` = {{check_id}}" param["check_id"] = checkInfo.ID type ChecksList struct { ID int `json:"id"` CheckId int `json:"check_id"` Type int `json:"type"` QuestionId int `json:"question_id"` Content string `json:"content"` Options string `json:"options"` Answer string `json:"answer"` Right string `json:"right"` CreatedAt string `json:"created_at"` UpdatedAt string `json:"updated_at"` } checksList := make([]ChecksList, 0) if err = checks.GetCheckssRaw(where, param, &checksList); err != nil { app.Error(c, err.Error()) return } for k, v := range checksList { v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm") v.UpdatedAt = utils.DateS(v.UpdatedAt, "YYYY-MM-DD HH:mm") checksList[k] = v } data := gin.H{ "info": checkInfo, "checks": checksList, } app.Success(c, data) } func SelectChecks(c *gin.Context) { var form form.ChecksSelect if app.Bind(c, &form) != nil { return } err := checks.SelectChecks(form, c.GetInt("userID")) if err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) } func SubmitCheck(c *gin.Context) { check_id := utils.ToInt(c.Param("id")) if check_id <= 0 { app.Error(c, "考核id有误") return } err := check.SubmitCheck(check_id, c.GetInt("userID")) if err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) } func CheckContract(c *gin.Context) { customer_id := utils.ToInt(c.Param("id")) if customer_id <= 0 { app.Error(c, "房屋不存在") return } var customer budget.Customer db.GetModel(map[string]interface{}{"id": customer_id}, &customer) if customer.ID == 0 { app.ErrorMsg(c, "客户不存在", nil) return } if customer.State != 1 { app.ErrorMsg(c, "状态错误", nil) return } type UserInfo struct { ID int `json:"id"` Phone string `json:"phone"` } var userInfo UserInfo _, err := user.GetInfoByID(c.GetInt("userID"), nil, &userInfo) if err != nil { app.ErrorMsg(c, "客户不存在", nil) return } if userInfo.Phone != customer.Phone { app.ErrorMsg(c, "客户信息错误", nil) return } err = db.UpdateModel(db.Type(customer), customer.ID, map[string]interface{}{ "state": 2, }) if err != nil { app.Error(c, err.Error()) return } db.InsertModel(db.Type(budget.BudgetCustomerProcess{}), map[string]interface{}{ "customer_id": customer.ID, "order_id": customer.OrderId, "type": 2, "created_id": c.GetInt("userID"), }) app.Success(c, nil) } func CheckStart(c *gin.Context) { customer_id := utils.ToInt(c.Param("id")) if customer_id <= 0 { app.Error(c, "房屋不存在") return } var customer budget.Customer db.GetModel(map[string]interface{}{"id": customer_id}, &customer) if customer.ID == 0 { app.ErrorMsg(c, "客户不存在", nil) return } if customer.State != 3 { app.ErrorMsg(c, "状态错误", nil) return } type UserInfo struct { ID int `json:"id"` Phone string `json:"phone"` } var userInfo UserInfo _, err := user.GetInfoByID(c.GetInt("userID"), nil, &userInfo) if err != nil { app.ErrorMsg(c, "客户不存在", nil) return } if userInfo.Phone != customer.Phone { app.ErrorMsg(c, "客户信息错误", nil) return } err = db.UpdateModel(db.Type(customer), customer.ID, map[string]interface{}{ "state": 4, }) if err != nil { app.Error(c, err.Error()) return } db.InsertModel(db.Type(budget.BudgetCustomerProcess{}), map[string]interface{}{ "customer_id": customer.ID, "order_id": customer.OrderId, "site_id": customer.SiteId, "type": 4, "created_id": c.GetInt("userID"), }) app.Success(c, nil) }