package hdl import ( "applet/app/admin/enum" "applet/app/admin/lib/validate" "applet/app/admin/md" "applet/app/admin/svc" "applet/app/db" "applet/app/db/model" "applet/app/e" "github.com/gin-gonic/gin" "time" ) func EnterpriseList(c *gin.Context) { var req md.EnterpriseListReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } enterpriseDb := db.EnterpriseDb{} enterpriseDb.Set() engine := enterpriseDb.Db.Desc("enterprise.id").Limit(req.Limit, (req.Page-1)*req.Limit) if req.Name != "" { engine.And("enterprise.name like ?", "%"+req.Name+"%") } if req.Memo != "" { engine.And("enterprise.memo like ?", "%"+req.Memo+"%") } if req.LeadPhone != "" { engine.And("company.lead_phone like ?", "%"+req.LeadPhone+"%") } if req.LeadName != "" { engine.And("company.lead_name like ?", "%"+req.LeadName+"%") } var list []db.EnterpriseWithCompany count, err := engine.Join("LEFT", "company", "enterprise.company_id = company.id"). FindAndCount(&list) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } var result []md.EnterpriseListResp for _, v := range list { result = append(result, md.EnterpriseListResp{ Id: v.Enterprise.Id, State: v.Enterprise.State, Name: v.Enterprise.Name, Memo: v.Enterprise.Memo, CompanyName: v.Company.Name, CompanyId: v.Company.Id, LeadName: v.Company.LeadName, LeadPhone: v.Company.LeadPhone, CreateAt: v.Enterprise.CreateAt, UpdateAt: v.Enterprise.UpdateAt, }) } e.OutSuc(c, map[string]interface{}{ "list": result, "total": count, "state": []map[string]interface{}{ { "name": enum.EnterpriseState(enum.EnterpriseStateForNormal).String(), "value": enum.EnterpriseStateForNormal, }, { "name": enum.EnterpriseState(enum.EnterpriseStateForFreeze).String(), "value": enum.EnterpriseStateForFreeze, }, }, }, nil) return } func EnterpriseUpdateState(c *gin.Context) { var req md.EnterpriseUpdateStateReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } enterpriseDb := db.EnterpriseDb{} enterpriseDb.Set() enterprise, err := enterpriseDb.GetEnterprise(req.Id) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if enterprise == nil { e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录") return } now := time.Now() enterprise.State = req.State enterprise.UpdateAt = now.Format("2006-01-02 15:04:05") _, err = enterpriseDb.EnterpriseUpdate(enterprise, "state", "update_at") if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } e.OutSuc(c, "success", nil) return } func EnterpriseDelete(c *gin.Context) { var req md.EnterpriseDeleteReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } err = svc.EnterpriseDelete(req.EnterpriseIds) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } e.OutSuc(c, "success", nil) return } func EnterpriseAdd(c *gin.Context) { var req md.EnterpriseAddReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } enterpriseDb := db.EnterpriseDb{} enterpriseDb.Set() now := time.Now() enterprise := model.Enterprise{ Name: req.Name, CompanyId: req.CompanyId, State: enum.EnterpriseStateForNormal, Memo: req.Memo, CreateAt: now.Format("2006-01-02 15:04:05"), UpdateAt: now.Format("2006-01-02 15:04:05"), } _, err = enterpriseDb.EnterpriseInsert(&enterprise) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } e.OutSuc(c, "success", nil) return } func EnterpriseUpdate(c *gin.Context) { var req md.EnterpriseUpdateReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } enterpriseDb := db.EnterpriseDb{} enterpriseDb.Set() enterprise, err := enterpriseDb.GetEnterprise(req.Id) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if enterprise == nil { e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录") return } now := time.Now() enterprise.Name = req.Name enterprise.Memo = req.Name enterprise.CompanyId = req.CompanyId enterprise.UpdateAt = now.Format("2006-01-02 15:04:05") _, err = enterpriseDb.EnterpriseUpdate(enterprise, "name", "memo", "update_at") if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } e.OutSuc(c, "success", nil) return }