<?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.lyms.platform.permission.dao.master.CouponMapper">
<resultMap type="com.lyms.platform.permission.model.CouponInfo" id="couponInfoMap">
<result column="id" property="id"/>
<result column="sequence_id" property="sequenceId"/>
<result column="create_date" property="createDate"/>
<result column="use_date" property="useDate"/>
<result column="user_id" property="userId"/>
<result column="create_user_Id" property="createUserId"/>
<result column="coupon_template_id" property="couponTemplateId"/>
<result column="create_hospital_id" property="createHospitalId"/>
<result column="used_hospital_id" property="usedHospitalId"/>
<result column="status" property="status"/>
<result column="operator_use_id" property="operatorUseId"/>
<result column="used_id" property="usedId"/>
</resultMap>
<sql id="columnList">
sequence_id,create_date,use_date, operator_use_id, user_id,create_user_id,coupon_template_id,create_hospital_id,used_id,used_hospital_id,status
</sql>
<select id="findList" parameterType="map" resultMap="couponInfoMap">
select id,<include refid="columnList" /> from coupon_info
<where>
<if test="userId != null">
and user_id = #{userId}
</if>
<if test="code != null">
and sequence_id = #{code}
</if>
<if test="hospitalId != null">
and create_hospital_id = #{hospitalId}
</if>
<if test="status != null">
and status = #{status}
</if>
</where>
</select>
<insert id="save" parameterType="com.lyms.platform.permission.model.CouponInfo">
insert into coupon_info(id, <include refid="columnList" />) values(#{id},#{sequenceId},#{createDate},#{useDate},#{operatorUseId},#{userId},#{createUserId},#{couponTemplateId},#{createHospitalId},#{usedId},#{usedHospitalId},#{status})
</insert>
<select id="findTemp" parameterType="map" resultType="map">
select b.id, b.id as coupon_template_id, b.actual_start, b.actual_end, b.unit_type, c.send_type, c.type from
hospital_coupon_template_group a, coupon_template b, coupon_type c
where a.hospital_id = #{hospitalId} and a.coupon_template_group_id = b.group_id and b.type_id = c.id and c.type in
<foreach collection="types" open="(" close=")" separator="," item="type">
#{type}
</foreach>
order by b.coupon_order
</select>
<select id="findTopName" parameterType="string" resultType="map">
select b.name, a.coupon_desc, a.img_base64
from hospital_coupon_template_group a, coupon_template_group b
where a.coupon_template_group_id = b.id and a.hospital_id = #{hospital_id}
</select>
<select id="findInfo" parameterType="map" resultType="map">
select a.create_date, b.name, b.title, b.show_start, b.show_end, b.unit_type, b.content, a.sequence_id, a.status
from coupon_info a, coupon_template b, coupon_type c, hospital_coupon_template_group d
where a.user_id = #{userId} and a.create_hospital_id = #{hospitalId} and a.coupon_template_id = b.id and b.type_id=c.id and d.hospital_id = #{hospitalId}
order by c.type desc, b.coupon_order desc
</select>
<select id="findValidateParam" parameterType="map" resultType="map">
select b.actual_start, b.actual_end, b.unit_type, a.create_hospital_id, c.type, c.area_type
from coupon_info a, coupon_template b, coupon_type c
where a.sequence_id = #{code}
and a.coupon_template_id = b.id and b.type_id = c.id and c.type = #{type}
</select>
<update id="use" parameterType="map">
update coupon_info
set use_date = #{useDate}, operator_use_id = #{operatorUseId}, used_hospital_id = #{hospitalId}, status = #{status}, used_id = #{usedId}
where sequence_id = #{code}
</update>
<update id="update" parameterType="map">
update coupon_info
set use_date = #{useDate}, operator_use_id = #{operatorUseId}, used_hospital_id = #{hospitalId}, status = #{status}
where user_id = #{userId}
</update>
<select id="findTypes" parameterType="map" resultType="integer">
select c.type
from hospital_coupon_template_group a,coupon_template b, coupon_type c
where a.hospital_id = #{hospitalId} and a.coupon_template_group_id = b.group_id and b.type_id = c.id
</select>
<select id="findOrgUsers" parameterType="string" resultType="map">
select name
from users
where org_id = #{id} and yn=1 and enable = 1
</select>
<select id="findOrgAreas" parameterType="string" resultType="map">
select area_name as name
from coupon_hospial_area
where hospital_id = #{id}
</select>
<select id="findUrl" parameterType="string" resultType="map">
SELECT create_hospital_id, user_id FROM coupon_info where sequence_id = #{id}
</select>
<update id="invalid" parameterType="map">
update coupon_info a inner join (
select a.id
from coupon_info a, coupon_template b, coupon_type c
where a.coupon_template_id = b.id and b.type_id = c.id and a.status=1 and c.type in
<foreach collection="couponTypes" open="(" close=")" separator="," item="type">
#{type}
</foreach>
and a.user_id = #{personId}
) b on a.id = b.id set a.status = 3
</update>
<select id="findByUsedId" parameterType="string" resultType="string">
select sequence_id from coupon_info where used_id = #{id} limit 0,1
</select>
<select id="findCityId" parameterType="string" resultType="string">
select city_id from organization where id = #{hospitalId}
</select>
<select id="queryCouponItemsByType" parameterType="java.util.Map" resultType="java.util.Map">
SELECT t.name,t.title,t.coupon_order from hospital_coupon_template_group h
left join coupon_type ct on h.coupon_template_group_id=ct.template_group_id
INNER JOIN coupon_template t on t.type_id=ct.id
where h.hospital_id=#{hospitalId} and ct.type=#{type} order by t.coupon_order ASC
</select>
<select id="findReport" parameterType="map" resultType="map">
select a.people_send_count, a.coupon_send_count, a.create_hospital_id, a.user_used_count, a.coupon_used_count, a.used_hospital_id, b.province_id, b.city_id, b.area_id, b.name from(
select a.people_send_count, a.coupon_send_count, a.create_hospital_id, b.user_used_count, b.coupon_used_count, b.used_hospital_id
from (
select count(distinct(a.user_id)) as people_send_count, count(1) as coupon_send_count, a.create_hospital_id
from coupon_info a, coupon_template b, coupon_type c, organization d, hospital_coupon_template_group e
where a.create_hospital_id = d.id and a.create_hospital_id in
<foreach collection="hospitalId" open="(" close=")" separator="," item="hid">
#{hid}
</foreach>
and a.coupon_template_id = b.id and b.type_id = c.id and c.type in
<foreach collection="couponType" open="(" close=")" separator="," item="type">
#{type}
</foreach>
<if test="startDate != null">
and a.create_date >= #{startDate}
</if>
<if test="endDate != null">
and a.create_date <![CDATA[ < ]]> #{endDate}
</if>
<if test="tempId != null and tempId != ''">
and e.coupon_template_group_id = #{tempId}
</if>
and a.create_hospital_id = e.hospital_id
group by a.create_hospital_id
) a left join (
select count(distinct(a.user_id)) as user_used_count, count(1) as coupon_used_count, a.used_hospital_id
from coupon_info a, coupon_template b, coupon_type c
where a.used_hospital_id in
<foreach collection="hospitalId" open="(" close=")" separator="," item="hid">
#{hid}
</foreach>
and a.status = 2 and a.coupon_template_id = b.id and b.type_id = c.id and c.type in
<foreach collection="couponType" open="(" close=")" separator="," item="type">
#{type}
</foreach>
group by a.used_hospital_id
) b
on a.create_hospital_id = b.used_hospital_id
union
select a.people_send_count, a.coupon_send_count, a.create_hospital_id, b.user_used_count, b.coupon_used_count, b.used_hospital_id
from (
select count(distinct(a.user_id)) as people_send_count, count(1) as coupon_send_count, a.create_hospital_id
from coupon_info a, coupon_template b, coupon_type c, organization d, hospital_coupon_template_group e
where a.create_hospital_id = d.id and a.create_hospital_id in
<foreach collection="hospitalId" open="(" close=")" separator="," item="hid">
#{hid}
</foreach>
and a.coupon_template_id = b.id and b.type_id = c.id and c.type in
<foreach collection="couponType" open="(" close=")" separator="," item="type">
#{type}
</foreach>
<if test="startDate != null">
and a.create_date >= #{startDate}
</if>
<if test="endDate != null">
and a.create_date <![CDATA[ < ]]> #{endDate}
</if>
<if test="tempId != null and tempId != ''">
and e.coupon_template_group_id = #{tempId}
</if>
and a.create_hospital_id = e.hospital_id
group by a.create_hospital_id
) a right join (
select count(distinct(a.user_id)) as user_used_count, count(1) as coupon_used_count, a.used_hospital_id
from coupon_info a, coupon_template b, coupon_type c
where a.used_hospital_id in
<foreach collection="hospitalId" open="(" close=")" separator="," item="hid">
#{hid}
</foreach>
and a.status = 2 and a.coupon_template_id = b.id and b.type_id = c.id and c.type in
<foreach collection="couponType" open="(" close=")" separator="," item="type">
#{type}
</foreach>
group by a.used_hospital_id
) b
on a.create_hospital_id = b.used_hospital_id
)a, organization b
where a.create_hospital_id = b.id or a.used_hospital_id = b.id
</select>
<select id="findUsedInfo" parameterType="map" resultType="map">
select count(1) as type_used_count, b.coupon_order, b.type, a.used_hospital_id
from coupon_info a, (
select a.id, a.coupon_order, b.type, d.hospital_id
from coupon_template a, coupon_type b, hospital_coupon_template_group d
where a.group_id = d.coupon_template_group_id and b.id = a.type_id and d.hospital_id in
<foreach collection="hospitalId" open="(" close=")" separator="," item="hid">
#{hid}
</foreach>
and b.type in
<foreach collection="couponType" open="(" close=")" separator="," item="type">
#{type}
</foreach>
) b
where a.used_hospital_id = b.hospital_id and a.coupon_template_id = b.id and a.status = 2
<if test="startDate != null">
and a.use_date >= #{startDate}
</if>
<if test="endDate != null">
and a.use_date <![CDATA[ < ]]> #{endDate}
</if>
group by b.coupon_order, b.type, a.used_hospital_id
</select>
<select id="findHospitalUsedInfo" parameterType="map" resultType="map">
select a.id, count(distinct(b.user_id)) as user_used_count, count(1) as coupon_used_count, a.area_id, a.city_id, a.province_id
from organization a, coupon_info b, coupon_template c, coupon_type d
where a.id = b.used_hospital_id and b.coupon_template_id = c.id and c.type_id = d.id
and a.id = #{hid}
and d.type in
<foreach collection="couponType" open="(" close=")" separator="," item="type">
#{type}
</foreach>
<if test="startDate != null">
and b.use_date >= #{startDate}
</if>
<if test="endDate != null">
and b.use_date <![CDATA[ < ]]> #{endDate}
</if>
</select>
<select id="findHospitalSendInfo" parameterType="map" resultType="map">
select a.id, count(distinct(b.user_id)) as people_send_count, count(1) as coupon_send_count, a.area_id, a.city_id, a.province_id
from organization a, coupon_info b, coupon_template c, coupon_type d
where a.id = b.create_hospital_id and b.coupon_template_id = c.id and c.type_id = d.id
and a.id = #{hid}
and d.type in
<foreach collection="couponType" open="(" close=")" separator="," item="type">
#{type}
</foreach>
<if test="startDate != null">
and b.create_date >= #{startDate}
</if>
<if test="endDate != null">
and b.create_date <![CDATA[ < ]]> #{endDate}
</if>
</select>
<select id="findHospitalNameById" parameterType="string" resultType="string">
select name from organization where id = #{id}
</select>
<select id="findHospitals" parameterType="map" resultType="map">
select a.coupon_template_group_id as id, b.name
from hospital_coupon_template_group a, coupon_template_group b
where a.coupon_template_group_id = b.id and a.hospital_id in
<foreach collection="hospitalIds" open="(" close=")" separator="," item="hid">
#{hid}
</foreach>
group by a.coupon_template_group_id,b.name
</select>
<select id="findHospitalName" parameterType="map" resultType="map">
select name from organization where id = #{hospitalId}
</select>
<select id="findUserName" parameterType="string" resultType="string">
select name from users where id = #{id}
</select>
<select id="findPhone" parameterType="string" resultType="string">
select phone from users where id = #{id}
</select>
<select id="couponInfo" parameterType="map" resultType="map">
select a.used_hospital_id, c.type, b.coupon_order, a.sequence_id, d.name as send_hospital, a.use_date, a.used_id, a.operator_use_id
from coupon_info a, coupon_template b, coupon_type c, organization d
where a.coupon_template_id = b.id and b.type_id = c.id and a.create_hospital_id = d.id
and a.used_hospital_id in
<foreach collection="hospitalId" open="(" close=")" separator="," item="hid">
#{hid}
</foreach>
and c.type in
<foreach collection="couponType" open="(" close=")" separator="," item="type">
#{type}
</foreach>
<if test="coupon_order != null">
and b.coupon_order = #{coupon_order}
</if>
<if test="startDate != null">
and a.use_date >= #{startDate}
</if>
<if test="endDate != null">
and a.use_date <![CDATA[ < ]]> #{endDate}
</if>
limit #{currentPage},#{pageSize}
</select>
<!-- <select id="findCouponInfo" parameterType="map" resultType="map">
select c.type, a.sequence_id, d.name, e.name as send_hospital, a.use_date, a.used_id, b.coupon_order
from coupon_info a, coupon_template b, coupon_type c, users d, organization e, hospital_coupon_template_group f
where a.coupon_template_id = b.id and b.type_id = c.id and a.user_id = d.id and a.status=2 and a.create_hospital_id = e.id
and a.used_hospital_id in
<foreach collection="hospitalId" open="(" close=")" separator="," item="hid">
#{hid}
</foreach>
and f.hospital_id = a.used_hospital_id and c.type in
<foreach collection="couponType" open="(" close=")" separator="," item="type">
#{type}
</foreach>
<if test="startDate != null">
and a.create_date >= #{startDate}
</if>
<if test="endDate != null">
and a.create_date <![CDATA[ < ]]> #{endDate}
</if>
<if test="tempId != null and tempId != ''">
and f.coupon_template_group_id = #{tempId}
</if>
limit #{currentPage},#{pageSize}
</select>-->
<select id="findCouponInfoCount" parameterType="map" resultType="integer">
select count(1) from (
select a.used_hospital_id, c.type, b.coupon_order, a.sequence_id, d.name as send_hospital, a.use_date, a.used_id
from coupon_info a, coupon_template b, coupon_type c, organization d
where a.coupon_template_id = b.id and b.type_id = c.id and a.create_hospital_id = d.id
and a.used_hospital_id in
<foreach collection="hospitalId" open="(" close=")" separator="," item="hid">
#{hid}
</foreach>
and c.type in
<foreach collection="couponType" open="(" close=")" separator="," item="type">
#{type}
</foreach>
<if test="startDate != null">
and a.use_date >= #{startDate}
</if>
<if test="endDate != null">
and a.use_date <![CDATA[ < ]]> #{endDate}
</if>
) a
</select>
<select id="doHospitalFilter" parameterType="map" resultType="string">
select a.id from organization a, hospital_coupon_template_group b
where a.id = b.hospital_id and a.id in
<foreach collection="hospitalId" open="(" close=")" separator="," item="hid">
#{hid}
</foreach>
<if test="provinceId != null and provinceId != ''">
and a.province_id = #{provinceId}
</if>
<if test="cityId != null and cityId != '' ">
and a.city_id = #{cityId}
</if>
<if test="areaId != null and areaId != ''">
and a.area_id = #{areaId}
</if>
<if test="tempId != null and tempId != ''">
and b.coupon_template_group_id = #{tempId}
</if>
</select>
<select id="findHospitalNames" parameterType="list" resultType="map">
select id, name from organization where id in
<foreach collection="list" open="(" close=")" separator="," item="hid">
#{hid}
</foreach>
</select>
<select id="findCouponTitleByTemp" parameterType="map" resultType="string">
select concat(b.type, "_", a.coupon_order)
from coupon_template a, coupon_type b, (
select distinct(b.coupon_template_group_id)
from organization a, hospital_coupon_template_group b
where a.id = b.hospital_id
and b.coupon_template_group_id = #{tempId}
and a.id in
<foreach collection="hospitalId" open="(" close=")" separator="," item="hid">
#{hid}
</foreach>
<if test="provinceId != null and provinceId != ''">
and a.province_id = #{provinceId}
</if>
<if test="cityId != null and cityId != '' ">
and a.city_id = #{cityId}
</if>
<if test="areaId != null and areaId != ''">
and a.area_id = #{areaId}
</if>
) c
where a.type_id = b.id
and a.group_id = c.coupon_template_group_id
and b.type in
<foreach collection="couponType" open="(" close=")" separator="," item="type">
#{type}
</foreach>
</select>
<select id="findUserSendInfo" parameterType="map" resultType="map">
select a.user_id, a.create_date, e.name as username, d.name as send_hospital, f.name as doctoer_name
from
coupon_info a, coupon_template b, coupon_type c, organization d, users e, users f
where a.coupon_template_id = b.id and b.type_id = c.id
and a.create_hospital_id = d.id and a.user_id = e.id and a.create_user_id = f.id
and a.create_hospital_id in
<foreach collection="hospitalId" open="(" close=")" separator="," item="hid">
#{hid}
</foreach>
and c.type in
<foreach collection="couponType" open="(" close=")" separator="," item="type">
#{type}
</foreach>
<if test="startDate != null">
and a.create_date >= #{startDate}
</if>
<if test="endDate != null">
and a.create_date <![CDATA[ < ]]> #{endDate}
</if>
limit #{currentPage},#{pageSize}
</select>
<select id="findUserSendInfoCount" parameterType="map" resultType="integer">
select count(1) from (
select a.user_id, a.create_date, e.name as username, d.name as send_hospital, f.name as doctoer_name
from
coupon_info a, coupon_template b, coupon_type c, organization d, users e, users f
where a.coupon_template_id = b.id and b.type_id = c.id
and a.create_hospital_id = d.id and a.user_id = e.id and a.create_user_id = f.id
and a.create_hospital_id in
<foreach collection="hospitalId" open="(" close=")" separator="," item="hid">
#{hid}
</foreach>
and c.type in
<foreach collection="couponType" open="(" close=")" separator="," item="type">
#{type}
</foreach>
<if test="startDate != null">
and a.create_date >= #{startDate}
</if>
<if test="endDate != null">
and a.create_date <![CDATA[ < ]]> #{endDate}
</if>
) a
</select>
</mapper>