refactor: service module and dao module
This commit is contained in:
+26
-64
@@ -2,7 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"crm/common"
|
||||
"crm/global"
|
||||
"crm/dao"
|
||||
"crm/models"
|
||||
"crm/response"
|
||||
"strconv"
|
||||
@@ -11,23 +11,20 @@ import (
|
||||
)
|
||||
|
||||
type ContractService struct {
|
||||
contractDao *dao.ContractDao
|
||||
productDao *dao.ProductDao
|
||||
}
|
||||
|
||||
func NewContractService() *ContractService {
|
||||
return &ContractService{
|
||||
contractDao: dao.NewContractDao(),
|
||||
productDao: dao.NewProductDao(),
|
||||
}
|
||||
}
|
||||
|
||||
// 创建合同
|
||||
func (c *ContractService) Create(param *models.ContractCreateParam) int {
|
||||
contract := models.Contract{
|
||||
Name: param.Name,
|
||||
Amount: param.Amount,
|
||||
BeginTime: param.BeginTime,
|
||||
OverTime: param.OverTime,
|
||||
Remarks: param.Remarks,
|
||||
Cid: param.Cid,
|
||||
Productlist: param.Productlist,
|
||||
Status: param.Status,
|
||||
Creator: param.Creator,
|
||||
Created: time.Now().Unix(),
|
||||
}
|
||||
if err := global.Db.Create(&contract).Error; err != nil {
|
||||
if err := c.contractDao.Create(param); err != nil {
|
||||
return response.ErrCodeFailed
|
||||
}
|
||||
return response.ErrCodeSuccess
|
||||
@@ -35,20 +32,7 @@ func (c *ContractService) Create(param *models.ContractCreateParam) int {
|
||||
|
||||
// 更新合同
|
||||
func (c *ContractService) Update(param *models.ContractUpdateParam) int {
|
||||
contract := models.Contract{
|
||||
Id: param.Id,
|
||||
Name: param.Name,
|
||||
Amount: param.Amount,
|
||||
BeginTime: param.BeginTime,
|
||||
OverTime: param.OverTime,
|
||||
Remarks: param.Remarks,
|
||||
Cid: param.Cid,
|
||||
Productlist: param.Productlist,
|
||||
Status: param.Status,
|
||||
Updated: time.Now().Unix(),
|
||||
}
|
||||
db := global.Db.Model(&contract).Select("*").Omit("id", "creator", "created")
|
||||
if err := db.Updates(&contract).Error; err != nil {
|
||||
if err := c.contractDao.Update(param); err != nil {
|
||||
return response.ErrCodeFailed
|
||||
}
|
||||
return response.ErrCodeSuccess
|
||||
@@ -56,61 +40,42 @@ func (c *ContractService) Update(param *models.ContractUpdateParam) int {
|
||||
|
||||
// 删除合同
|
||||
func (c *ContractService) Delete(param *models.ContractDeleteParam) int {
|
||||
if err := global.Db.Delete(&models.Contract{}, param.Ids).Error; err != nil {
|
||||
if err := c.contractDao.Delete(param); err != nil {
|
||||
return response.ErrCodeFailed
|
||||
}
|
||||
return response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
// 查询合同列表
|
||||
func (c *ContractService) QueryList(param *models.ContractQueryParam) ([]*models.ContractList, int64, int) {
|
||||
contractList := make([]*models.ContractList, 0)
|
||||
s := "contract.id, contract.name, contract.amount, contract.begin_time, contract.over_time, customer.name as cname, contract.remarks, contract.status, contract.created, contract.updated"
|
||||
j := "inner join customer on contract.cid = customer.id and contract.creator = ?"
|
||||
|
||||
// 分页查询
|
||||
offset := (param.Page.PageNum - 1) * param.Page.PageSize
|
||||
mdb := global.Db.Offset(offset).Limit(param.Page.PageSize).Table(CONTRACT).Select(s)
|
||||
var err error
|
||||
if param.Id != 0 {
|
||||
err = mdb.Joins(j+" and contract.id = ?", param.Creator, param.Id).Scan(&contractList).Error
|
||||
} else {
|
||||
err = mdb.Joins(j, param.Creator).Scan(&contractList).Error
|
||||
}
|
||||
func (c *ContractService) GetList(param *models.ContractQueryParam) ([]*models.ContractList, int64, int) {
|
||||
contractList, rows, err := c.contractDao.GetList(param)
|
||||
if err != nil {
|
||||
return nil, 0, response.ErrCodeFailed
|
||||
return nil, NumberNull, response.ErrCodeFailed
|
||||
}
|
||||
var rows int64
|
||||
global.Db.Raw("select count(*) from contract where creator = ?", param.Creator).Scan(&rows)
|
||||
return contractList, rows, response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
// 查询合同信息
|
||||
func (c *ContractService) QueryInfo(param *models.ContractQueryParam) (*models.ContractInfo, int) {
|
||||
contract := models.Contract{
|
||||
Id: param.Id,
|
||||
}
|
||||
contractInfo := models.ContractInfo{}
|
||||
err := global.Db.Table(CONTRACT).Where(&contract).First(&contractInfo).Error
|
||||
func (c *ContractService) GetInfo(param *models.ContractQueryParam) (*models.ContractInfo, int) {
|
||||
contractInfo, err := c.contractDao.GetInfo(param)
|
||||
if err != nil {
|
||||
return nil, response.ErrCodeFailed
|
||||
}
|
||||
return &contractInfo, response.ErrCodeSuccess
|
||||
return contractInfo, response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
// 在编辑合同中,添加产品后,返回已添加的产品列表
|
||||
func (c *ContractService) QueryPlist(param *models.ContractQueryParam) ([]*models.Products, int) {
|
||||
func (c *ContractService) GetProductList(param *models.ContractQueryParam) ([]*models.Products, int) {
|
||||
if param.Id == 0 {
|
||||
products := make([]*models.Products, 0)
|
||||
if err := global.Db.Table(PRODUCT).Find(&products, param.Pids).Error; err != nil {
|
||||
products, err := c.productDao.GetListByIds(param.Pids)
|
||||
if err != nil {
|
||||
return nil, response.ErrCodeFailed
|
||||
}
|
||||
return products, response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
// 默认已添加的产品列表
|
||||
var contract models.Contract
|
||||
err := global.Db.Table(CONTRACT).Select("productlist").First(&contract, param.Id).Error
|
||||
contract, err := c.contractDao.GetAddedPList(param.Id)
|
||||
if err != nil {
|
||||
return nil, response.ErrCodeFailed
|
||||
}
|
||||
@@ -136,8 +101,8 @@ func (c *ContractService) QueryPlist(param *models.ContractQueryParam) ([]*model
|
||||
addedPids = append(addedPids, pid)
|
||||
}
|
||||
}
|
||||
products := make([]*models.Products, 0)
|
||||
if err := global.Db.Table(PRODUCT).Find(&products, addedPids).Error; err != nil {
|
||||
products, err := c.productDao.GetListByIds(addedPids)
|
||||
if err != nil {
|
||||
return nil, response.ErrCodeFailed
|
||||
}
|
||||
addedProductList = append(addedProductList, products...)
|
||||
@@ -146,10 +111,7 @@ func (c *ContractService) QueryPlist(param *models.ContractQueryParam) ([]*model
|
||||
|
||||
// 导出Excel文件
|
||||
func (c *ContractService) Export(uid int64) (string, int) {
|
||||
contracts := make([]models.ContractList, 0)
|
||||
s := "contract.id, contract.name, contract.amount, contract.begin_time, contract.over_time, customer.name as cname, contract.remarks, contract.status, contract.created, contract.updated"
|
||||
j := "left join customer on contract.cid = customer.id and contract.creator = ?"
|
||||
err := global.Db.Table(CONTRACT).Select(s).Joins(j, uid).Scan(&contracts).Error
|
||||
contracts, err := c.contractDao.GetListByUid(uid)
|
||||
if err != nil {
|
||||
return StringNull, response.ErrCodeFailed
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user