cooladmin 后端 查询记录

查询记录:pageQueryOp中列表查询的group by

node ts controller代码如下

TypeScript 复制代码
import { CoolController, BaseController } from '@cool-midway/core';
import { Inject, Post, Get, Param } from '@midwayjs/decorator';
import { ComparePricesPlanInfoEntity } from '../../entity/comparePricesPlanInfo';
import { InventoryOrderInfoService } from '../../service/inventoryOrderInfo';
import { PlanInfoEntity } from '../../entity/planInfo';
import { ConsumableDetailEntity } from '../../../consumable/entity/detail';
import { Context } from 'vm';
import { OrderEntity } from '../../entity/order';
import { ComparePricesEntity } from '../../entity/comparePrices';
import { ComparePricesInfoEntity } from '../../entity/comparePricesInfo';
import { VendorEntity } from '../../entity/vendor';
import { QueryBuilder } from 'typeorm';

/**
 * 描述
 */
@CoolController({
    api: ['add', 'delete', 'update', 'info', 'list', 'page'],
    entity: OrderEntity,
    service: InventoryOrderInfoService,
    pageQueryOp: {
        // keyWordLikeFields: ['vendorName'],
        // 指定返回字段
        select: [
            //采购单信息
            'a.id',
            'a.state',//状态:待确认(0)、已发货(1)、已收货(2)
            'a.inspectorName',//验收人员姓名
            'a.acceptanceContent',//验收内容
            'a.resultState',//验收结果状态:未验收(0)、通过(1)、不通过(2)
            'a.reason',//通过/不通过原因
            'a.createBy',//创建者
            'a.createTime',//创建时间
            'a.code',//订单编码
            'a.title',//订单标题
            'a.content',//订单内容
            'a.remark as orderRemark',//订单备注
            //比价
            'cp.comparePricesInfoId',//比价信息ID
            'cp.consumableDetailId',//耗材ID
            //采用价格信息
            'cpinfo.quotedPrice',//报价
            'cpinfo.serviceContent',//服务内容
            'cpinfo.deliveryTime',//货期
            'cpinfo.isProposed',//拟用采纳
            'cpinfo.vendorId',//供应商ID
            // 'cpinfo.consumableDetailId',//耗材ID
            'cpinfo.planInfoId',//计划明细表ID
            'cpinfo.vcid',//供应商-耗材关联表ID
            //耗材信息
            'c.consumableNo',
            'c.consumableName',
            'c.brand',
            'c.model',
            'c.type',
            'c.remark as consumableDetailRemark',
            'c.status',
            //供应商信息
            'v.vendorCode',
            'v.vendorName',
            'v.vendorNick',
            'v.vendorEn',
            //采购计划数量
            'SUM(pinfo.number) as sumNum',
            //采购计划规格
            'pinfo.specification',
            //采购单信息
            // 'a.id',
            // 'a.planInfoId',
            // 'a.comparePricesId',
            // 'a.createTime',
            // 'a.updateTime',
            // //采购计划详情
            // 'b.planId',
            // 'b.consumableDetailId',
            // 'b.specification',
            // 'b.number',
            // 'b.budget',
            // 'b.deliveryTime',
            // // 'b.createBy',
            // // 'b.createTime',
        ],
        // 联表查询
        join: [
            //ComparePricesEntity
            {
                entity: ComparePricesEntity,
                alias: 'cp',
                condition: 'a.comparePricesId = cp.id',
                type: 'leftJoin',
            },
            //ComparePricesInfoEntity
            {
                entity: ComparePricesInfoEntity,
                alias: 'cpinfo',
                condition: 'cp.comparePricesInfoId = cpinfo.id',
                type: 'leftJoin',
            },
            //ConsumableDetailEntity
            {
                entity: ConsumableDetailEntity,
                alias: 'c',
                condition: 'cp.consumableDetailId = c.id',
                type: 'leftJoin',
            },
            //VendorEntity
            {
                entity: VendorEntity,
                alias: 'v',
                condition: 'cpinfo.vendorId = v.id',
                type: 'leftJoin',
            },
            //ComparePricesPlanInfoEntity
            {
                entity: ComparePricesPlanInfoEntity,
                alias: 'cppinfo',
                condition: 'a.comparePricesId = cppinfo.comparePricesId',
                type: 'leftJoin',
            },
            //PlanInfoEntity
            {
                entity: PlanInfoEntity,
                alias: 'pinfo',
                condition: 'cppinfo.planInfoId = pinfo.id',
                type: 'leftJoin',
            },
            // {
            //     entity: OrderEntity,
            //     alias: 'o',
            //     condition: 'a.comparePricesId = o.comparePricesId',
            //     type: 'leftJoin',
            // },
        ],
        where: async (ctx: Context) => {
            return [
                // ['a.id not in (SELECT comparePricesId from pi_purchase_order)', {}],
                // ['a.comparePricesId = :comparePricesId', { comparePricesId: ctx.request.body.comparePricesId }]
            ];
        },
        extend: (queryBuilder: QueryBuilder<any>) => {
            queryBuilder.groupBy([
                'a.id',
                'pinfo.specification',
            ]);
        },
        addOrderBy: {
            'a.createTime': 'asc',
        },
    },
})
export class InventoryOrderInfoController extends BaseController {
    @Inject()
    inventoryOrderInfoService: InventoryOrderInfoService;
}

具体的SQL执行语句:

sql 复制代码
SELECT
	a.id,
	a.state,
	a.inspectorName,
	a.acceptanceContent,
	a.resultState,
	a.reason,
	a.createBy,
	a.createTime,
	a.CODE,
	a.title,
	a.content,
	a.remark AS orderRemark,
	cp.comparePricesInfoId,
	cp.consumableDetailId,
	cpinfo.quotedPrice,
	cpinfo.serviceContent,
	cpinfo.deliveryTime,
	cpinfo.isProposed,
	cpinfo.vendorId,
	cpinfo.planInfoId,
	cpinfo.vcid,
	c.consumableNo,
	c.consumableName,
	c.brand,
	c.model,
	c.type,
	c.remark AS consumableDetailRemark,
	c.STATUS,
	v.vendorCode,
	v.vendorName,
	v.vendorNick,
	v.vendorEn,
	SUM( pinfo.number ) AS sumNum,
	pinfo.specification 
FROM
	`pi_purchase_order` `a`
	LEFT JOIN `pi_compare_prices` `cp` ON `a`.`comparePricesId` = `cp`.`id`
	LEFT JOIN `pi_compare_prices_info` `cpinfo` ON `cp`.`comparePricesInfoId` = `cpinfo`.`id`
	LEFT JOIN `consumable_detail` `c` ON `cp`.`consumableDetailId` = `c`.`id`
	LEFT JOIN `pi_vendor` `v` ON `cpinfo`.`vendorId` = `v`.`id`
	LEFT JOIN `pi_compare_prices_plan_info` `cppinfo` ON `a`.`comparePricesId` = `cppinfo`.`comparePricesId`
	LEFT JOIN `pi_plan_info` `pinfo` ON `cppinfo`.`planInfoId` = `pinfo`.`id` 
GROUP BY
	`a`.`id`,
	pinfo.specification 
ORDER BY
	`a`.`createTime` ASC,
	`createTime` DESC
相关推荐
秋千码途1 小时前
一道同分排名的SQL题
数据库·sql
秋难降3 小时前
零基础学SQL(八)——事务
数据库·sql·mysql
Starry_hello world3 小时前
MySql 表的约束
数据库·笔记·mysql·有问必答
RestCloud4 小时前
ETLCloud中的数据转化规则是什么意思?怎么执行
数据库·数据仓库·etl
一个天蝎座 白勺 程序猿4 小时前
Apache IoTDB(4):深度解析时序数据库 IoTDB 在Kubernetes 集群中的部署与实践指南
数据库·深度学习·kubernetes·apache·时序数据库·iotdb
.Shu.4 小时前
Redis zset 渐进式rehash 实现原理、触发条件、执行流程以及数据一致性保障机制【分步源码解析】
数据库·redis·缓存
君不见,青丝成雪4 小时前
大数据技术栈 —— Redis与Kafka
数据库·redis·kafka
悟能不能悟5 小时前
排查Redis数据倾斜引发的性能瓶颈
java·数据库·redis
DemonAvenger5 小时前
事务管理:ACID特性与隔离级别详解
数据库·mysql·性能优化
xiao-xiang6 小时前
redis-集成prometheus监控(k8s)
数据库·redis·kubernetes·k8s·grafana·prometheus