mybatis示例用法
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD MMapper w.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.black.mapper.FactStoreLabelMMMapper">
<resultMap id="factStoreLabelMMap" type="cn.wonhigh.dc.bdp.common.model.FactStoreLabelM">
<result column="id" property="id" />
<result column="settle_month" property="settleMonth" />
<result column="store_no" property="storeNo" />
<result column="store_code" property="storeCode" />
<result column="store_name" property="storeName" />
<result column="brand_unit_code" property="brandUnitCode" />
<result column="brand_unit_name" property="brandUnitName" />
<result column="brand_code" property="brandCode" />
<result column="brand_name" property="brandName" />
<result column="shop_brand_flag" property="shopBrandFlag" />
<result column="store_category_no1" property="storeCategoryNo1" />
<result column="store_category_desc1" property="storeCategoryDesc1" />
<result column="normal_province_no" property="normalProvinceNo" />
<result column="normal_city_no" property="normalCityNo" />
<result column="normal_county_no" property="normalCountyNo" />
<result column="normal_province_name" property="normalProvinceName" />
<result column="normal_city_name" property="normalCityName" />
<result column="normal_county_name" property="normalCountyName" />
<result column="store_channel_no2" property="storeChannelNo2" />
<result column="store_channel_desc2" property="storeChannelDesc2" />
<result column="business_city_no" property="businessCityNo" />
<result column="business_city_name" property="businessCityName" />
<result column="manage_city_no" property="manageCityNo" />
<result column="manage_city_name" property="manageCityName" />
<result column="order_unit_no" property="orderUnitNo" />
<result column="order_unit_name" property="orderUnitName" />
<result column="region_no" property="regionNo" />
<result column="region_name" property="regionName" />
<result column="affiliation" property="affiliation" />
<result column="affiliation_desc" property="affiliationDesc" />
<result column="main_brand_detail_no" property="mainBrandDetailNo" />
<result column="secondary_brand_detail_no" property="secondaryBrandDetailNo" />
<result column="area_total" property="areaTotal" />
<result column="business_area" property="businessArea" />
<result column="opendt" property="opendt" />
<result column="closedt" property="closedt" />
<result column="store_level" property="storeLevel" />
<result column="digits" property="digits" />
<result column="staff_number" property="staffNumber" />
<result column="floor_number" property="floorNumber" />
<result column="telno" property="telno" />
<result column="contact" property="contact" />
<result column="mobile_phone" property="mobilePhone" />
<result column="assistant_amount" property="assistantAmount" />
<result column="store_age" property="storeAge" />
<result column="store_open_status" property="storeOpenStatus" />
<result column="cman_address" property="cmanAddress" />
<result column="business_circle_no" property="businessCircleNo" />
<result column="business_circle_name" property="businessCircleName" />
<result column="overall_operation_label_no" property="overallOperationLabelNo" />
<result column="profit_loss_label_no" property="profitLossLabelNo" />
<result column="abcd_label_no" property="abcdLabelNo" />
<result column="holidays_effect_label_no" property="holidaysEffectLabelNo" />
<result column="business_city_label_no" property="businessCityLabelNo" />
<result column="store_age_label_no" property="storeAgeLabelNo" />
<result column="color_hot_label_no" property="colorHotLabelNo" />
<result column="heel_hot_label_no" property="heelHotLabelNo" />
<result column="style_hot_label_no" property="styleHotLabelNo" />
<result column="order_price_label_no" property="orderPriceLabelNo" />
<result column="order_avg_label_no" property="orderAvgLabelNo" />
<result column="payway_favour_label_no" property="paywayFavourLabelNo" />
<result column="overall_operation_label_desc" property="overallOperationLabelDesc" />
<result column="profit_loss_label_desc" property="profitLossLabelDesc" />
<result column="abcd_label_desc" property="abcdLabelDesc" />
<result column="holidays_effect_label_desc" property="holidaysEffectLabelDesc" />
<result column="business_city_label_desc" property="businessCityLabelDesc" />
<result column="store_age_label_desc" property="storeAgeLabelDesc" />
<result column="color_hot_label_desc" property="colorHotLabelDesc" />
<result column="heel_hot_label_desc" property="heelHotLabelDesc" />
<result column="style_hot_label_desc" property="styleHotLabelDesc" />
<result column="order_price_label_desc" property="orderPriceLabelDesc" />
<result column="order_avg_label_desc" property="orderAvgLabelDesc" />
<result column="payway_favour_label_desc" property="paywayFavourLabelDesc" />
<result column="bdp_update_time" property="bdpUpdateTime" />
<result column="start_settle_month" property="startSettleMonth"/>
<result column="end_settle_month" property="endSettleMonth"/>
<result column="terminal_income" property="terminalIncome" />
<result column="ground_effect" property="groundEffect"/>
<result column="avg_discount" property="avgDiscount" />
<result column="gross_profit_margin" property="grossProfitMargin" />
<result column="expense_rate" property="expenseRate" />
<result column="profit_margin" property="profitMargin" />
</resultMap>
<!-- 店铺列表 -->
<resultMap id="shopListMap" type="cn.wonhigh.dc.bdp.common.vo.ShopListVo">
<result column="id" property="id" />
<result column="store_no" property="storeNo" />
<result column="store_name" property="storeName" />
<result column="store_channel_desc2" property="storeChannelDesc2" />
<result column="region_name" property="regionName" />
<result column="normal_province_name" property="normalProvinceName" />
<result column="brand_name" property="brandName" />
<result column="label" property="label" />
<result column="terminal_income" property="terminalIncome" />
<result column="ground_effect" property="groundEffect" />
<result column="avg_discount" property="avgDiscount" />
<result column="gross_profit_margin" property="grossProfitMargin" />
<result column="expense_rate" property="expenseRate" />
<result column="profit_margin" property="profitMargin" />
</resultMap>
<!-- 通过id查询实体 -->
<select id="findById" resultMap="factStoreLabelMMap" parameterType="Integer">
SELECT <include refid="baseColumn" />
FROM fact_store_label_m
WHERE id=#{id}
</select>
<!-- 分页查询 -->
<select id="findByPage" resultMap="factStoreLabelMMap" parameterType="String">
SELECT <include refid="baseColumn" />
FROM fact_store_label_m
WHERE 1=1
<include refid="sqlCondition" />
<if test="null!=params.orderBy and ''!=params.orderBy">
ORDER BY #{params.orderBy}
</if>
LIMIT #{params.page},#{params.pageSize}
</select>
<!-- 统计 -->
<select id="countByParams" resultType="Integer" parameterType="String">
SELECT count(1) FROM fact_store_label_m WHERE 1=1
<include refid="sqlCondition" />
</select>
<!-- 查询最大月份和最小月份 -->
<select id="getGroupMonth" resultMap="factStoreLabelMMap">
select concat(SUBSTRING(MIN(settle_month),1,4),'01') start_settle_month,max(settle_month) end_settle_month from fact_store_label_m limit 1;
</select>
<!-- 店铺列表 -->
<select id="getShopList" resultMap="shopListMap" parameterType="String">
select
t.id id,
t.store_no store_no,
t.store_name store_name,
t.store_channel_desc2 store_channel_desc2,
t.region_name region_name,
concat(t.normal_province_name,t.normal_city_name) normal_province_name,
GROUP_CONCAT(distinct (case shop_brand_flag when '主营' then brand_name else '' end)) brand_name,
concat((case d.profit_loss
when '00000100020001' then '高利润店铺'
when '00000100020002' then '正常店铺'
when '00000100020003' then '预警店铺'
when '00000100020004' then '亏损店铺'
when '00000100020005' then '非正常店铺'
else ''
end),'、',
ifnull(REPLACE(GROUP_CONCAT(distinct t.overall_operation_label_desc),',','、'),''),'、',
ifnull(REPLACE(GROUP_CONCAT(distinct t.abcd_label_desc),',','、'),''),'、',
ifnull(REPLACE(GROUP_CONCAT(distinct t.holidays_effect_label_desc),',','、'),''),'、',
ifnull(REPLACE(GROUP_CONCAT(distinct t.business_city_label_desc),',','、'),''),'、',
ifnull(REPLACE(GROUP_CONCAT(distinct t.store_age_label_desc),',','、'),''),'、',
ifnull(REPLACE(GROUP_CONCAT(distinct t.color_hot_label_desc),',','、'),''),'、',
ifnull(REPLACE(GROUP_CONCAT(distinct t.heel_hot_label_desc),',','、'),''),'、',
ifnull(REPLACE(GROUP_CONCAT(distinct t.style_hot_label_desc),',','、'),''),'、',
ifnull(REPLACE(GROUP_CONCAT(distinct t.order_price_label_desc),',','、'),''),'、',
ifnull(REPLACE(GROUP_CONCAT(distinct t.order_avg_label_desc),',','、'),''),'、',
ifnull(REPLACE(GROUP_CONCAT(distinct t.payway_favour_label_desc),',','、'),''),'、') label,
round(d.terminal_revenue,2) terminal_income,
round((d.terminal_revenue/sum(business_area)),2) ground_effect,
round((d.terminal_revenue/d.price_revenue)*100,2) avg_discount,
round((d.gross_profit/d.terminal_revenue)*100,2) gross_profit_margin,
round((d.business_expense/d.terminal_revenue)*100,2) expense_rate,
round((d.profit/d.terminal_revenue)*100,2) profit_margin
from fact_store_label_m t
left join dw_shop_channel_analyze d
on t.store_no=d.store_no
and t.settle_month=d.financial_month
where 1=1
<include refid="sqlCondition"/>
<if test="params.profitLossLabelNo!=null and params.profitLossLabelNo!=''">
and d.profit_loss=#{params.profitLossLabelNo}
</if>
group by t.store_no
order by ${params.orderBy}
LIMIT #{params.page},#{params.pageSize}
</select>
<!-- 店铺列表总数 -->
<select id="countShop" resultType="Integer" parameterType="String">
SELECT
count(f.c)
FROM
(
SELECT
count(1) c
FROM
fact_store_label_m t
LEFT JOIN dw_shop_channel_analyze d
ON t.store_no = d.store_no
AND t.settle_month = d.financial_month
where 1=1
<include refid="sqlCondition"/>
<if test="params.profitLossLabelNo!=null and params.profitLossLabelNo!=''">
and d.profit_loss=#{params.profitLossLabelNo}
</if>
GROUP BY
t.store_no
) f
</select>
<!-- 店铺详情 -->
<select id="getShopDetailByParams" resultMap="factStoreLabelMMap" parameterType="String">
SELECT
t.store_no,
t.store_name,
GROUP_CONCAT(distinct (case shop_brand_flag when '主营' then brand_name else '' end)) brand_name,
t.store_age,
t.store_open_status,
sum(t.business_area) business_area,
t.business_circle_name,
t.staff_number,
t.region_name,
t.order_unit_name,
t.manage_city_name,
t.business_city_name,
t.cman_address,
t.store_channel_desc2,
replace(GROUP_CONCAT(distinct t.overall_operation_label_desc),',','、') overall_operation_label_desc,
(case d.profit_loss
when '00000100020001' then '高利润店铺'
when '00000100020002' then '正常店铺'
when '00000100020003' then '预警店铺'
when '00000100020004' then '亏损店铺'
when '00000100020005' then '非正常店铺'
else ''
end) as profit_loss_label_desc,
replace(GROUP_CONCAT(distinct t.abcd_label_desc),',','、') abcd_label_desc,
replace(GROUP_CONCAT(distinct t.holidays_effect_label_desc),',','、') holidays_effect_label_desc,
replace(GROUP_CONCAT(distinct t.business_city_label_desc),',','、') business_city_label_desc,
replace(GROUP_CONCAT(distinct t.store_age_label_desc),',','、') store_age_label_desc,
replace(GROUP_CONCAT(distinct t.color_hot_label_desc),',','、') color_hot_label_desc,
replace(GROUP_CONCAT(distinct t.heel_hot_label_desc),',','、') heel_hot_label_desc,
replace(GROUP_CONCAT(distinct t.style_hot_label_desc),',','、') style_hot_label_desc,
replace(GROUP_CONCAT(distinct t.order_price_label_desc),',','、') order_price_label_desc,
replace(GROUP_CONCAT(distinct t.order_avg_label_desc),',','、') order_avg_label_desc,
replace(GROUP_CONCAT(distinct t.payway_favour_label_desc),',','、') payway_favour_label_desc,
round(d.terminal_revenue,2) terminal_income,
round((d.terminal_revenue/sum(t.business_area)),2) ground_effect,
round((d.terminal_revenue/d.price_revenue)*100,2) avg_discount,
round((d.gross_profit/d.terminal_revenue)*100,2) gross_profit_margin,
round((d.business_expense/d.terminal_revenue)*100,2) expense_rate,
round((d.profit/d.terminal_revenue)*100,2) profit_margin
FROM
fact_store_label_m t
LEFT JOIN dw_shop_channel_analyze d ON t.store_no = d.store_no
AND t.settle_month = d.financial_month
WHERE 1=1
<if test="params.storeNo!=null and params.storeNo!=''">
and t.store_no=#{params.storeNo}
</if>
<include refid="sqlCondition"/>
GROUP BY
t.store_no
LIMIT 1
</select>
<!-- 获取所有品牌列表 -->
<select id="findByStoreNo" resultMap="factStoreLabelMMap" >
SELECT
<include refid="baseColumn"></include>
FROM
fact_store_label_m
where 1=1 AND settle_month = #{settleMonth} AND store_no = #{storeNo}
</select>
<!-- 获取开业时长最小值 -->
<select id="getMinOpentd" resultMap="factStoreLabelMMap">
select min(opendt) start_settle_month from fact_store_label_m group by opendt limit 1
</select>
<!-- 根据盈亏标签获取月份 -->
<select id="getMonthByProfitLoss" resultType="String">
SELECT
d.financial_month
FROM
dw_shop_channel_analyze d
INNER JOIN fact_store_label_m m ON d.financial_month = m.settle_month
AND d.store_no = m.store_no
group by d.financial_month
</select>
<!-- 公共查询字段 -->
<sql id="baseColumn">
id,settle_month,store_no,store_code,store_name,brand_unit_code,brand_unit_name,brand_code,brand_name,shop_brand_flag,store_category_no1,store_category_desc1,normal_province_no,normal_city_no,normal_county_no,normal_province_name,normal_city_name,normal_county_name,store_channel_no2,store_channel_desc2,business_city_no,business_city_name,manage_city_no,manage_city_name,order_unit_no,order_unit_name,region_no,region_name,affiliation,affiliation_desc,main_brand_detail_no,secondary_brand_detail_no,area_total,business_area,opendt,closedt,store_level,digits,staff_number,floor_number,telno,contact,mobile_phone,assistant_amount,store_age,store_open_status,cman_address,business_circle_no,business_circle_name,overall_operation_label_no,profit_loss_label_no,abcd_label_no,holidays_effect_label_no,business_city_label_no,store_age_label_no,color_hot_label_no,heel_hot_label_no,style_hot_label_no,order_price_label_no,order_avg_label_no,payway_favour_label_no,overall_operation_label_desc,profit_loss_label_desc,abcd_label_desc,holidays_effect_label_desc,business_city_label_desc,store_age_label_desc,color_hot_label_desc,heel_hot_label_desc,style_hot_label_desc,order_price_label_desc,order_avg_label_desc,payway_favour_label_desc,bdp_update_time
</sql>
<!-- 公共sql查询条件 -->
<sql id="sqlCondition">
<if test="null!=params">
<if test="params.keyword!=null and params.keyword!=''">
and (t.store_no like concat(concat('%',#{params.keyword}),'%') or t.store_name like concat(concat('%',#{params.keyword}),'%'))
</if>
<if test="params.settleMonth!=null and params.settleMonth!=''">
and t.settle_month=#{params.settleMonth}
</if>
<if test="params.affiliation!=null and params.affiliation!=''">
and t.affiliation=#{params.affiliation}
</if>
<if test="params.regionNo!=null and params.regionNo!=''">
and t.region_no=#{params.regionNo}
</if>
<if test="params.channel!=null and params.channel!=''">
and t.store_channel_no2=#{params.channel}
</if>
<if test="params.brandUnitCode!=null and params.brandUnitCode!=''">
and t.brand_unit_code=#{params.brandUnitCode}
</if>
<if test="params.brandNo!=null and params.brandNo!=''">
and t.brand_code=#{params.brandNo}
</if>
<if test="(params.brandUnitCode!=null and params.brandUnitCode!='') or (params.brandNo!=null and params.brandNo!='')">
and t.shop_brand_flag='主营'
</if>
<if test="params.provinceNo!=null and params.provinceNo!=''">
and t.normal_province_no=#{params.provinceNo}
</if>
<if test="params.overallOperationLabelNo!=null and params.overallOperationLabelNo!=''">
and t.overall_operation_label_no=#{params.overallOperationLabelNo}
</if>
<if test="params.abcdLabelNo!=null and params.abcdLabelNo!=''">
and t.abcd_label_no=#{params.abcdLabelNo}
</if>
<if test="params.holidaysEffectLabelNo!=null and params.holidaysEffectLabelNo!=''">
and t.holidays_effect_label_no=#{params.holidaysEffectLabelNo}
</if>
<if test="params.businessCityLabelNo!=null and params.businessCityLabelNo!=''">
and t.business_city_label_no=#{params.businessCityLabelNo}
</if>
<if test="params.storeAgeLabelNo!=null and params.storeAgeLabelNo!=''">
and t.store_age_label_no=#{params.storeAgeLabelNo}
</if>
<if test="params.colorHotLabelNo!=null and params.colorHotLabelNo!=''">
and FIND_IN_SET(#{params.colorHotLabelNo},t.color_hot_label_no)
</if>
<if test="params.heelHotLabelNo!=null and params.heelHotLabelNo!=''">
and FIND_IN_SET(#{params.heelHotLabelNo},t.heel_hot_label_no)
</if>
<if test="params.styleHotLabelNo!=null and params.styleHotLabelNo!=''">
and t.style_hot_label_no=#{params.styleHotLabelNo}
</if>
<if test="params.orderPriceLabelNo!=null and params.orderPriceLabelNo!=''">
and t.order_price_label_no=#{params.orderPriceLabelNo}
</if>
<if test="params.orderAvgLabelNo!=null and params.orderAvgLabelNo!=''">
and t.order_avg_label_no=#{params.orderAvgLabelNo}
</if>
<if test="params.paywayFavourLabelNo!=null and params.paywayFavourLabelNo!=''">
and FIND_IN_SET(#{params.paywayFavourLabelNo},t.payway_favour_label_no)
</if>
</if>
</sql>
</mapper>