product_controller.go 4.0 KB
package controllers

import (
	"github.com/linmadan/egglib-go/web/beego"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/product/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/product/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/product/service"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
)

type ProductController struct {
	beego.BaseController
}

func (controller *ProductController) CreateProduct() {
	productService := service.NewProductService(nil)
	createProductCommand := &command.CreateProductCommand{}
	Must(controller.Unmarshal(createProductCommand))
	op := ParseOperateInfo(controller.BaseController)
	createProductCommand.CompanyId = op.CompanyId
	createProductCommand.OrgId = op.OrgId
	data, err := productService.CreateProduct(ParseOperateInfo(controller.BaseController), createProductCommand)
	controller.Response(data, err)
}

func (controller *ProductController) UpdateProduct() {
	productService := service.NewProductService(nil)
	updateProductCommand := &command.UpdateProductCommand{}
	Must(controller.Unmarshal(updateProductCommand))
	productId, _ := controller.GetInt(":productId")
	updateProductCommand.ProductId = productId
	data, err := productService.UpdateProduct(ParseOperateInfo(controller.BaseController), updateProductCommand)
	controller.Response(data, err)
}

func (controller *ProductController) GetProduct() {
	productService := service.NewProductService(nil)
	getProductQuery := &query.GetProductQuery{}
	productId, _ := controller.GetInt(":productId")
	getProductQuery.ProductId = productId
	data, err := productService.GetProduct(getProductQuery)
	controller.Response(data, err)
}

func (controller *ProductController) RemoveProduct() {
	productService := service.NewProductService(nil)
	removeProductCommand := &command.RemoveProductCommand{}
	controller.Unmarshal(removeProductCommand)
	productId, _ := controller.GetInt(":productId")
	removeProductCommand.ProductId = productId
	data, err := productService.RemoveProduct(removeProductCommand)
	controller.Response(data, err)
}

func (controller *ProductController) BatchRemoveProduct() {
	productService := service.NewProductService(nil)
	removeProductCommand := &command.BatchRemoveProductCommand{}
	Must(controller.Unmarshal(removeProductCommand))
	data, err := productService.BatchRemoveProduct(removeProductCommand)
	controller.Response(data, err)
}

func (controller *ProductController) ListProduct() {
	productService := service.NewProductService(nil)
	listProductQuery := &query.ListProductQuery{}
	offset, _ := controller.GetInt("offset")
	listProductQuery.Offset = offset
	limit, _ := controller.GetInt("limit")
	listProductQuery.Limit = limit
	data, err := productService.ListProduct(listProductQuery)
	controller.Response(data, err)
}

func (controller *ProductController) SearchProduct() {
	productService := service.NewProductService(nil)
	cmd := &query.SearchProductQuery{}
	Must(controller.Unmarshal(cmd))
	operateInfo := ParseOperateInfo(controller.BaseController)
	//cmd.OrgId = operateInfo.OrgId
	cmd.CompanyId = operateInfo.CompanyId
	cmd.InOrgIds = operateInfo.OrgIds
	total, data, err := productService.SearchProduct(ParseOperateInfo(controller.BaseController), cmd)
	ResponseGrid(controller.BaseController, total, data, err)
}

func (controller *ProductController) BatchAddProduct() {
	productService := service.NewProductService(nil)
	cmd := &struct {
		List []*domain.ImportProductItem `json:"list"`
	}{}
	Must(controller.Unmarshal(cmd))
	data, err := productService.BatchAddProduct(ParseOperateInfo(controller.BaseController), cmd.List)
	controller.Response(data, err)
}

func (controller *ProductController) GenProductCode() {
	productService := service.NewProductService(nil)
	cmd := &query.GenProductCodeQuery{}
	Must(controller.Unmarshal(cmd))
	op := ParseOperateInfo(controller.BaseController)
	cmd.CompanyId = op.CompanyId
	data, err := productService.GenProductCode(cmd)
	controller.Response(data, err)
}