完善配送订单创建联动
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// 功能描述:实现配送点范围内的合同、合同气瓶和配送订单接口。
|
||||
// 版本:v1.1.0。
|
||||
// 版本:v1.2.0。
|
||||
package delivery
|
||||
|
||||
import (
|
||||
@@ -49,23 +49,33 @@ func scopedContract(ctx *gin.Context, identity string, pointID uint64) (models.G
|
||||
return contract, true
|
||||
}
|
||||
|
||||
// deliveryContractListQuery 构造当前配送点合同列表;订单候选模式额外限定可履约状态。
|
||||
func deliveryContractListQuery(database *gorm.DB, gasID, pointID uint64, candidate string, now time.Time) *gorm.DB {
|
||||
query := platformgasorder.ContractPartyDisplayQuery(database).
|
||||
Where("gasorder_contract.gas_basic_id = ? AND gasorder_contract.delivery_basic_id = ?", gasID, pointID)
|
||||
if candidate == "order" {
|
||||
return platformgasorder.FilterGasorderContractCandidates(query, "order", now)
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
func ListContract(ctx *gin.Context) {
|
||||
point, _, ok := currentScope(ctx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
page, size := common.PageSize(ctx)
|
||||
query := common.ApplyKeywordFilter(ctx,
|
||||
common.ActiveRecords(db().Model(&models.GasorderContract{})).
|
||||
Where("gas_basic_id = ? AND delivery_basic_id = ?", point.GasBasicID, point.ID),
|
||||
&models.GasorderContract{})
|
||||
query := deliveryContractListQuery(
|
||||
db(), point.GasBasicID, point.ID, strings.TrimSpace(ctx.Query("candidate")), time.Now(),
|
||||
)
|
||||
query = common.ApplyKeywordFilter(ctx, query, &models.GasorderContract{})
|
||||
var total int64
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
var list []models.GasorderContract
|
||||
if err := query.Order("gasorder_contract.created_at desc").Offset((page - 1) * size).Limit(size).Find(&list).Error; err != nil {
|
||||
var list []platformgasorder.ContractPartyDisplay
|
||||
if err := query.Order("gasorder_contract.created_at desc").Offset((page - 1) * size).Limit(size).Scan(&list).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
@@ -74,7 +84,11 @@ func ListContract(ctx *gin.Context) {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
protected, err := protectDeliveryContractListResponse(ctx, response, list)
|
||||
contracts := make([]models.GasorderContract, 0, len(list))
|
||||
for _, item := range list {
|
||||
contracts = append(contracts, item.GasorderContract)
|
||||
}
|
||||
protected, err := protectDeliveryContractListResponse(ctx, response, contracts)
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
@@ -186,15 +200,63 @@ func TerminateContract(ctx *gin.Context) {
|
||||
withContract(ctx, platformgasorder.TerminateGasorderContract)
|
||||
}
|
||||
|
||||
// deliveryContractProductQuery 构造当前配送点合同气瓶查询;指定合同时仅保留未解绑候选。
|
||||
func deliveryContractProductQuery(database *gorm.DB, pointID, contractID uint64, keyword string) *gorm.DB {
|
||||
query := common.ActiveRecords(database.Model(&models.GasorderContractProduct{})).
|
||||
Select("gasorder_contract_product.*, COALESCE(display_product.name, '') AS product_name").
|
||||
Joins("JOIN gasorder_contract ON gasorder_contract.id = gasorder_contract_product.gasorder_contract_id").
|
||||
Joins("LEFT JOIN product_info AS display_product ON display_product.id = gasorder_contract_product.product_info_id AND display_product.deleted_at IS NULL AND display_product.status <> ?", common.StatusArchived).
|
||||
Where("gasorder_contract.delivery_basic_id = ?", pointID)
|
||||
if contractID > 0 {
|
||||
query = query.Where("gasorder_contract_product.gasorder_contract_id = ? AND gasorder_contract_product.unbound_at IS NULL", contractID)
|
||||
}
|
||||
keyword = strings.ToLower(strings.TrimSpace(keyword))
|
||||
if keyword == "" {
|
||||
return query
|
||||
}
|
||||
pattern := "%" + keyword + "%"
|
||||
return query.Where("(LOWER(COALESCE(display_product.name, '')) LIKE ? OR LOWER(gasorder_contract_product.product_code) LIKE ? OR LOWER(gasorder_contract_product.product_type_name) LIKE ?)", pattern, pattern, pattern)
|
||||
}
|
||||
|
||||
func ListContractProduct(ctx *gin.Context) {
|
||||
point, _, ok := currentScope(ctx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
query := common.ActiveRecords(db().Model(&models.GasorderContractProduct{})).
|
||||
Joins("JOIN gasorder_contract ON gasorder_contract.id = gasorder_contract_product.gasorder_contract_id").
|
||||
Where("gasorder_contract.delivery_basic_id = ?", point.ID)
|
||||
listScoped(ctx, &models.GasorderContractProduct{}, query, "gasorder_contract_product.created_at desc")
|
||||
contractIdentity := strings.TrimSpace(ctx.Query("contract_identity"))
|
||||
var contractID uint64
|
||||
if contractIdentity != "" {
|
||||
contract, valid := scopedContract(ctx, contractIdentity, point.ID)
|
||||
if !valid {
|
||||
return
|
||||
}
|
||||
contractID = contract.ID
|
||||
}
|
||||
query := deliveryContractProductQuery(db(), point.ID, contractID, ctx.Query("keyword"))
|
||||
page, size := common.PageSize(ctx)
|
||||
var total int64
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
type contractProductDisplay struct {
|
||||
models.GasorderContractProduct
|
||||
ProductName string `gorm:"column:product_name" json:"product_name"`
|
||||
}
|
||||
var list []contractProductDisplay
|
||||
if err := query.Order("gasorder_contract_product.created_at desc").Offset((page - 1) * size).Limit(size).Scan(&list).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
response, err := common.PublicResourceResponse(list)
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(ctx, gin.H{
|
||||
"total": total,
|
||||
"list": common.ProtectPreciseLocation(ctx, &models.GasorderContractProduct{}, response),
|
||||
})
|
||||
}
|
||||
|
||||
func GetContractProduct(ctx *gin.Context) {
|
||||
|
||||
Reference in New Issue
Block a user