package dao

import (
	"github.com/shopspring/decimal"
	"server/global"
)

type Constructor struct {
	global.GVA_MODEL
	Name     string          `json:"name" form:"name" gorm:"comment:施工人员姓名"`
	OnePrice decimal.Decimal `json:"onePrice" form:"onePrice" gorm:"type:decimal(10,2);not null;comment:人天单价"`
}

func (Constructor) TableName() string {
	return "constructor"
}

// QueryConstructorList 查询施工人员列表
func QueryConstructorList(limit, offset int, name string) (constructors []Constructor, total int64, err error) {
	// 创建db
	db := global.GVA_DB.Model(&Constructor{})
	// 如果有条件搜索 下方会自动创建搜索语句
	if name != "" {
		db = db.Where("people LIKE ?", "%"+name+"%")
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Order("id desc").Limit(limit).Offset(offset).Find(&constructors).Error
	return constructors, total, err
}

func QueryConstructor() (constructors []Constructor, err error) {
	err = global.GVA_DB.Model(&Constructor{}).Find(&constructors).Error
	return
}

// CreateConstructor 新增施工人员
func (c Constructor) CreateConstructor() error {
	return global.GVA_DB.Create(&c).Error
}

// UpdateConstructor 修改施工人员
func (c Constructor) UpdateConstructor() error {
	return global.GVA_DB.Model(&c).Where("id = ?", c.ID).Updates(&c).Error
}

// DeleteConstructor 删除施工人员
func (c Constructor) DeleteConstructor() error {
	return global.GVA_DB.Unscoped().Delete(&c).Error
}
