liujie
昨天 c983a0a398504697a777ffae48e59961b5ee2bd7
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
<?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.ruoyi.user.mapper.AccountProviderMapper">
 
    <resultMap type="AccountProvider" id="AccountProviderResult">
        <id     property="id"           column="id"            />
        <result property="userId"       column="user_id"       />
        <result property="companyName"  column="company_name"  />
        <result property="contactName"  column="contact_name"  />
        <result property="contactPhone" column="contact_phone" />
        <result property="creditCode"   column="credit_code"   />
        <result property="legalPerson"  column="legal_person"  />
        <result property="region"       column="region"        />
        <result property="registerDate" column="register_date" />
        <result property="address"      column="address"       />
        <result property="licenseUrl"   column="license_url"   />
        <result property="l1Quota"      column="l1_quota"      />
        <result property="l2Quota"      column="l2_quota"      />
        <result property="l3Quota"      column="l3_quota"      />
        <result property="status"       column="status"        />
        <result property="delFlag"      column="del_flag"      />
        <result property="createBy"     column="create_by"     />
        <result property="createTime"   column="create_time"   />
        <result property="updateBy"     column="update_by"     />
        <result property="updateTime"   column="update_time"   />
        <result property="remark"       column="remark"        />
    </resultMap>
 
    <resultMap type="AccountProviderVo" id="AccountProviderVoResult" extends="AccountProviderResult">
        <result property="userName"   column="user_name"   />
        <result property="l1Granted"  column="l1_granted"  />
        <result property="l2Granted"  column="l2_granted"  />
        <result property="l3Granted"  column="l3_granted"  />
    </resultMap>
 
    <!--
        服务商列表。两个 join 都必须是 1:1,否则 PageHelper 的 count SQL 与行数会一起被放大
        (列表出现重复行,total 也对不上):
          * u:account_user.user_id 是主键,1:1;
          * g:按 supplier_id 分组的派生表,对一个 supplier_id 只出一行,也是 1:1。
        反例是 account_certification——它没有 UNIQUE(user_id)(审核流转要留历史行),
        一 join 就会让某些服务商出多行,故这里坚决不 join 它。
 
        已授权数只数 del_flag = '0':口径是「发出去多少」,不是「还剩多少有效」。
        派生表里 supplier_id is not null 可省(NULL 永远匹配不上 p.user_id),
        写上只是让派生表小一点。没有任何授权时 g 侧为 NULL,前端按 0 渲染。
 
        筛选「服务商名称」同时糊匹配登录账号/昵称/联系人三列:列表的「账号」与「管理人」
        分属两列,而运营口中的「服务商名称」两种叫法都有,故一并匹配,不是冗余条件。
        注意这条条件字面量挂在 u 上,会让 LEFT JOIN 退化成 INNER——账号被逻辑删除的孤儿行
        在填了这个关键字后查不出来。可接受(孤儿行本来也点不进详情),但不填关键字时它要照常出现。
    -->
    <select id="selectProviderList" parameterType="AccountProviderVo" resultMap="AccountProviderVoResult">
        select p.id, p.user_id, p.company_name, p.contact_name, p.contact_phone,
               p.credit_code, p.legal_person, p.region, p.register_date, p.address, p.license_url,
               p.l1_quota, p.l2_quota, p.l3_quota, p.status, p.del_flag,
               p.create_by, p.create_time, p.update_by, p.update_time, p.remark,
               u.username as user_name,
               g.l1_granted, g.l2_granted, g.l3_granted
        from account_provider p
        left join account_user u on u.user_id = p.user_id and u.del_flag = '0'
        left join (
            select supplier_id,
                   sum(system_level = 'L1') as l1_granted,
                   sum(system_level = 'L2') as l2_granted,
                   sum(system_level = 'L3') as l3_granted
            from account_user
            where del_flag = '0' and supplier_id is not null
            group by supplier_id
        ) g on g.supplier_id = p.user_id
        <where>
            p.del_flag = '0'
            <if test="providerName != null and providerName != ''">
                and (u.username     like concat('%', #{providerName}, '%')
                     or u.nickname  like concat('%', #{providerName}, '%')
                     or p.contact_name like concat('%', #{providerName}, '%'))
            </if>
            <if test="companyName != null and companyName != ''"> and p.company_name like concat('%', #{companyName}, '%')</if>
            <if test="status != null and status != ''"> and p.status = #{status}</if>
        </where>
        order by p.id desc
    </select>
 
    <!--
        表单回填(新增后查看与修改共用):只多取一个登录账号,不取密码。
        账号侧同样带 del_flag = '0',账号被删则 user_name 为 NULL,前端显示为空。
    -->
    <select id="selectProviderById" parameterType="Long" resultMap="AccountProviderVoResult">
        select p.id, p.user_id, p.company_name, p.contact_name, p.contact_phone,
               p.credit_code, p.legal_person, p.region, p.register_date, p.address, p.license_url,
               p.l1_quota, p.l2_quota, p.l3_quota, p.status, p.del_flag,
               p.create_by, p.create_time, p.update_by, p.update_time, p.remark,
               u.username as user_name
        from account_provider p
        left join account_user u on u.user_id = p.user_id and u.del_flag = '0'
        where p.id = #{id} and p.del_flag = '0'
    </select>
 
    <!-- 权益数量不传即落库默认 0,故走 <if> 即可,不必在服务层兜底补 0 -->
    <insert id="insertProvider" parameterType="AccountProvider" useGeneratedKeys="true" keyProperty="id">
        insert into account_provider(
            <if test="userId != null">user_id,</if>
            <if test="companyName != null">company_name,</if>
            <if test="contactName != null">contact_name,</if>
            <if test="contactPhone != null">contact_phone,</if>
            <if test="creditCode != null">credit_code,</if>
            <if test="legalPerson != null">legal_person,</if>
            <if test="region != null">region,</if>
            <if test="registerDate != null">register_date,</if>
            <if test="address != null">address,</if>
            <if test="licenseUrl != null">license_url,</if>
            <if test="l1Quota != null">l1_quota,</if>
            <if test="l2Quota != null">l2_quota,</if>
            <if test="l3Quota != null">l3_quota,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            create_time
        )values(
            <if test="userId != null">#{userId},</if>
            <if test="companyName != null">#{companyName},</if>
            <if test="contactName != null">#{contactName},</if>
            <if test="contactPhone != null">#{contactPhone},</if>
            <if test="creditCode != null">#{creditCode},</if>
            <if test="legalPerson != null">#{legalPerson},</if>
            <if test="region != null">#{region},</if>
            <if test="registerDate != null">#{registerDate},</if>
            <if test="address != null">#{address},</if>
            <if test="licenseUrl != null">#{licenseUrl},</if>
            <if test="l1Quota != null">#{l1Quota},</if>
            <if test="l2Quota != null">#{l2Quota},</if>
            <if test="l3Quota != null">#{l3Quota},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            sysdate()
        )
    </insert>
 
    <!--
        同一个 update 兼作「编辑」与「禁用/启用」:后者只带 id + status。
        user_id 不在列里:服务商与账号的绑定建成后不可改(要换账号就是另一个服务商)。
        权益三列用 != null 判空:changeStatus 只传 id + status,此时必须跳过,
        否则一次禁用会把 L1/L2/L3 分配数量全刷成 0。
        选填的文本列(信用代码/法人/地址/执照图)同理用 != null:前端清空输入框会送空串而非 null,
        空串要能写进去(即允许把已填的内容删掉)。
    -->
    <update id="updateProvider" parameterType="AccountProvider">
        update account_provider
        <set>
            <if test="companyName != null">company_name = #{companyName},</if>
            <if test="contactName != null">contact_name = #{contactName},</if>
            <if test="contactPhone != null">contact_phone = #{contactPhone},</if>
            <if test="creditCode != null">credit_code = #{creditCode},</if>
            <if test="legalPerson != null">legal_person = #{legalPerson},</if>
            <if test="region != null">region = #{region},</if>
            <if test="registerDate != null">register_date = #{registerDate},</if>
            <if test="address != null">address = #{address},</if>
            <if test="licenseUrl != null">license_url = #{licenseUrl},</if>
            <if test="l1Quota != null">l1_quota = #{l1Quota},</if>
            <if test="l2Quota != null">l2_quota = #{l2Quota},</if>
            <if test="l3Quota != null">l3_quota = #{l3Quota},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate()
        </set>
        where id = #{id} and del_flag = '0'
    </update>
 
</mapper>