79614b2d07
- Updated multiple repository and service files to replace imports from "github.com/mlogclub/simple/web/params" with "cs-agent/internal/pkg/httpx/params". - Adjusted context handling in auth_service and ws_service to use gin.Context instead of iris.Context. - Ensured consistent usage of HTTP status responses across websocket handlers.
114 lines
3.5 KiB
Go
114 lines
3.5 KiB
Go
package dashboard
|
|
|
|
import (
|
|
"cs-agent/internal/builders"
|
|
"cs-agent/internal/pkg/constants"
|
|
"cs-agent/internal/pkg/dto/request"
|
|
"cs-agent/internal/pkg/enums"
|
|
"cs-agent/internal/services"
|
|
|
|
"cs-agent/internal/pkg/httpx/params"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/mlogclub/simple/web"
|
|
)
|
|
|
|
type CompanyController struct {
|
|
Ctx *gin.Context
|
|
}
|
|
|
|
func (c *CompanyController) AnyList() *web.JsonResult {
|
|
if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCompanyView); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
list, paging := services.CompanyService.FindPageByCnd(params.NewPagedSqlCnd(c.Ctx,
|
|
params.QueryFilter{ParamName: "status"},
|
|
params.QueryFilter{ParamName: "name", Op: params.Like},
|
|
params.QueryFilter{ParamName: "code", Op: params.Like},
|
|
).Where("status <> ?", enums.StatusDeleted).Desc("id"))
|
|
|
|
results := builders.BuildCompanyList(list)
|
|
companyIDs := make([]int64, 0, len(results))
|
|
for _, item := range results {
|
|
companyIDs = append(companyIDs, item.ID)
|
|
}
|
|
countMap := services.CustomerService.CountByCompanyIDs(companyIDs)
|
|
for i := range results {
|
|
results[i].CustomerCount = countMap[results[i].ID]
|
|
}
|
|
return web.JsonData(&web.PageResult{Results: results, Page: paging})
|
|
}
|
|
|
|
func (c *CompanyController) GetBy(id int64) *web.JsonResult {
|
|
if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCompanyView); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
item := services.CompanyService.Get(id)
|
|
if item == nil || item.Status == enums.StatusDeleted {
|
|
return web.JsonData(nil)
|
|
}
|
|
ret := builders.BuildCompany(item)
|
|
return web.JsonData(&ret)
|
|
}
|
|
|
|
func (c *CompanyController) PostCreate() *web.JsonResult {
|
|
user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCompanyCreate)
|
|
if err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
req := request.CreateCompanyRequest{}
|
|
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
item, err := services.CompanyService.CreateCompany(req, user)
|
|
if err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
ret := builders.BuildCompany(item)
|
|
return web.JsonData(&ret)
|
|
}
|
|
|
|
func (c *CompanyController) PostUpdate() *web.JsonResult {
|
|
user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCompanyUpdate)
|
|
if err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
req := request.UpdateCompanyRequest{}
|
|
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
if err := services.CompanyService.UpdateCompany(req, user); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
return web.JsonSuccess()
|
|
}
|
|
|
|
func (c *CompanyController) PostDelete() *web.JsonResult {
|
|
user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCompanyDelete)
|
|
if err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
req := request.DeleteCompanyRequest{}
|
|
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
if err := services.CompanyService.DeleteCompany(req.ID, *user); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
return web.JsonSuccess()
|
|
}
|
|
|
|
func (c *CompanyController) PostUpdate_status() *web.JsonResult {
|
|
user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCompanyUpdate)
|
|
if err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
req := request.UpdateCompanyStatusRequest{}
|
|
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
if err := services.CompanyService.UpdateStatus(req.ID, req.Status, user); err != nil {
|
|
return web.JsonError(err)
|
|
}
|
|
return web.JsonSuccess()
|
|
}
|