作者 tangxuhui

新建项目

不能预览此文件类型
  1 +#!/bin/bash
  2 +export PATH=/root/local/bin:$PATH
  3 +kubectl -n <replace-your-k8s-namespace> get pods | grep -q project
  4 +if [ "$?" == "1" ];then
  5 + kubectl create -f /tmp/dev/project/project.yaml --record
  6 + kubectl -n <replace-your-k8s-namespace> get svc | grep -q project
  7 + if [ "$?" == "0" ];then
  8 + echo "project service install success!"
  9 + else
  10 + echo "project service install fail!"
  11 + fi
  12 + kubectl -n <replace-your-k8s-namespace> get pods | grep -q project
  13 + if [ "$?" == "0" ];then
  14 + echo "project deployment install success!"
  15 + else
  16 + echo "project deployment install fail!"
  17 + fi
  18 +else
  19 + kubectl delete -f /tmp/dev/project/project.yaml
  20 + kubectl -n <replace-your-k8s-namespace> get svc | grep -q project
  21 + while [ "$?" == "0" ]
  22 + do
  23 + kubectl -n <replace-your-k8s-namespace> get svc | grep -q project
  24 + done
  25 + kubectl -n <replace-your-k8s-namespace> get pods | grep -q project
  26 + while [ "$?" == "0" ]
  27 + do
  28 + kubectl -n <replace-your-k8s-namespace> get pods | grep -q project
  29 + done
  30 + kubectl create -f /tmp/dev/project/project.yaml --record
  31 + kubectl -n <replace-your-k8s-namespace> get svc | grep -q project
  32 + if [ "$?" == "0" ];then
  33 + echo "project service update success!"
  34 + else
  35 + echo "project service update fail!"
  36 + fi
  37 + kubectl -n <replace-your-k8s-namespace> get pods | grep -q project
  38 + if [ "$?" == "0" ];then
  39 + echo "project deployment update success!"
  40 + else
  41 + echo "project deployment update fail!"
  42 + fi
  43 +fi
  1 +apiVersion: v1
  2 +kind: Service
  3 +metadata:
  4 + name: project
  5 + namespace: <replace-your-k8s-namespace>
  6 + labels:
  7 + k8s-app: project
  8 +spec:
  9 + ports:
  10 + - name: "http"
  11 + port: 80
  12 + targetPort: 8082
  13 + selector:
  14 + k8s-app: project
  15 +---
  16 +apiVersion: extensions/v1beta1
  17 +kind: Deployment
  18 +metadata:
  19 + name: project
  20 + namespace: <replace-your-k8s-namespace>
  21 + labels:
  22 + k8s-app: project
  23 +spec:
  24 + replicas: 1
  25 + template:
  26 + metadata:
  27 + labels:
  28 + k8s-app: project
  29 + spec:
  30 + affinity:
  31 + nodeAffinity:
  32 + preferredDuringSchedulingIgnoredDuringExecution:
  33 + - preference: {}
  34 + weight: 100
  35 + requiredDuringSchedulingIgnoredDuringExecution:
  36 + nodeSelectorTerms:
  37 + - matchExpressions:
  38 + - key: kubernetes.io/hostname
  39 + operator: In
  40 + values:
  41 + - cn-hangzhou.i-bp1djh1xn7taumbue1ze
  42 + - cn-hangzhou.i-bp1djh1xn7taumbue1zd
  43 + - cn-hangzhou.i-bp1euf5u1ph9kbhtndhb
  44 + - cn-hangzhou.i-bp1hyp5oips9cdwxxgxy
  45 + containers:
  46 + - name: project
  47 + image: 192.168.0.243:5000/mmm/project:dev
  48 + imagePullPolicy: Always
  49 + ports:
  50 + - containerPort: 8082
  51 + volumeMounts:
  52 + - mountPath: /opt/logs
  53 + name: accesslogs
  54 + env:
  55 + - name: LOG_LEVEL
  56 + value: "debug"
  57 + - name: ERROR_BASE_CODE
  58 + value: "1"
  59 + - name: ERROR_BASE_CODE_MULTIPLE
  60 + value: "1000"
  61 + volumes:
  62 + - name: accesslogs
  63 + emptyDir: {}
1 -module gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway 1 +module gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic
2 2
3 go 1.16 3 go 1.16
4 4
5 -require github.com/beego/beego/v2 v2.0.1 5 +require (
  6 + github.com/beego/beego/v2 v2.0.1
  7 + github.com/go-pg/pg/v10 v10.10.2
  8 + github.com/linmadan/egglib-go v0.0.0-20210527091316-06b0732fb5f6
  9 +)
@@ -5,5 +5,6 @@ import ( @@ -5,5 +5,6 @@ import (
5 ) 5 )
6 6
7 func main() { 7 func main() {
  8 +
8 web.Run() 9 web.Run()
9 } 10 }
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/beego/beego/v2/core/validation"
  7 +)
  8 +
  9 +type CreateDictionaryCommand struct {
  10 + // 字典编号 主键
  11 + DictionaryId int `json:"dictionaryId,omitempty"`
  12 + // 字典编码
  13 + DictCode string `json:"dictCode,omitempty"`
  14 + // 字典名称
  15 + DictName string `json:"dictName,omitempty"`
  16 + // 备注信息
  17 + Desc string `json:"desc,omitempty"`
  18 + // 是否可见【1:不可以】【2:可以】
  19 + IsShow int `json:"isShow,omitempty"`
  20 + // 字典值列表
  21 + DictItems []*DictionaryItem `json:"dictItems,omitempty"`
  22 +}
  23 +
  24 +func (createDictionaryCommand *CreateDictionaryCommand) Valid(validation *validation.Validation) {
  25 + validation.SetError("CustomValid", "未实现的自定义认证")
  26 +}
  27 +
  28 +func (createDictionaryCommand *CreateDictionaryCommand) ValidateCommand() error {
  29 + valid := validation.Validation{}
  30 + b, err := valid.Valid(createDictionaryCommand)
  31 + if err != nil {
  32 + return err
  33 + }
  34 + if !b {
  35 + for _, validErr := range valid.Errors {
  36 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  37 + }
  38 + }
  39 + return nil
  40 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/beego/beego/v2/core/validation"
  7 +)
  8 +
  9 +type RemoveDictionaryCommand struct {
  10 + // 字典编号 主键
  11 + DictionaryId int `json:"dictionaryId,omitempty"`
  12 +}
  13 +
  14 +func (removeDictionaryCommand *RemoveDictionaryCommand) Valid(validation *validation.Validation) {
  15 + validation.SetError("CustomValid", "未实现的自定义认证")
  16 +}
  17 +
  18 +func (removeDictionaryCommand *RemoveDictionaryCommand) ValidateCommand() error {
  19 + valid := validation.Validation{}
  20 + b, err := valid.Valid(removeDictionaryCommand)
  21 + if err != nil {
  22 + return err
  23 + }
  24 + if !b {
  25 + for _, validErr := range valid.Errors {
  26 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  27 + }
  28 + }
  29 + return nil
  30 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/beego/beego/v2/core/validation"
  7 +)
  8 +
  9 +type UpdateDictionaryCommand struct {
  10 + // 字典编号 主键
  11 + DictionaryId int `json:"dictionaryId,omitempty"`
  12 + // 字典编码
  13 + DictCode string `json:"dictCode,omitempty"`
  14 + // 字典名称
  15 + DictName string `json:"dictName,omitempty"`
  16 + // 备注信息
  17 + Desc string `json:"desc,omitempty"`
  18 + // 是否可见【1:不可以】【2:可以】
  19 + IsShow int `json:"isShow,omitempty"`
  20 + // 字典值列表
  21 + DictItems []*DictionaryItem `json:"dictItems,omitempty"`
  22 +}
  23 +
  24 +func (updateDictionaryCommand *UpdateDictionaryCommand) Valid(validation *validation.Validation) {
  25 + validation.SetError("CustomValid", "未实现的自定义认证")
  26 +}
  27 +
  28 +func (updateDictionaryCommand *UpdateDictionaryCommand) ValidateCommand() error {
  29 + valid := validation.Validation{}
  30 + b, err := valid.Valid(updateDictionaryCommand)
  31 + if err != nil {
  32 + return err
  33 + }
  34 + if !b {
  35 + for _, validErr := range valid.Errors {
  36 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  37 + }
  38 + }
  39 + return nil
  40 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/beego/beego/v2/core/validation"
  7 +)
  8 +
  9 +type GetDictionaryQuery struct {
  10 + // 字典编码
  11 + DictCode string `json:"dictCode,omitempty"`
  12 +}
  13 +
  14 +func (getDictionaryQuery *GetDictionaryQuery) Valid(validation *validation.Validation) {
  15 + validation.SetError("CustomValid", "未实现的自定义认证")
  16 +}
  17 +
  18 +func (getDictionaryQuery *GetDictionaryQuery) ValidateQuery() error {
  19 + valid := validation.Validation{}
  20 + b, err := valid.Valid(getDictionaryQuery)
  21 + if err != nil {
  22 + return err
  23 + }
  24 + if !b {
  25 + for _, validErr := range valid.Errors {
  26 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  27 + }
  28 + }
  29 + return nil
  30 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/beego/beego/v2/core/validation"
  7 +)
  8 +
  9 +type ListDictionaryQuery struct {
  10 + // 查询偏离量
  11 + Offset int `json:"offset" valid:"Required"`
  12 + // 查询限制
  13 + Limit int `json:"limit" valid:"Required"`
  14 +}
  15 +
  16 +func (listDictionaryQuery *ListDictionaryQuery) Valid(validation *validation.Validation) {
  17 + validation.SetError("CustomValid", "未实现的自定义认证")
  18 +}
  19 +
  20 +func (listDictionaryQuery *ListDictionaryQuery) ValidateQuery() error {
  21 + valid := validation.Validation{}
  22 + b, err := valid.Valid(listDictionaryQuery)
  23 + if err != nil {
  24 + return err
  25 + }
  26 + if !b {
  27 + for _, validErr := range valid.Errors {
  28 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  29 + }
  30 + }
  31 + return nil
  32 +}
  1 +package service
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/core/application"
  5 + "github.com/linmadan/egglib-go/utils/tool_funs"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/dictionary/command"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/dictionary/query"
  8 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/factory"
  9 +)
  10 +
  11 +// 数据字典设置
  12 +type DictionaryService struct {
  13 +}
  14 +
  15 +// 创建数据字典设置
  16 +func (dictionaryService *DictionaryService) CreateDictionary(createDictionaryCommand *command.CreateDictionaryCommand) (interface{}, error) {
  17 + if err := createDictionaryCommand.ValidateCommand(); err != nil {
  18 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  19 + }
  20 + transactionContext, err := factory.CreateTransactionContext(nil)
  21 + if err != nil {
  22 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  23 + }
  24 + if err := transactionContext.StartTransaction(); err != nil {
  25 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  26 + }
  27 + defer func() {
  28 + transactionContext.RollbackTransaction()
  29 + }()
  30 + newDictionary := &dictionary.Dictionary{
  31 + Dictionary: createDictionaryCommand.Dictionary,
  32 + }
  33 + var dictionaryRepository dictionary.DictionaryRepository
  34 + if value, err := factory.CreateDictionaryRepository(map[string]interface{}{
  35 + "transactionContext": transactionContext,
  36 + }); err != nil {
  37 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  38 + } else {
  39 + dictionaryRepository = value
  40 + }
  41 + if dictionary, err := dictionaryRepository.Save(newDictionary); err != nil {
  42 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  43 + } else {
  44 + if err := transactionContext.CommitTransaction(); err != nil {
  45 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  46 + }
  47 + return dictionary, nil
  48 + }
  49 +}
  50 +
  51 +// 返回数据字典设置
  52 +func (dictionaryService *DictionaryService) GetDictionary(getDictionaryQuery *query.GetDictionaryQuery) (interface{}, error) {
  53 + if err := getDictionaryQuery.ValidateQuery(); err != nil {
  54 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  55 + }
  56 + transactionContext, err := factory.CreateTransactionContext(nil)
  57 + if err != nil {
  58 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  59 + }
  60 + if err := transactionContext.StartTransaction(); err != nil {
  61 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  62 + }
  63 + defer func() {
  64 + transactionContext.RollbackTransaction()
  65 + }()
  66 + var dictionaryRepository dictionary.DictionaryRepository
  67 + if value, err := factory.CreateDictionaryRepository(map[string]interface{}{
  68 + "transactionContext": transactionContext,
  69 + }); err != nil {
  70 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  71 + } else {
  72 + dictionaryRepository = value
  73 + }
  74 + dictionary, err := dictionaryRepository.FindOne(map[string]interface{}{"dictionaryId": getDictionaryQuery.DictionaryId})
  75 + if err != nil {
  76 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  77 + }
  78 + if dictionary == nil {
  79 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getDictionaryQuery.DictionaryId)))
  80 + } else {
  81 + if err := transactionContext.CommitTransaction(); err != nil {
  82 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  83 + }
  84 + return dictionary, nil
  85 + }
  86 +}
  87 +
  88 +// 返回数据字典设置列表
  89 +func (dictionaryService *DictionaryService) ListDictionary(listDictionaryQuery *query.ListDictionaryQuery) (interface{}, error) {
  90 + if err := listDictionaryQuery.ValidateQuery(); err != nil {
  91 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  92 + }
  93 + transactionContext, err := factory.CreateTransactionContext(nil)
  94 + if err != nil {
  95 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  96 + }
  97 + if err := transactionContext.StartTransaction(); err != nil {
  98 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  99 + }
  100 + defer func() {
  101 + transactionContext.RollbackTransaction()
  102 + }()
  103 + var dictionaryRepository dictionary.DictionaryRepository
  104 + if value, err := factory.CreateDictionaryRepository(map[string]interface{}{
  105 + "transactionContext": transactionContext,
  106 + }); err != nil {
  107 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  108 + } else {
  109 + dictionaryRepository = value
  110 + }
  111 + if count, dictionarys, err := dictionaryRepository.Find(tool_funs.SimpleStructToMap(listDictionaryQuery)); err != nil {
  112 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  113 + } else {
  114 + if err := transactionContext.CommitTransaction(); err != nil {
  115 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  116 + }
  117 + return map[string]interface{}{
  118 + "count": count,
  119 + "dictionarys": dictionarys,
  120 + }, nil
  121 + }
  122 +}
  123 +
  124 +// 移除数据字典设置
  125 +func (dictionaryService *DictionaryService) RemoveDictionary(removeDictionaryCommand *command.RemoveDictionaryCommand) (interface{}, error) {
  126 + if err := removeDictionaryCommand.ValidateCommand(); err != nil {
  127 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  128 + }
  129 + transactionContext, err := factory.CreateTransactionContext(nil)
  130 + if err != nil {
  131 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  132 + }
  133 + if err := transactionContext.StartTransaction(); err != nil {
  134 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  135 + }
  136 + defer func() {
  137 + transactionContext.RollbackTransaction()
  138 + }()
  139 + var dictionaryRepository dictionary.DictionaryRepository
  140 + if value, err := factory.CreateDictionaryRepository(map[string]interface{}{
  141 + "transactionContext": transactionContext,
  142 + }); err != nil {
  143 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  144 + } else {
  145 + dictionaryRepository = value
  146 + }
  147 + dictionary, err := dictionaryRepository.FindOne(map[string]interface{}{"dictionaryId": removeDictionaryCommand.DictionaryId})
  148 + if err != nil {
  149 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  150 + }
  151 + if dictionary == nil {
  152 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeDictionaryCommand.DictionaryId)))
  153 + }
  154 + if dictionary, err := dictionaryRepository.Remove(dictionary); err != nil {
  155 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  156 + } else {
  157 + if err := transactionContext.CommitTransaction(); err != nil {
  158 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  159 + }
  160 + return dictionary, nil
  161 + }
  162 +}
  163 +
  164 +// 更新数据字典设置
  165 +func (dictionaryService *DictionaryService) UpdateDictionary(updateDictionaryCommand *command.UpdateDictionaryCommand) (interface{}, error) {
  166 + if err := updateDictionaryCommand.ValidateCommand(); err != nil {
  167 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  168 + }
  169 + transactionContext, err := factory.CreateTransactionContext(nil)
  170 + if err != nil {
  171 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  172 + }
  173 + if err := transactionContext.StartTransaction(); err != nil {
  174 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  175 + }
  176 + defer func() {
  177 + transactionContext.RollbackTransaction()
  178 + }()
  179 + var dictionaryRepository dictionary.DictionaryRepository
  180 + if value, err := factory.CreateDictionaryRepository(map[string]interface{}{
  181 + "transactionContext": transactionContext,
  182 + }); err != nil {
  183 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  184 + } else {
  185 + dictionaryRepository = value
  186 + }
  187 + dictionary, err := dictionaryRepository.FindOne(map[string]interface{}{"dictionaryId": updateDictionaryCommand.DictionaryId})
  188 + if err != nil {
  189 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  190 + }
  191 + if dictionary == nil {
  192 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateDictionaryCommand.DictionaryId)))
  193 + }
  194 + if err := dictionary.Update(tool_funs.SimpleStructToMap(updateDictionaryCommand)); err != nil {
  195 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  196 + }
  197 + if dictionary, err := dictionaryRepository.Save(dictionary); err != nil {
  198 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  199 + } else {
  200 + if err := transactionContext.CommitTransaction(); err != nil {
  201 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  202 + }
  203 + return dictionary, nil
  204 + }
  205 +}
  206 +
  207 +func NewDictionaryService(options map[string]interface{}) *DictionaryService {
  208 + newDictionaryService := &DictionaryService{}
  209 + return newDictionaryService
  210 +}
  1 +package factory
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/transaction/pg"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/domain"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/infrastructure/repository"
  7 +)
  8 +
  9 +func CreateDictionaryRepository(options map[string]interface{}) (domain.DictionaryRepository, error) {
  10 + var transactionContext *pg.TransactionContext
  11 + if value, ok := options["transactionContext"]; ok {
  12 + transactionContext = value.(*pg.TransactionContext)
  13 + }
  14 + return repository.NewDictionaryRepository(transactionContext)
  15 +}
  1 +package factory
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/core/application"
  5 + pG "github.com/linmadan/egglib-go/transaction/pg"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/infrastructure/pg"
  7 +)
  8 +
  9 +func CreateTransactionContext(options map[string]interface{}) (application.TransactionContext, error) {
  10 + return pG.NewPGTransactionContext(pg.DB), nil
  11 +}
  1 +package constant
  2 +
  3 +import "os"
  4 +
  5 +const SERVICE_NAME = "project"
  6 +
  7 +var LOG_LEVEL = "debug"
  8 +
  9 +func init() {
  10 + if os.Getenv("LOG_LEVEL") != "" {
  11 + LOG_LEVEL = os.Getenv("LOG_LEVEL")
  12 + }
  13 +}
  1 +package constant
  2 +
  3 +import "os"
  4 +
  5 +var POSTGRESQL_DB_NAME = "project"
  6 +var POSTGRESQL_USER = "postgres"
  7 +var POSTGRESQL_PASSWORD = ""
  8 +var POSTGRESQL_HOST = "127.0.0.1"
  9 +var POSTGRESQL_PORT = "32432"
  10 +var DISABLE_CREATE_TABLE = false
  11 +var DISABLE_SQL_GENERATE_PRINT = false
  12 +
  13 +func init() {
  14 + if os.Getenv("POSTGRESQL_DB_NAME") != "" {
  15 + POSTGRESQL_DB_NAME = os.Getenv("POSTGRESQL_DB_NAME")
  16 + }
  17 + if os.Getenv("POSTGRESQL_USER") != "" {
  18 + POSTGRESQL_USER = os.Getenv("POSTGRESQL_USER")
  19 + }
  20 + if os.Getenv("POSTGRESQL_PASSWORD") != "" {
  21 + POSTGRESQL_PASSWORD = os.Getenv("POSTGRESQL_PASSWORD")
  22 + }
  23 + if os.Getenv("POSTGRESQL_HOST") != "" {
  24 + POSTGRESQL_HOST = os.Getenv("POSTGRESQL_HOST")
  25 + }
  26 + if os.Getenv("POSTGRESQL_PORT") != "" {
  27 + POSTGRESQL_PORT = os.Getenv("POSTGRESQL_PORT")
  28 + }
  29 + if os.Getenv("DISABLE_CREATE_TABLE") != "" {
  30 + DISABLE_CREATE_TABLE = true
  31 + }
  32 + if os.Getenv("DISABLE_SQL_GENERATE_PRINT") != "" {
  33 + DISABLE_SQL_GENERATE_PRINT = true
  34 + }
  35 +}
  1 +package domain
  2 +
  3 +// 字典
  4 +type Dictionary struct {
  5 + // 字典编号 主键
  6 + DictionaryId int `json:"dictionaryId"`
  7 + // 字典编码
  8 + DictCode string `json:"dictCode"`
  9 + // 字典名称
  10 + DictName string `json:"dictName"`
  11 + // 备注信息
  12 + Desc string `json:"desc"`
  13 + // 是否可见【1:不可以】【2:可以】
  14 + IsShow int `json:"isShow"`
  15 + // 字典值列表
  16 + DictItems []*DictionaryItem `json:"dictItems"`
  17 +}
  18 +
  19 +type DictionaryRepository interface {
  20 + Save(dictionary *Dictionary) (*Dictionary, error)
  21 + Remove(dictionary *Dictionary) (*Dictionary, error)
  22 + FindOne(queryOptions map[string]interface{}) (*Dictionary, error)
  23 + Find(queryOptions map[string]interface{}) (int64, []*Dictionary, error)
  24 +}
  25 +
  26 +func (dictionary *Dictionary) Identify() interface{} {
  27 + if dictionary.DictionaryId == 0 {
  28 + return nil
  29 + }
  30 + return dictionary.DictionaryId
  31 +}
  32 +
  33 +func (dictionary *Dictionary) Update(data map[string]interface{}) error {
  34 + if dictionaryId, ok := data["dictionaryId"]; ok {
  35 + dictionary.DictionaryId = dictionaryId.(int)
  36 + }
  37 + if dictCode, ok := data["dictCode"]; ok {
  38 + dictionary.DictCode = dictCode.(string)
  39 + }
  40 + if dictName, ok := data["dictName"]; ok {
  41 + dictionary.DictName = dictName.(string)
  42 + }
  43 + if desc, ok := data["desc"]; ok {
  44 + dictionary.Desc = desc.(string)
  45 + }
  46 + if isShow, ok := data["isShow"]; ok {
  47 + dictionary.IsShow = isShow.(int)
  48 + }
  49 + if dictItems, ok := data["dictItems"]; ok {
  50 + dictionary.DictItems = dictItems.([]*DictionaryItem)
  51 + }
  52 + return nil
  53 +}
  1 +package domain
  2 +
  3 +// 字典明细项
  4 +type DictionaryItem struct {
  5 + // 项编码
  6 + ItemCode string `json:"itemCode"`
  7 + // 项标签
  8 + ItemLabel string `json:"itemLabel"`
  9 + // 值
  10 + ItemValue string `json:"itemValue"`
  11 + // 启用状态(启用:1 禁用:2)
  12 + EnableStatus string `json:"enableStatus"`
  13 + // 显示序号
  14 + Sort int `json:"sort"`
  15 +}
  1 +package pg
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/go-pg/pg/v10"
  7 + "github.com/go-pg/pg/v10/orm"
  8 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/constant"
  9 +
  10 + //_ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/infrastructure/pg/models"
  11 + "github.com/linmadan/egglib-go/persistent/pg/hooks"
  12 +)
  13 +
  14 +var DB *pg.DB
  15 +
  16 +func init() {
  17 + DB = pg.Connect(&pg.Options{
  18 + User: constant.POSTGRESQL_USER,
  19 + Password: constant.POSTGRESQL_PASSWORD,
  20 + Database: constant.POSTGRESQL_DB_NAME,
  21 + Addr: fmt.Sprintf("%s:%s", constant.POSTGRESQL_HOST, constant.POSTGRESQL_PORT),
  22 + })
  23 + if !constant.DISABLE_SQL_GENERATE_PRINT {
  24 + DB.AddQueryHook(hooks.SqlGeneratePrintHook{})
  25 + }
  26 + if !constant.DISABLE_CREATE_TABLE {
  27 + for _, model := range []interface{}{} {
  28 + err := DB.Model(model).CreateTable(&orm.CreateTableOptions{
  29 + Temp: false,
  30 + IfNotExists: true,
  31 + FKConstraints: true,
  32 + })
  33 + if err != nil {
  34 + panic(err)
  35 + }
  36 + }
  37 + }
  38 +}
  1 +package models
  2 +
  3 +type Dictionary struct {
  4 + tableName string `pg:"dictionarys,alias:dictionary"`
  5 + // 字典编号 主键
  6 + DictionaryId int
  7 + // 字典编码
  8 + DictCode string
  9 + // 字典名称
  10 + DictName string
  11 + // 备注信息
  12 + Desc string
  13 + // 是否可见【1:不可以】【2:可以】
  14 + IsShow int
  15 + // 字典值列表
  16 + DictItems []*DictionaryItem `pg:",array"`
  17 +}
  1 +package transform
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/domain"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/infrastructure/pg/models"
  6 +)
  7 +
  8 +func TransformToDictionaryDomainModelFromPgModels(dictionaryModel *models.Dictionary) (*domain.Dictionary, error) {
  9 + return &domain.Dictionary{
  10 + DictionaryId: dictionaryModel.DictionaryId,
  11 + DictCode: dictionaryModel.DictCode,
  12 + DictName: dictionaryModel.DictName,
  13 + Desc: dictionaryModel.Desc,
  14 + IsShow: dictionaryModel.IsShow,
  15 + DictItems: dictionaryModel.DictItems,
  16 + }, nil
  17 +}
  1 +package repository
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
  7 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  8 + "github.com/linmadan/egglib-go/utils/snowflake"
  9 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/domain"
  10 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/infrastructure/pg/models"
  11 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/infrastructure/pg/transform"
  12 +)
  13 +
  14 +type DictionaryRepository struct {
  15 + transactionContext *pgTransaction.TransactionContext
  16 +}
  17 +
  18 +func (repository *DictionaryRepository) nextIdentify() (int64, error) {
  19 + IdWorker, err := snowflake.NewIdWorker(1)
  20 + if err != nil {
  21 + return 0, err
  22 + }
  23 + id, err := IdWorker.NextId()
  24 + return id, err
  25 +}
  26 +func (repository *DictionaryRepository) Save(dictionary *domain.Dictionary) (*domain.Dictionary, error) {
  27 + sqlBuildFields := []string{
  28 + "dictionary_id",
  29 + "dict_code",
  30 + "dict_name",
  31 + "desc",
  32 + "is_show",
  33 + "dict_items",
  34 + }
  35 + insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
  36 + insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
  37 + returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
  38 + updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "dictionary_id")
  39 + updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
  40 + tx := repository.transactionContext.PgTx
  41 + if dictionary.Identify() == nil {
  42 + dictionaryId, err := repository.nextIdentify()
  43 + if err != nil {
  44 + return dictionary, err
  45 + } else {
  46 + dictionary.DictionaryId = dictionaryId
  47 + }
  48 + if _, err := tx.QueryOne(
  49 + pg.Scan(
  50 + &dictionary.DictionaryId,
  51 + &dictionary.DictCode,
  52 + &dictionary.DictName,
  53 + &dictionary.Desc,
  54 + &dictionary.IsShow,
  55 + pg.Array(&dictionary.DictItems),
  56 + ),
  57 + fmt.Sprintf("INSERT INTO dictionarys (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
  58 + dictionary.DictionaryId,
  59 + dictionary.DictCode,
  60 + dictionary.DictName,
  61 + dictionary.Desc,
  62 + dictionary.IsShow,
  63 + pg.Array(dictionary.DictItems),
  64 + ); err != nil {
  65 + return dictionary, err
  66 + }
  67 + } else {
  68 + if _, err := tx.QueryOne(
  69 + pg.Scan(
  70 + &dictionary.DictionaryId,
  71 + &dictionary.DictCode,
  72 + &dictionary.DictName,
  73 + &dictionary.Desc,
  74 + &dictionary.IsShow,
  75 + pg.Array(&dictionary.DictItems),
  76 + ),
  77 + fmt.Sprintf("UPDATE dictionarys SET %s WHERE dictionary_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
  78 + dictionary.DictionaryId,
  79 + dictionary.DictCode,
  80 + dictionary.DictName,
  81 + dictionary.Desc,
  82 + dictionary.IsShow,
  83 + pg.Array(dictionary.DictItems),
  84 + dictionary.Identify(),
  85 + ); err != nil {
  86 + return dictionary, err
  87 + }
  88 + }
  89 + return dictionary, nil
  90 +}
  91 +func (repository *DictionaryRepository) Remove(dictionary *domain.Dictionary) (*domain.Dictionary, error) {
  92 + tx := repository.transactionContext.PgTx
  93 + dictionaryModel := new(models.Dictionary)
  94 + dictionaryModel.DictionaryId = dictionary.Identify().(int64)
  95 + if _, err := tx.Model(dictionaryModel).WherePK().Delete(); err != nil {
  96 + return dictionary, err
  97 + }
  98 + return dictionary, nil
  99 +}
  100 +func (repository *DictionaryRepository) FindOne(queryOptions map[string]interface{}) (*domain.Dictionary, error) {
  101 + tx := repository.transactionContext.PgTx
  102 + dictionaryModel := new(models.Dictionary)
  103 + query := sqlbuilder.BuildQuery(tx.Model(dictionaryModel), queryOptions)
  104 + query.SetWhereByQueryOption("dictionary.dictionary_id = ?", "dictionaryId")
  105 + if err := query.First(); err != nil {
  106 + if err.Error() == "pg: no rows in result set" {
  107 + return nil, fmt.Errorf("没有此资源")
  108 + } else {
  109 + return nil, err
  110 + }
  111 + }
  112 + if dictionaryModel.DictionaryId == 0 {
  113 + return nil, nil
  114 + } else {
  115 + return transform.TransformToDictionaryDomainModelFromPgModels(dictionaryModel)
  116 + }
  117 +}
  118 +func (repository *DictionaryRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Dictionary, error) {
  119 + tx := repository.transactionContext.PgTx
  120 + var dictionaryModels []*models.Dictionary
  121 + dictionarys := make([]*domain.Dictionary, 0)
  122 + query := sqlbuilder.BuildQuery(tx.Model(&dictionaryModels), queryOptions)
  123 + query.SetOffsetAndLimit(20)
  124 + query.SetOrderDirect("dictionary_id", "DESC")
  125 + if count, err := query.SelectAndCount(); err != nil {
  126 + return 0, dictionarys, err
  127 + } else {
  128 + for _, dictionaryModel := range dictionaryModels {
  129 + if dictionary, err := transform.TransformToDictionaryDomainModelFromPgModels(dictionaryModel); err != nil {
  130 + return 0, dictionarys, err
  131 + } else {
  132 + dictionarys = append(dictionarys, dictionary)
  133 + }
  134 + }
  135 + return int64(count), dictionarys, nil
  136 + }
  137 +}
  138 +func NewDictionaryRepository(transactionContext *pgTransaction.TransactionContext) (*DictionaryRepository, error) {
  139 + if transactionContext == nil {
  140 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  141 + } else {
  142 + return &DictionaryRepository{
  143 + transactionContext: transactionContext,
  144 + }, nil
  145 + }
  146 +}
  1 +package log
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/log"
  5 + "github.com/linmadan/egglib-go/log/logrus"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/constant"
  7 +)
  8 +
  9 +var Logger log.Logger
  10 +
  11 +func init() {
  12 + Logger = logrus.NewLogrusLogger()
  13 + Logger.SetServiceName(constant.SERVICE_NAME)
  14 + Logger.SetLevel(constant.LOG_LEVEL)
  15 +}
  1 +package beego
  2 +
  3 +import (
  4 + "github.com/beego/beego/v2/server/web"
  5 + "github.com/linmadan/egglib-go/web/beego/filters"
  6 +
  7 + //_ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/port/beego/routers"
  8 + . "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/log"
  9 +)
  10 +
  11 +func init() {
  12 + web.BConfig.AppName = "project"
  13 + web.BConfig.CopyRequestBody = true
  14 + web.BConfig.RunMode = "dev"
  15 + web.BConfig.Listen.HTTPPort = 8080
  16 + web.BConfig.Listen.EnableAdmin = false
  17 + web.BConfig.WebConfig.CommentRouterPath = "/pkg/port/beego"
  18 + if os.Getenv("RUN_MODE") != "" {
  19 + web.BConfig.RunMode = os.Getenv("RUN_MODE")
  20 + }
  21 + if os.Getenv("HTTP_PORT") != "" {
  22 + portStr := os.Getenv("HTTP_PORT")
  23 + if port, err := strconv.Atoi(portStr); err == nil {
  24 + web.BConfig.Listen.HTTPPort = port
  25 + }
  26 + }
  27 + web.InsertFilter("/*", web.BeforeExec, filters.AllowCors())
  28 + web.InsertFilter("/*", web.BeforeExec, filters.CreateRequstLogFilter(Logger))
  29 + web.InsertFilter("/*", web.AfterExec, filters.CreateResponseLogFilter(Logger), web.WithReturnOnOutput(false))
  30 +}
  1 +package controllers
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/web/beego"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/dictionary/command"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/dictionary/query"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/dictionary/service"
  8 +)
  9 +
  10 +type DictionaryController struct {
  11 + beego.BaseController
  12 +}
  13 +
  14 +func (controller *DictionaryController) CreateDictionary() {
  15 + dictionaryService := service.NewDictionaryService(nil)
  16 + createDictionaryCommand := &command.CreateDictionaryCommand{}
  17 + controller.Unmarshal(createDictionaryCommand)
  18 + data, err := dictionaryService.CreateDictionary(createDictionaryCommand)
  19 + controller.Response(data, err)
  20 +}
  21 +
  22 +func (controller *DictionaryController) UpdateDictionary() {
  23 + dictionaryService := service.NewDictionaryService(nil)
  24 + updateDictionaryCommand := &command.UpdateDictionaryCommand{}
  25 + controller.Unmarshal(updateDictionaryCommand)
  26 + dictionaryId, _ := controller.GetString(":dictionaryId")
  27 + updateDictionaryCommand.DictionaryId = dictionaryId
  28 + data, err := dictionaryService.UpdateDictionary(updateDictionaryCommand)
  29 + controller.Response(data, err)
  30 +}
  31 +
  32 +func (controller *DictionaryController) GetDictionary() {
  33 + dictionaryService := service.NewDictionaryService(nil)
  34 + getDictionaryQuery := &query.GetDictionaryQuery{}
  35 + dictionaryId, _ := controller.GetString(":dictionaryId")
  36 + getDictionaryQuery.DictionaryId = dictionaryId
  37 + data, err := dictionaryService.GetDictionary(getDictionaryQuery)
  38 + controller.Response(data, err)
  39 +}
  40 +
  41 +func (controller *DictionaryController) ListDictionary() {
  42 + dictionaryService := service.NewDictionaryService(nil)
  43 + listDictionaryQuery := &query.ListDictionaryQuery{}
  44 + offset, _ := controller.GetInt("offset")
  45 + listDictionaryQuery.Offset = offset
  46 + limit, _ := controller.GetInt("limit")
  47 + listDictionaryQuery.Limit = limit
  48 + data, err := dictionaryService.ListDictionary(listDictionaryQuery)
  49 + controller.Response(data, err)
  50 +}
  1 +package routers
  2 +
  3 +import (
  4 + "github.com/beego/beego/v2/server/web"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/port/beego/controllers"
  6 +)
  7 +
  8 +func init() {
  9 + web.Router("/dictionarys/", &controllers.DictionaryController{}, "Post:CreateDictionary")
  10 + web.Router("/dictionarys/:dictionaryId", &controllers.DictionaryController{}, "Put:UpdateDictionary")
  11 + web.Router("/dictionarys/:dictionaryId", &controllers.DictionaryController{}, "Get:GetDictionary")
  12 + web.Router("/dictionarys/", &controllers.DictionaryController{}, "Get:ListDictionary")
  13 +}