有一个业务sql,它有一些任性,它还有一点麻烦.
首先这个sql的业务通过搜索到该条目上个时间点的一个字段的值,填充回要插入的数据的字段
整体是要根据不同的物品名或其他判定字段,来进行新插入数据的期初数量的填充
这里比如上图 ID 5 的篮球的期初数量是根据上一个时间点的结余数量来填充的
而不能是ID 1羽毛球的结余数量3,也不能是ID 3的不是挨着的上个时间点的结余5
这样再去统计库存的不同时间段区间搜索时就有效了
通过统计不同时间区间的GROUP BY下的物品,来看这个区间的库存量(包含上一个时间的期初数量)
所以上图中,如果搜索到的时间点是介于包含ID4,5,那么当时获取的期初库存就是5,即该物品的ID3时候的结余量
测试通过的SQL:
<!--物料日志表,这些是可以重复物料名称(mingchengguige)的子数据(详细)--> <!--其中期初库存字段插入时,不用判断是否为空,值为写好的子sql,--> <!--fachuleixing根据正常发出(出库单),破损发出(破损单),维修发出(维修单)--> <!--qichukucun和jieyushuliang不需要判定为空与否,直接新增(首先进行了计算)--> <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.tansuo365.test1.bean.WuliaoKucunRiZhi" useGeneratedKeys="true"> insert into wuliaokucunrizhi <trim prefix="(" suffix=")" suffixOverrides=","> <if test="mingchengguige != null"> mingchengguige, </if> qichukucun, <if test="shourushuliang != null"> shourushuliang, </if> <if test="fachushuliang != null"> fachushuliang, </if> <if test="tuihuishuliang != null"> tuihuishuliang, </if> jieyushuliang, <if test="jiagongyigong != null"> jiagongyigong, </if> <if test="danwei != null"> danwei, </if> <if test="wuliaobianma != null"> wuliaobianma, </if> <if test="dalei != null"> dalei, </if> <if test="rukukufang != null"> rukukufang, </if> <if test="xiangmuweihu != null"> xiangmuweihu, </if> <if test="fachuleixing != null"> fachuleixing, </if> <if test="createtime != null"> createtime, </if> <if test="updatetime != null"> updatetime, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="mingchengguige != null"> #{mingchengguige}, </if> IFNULL((select jie.jieyushuliang from (select jieyushuliang from wuliaokucunrizhi <where> <if test="mingchengguige != null and mingchengguige != ‘‘"> and mingchengguige = #{mingchengguige} </if> <if test="rukukufang != null and rukukufang != ‘‘ "> and rukukufang = #{rukukufang} </if> <if test="dalei != null and dalei != ‘‘ "> and dalei = #{dalei} </if> <if test="xiangmuweihu != null and xiangmuweihu != ‘‘ "> and xiangmuweihu = #{xiangmuweihu} </if> and createtime < (select now()) </where> order by createtime desc limit 1) jie),0), <if test="shourushuliang != null"> #{shourushuliang}, </if> <if test="fachushuliang != null"> #{fachushuliang}, </if> <if test="tuihuishuliang != null"> #{tuihuishuliang}, </if> IFNULL((select jie.jieyushuliang from (select jieyushuliang from wuliaokucunrizhi where createtime <(select now()) order by createtime desc limit 1) jie),0) +IFNULL(#{shourushuliang},0) -IFNULL(#{fachushuliang},0) +IFNULL(#{tuihuishuliang},0), <if test="jiagongyigong != null"> #{jiagongyigong}, </if> <if test="danwei != null"> #{danwei}, </if> <if test="wuliaobianma != null"> #{wuliaobianma}, </if> <if test="dalei != null"> #{dalei}, </if> <if test="rukukufang != null"> #{rukukufang}, </if> <if test="xiangmuweihu != null"> #{xiangmuweihu}, </if> <if test="fachuleixing != null"> #{fachuleixing}, </if> <if test="createtime != null"> #{createtime}, </if> <if test="updatetime != null"> #{updatetime}, </if> </trim> </insert>
注意其中的
IFNULL((select jie.jieyushuliang from (select jieyushuliang from wuliaokucunrizhi <where> <if test="mingchengguige != null and mingchengguige != ‘‘"> and mingchengguige = #{mingchengguige} </if> <if test="rukukufang != null and rukukufang != ‘‘ "> and rukukufang = #{rukukufang} </if> <if test="dalei != null and dalei != ‘‘ "> and dalei = #{dalei} </if> <if test="xiangmuweihu != null and xiangmuweihu != ‘‘ "> and xiangmuweihu = #{xiangmuweihu} </if> and createtime < (select now()) </where> order by createtime desc limit 1) jie),0),
这段代码是获取上个时间点的结余数量作为新数据的期初库存数量的
同时还有jieyushuliang
jieyushuliang,
而这段代码是计算了当时的结余数量
IFNULL((select jie.jieyushuliang from (select jieyushuliang from wuliaokucunrizhi where createtime <(select now()) order by createtime desc limit 1) jie),0) +IFNULL(#{shourushuliang},0) -IFNULL(#{fachushuliang},0) +IFNULL(#{tuihuishuliang},0),
在下面代码
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.tansuo365.test1.bean.WuliaoKucunRiZhi" useGeneratedKeys="true"> insert into wuliaokucunrizhi <trim prefix="(" suffix=")" suffixOverrides=","> <if test="mingchengguige != null"> mingchengguige, </if> qichukucun,
这里没有进行qichukucun字段的判空,而直接会选定这个字段要进行数据插入
执行结果
==> Preparing: insert into wuliaokucunrizhi ( mingchengguige, qichukucun, shourushuliang, jieyushuliang, jiagongyigong, danwei, wuliaobianma, dalei, rukukufang, xiangmuweihu ) values ( ?, IFNULL((select jie.jieyushuliang from (select jieyushuliang from wuliaokucunrizhi WHERE mingchengguige = ? and rukukufang = ? and dalei = ? and xiangmuweihu = ? and createtime < (select now()) order by createtime desc limit 1) jie),0), ?, IFNULL((select jie.jieyushuliang from (select jieyushuliang from wuliaokucunrizhi where createtime <(select now()) order by createtime desc limit 1) jie),0) +IFNULL(?,0) -IFNULL(?,0) +IFNULL(?,0), ?, ?, ?, ?, ?, ? ) ==> Parameters: 甲板(String), 甲板(String), A(String), 甲类(String), 甲项目1(String), 3.0(Double), 3.0(Double), null, null, 甲供(String), 吨(String), jb1101(String), 甲类(String), A(String), 甲项目1(String) <== Updates: 1
注意在这里的数值在数据库中都设定了默认值0
在获取不到期初库存(比如该物品第一条),和其它的字段数值时,会默认填0,也利于了结余的计算
原文地址:https://www.cnblogs.com/ukzq/p/12118380.html
时间: 2024-10-12 19:11:33