123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- 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)
- }
|