邱宇豪
2023-11-03 2896d6fa447cf6a678ba27ba2e19224379f14dd4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.thhy.materials.modules.biz.rsbmaterials.mapper.RsbMaterialsMapper">
 
    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        t.id as id,
        t.rsb_id as rsbId,
        t.material_name as materialName,
        t.material_value as materialValue
    </sql>
 
    <sql id="condition_query">
        <where>
            <trim suffixOverrides=" AND ">
                <if test="rsbId!=null and rsbId!=''">
                    t.rsb_id = #{rsbId} AND
                </if>
                <if test="materialName!=null and materialName!=''">
                    t.material_name = #{materialName} AND
                </if>
                <if test="materialValue!=null">
                    t.material_value = #{materialValue} AND
                </if>
            </trim>
        </where>
    </sql>
 
    <select id="queryById" resultType="com.thhy.materials.modules.biz.rsbmaterials.entity.RsbMaterials">
        select <include refid="Base_Column_List" />
        from t_rsb_materials t
        where t.id=#{id}
    </select>
 
    <select id="queryVersionById" resultType="integer">
        select version from t_rsb_materials
        where id=#{id}
    </select>
 
    <!--查询列表-->
    <select id="findList" resultType="com.thhy.materials.modules.biz.rsbmaterials.entity.RsbMaterials">
        SELECT
        <include refid="Base_Column_List" />
        from t_rsb_materials t
        <include refid="condition_query" />
    </select>
 
    <!--查询列表-->
    <select id="findAll" resultType="com.thhy.materials.modules.biz.rsbmaterials.entity.RsbMaterials">
        SELECT
        <include refid="Base_Column_List" />
        from t_rsb_materials t
    </select>
 
    <!--插入操作-->
    <insert id="insert">
        insert into t_rsb_materials
        <trim prefix="(" suffix=")" suffixOverrides="," >
            <if test="id != null">
                id,
            </if>
            <if test="rsbId != null">
                rsb_id,
            </if>
            <if test="materialName != null">
                material_name,
            </if>
            <if test="materialValue != null">
                material_value,
            </if>
        </trim>
 
        <trim prefix="values (" suffix=")" suffixOverrides="," >
            <if test="id != null">
                #{id},
            </if>
            <if test="rsbId != null">
                #{rsbId},
            </if>
            <if test="materialName != null">
                #{materialName},
            </if>
            <if test="materialValue != null">
                #{materialValue},
            </if>
        </trim>
    </insert>
 
 
    <!--更新操作-->
    <update id="update">
        update t_rsb_materials
        <set>
            <if test="rsbId != null">
                rsb_id=#{rsbId},
            </if>
            <if test="materialName != null">
                material_name=#{materialName},
            </if>
            <if test="materialValue != null">
                material_value=#{materialValue},
            </if>
        </set>
        where id=#{id}
    </update>
 
    <!--逻辑删除-->
    <update id="deletelogic">
        update t_rsb_materials
        SET is_use = 0
        where id=#{id}
    </update>
 
    <!--根据ID删除-->
    <delete id="deleteById">
        delete from t_rsb_materials
        where id=#{id}
    </delete>
 
    <delete id="deleteByRabPlan">
        delete from t_rsb_materials
        where rsb_id = #{rsbId}
    </delete>
 
 
    <select id="queryValueMonth" resultType="com.thhy.materials.modules.biz.materialsplan.entity.StatVo">
        SELECT DATE_FORMAT(a.month,'%Y-%m') as shuDate, case when b.materialValue is null then 0 else b.materialValue end as materialValue from (
 
        SELECT @s := DATE_ADD(@s,INTERVAL 1 MONTH) as `month` FROM mysql.help_topic,(SELECT @s := DATE_ADD(#{startTime},INTERVAL -1 MONTH)) temp WHERE @s &lt; DATE_ADD(#{endTime},INTERVAL -1 MONTH)) a
        left join
 
        (select a.ym,sum(a.material_value) as materialValue from
        (
            select mmi.material_name,mmi.material_value,DATE_FORMAT(mm.create_time,'%Y-%m') as ym
            from t_mix_material_info mmi
            left join t_mix_material mm on mm.id = mmi.mix_material_id
            where mm.is_use = 1 and mmi.material_name = #{materialName} and mm.company_id = #{companyId} and mm.create_time BETWEEN #{startTime} AND DATE_ADD(DATE_ADD(DATE_FORMAT(#{endTime},'%Y-%m-01 23:59:59'),INTERVAL 1 MONTH),INTERVAL -1 DAY)
        ) a
        group by a.ym) b on DATE_FORMAT(a.month,'%Y-%m') = b.ym
    </select>
 
    <select id="queryValueDay" resultType="com.thhy.materials.modules.biz.materialsplan.entity.StatVo">
        SELECT DATE_FORMAT(a.month,'%Y-%m-%d') as shuDate, case when b.materialValue is null then 0 else b.materialValue end as materialValue from (
 
        SELECT @s := DATE_ADD(@s,INTERVAL 1 DAY) as `month` FROM mysql.help_topic,(SELECT @s := DATE_ADD(#{startTime},INTERVAL -1 DAY)) temp WHERE @s &lt; #{endTime}) a
        left join
 
        (
        select a.ym,sum(a.material_value) as materialValue FROM
        (
        select mmi.material_name,mmi.material_value,DATE_FORMAT(mm.create_time,'%Y-%m-%d') as ym
        from t_mix_material_info mmi
        left join t_mix_material mm on mm.id = mmi.mix_material_id
        where mm.is_use = 1 and mmi.material_name = #{materialName} and mm.company_id = #{companyId} and mm.create_time BETWEEN #{startTime} AND #{endTime}
        ) a group by a.ym
        ) b on DATE_FORMAT(a.month,'%Y-%m-%d') = b.ym
    </select>
 
    <select id="queryMaterials" resultType="com.thhy.materials.modules.biz.materialsplan.entity.MaterialVo">
        <!--select sd.dict_name as materialCName,a.material_name as materialName,a.material_value as materialValue from
            (select rm.material_name,sum(rm.material_value) as material_value
             from t_rsb_materials rm
                      left join t_rsb_material_plan rmp on rmp.id = rm.rsb_id
             where rmp.create_time BETWEEN #{startTime} and #{endTime}
             group by rm.material_name) a
                left join sys_dict sd on sd.dict_id = a.material_name
        order by sd.dict_value-->
 
        select sd.dict_id as materialName,sd.dict_name as materialCName,
        (
        select case when sum(mmi.material_value) is null then 0 else sum(mmi.material_value) end as material_value
        from t_mix_material_info mmi
        left join t_mix_material tmm on mmi.mix_material_id = tmm.id
        where tmm.is_use = 1 and mmi.material_name = sd.dict_id and tmm.company_id = #{companyId}
          <if test="dateType==1">
              and (tmm.create_time BETWEEN #{startTime} and #{endTime})
          </if>
            <if test="dateType==2">
                and DATE_FORMAT(tmm.create_time ,'%Y-%m') &gt;= DATE_FORMAT(#{startDay} ,'%Y-%m')
                and DATE_FORMAT(tmm.create_time ,'%Y-%m') &lt;= DATE_FORMAT(#{endDay} ,'%Y-%m')
            </if>
        ) as materialValue
        from sys_dict sd where sd.dict_type = "pipe_materials" order by sd.dict_value
    </select>
 
    <select id="queryAllMaterial" resultType="com.thhy.materials.modules.biz.materialsplan.entity.StatResultVo">
        select dict_id as materialName,dict_name as materialCName
        from sys_dict
        where dict_type = 'pipe_materials'
        <if test="materialName !=null and materialName !=''">
            AND dict_id = #{materialName}
        </if>
        order by dict_value
    </select>
 
 
 
</mapper>