【springmvc+mybatis项目实战】杰信商贸-21.合同总金额SQL

我们的购销合同的总金额由以下金额构成:货物的总金额,而货物的总金额由货物本身的金额+附件金额构成,也就是说,我们想要获取购销合同的总金额,就要获取合同下的所有货物的总金额(货物总金额要获取所有货物的金额,以及这个货物下的所有附件的总金额)。

听起来有点绕,但是我们把它写成sql语句,分好层次,我们就能够清晰的理解购销合同的总金额是如何来的了:

1)可以通过货物的新增时,同步计算货物的总金额和和合同的总金额;在附件新增时,同步计算附件的总金额和合同的总金额;(程序来完成)(修改、删除时重新计算)
2)SQL查询实现
货物的总金额
[sql]  view plain copy
  1. select sum(cnumber*price) as cptotal from contract_product_c  
  2. where contract_id ='471e562b-bfa5-4ba7-a2b5-17e9b0d40179'  

附件的总金额
[sql]  view plain copy
  1. select sum(cnumber*price) as exttotal from ext_cproduct_c  
  2.        where contract_product_id in (select contract_product_id from contract_product_c where contract_id=c.contract_id)  

最终
[sql]  view plain copy
  1. select   
  2.   (select count(*) from contract_product_c  
  3.   where contract_id=c.contract_id)  as cpnum,  
  4.   (select count(*) from ext_cproduct_c  
  5.           where contract_product_id in (select contract_product_id from contract_product_c where contract_id=c.contract_id)  
  6.       ) as extnum,  
  7.   (   
  8.       nvl((select sum(cnumber*price) as cptotal from contract_product_c  
  9.       where contract_id =c.contract_id),0)  
  10.       +  
  11.       nvl(  
  12.       (select sum(cnumber*price) as exttotal from ext_cproduct_c  
  13.        where contract_product_id in (select contract_product_id from contract_product_c where contract_id=c.contract_id))  
  14.        ,0)  
  15.   ) as total_amount,  
  16. c.CONTRACT_ID,c.OFFEROR,c.CONTRACT_NO,c.SIGNING_DATE,c.INPUT_BY,c.CHECK_BY,c.INSPECTOR,c.IMPORT_NUM,c.CREQUEST,c.CUSTOM_NAME,c.DELIVERY_PERIOD,c.SHIP_TIME,c.TRADE_TERMS,c.REMARK,c.PRINT_STYLE,c.OLD_STATE,c.STATE,c.OUT_STATE  
  17.   
  18. from contract_c c  
注:nvl(a,b),nvl函数是Oracle的一个函数(其他数据库要进行修改),参数a是取出的值,参数b是当a值为空null的时候值默认为b值。

我们修改ContractMapper的find配置语句块:
[html]  view plain copy
  1. <!-- 查询多个 -->  
  2.     <!-- 如果支持异构数据,必须针对每个数据库写一个SQL语句,因为使用了底层数据函数nvl -->  
  3.     <select id="find" parameterType="map" resultMap="contractRM">  
  4.         select   
  5.         (select count(*) from contract_product_c  
  6.          where contract_id=c.contract_id)  as cpnum,  
  7.         (select count(*) from ext_cproduct_c  
  8.           where contract_product_id in (select contract_product_id from   
  9.           contract_product_c where contract_id=c.contract_id)  
  10.          ) as extnum,  
  11.         ( nvl((select sum(cnumber*price) as cptotal from contract_product_c  
  12.           where contract_id =c.contract_id),0)  
  13.             +  
  14.           nvl((select sum(cnumber*price) as exttotal from ext_cproduct_c  
  15.           where contract_product_id in (select contract_product_id from   
  16.           contract_product_c where contract_id=c.contract_id)),0)  
  17.           ) as total_amount,  
  18.         c.CONTRACT_ID,c.OFFEROR,c.CONTRACT_NO,c.SIGNING_DATE,c.INPUT_BY,c.CHECK_BY,  
  19.         c.INSPECTOR,c.IMPORT_NUM,c.CREQUEST,c.CUSTOM_NAME,c.DELIVERY_PERIOD,c.SHIP_TIME,c.  
  20.         TRADE_TERMS,c.REMARK,c.PRINT_STYLE,c.OLD_STATE,c.STATE,c.OUT_STATE  
  21.         from contract_c c  
  22.     </select>  

接下来我们重启服务器来测试:
我们可以看到,这个购销合同的总结是1600

我们来检验一下是不是1600。

首先,它有两个货物

总金额为:200+1200=1400。然后看一下附件,只有一件附件


附件总金额为200,所以最终总金额为1400+200=1600,和我们程序计算的一模一样。我们的总金额计算方法成功!


但是,我们要在增加、删除以及修改的时候对总金额进行更新,所以要修改一部分代码,这里我们就不在赘述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值