calculator2
This commit is contained in:
parent
64594c0789
commit
ced1ff01cc
|
@ -0,0 +1,78 @@
|
||||||
|
package dao
|
||||||
|
|
||||||
|
import (
|
||||||
|
"enterprise/common/model"
|
||||||
|
"fmt"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SalaryCalculatorDao struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSalaryCalculatorDao() *SalaryCalculatorDao {
|
||||||
|
return &SalaryCalculatorDao{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *SalaryCalculatorDao) TableName() string {
|
||||||
|
return "salary_calculator"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *SalaryCalculatorDao) Create(o *model.SalaryCalculator) (int64, error) {
|
||||||
|
o.CreateTime = time.Now().Unix()
|
||||||
|
res := GetDB().Table(d.TableName()).Create(o)
|
||||||
|
return o.Id, res.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *SalaryCalculatorDao) Update(o *model.SalaryCalculator) error {
|
||||||
|
o.UpdateTime = time.Now().Unix()
|
||||||
|
tx := GetDB().Table(d.TableName())
|
||||||
|
res := tx.Save(o)
|
||||||
|
return res.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *SalaryCalculatorDao) Delete(id int64) error {
|
||||||
|
res := GetDB().Table(d.TableName()).Delete(&model.SalaryCalculator{}, id)
|
||||||
|
return res.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *SalaryCalculatorDao) Get(id int64) (*model.SalaryCalculator, error) {
|
||||||
|
var u model.SalaryCalculator
|
||||||
|
tx := GetDB().Table(d.TableName())
|
||||||
|
tx = tx.Where("id = ?", id)
|
||||||
|
res := tx.First(&u)
|
||||||
|
if res.Error == gorm.ErrRecordNotFound {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if res.Error != nil {
|
||||||
|
return nil, res.Error
|
||||||
|
}
|
||||||
|
return &u, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *SalaryCalculatorDao) Query(page, size int, corpId int64, name string) ([]*model.SalaryCalculator, int64, error) {
|
||||||
|
var u []*model.SalaryCalculator
|
||||||
|
tx := GetDB().Table(d.TableName())
|
||||||
|
|
||||||
|
tx.Where("corp_id = ?", corpId)
|
||||||
|
|
||||||
|
if name != "" {
|
||||||
|
tx = tx.Where("name LIKE ?", fmt.Sprintf("%%%s%%", name))
|
||||||
|
}
|
||||||
|
|
||||||
|
var count int64
|
||||||
|
tx.Count(&count)
|
||||||
|
tx.Offset((page - 1) * size).Limit(size)
|
||||||
|
tx.Order("create_time DESC")
|
||||||
|
|
||||||
|
res := tx.Find(&u)
|
||||||
|
if res.Error == gorm.ErrRecordNotFound {
|
||||||
|
return nil, 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if res.Error != nil {
|
||||||
|
return nil, 0, res.Error
|
||||||
|
}
|
||||||
|
return u, count, nil
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package model
|
||||||
|
|
||||||
|
type SalaryCalculator struct {
|
||||||
|
Id int64
|
||||||
|
CorpId int64
|
||||||
|
Name string
|
||||||
|
Expression string
|
||||||
|
CreateTime int64
|
||||||
|
UpdateTime int64
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"enterprise/common/model"
|
||||||
|
"github.com/smbrave/goutil"
|
||||||
|
"github.com/spf13/cast"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Calculator struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Expression string `json:"expression"`
|
||||||
|
CreateTime string `json:"create_time"`
|
||||||
|
UpdateTime string `json:"update_time"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ListCalculatorReq struct {
|
||||||
|
BaseRequest
|
||||||
|
Name string `form:"name"`
|
||||||
|
Expression string `form:"expression"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CreateCalculatorReq struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Expression string `json:"expression"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UpdateCalculatorReq struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Expression string `json:"expression"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Calculator) From(m *model.SalaryCalculator) {
|
||||||
|
c.Id = cast.ToString(m.Id)
|
||||||
|
c.Name = m.Name
|
||||||
|
c.Expression = m.Expression
|
||||||
|
c.CreateTime = goutil.TimeToDateTime(m.CreateTime)
|
||||||
|
c.UpdateTime = goutil.TimeToDateTime(m.UpdateTime)
|
||||||
|
}
|
|
@ -0,0 +1,76 @@
|
||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"enterprise/common/dao"
|
||||||
|
"enterprise/common/model"
|
||||||
|
"enterprise/server/api"
|
||||||
|
"enterprise/server/session"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/smbrave/goutil"
|
||||||
|
"github.com/spf13/cast"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Calculator struct{}
|
||||||
|
|
||||||
|
func NewCalculator() *Calculator {
|
||||||
|
return &Calculator{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create 创建一个新的计算项
|
||||||
|
func (c *Calculator) Create(ctx *gin.Context) {
|
||||||
|
sess := ctx.Keys[session.ContextSession].(*session.AdminSession)
|
||||||
|
var req api.CreateCalculatorReq
|
||||||
|
session.CheckParamError(ctx.ShouldBindJSON(&req))
|
||||||
|
|
||||||
|
calculator := new(model.SalaryCalculator)
|
||||||
|
calculator.CorpId = sess.GetAdmin().CorpId
|
||||||
|
calculator.Name = req.Name
|
||||||
|
calculator.Expression = req.Expression
|
||||||
|
_, err := dao.NewSalaryCalculatorDao().Create(calculator)
|
||||||
|
session.CheckDBError(err)
|
||||||
|
ctx.JSON(http.StatusOK, session.NewRspOk())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update 更新已有的计算项
|
||||||
|
func (c *Calculator) Update(ctx *gin.Context) {
|
||||||
|
var req api.UpdateCalculatorReq
|
||||||
|
session.CheckParamError(ctx.ShouldBindJSON(&req))
|
||||||
|
|
||||||
|
calculator, err := dao.NewSalaryCalculatorDao().Get(cast.ToInt64(req.Id))
|
||||||
|
session.CheckDBError(err)
|
||||||
|
session.CheckNilError(calculator, "计算项不存在")
|
||||||
|
|
||||||
|
calculator.Name = goutil.If(req.Name != "", req.Name, calculator.Name)
|
||||||
|
calculator.Expression = goutil.If(req.Expression != "", req.Expression, calculator.Expression)
|
||||||
|
|
||||||
|
err = dao.NewSalaryCalculatorDao().Update(calculator)
|
||||||
|
session.CheckDBError(err)
|
||||||
|
ctx.JSON(http.StatusOK, session.NewRspOk())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete 删除指定的计算项
|
||||||
|
func (c *Calculator) Delete(ctx *gin.Context) {
|
||||||
|
id := cast.ToInt64(ctx.Query("id"))
|
||||||
|
session.CheckDBError(dao.NewSalaryCalculatorDao().Delete(id))
|
||||||
|
ctx.JSON(http.StatusOK, session.NewRspOk())
|
||||||
|
}
|
||||||
|
|
||||||
|
// List 列出符合条件的计算项
|
||||||
|
func (c *Calculator) List(ctx *gin.Context) {
|
||||||
|
sess := ctx.Keys[session.ContextSession].(*session.AdminSession)
|
||||||
|
var req api.ListCalculatorReq
|
||||||
|
session.CheckParamError(ctx.ShouldBind(&req))
|
||||||
|
req.Default()
|
||||||
|
|
||||||
|
calculators, total, err := dao.NewSalaryCalculatorDao().Query(req.Page, req.Size, sess.GetCorpId(), req.Name)
|
||||||
|
session.CheckDBError(err)
|
||||||
|
items := make([]*api.Calculator, 0)
|
||||||
|
for _, calc := range calculators {
|
||||||
|
i := new(api.Calculator)
|
||||||
|
i.From(calc)
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.JSON(http.StatusOK, session.NewListRsp(total, items))
|
||||||
|
}
|
Loading…
Reference in New Issue