package usecases import ( "cargo-erp-backend/internal/domain" "cargo-erp-backend/internal/handlers/dto" "cargo-erp-backend/pkg/datatable" "cargo-erp-backend/pkg/helpers" "fmt" "reflect" "time" ) type CostComponentUsecaseInterface interface { GetAll(filter dto.CostComponentFilterAllRequest) ([]domain.CostComponent, int64, error) GetList(dto.DataTableRequest) (dto.DataTableResponse, error) GetByID(id string) (domain.CostComponent, error) Create(name string, code string, inc int32, value string, costComponentGroupID string, createdBy string, merchantid *string) (domain.CostComponent, error) Update(id string, name string, code string, inc int32, value string, costComponentGroupID string, updatedBy string) (domain.CostComponent, error) Delete(id string) error } type CostComponentUsecase struct { Helper helpers.HelperInterface AuthUser domain.User } func NewCostComponentUsecase(helper helpers.HelperInterface, authUser domain.User) CostComponentUsecaseInterface { return &CostComponentUsecase{Helper: helper, AuthUser: authUser} } func (u *CostComponentUsecase) GetAll(filter dto.CostComponentFilterAllRequest) ([]domain.CostComponent, int64, error) { var components []domain.CostComponent var total int64 db := u.Helper.GetDB("slave") tx := db.Model(&components) if u.AuthUser.UsersRole_Relation.RoleKey != "SPM" { tx.Where(db.Where("merchant_id IS NULL").Or("merchant_id=?", u.AuthUser.MerchantID)) } v := reflect.ValueOf(filter) t := reflect.TypeOf(filter) for i := 0; i < v.NumField(); i++ { if v.Field(i).String() != "" { tagKey := t.Field(i).Tag.Get("form") tx.Where(fmt.Sprintf("%v=?", tagKey), v.Field(i)) } } tx.Model(&domain.CostComponent{}).Count(&total) tx.Order("name ASC") if err := tx.Find(&components).Error; err != nil { return nil, 0, err } return components, total, nil } func (u *CostComponentUsecase) GetList(req dto.DataTableRequest) (dto.DataTableResponse, error) { var response dto.DataTableResponse db := u.Helper.GetDB("slave") tx := db.Table("cost_component"). Joins("LEFT JOIN cost_component_group ON cost_component.cost_component_group_id=cost_component_group.id"). Joins("LEFT JOIN merchant ON cost_component.merchant_id=merchant.id") dt := datatable.NewDatatable(tx, req) coldef := make([]dto.DataTableColDef, 0) coldef = append(coldef, dto.DataTableColDef{ Field: "cost_component.name", Alias: "name", }) coldef = append(coldef, dto.DataTableColDef{ Field: "cost_component.code", Alias: "code", }) coldef = append(coldef, dto.DataTableColDef{ Field: "cost_component.inc", Alias: "inc", }) coldef = append(coldef, dto.DataTableColDef{ Field: "cost_component.value", Alias: "value", }) coldef = append(coldef, dto.DataTableColDef{ Field: "cost_component_group.name", Alias: "cost_component_group_name", }) coldef = append(coldef, dto.DataTableColDef{ Field: "cost_component.cost_component_group_id", Alias: "cost_component_group_id", }) coldef = append(coldef, dto.DataTableColDef{ Field: "merchant.id", Alias: "merchant_id", }) coldef = append(coldef, dto.DataTableColDef{ Field: "cost_component.id", Alias: "id", }) response = dt.Render(coldef) return response, nil } func (u *CostComponentUsecase) GetByID(id string) (domain.CostComponent, error) { var component domain.CostComponent db := u.Helper.GetDB("slave") if err := db.Where("id = ?", id).First(&component).Error; err != nil { return domain.CostComponent{}, err } return component, nil } func (u *CostComponentUsecase) Create(name string, code string, inc int32, value string, costComponentGroupID string, createdBy string, merchantid *string) (domain.CostComponent, error) { now := time.Now() component := domain.CostComponent{ Name: &name, Code: &code, Inc: &inc, Value: &value, CostComponentGroupID: &costComponentGroupID, MerchantID: merchantid, CreatedBy: &createdBy, CreatedOn: &now, UpdatedBy: &createdBy, UpdatedOn: &now, } db := u.Helper.GetDB("master") if err := db.Create(&component).Error; err != nil { return domain.CostComponent{}, err } return component, nil } func (u *CostComponentUsecase) Update(id string, name string, code string, inc int32, value string, costComponentGroupID string, updatedBy string) (domain.CostComponent, error) { var component domain.CostComponent db := u.Helper.GetDB("master") if u.AuthUser.UsersRole_Relation.RoleKey != "SPM" { db.Where("merchant_id=?", u.AuthUser.MerchantID) } if err := db.Where("id = ?", id).First(&component).Error; err != nil { return domain.CostComponent{}, err } now := time.Now() component.Name = &name component.Code = &code component.Inc = &inc component.Value = &value component.CostComponentGroupID = &costComponentGroupID component.UpdatedBy = &updatedBy component.UpdatedOn = &now if err := db.Save(&component).Error; err != nil { return domain.CostComponent{}, err } return component, nil } func (u *CostComponentUsecase) Delete(id string) error { db := u.Helper.GetDB("master") if u.AuthUser.UsersRole_Relation.RoleKey != "SPM" { db.Where("merchant_id=?", u.AuthUser.MerchantID) } if err := db.Delete(&domain.CostComponent{}, "id = ?", id).Error; err != nil { return err } return nil }