|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- 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.InsideName != "" {
- engine.And("enterprise.inside_name like ?", "%"+req.InsideName+"%")
- }
- 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,
- InsideName: v.Enterprise.InsideName,
- 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,
- InsideName: req.InsideName,
- 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.InsideName = req.InsideName
- enterprise.Memo = req.Memo
- enterprise.CompanyId = req.CompanyId
- enterprise.UpdateAt = now.Format("2006-01-02 15:04:05")
- _, err = enterpriseDb.EnterpriseUpdate(enterprise, "name", "memo", "update_at", "inside_name")
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
|