RevenueMapper.xml 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.huaxu.dao.RevenueMapper">
  4. <!-- 结果集 -->
  5. <resultMap type="com.huaxu.dto.MonthRevenueDto" id="revenueDtoMap">
  6. <result property="id" column="id" jdbcType="INTEGER"/>
  7. <result property="collectDateString" column="collect_date_string" jdbcType="VARCHAR"/>
  8. <result property="receivableTotalAmount" column="receivable_total_amount" jdbcType="VARCHAR"/>
  9. <result property="receivedTotalAmount" column="received_total_amount" jdbcType="VARCHAR"/>
  10. <result property="meterReadingUsage" column="meter_reading_usage" jdbcType="VARCHAR"/>
  11. <result property="generalUserUsage" column="general_user_usage" jdbcType="VARCHAR"/>
  12. <result property="bigUserUsage" column="big_user_usage" jdbcType="VARCHAR"/>
  13. <result property="generalUserUsageReceived" column="general_user_usage_received" jdbcType="VARCHAR"/>
  14. <result property="bigUserUsageReceived" column="big_user_usage_received" jdbcType="VARCHAR"/>
  15. <collection property="monthSellList" ofType="com.huaxu.entity.MonthSellwaterEntity" javaType="list">
  16. <result property="id" column="sell_id" jdbcType="INTEGER"/>
  17. <result property="propertyId" column="property_id" jdbcType="INTEGER"/>
  18. <result property="waterUsage" column="water_usage" jdbcType="VARCHAR"/>
  19. </collection>
  20. </resultMap>
  21. <!--查询本月售水信息-->
  22. <select id="selectMonthSell" resultType="com.huaxu.dto.MonthSellwaterDto">
  23. select t2.property_name ,sum(t1.water_usage) water_usage
  24. from sms_water_property t2
  25. left join sms_month_sellwater t1 on t1.property_id=t2.id and t1.collect_date>=date_format(curdate(), '%Y-%m' )
  26. <if test="tenantId != null and tenantId != ''">
  27. and t1.tenant_id=#{tenantId}
  28. </if>
  29. <if test="companyOrgId != null and companyOrgId !=''">
  30. and t1.company_org_id=#{companyOrgId}
  31. </if>
  32. <if test="userType!=null and userType!=-999 and userType!=-9999 and programItems != null and programItems.size() > 0">
  33. <if test="permissonType == 5 or permissonType == 2 or permissonType == 1">
  34. and t1.company_org_id in
  35. <foreach collection="programItems" item="item" open="(" close=")" separator=",">
  36. #{item.orgId}
  37. </foreach>
  38. </if>
  39. </if>
  40. inner join (
  41. select distinct property_id
  42. from sms_month_sellwater
  43. where 1=1
  44. <if test="tenantId != null and tenantId != ''">
  45. and tenant_id=#{tenantId}
  46. </if>
  47. <if test="companyOrgId != null and companyOrgId !=''">
  48. and company_org_id=#{companyOrgId}
  49. </if>
  50. )t3 on t2.id=t3.property_id
  51. group by t2.property_name
  52. order by water_usage desc,t2.property_name
  53. </select>
  54. <!--查询近6个月的售水量-->
  55. <select id="selectMonthSellTotal" resultType="com.huaxu.dto.MonthSellwaterDto">
  56. select date_format(t1.collect_date, '%Y-%m' ) collect_date_string,sum(t1.water_usage) water_usage
  57. from sms_month_sellwater t1
  58. where t1.collect_date>=date_sub(date_format(curdate(), '%Y-%m-1' ), interval 5 month)
  59. <if test="tenantId != null and tenantId != ''">
  60. and t1.tenant_id=#{tenantId}
  61. </if>
  62. <if test="companyOrgId != null and companyOrgId !=''">
  63. and t1.company_org_id=#{companyOrgId}
  64. </if>
  65. <if test="userType!=null and userType!=-999 and userType!=-9999 and programItems != null and programItems.size() > 0">
  66. <if test="permissonType == 5 or permissonType == 2 or permissonType == 1">
  67. and t1.company_org_id in
  68. <foreach collection="programItems" item="item" open="(" close=")" separator=",">
  69. #{item.orgId}
  70. </foreach>
  71. </if>
  72. </if>
  73. group by collect_date_string
  74. order by collect_date_string
  75. </select>
  76. <!--查询月营收情况-->
  77. <select id="selectMonthRevenue" resultType="com.huaxu.dto.MonthRevenueDto">
  78. select date_format(t1.collect_date, '%Y-%m' ) collect_date_string,sum(user_meter_count) user_meter_count,
  79. sum(receivable_total_amount) receivable_total_amount,sum(received_total_amount) received_total_amount,
  80. sum(general_user_usage) general_user_usage,sum(big_user_usage) big_user_usage,
  81. if(sum(receivable_total_amount)=0,null,sum(received_total_amount)/sum(receivable_total_amount)*100) water_fee_recovery_rate,
  82. if(sum(receivable_total_amount)=0,null,sum(general_user_usage)/sum(receivable_total_amount)*100) general_user_percent,
  83. if(sum(receivable_total_amount)=0,null,sum(big_user_usage)/sum(receivable_total_amount)*100) big_user_percent
  84. from sms_month_revenue t1
  85. where t1.collect_date>=date_sub(date_format(curdate(), '%y-%m-1' ), interval #{months}-1 month)
  86. <if test="tenantId != null and tenantId != ''">
  87. and t1.tenant_id=#{tenantId}
  88. </if>
  89. <if test="companyOrgId != null and companyOrgId !=''">
  90. and t1.company_org_id=#{companyOrgId}
  91. </if>
  92. <if test="userType!=null and userType!=-999 and userType!=-9999 and programItems != null and programItems.size() > 0">
  93. <if test="permissonType == 5 or permissonType == 2 or permissonType == 1">
  94. and t1.company_org_id in
  95. <foreach collection="programItems" item="item" open="(" close=")" separator=",">
  96. #{item.orgId}
  97. </foreach>
  98. </if>
  99. </if>
  100. group by collect_date_string
  101. order by collect_date_string
  102. </select>
  103. <!--查询本月各分公司的营收情况-->
  104. <select id="selectCompanyRevenue" resultType="com.huaxu.dto.MonthRevenueDto">
  105. select company_org_id,sum(receivable_total_amount) receivable_total_amount,sum(received_total_amount) received_total_amount,
  106. if(sum(receivable_total_amount)=0,null,sum(received_total_amount)/sum(receivable_total_amount)*100) water_fee_recovery_rate
  107. from sms_month_revenue t1
  108. where t1.collect_date>=date_format(curdate(), '%y-%m-1' )
  109. <if test="tenantId != null and tenantId != ''">
  110. and t1.tenant_id=#{tenantId}
  111. </if>
  112. <if test="userType!=null and userType!=-999 and userType!=-9999 and programItems != null and programItems.size() > 0">
  113. <if test="permissonType == 5 or permissonType == 2 or permissonType == 1">
  114. and t1.company_org_id in
  115. <foreach collection="programItems" item="item" open="(" close=")" separator=",">
  116. #{item.orgId}
  117. </foreach>
  118. </if>
  119. </if>
  120. group by company_org_id
  121. order by company_org_id
  122. </select>
  123. <!--App总览-生产-售水量(本月,本年)-->
  124. <select id="selectOverviewRevenue" resultType="com.huaxu.dto.MonthRevenueDto">
  125. select date_format(t1.collect_date, '%Y-%m' ) collect_date_string,
  126. sum(METER_READING_USAGE) meter_reading_usage,
  127. sum(RECEIVABLE_TOTAL_AMOUNT) receivable_total_amount,
  128. sum(RECEIVED_TOTAL_AMOUNT) received_total_amount,
  129. sum(GENERAL_USER_USAGE) general_user_usage,
  130. sum(BIG_USER_USAGE) big_user_usage
  131. from sms_month_revenue t1
  132. where t1.collect_date>=date_sub(date_format(curdate(), '%y-%m-1' ), interval #{months}-1 month)
  133. <if test="tenantId != null and tenantId != ''">
  134. and t1.tenant_id=#{tenantId}
  135. </if>
  136. <if test="companyOrgId != null and companyOrgId !=''">
  137. and t1.company_org_id=#{companyOrgId}
  138. </if>
  139. <if test="userType!=null and userType!=-999 and userType!=-9999 and programItems != null and programItems.size() > 0">
  140. <if test="permissonType == 5 or permissonType == 2 or permissonType == 1">
  141. and t1.company_org_id in
  142. <foreach collection="programItems" item="item" open="(" close=")" separator=",">
  143. #{item.orgId}
  144. </foreach>
  145. </if>
  146. </if>
  147. group by collect_date_string
  148. order by collect_date_string
  149. </select>
  150. <!--App总览-生产数据-按时间段查询(环比)-->
  151. <select id="selectRevenueByDate" resultType="com.huaxu.dto.MonthRevenueDto">
  152. select date_format(t1.collect_date, '%Y-%m' ) collect_date_string,
  153. sum(METER_READING_USAGE) meter_reading_usage,
  154. sum(RECEIVABLE_TOTAL_AMOUNT) receivable_total_amount,
  155. sum(RECEIVED_TOTAL_AMOUNT) received_total_amount,
  156. sum(GENERAL_USER_USAGE) general_user_usage,
  157. sum(BIG_USER_USAGE) big_user_usage,
  158. from sms_month_revenue t1
  159. where t1.collect_date>=date_format(#{startDate}, '%y-%m-1' ) and t1.collect_date &lt;= date_format(#{endDate}, '%y-%m-1')
  160. <if test="tenantId != null and tenantId != ''">
  161. and t1.tenant_id=#{tenantId}
  162. </if>
  163. <if test="companyOrgId != null and companyOrgId !=''">
  164. and t1.company_org_id=#{companyOrgId}
  165. </if>
  166. <if test="userType!=null and userType!=-999 and userType!=-9999 and programItems != null and programItems.size() > 0">
  167. <if test="permissonType == 5 or permissonType == 2 or permissonType == 1">
  168. and t1.company_org_id in
  169. <foreach collection="programItems" item="item" open="(" close=")" separator=",">
  170. #{item.orgId}
  171. </foreach>
  172. </if>
  173. </if>
  174. group by collect_date_string
  175. order by collect_date_string
  176. </select>
  177. <!--查询是否当月有营收数据-->
  178. <select id="isExistThisMonthRevenue" resultType="java.lang.Integer">
  179. select count(1)
  180. from sms_month_revenue t1
  181. where t1.collect_date>=date_format(curdate(), '%Y-%m' )
  182. <if test="tenantId != null and tenantId != ''">
  183. and t1.tenant_id=#{tenantId}
  184. </if>
  185. </select>
  186. <!-- 新增所有列 -->
  187. <insert id="insert" keyProperty="id" useGeneratedKeys="true">
  188. insert into sms_month_revenue(`tenant_id`, `company_org_id`, `collect_date`, `user_meter_count`, `user_meter_add_count`,
  189. `meter_reading_count`, `meter_reading_usage`, `charging_usage`, `meter_reading_finished_rate`, `meter_reading_arrival_rate`,
  190. `meter_reading_error_rate`, `receivable_total_amount`, `received_total_amount`, `invoiced_total_amount`,
  191. `arrears_total_amount`, `general_user_usage`, `big_user_usage`, `remark`, `date_create`, `create_by`, `date_update`, `update_by`,
  192. `general_user_usage_received`, `big_user_usage_received`)
  193. values (#{tenantId},#{companyOrgId},#{collectDate},#{userMeterCount},#{userMeterAddCount},#{meterReadingCount},
  194. #{meterReadingUsage},#{chargingUsage},#{meterReadingFinishedRate},#{meterReadingArrivalRate},#{meterReadingErrorRate},
  195. #{receivableTotalAmount},#{receivedTotalAmount},#{invoicedTotalAmount},#{arrearsTotalAmount},#{generalUserUsage},
  196. #{bigUserUsage},#{remark},now(),null,now(),null,#{generalUserUsageReceived},#{bigUserUsageReceived})
  197. </insert>
  198. <!-- 通过租户id修改当月数据 -->
  199. <update id="update">
  200. update sms_month_revenue
  201. <set>
  202. <if test="userMeterCount != null and userMeterCount != 0">
  203. user_meter_count = #{userMeterCount},
  204. </if>
  205. <if test="userMeterAddCount != null and userMeterAddCount != 0">
  206. user_meter_add_count = #{userMeterAddCount},
  207. </if>
  208. date_update = now()
  209. </set>
  210. where tenant_id = #{tenantId} and collect_date>=date_format(curdate(), '%Y-%m-1' )
  211. </update>
  212. <!--查询该租户一级公司id-->
  213. <select id="selectCompanyIdByTenant" resultType="java.lang.Integer">
  214. select id
  215. from uims.uims_org
  216. where parent_org_id=0 and tenant_id=#{tenantId}
  217. </select>
  218. <!-- 新增售水信息-->
  219. <insert id="insertSellWater">
  220. insert into sms_month_sellwater (tenant_id,company_org_id,property_id,collect_date,water_usage,remark,create_by)
  221. values(#{tenantId}, #{companyOrgId}, #{propertyId}, #{collectDate}, #{waterUsage},#{remark},#{createBy})
  222. </insert>
  223. <!-- 通过id修改营收数据 -->
  224. <update id="updateRevenue">
  225. update sms_month_revenue
  226. <set>
  227. <if test="collectDate != null">
  228. collect_date = #{collectDate},
  229. </if>
  230. <if test="userMeterAddCount != null">
  231. user_meter_add_count = #{userMeterAddCount},
  232. </if>
  233. <if test="userMeterCount != null">
  234. user_meter_count = #{userMeterCount},
  235. </if>
  236. <if test="meterReadingCount != null">
  237. meter_reading_count = #{meterReadingCount},
  238. </if>
  239. <if test="meterReadingUsage != null">
  240. meter_reading_usage = #{meterReadingUsage},
  241. </if>
  242. <if test="chargingUsage != null">
  243. charging_usage = #{chargingUsage},
  244. </if>
  245. <if test="meterReadingFinishedRate != null">
  246. meter_reading_finished_rate = #{meterReadingFinishedRate},
  247. </if>
  248. <if test="meterReadingArrivalRate != null">
  249. meter_reading_arrival_rate = #{meterReadingArrivalRate},
  250. </if>
  251. <if test="meterReadingErrorRate != null">
  252. meter_reading_error_rate = #{meterReadingErrorRate},
  253. </if>
  254. <if test="receivableTotalAmount != null">
  255. receivable_total_amount = #{receivableTotalAmount},
  256. </if>
  257. <if test="receivedTotalAmount != null">
  258. received_total_amount = #{receivedTotalAmount},
  259. </if>
  260. <if test="invoicedTotalAmount != null">
  261. invoiced_total_amount = #{invoicedTotalAmount},
  262. </if>
  263. <if test="arrearsTotalAmount != null">
  264. arrears_total_amount = #{arrearsTotalAmount},
  265. </if>
  266. <if test="generalUserUsage != null">
  267. general_user_usage = #{generalUserUsage},
  268. </if>
  269. <if test="bigUserUsage != null">
  270. big_user_usage = #{bigUserUsage},
  271. </if>
  272. <if test="remark != null">
  273. remark = #{remark},
  274. </if>
  275. <if test="updateBy != null">
  276. update_by = #{updateBy},
  277. </if>
  278. <if test="generalUserUsageReceived != null">
  279. general_user_usage_received = #{generalUserUsageReceived},
  280. </if>
  281. <if test="bigUserUsageReceived != null">
  282. big_user_usage_received = #{bigUserUsageReceived},
  283. </if>
  284. </set>
  285. where id = #{id}
  286. </update>
  287. <!-- 通过id修改售水数据 -->
  288. <update id="updateSellWater">
  289. update sms_month_sellwater
  290. <set>
  291. <if test="collectDate != null">
  292. collect_date = #{collectDate},
  293. </if>
  294. <if test="waterUsage != null">
  295. water_usage = #{waterUsage},
  296. </if>
  297. <if test="remark != null">
  298. remark = #{remark},
  299. </if>
  300. <if test="updateBy != null">
  301. update_by = #{updateBy},
  302. </if>
  303. </set>
  304. where id = #{id}
  305. </update>
  306. <!-- 通过公司id查询营收数据 -->
  307. <select id="selectList" resultMap="revenueDtoMap">
  308. select t1.id,date_format(t1.collect_date,'%Y年%m月') as collect_date_string,meter_reading_usage*10000 as meter_reading_usage
  309. ,receivable_total_amount*10000 as receivable_total_amount,received_total_amount*10000 as received_total_amount,big_user_usage*10000 as big_user_usage
  310. ,general_user_usage*10000 as general_user_usage,t2.id as property_id,t3.id as sell_id,t3.water_usage*10000 as water_usage
  311. ,general_user_usage_received*10000 as general_user_usage_received,big_user_usage_received*10000 as big_user_usage_received
  312. from sms_month_revenue t1
  313. left join sms_water_property t2 on t1.company_org_id=t2.company_org_id and t1.tenant_id=t2.tenant_id and t2.status=1
  314. left join sms_month_sellwater t3 on t2.id=t3.property_id and date_format(t1.collect_date,'%Y-%m-%d')= date_format(t3.collect_date,'%Y-%m-%d')
  315. where t1.company_org_id = #{companyOrgId} and t1.collect_date >= concat(#{startDate}, '-1')
  316. and t1.collect_date &lt; concat(#{endDate}, '-1') + interval 1 month
  317. order by t1.collect_date
  318. </select>
  319. </mapper>