<?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.goafanti.common.dao.DailySalesReportMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.DailySalesReport">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="admin_id" jdbcType="VARCHAR" property="adminId" />
    <result column="dep_id" jdbcType="VARCHAR" property="depId" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="order_count" jdbcType="INTEGER" property="orderCount" />
    <result column="order_amount" jdbcType="DECIMAL" property="orderAmount" />
  </resultMap>
  <sql id="Base_Column_List">
    id, admin_id, dep_id, create_time, order_count, order_amount
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from daily_sales_report
    where id = #{id,jdbcType=INTEGER}
  </select>
  
  <select id="selectAdmins" resultMap="BaseResultMap">
    select a.id as admin_id, dm.id as dep_id
    from admin a
    left join department dm on dm.id = a.department_id 
  </select>
  
  <select id="selectPersonalByPage" resultType="com.goafanti.report.bo.PersonalSalesReportBO">
    SELECT a.id, a.name, dm.name as departmentName, a.position, a.create_time as enrollTime
	FROM admin a
	left join department dm on dm.id = a.department_id 
	where 1=1
	<if test="depName != null">
		and dm.name like concat(#{depName,jdbcType=VARCHAR},'%')
	</if>
	<if test="name != null">
		and a.name like concat(#{name,jdbcType=VARCHAR},'%')
	</if>
	<if test="position != null">
		and a.position like concat(#{position,jdbcType=VARCHAR},'%')
	</if>
	order by a.name asc
	<if test="pageNo != null and pageSize != null">
		limit #{pageNo,jdbcType=INTEGER}, #{pageSize,jdbcType=INTEGER}
    </if>
  </select>
  
  <select id="selectDailySalesReports" resultMap="BaseResultMap">
    SELECT o.salesman_id as admin_id, a.department_id as dep_id,
	count(o.order_no) as order_count, sum(o.sign_total_amount) as order_amount
	FROM t_order o
	left join admin a on o.salesman_id=a.id
	where o.order_status=3 and o.sign_time &gt;= #{startTime,jdbcType=TIMESTAMP} and o.sign_time &lt; #{endTime,jdbcType=TIMESTAMP}
	group by o.salesman_id, a.department_id;
  </select>
  
  <delete id="deleteByDate">
    delete from daily_sales_report
    where create_time = #{createTime,jdbcType=TIMESTAMP}
  </delete>
  
  <insert id="insertBatch" parameterType="com.goafanti.common.model.DailySalesReport" >
    insert into daily_sales_report (admin_id, dep_id, order_count, order_amount, create_time)
    values 
    <foreach item="item" index="index" collection="list" separator=",">
        (#{item.adminId,jdbcType=VARCHAR},
        #{item.depId,jdbcType=VARCHAR},
        #{item.orderCount,jdbcType=INTEGER},
        #{item.orderAmount,jdbcType=DECIMAL},
        #{item.createTime,jdbcType=TIMESTAMP})
    </foreach>
  </insert>
  
  <select id="selectSalesPersonalReports" resultType="com.goafanti.report.bo.PersonalSalesReportBO">
    SELECT a.id, a.name, dm.name as departmentName, a.position, a.create_time as enrollTime, 
	sum(dsr.order_count) as ${countField}, sum(dsr.order_amount) as ${amountField}
	FROM admin a
	left join daily_sales_report dsr on dsr.admin_id = a.id 
	left join department dm on dm.id = dsr.dep_id 
	where dsr.create_time &gt;= #{startTime,jdbcType=TIMESTAMP} and dsr.create_time &lt; #{endTime,jdbcType=TIMESTAMP}
	<if test="depName != null">
		and dm.name like concat(#{depName,jdbcType=VARCHAR},'%')
	</if>
	<if test="name != null">
		and a.name like concat(#{name,jdbcType=VARCHAR},'%')
	</if>
	<if test="position != null">
		and a.position like concat(#{position,jdbcType=VARCHAR},'%')
	</if>
	group by a.id, dsr.dep_id, dm.id
	order by ${orderField} ${order}, a.name asc
	<if test="pageNo != null and pageSize != null">
		limit #{pageNo,jdbcType=INTEGER}, #{pageSize,jdbcType=INTEGER}
    </if>
  </select>
  
  <select id="selectSalesPersonalReportsCount" resultType="java.lang.Long">
    SELECT count(distinct(a.id))
	FROM admin a
	<if test="startTime != null and endTime != null"> 
	left join daily_sales_report dsr on dsr.admin_id = a.id
	</if>
	<if test="depName != null"> 
	left join department dm on dm.id = a.department_id 
	</if>
	where 1=1
	<if test="startTime != null and endTime != null"> 
		and dsr.create_time &gt;= #{startTime,jdbcType=TIMESTAMP} and dsr.create_time &lt; #{endTime,jdbcType=TIMESTAMP}
	</if>
	<if test="depName != null">
		and dm.name like concat(#{depName,jdbcType=VARCHAR},'%')
	</if>
	<if test="name != null">
		and a.name like concat(#{name,jdbcType=VARCHAR},'%')
	</if>
	<if test="position != null">
		and a.position like concat(#{position,jdbcType=VARCHAR},'%')
	</if>
  </select>
  
  <select id="selectSalesPersonalExtraReports" resultType="org.springframework.ui.ModelMap">
    SELECT dsr.admin_id as id, sum(dsr.order_count) as order_count, sum(dsr.order_amount) as order_amount
	FROM daily_sales_report dsr 
	where dsr.create_time &gt;= #{startTime,jdbcType=TIMESTAMP} and dsr.create_time &lt; #{endTime,jdbcType=TIMESTAMP}
	<if test="ids != null">
		and dsr.admin_id in 
		<foreach item="item" index="index" collection="ids" open="(" separator="," close=")">
			#{item}
		</foreach>	
	</if>
	group by dsr.admin_id
  </select>
  
  <select id="selectSalesDepartmentReports" resultType="com.goafanti.report.bo.DepartmentSalesReportBO">
    SELECT dm.id, a.name as managerName, dm.name as departmentName, count(a2.id) as memberCount, dm.super_id as superId
	FROM department dm
	left join admin a2 on a2.department_id = dm.id 
	left join admin a on a.id = dm.manager_id
	group by dm.id, a.id
	order by dm.name asc
  </select>
  
  <select id="selectSalesDepartmentExtraReports" resultType="org.springframework.ui.ModelMap">
    SELECT dsr.dep_id as id, sum(dsr.order_count) as order_count, sum(dsr.order_amount) as order_amount
	FROM daily_sales_report dsr 
	where dsr.create_time &gt;= #{startTime,jdbcType=TIMESTAMP} and dsr.create_time &lt; #{endTime,jdbcType=TIMESTAMP}
	group by dsr.dep_id
  </select>
  
  <select id="selectTotalReports" resultType="org.springframework.ui.ModelMap">
    SELECT sum(dsr.order_count) as totalCount, sum(dsr.order_amount) as totalAmount
	FROM admin a
	left join daily_sales_report dsr on dsr.admin_id = a.id
	<if test="depName != null"> 
	left join department dm on dm.id = a.department_id 
	</if>
	where dsr.create_time &gt;= #{startTime,jdbcType=TIMESTAMP} and dsr.create_time &lt; #{endTime,jdbcType=TIMESTAMP}
	<if test="depName != null">
		and dm.name like concat(#{depName,jdbcType=VARCHAR},'%')
	</if>
	<if test="name != null">
		and a.name like concat(#{name,jdbcType=VARCHAR},'%')
	</if>
	<if test="position != null">
		and a.position like concat(#{position,jdbcType=VARCHAR},'%')
	</if>
  </select>
  
   <select id="findmarketingStatisticsList" parameterType="String" resultType="com.goafanti.report.bo.marketingESBo">
		select t1.id,t1.name,t1.departmentId,t1.departmentName,t1.manager_id,t1.departmentManagerName,
		t1.roleName,t1.privateCustomersCount,t1.entryTime,
		 t2.yearFollowNo,t2.yearVisitNo,t2.yearTelNo,
			 t2.monthFollowNo,t2.monthVisitNo,t2.monthTelNo,
			 t2.weeklyFollowNo,t2.weeklyVisitNo,t2.weeklyTelNo,
			 t2.dayFollowNo,t2.dayVisitNo,t2.dayTelNo 
		from(
		select a.id as id,a.name as name,a.department_id as departmentId,d.name as departmentName,d.manager_id,f.name as departmentManagerName,
				c.role_name as roleName,ifnull(e.privateCustomersCount,0)as privateCustomersCount,a.create_time as entryTime
		from admin a left join user_role b
		on a.id=b.uid left join role c
		on b.rid=c.id left join department d
		on a.department_id=d.id left join (select aid,count(0) as privateCustomersCount from user where aid is not null and aid != '' and share_type=0 and status != 1 group by aid)e
		on a.id=e.aid left join admin f
		on d.manager_id=f.id
		where c.role_name in ('营销员' ,'营销经理','营销管理员')
		<if test="depId != null">
		and a.department_id = #{depId,jdbcType=VARCHAR}
	</if>
)t1
left join (
select x.aid,x.yearFollowNo,x.yearVisitNo,x.yearTelNo,
			 y.monthFollowNo,y.monthVisitNo,y.monthTelNo,
			 z.weeklyFollowNo,z.weeklyVisitNo,z.weeklyTelNo,
			 w.dayFollowNo,w.dayVisitNo,dayTelNo
			 from (
select
			aid,
			count(0) as yearFollowNo,
			ifnull(sum(case when contact_type = 0 then 1 else 0 end) ,0)as yearVisitNo, 
			ifnull(sum(case when contact_type = 1 then 1 else 0 end) ,0)as yearTelNo
			from user_follow where 1=1
			and create_time &gt; #{yearStart,jdbcType=VARCHAR}
			and create_time &lt; #{yearEnd,jdbcType=VARCHAR}
			group by aid
)x  left join (
select
			aid,
			count(0) as monthFollowNo,
			ifnull(sum(case when contact_type = 0 then 1 else 0 end) ,0)as monthVisitNo, 
			ifnull(sum(case when contact_type = 1 then 1 else 0 end) ,0)as monthTelNo
			from user_follow where 1=1
			and create_time &gt; #{monthStart,jdbcType=VARCHAR}
			and create_time &lt; #{monthEnd,jdbcType=VARCHAR}
			group by aid
)y on x.aid = y.aid
left join (
select
			aid,
			count(0) as weeklyFollowNo,
			ifnull(sum(case when contact_type = 0 then 1 else 0 end) ,0)as weeklyVisitNo, 
			ifnull(sum(case when contact_type = 1 then 1 else 0 end) ,0)as weeklyTelNo
			from user_follow where 1=1
			and create_time &gt; #{weeklyStart,jdbcType=VARCHAR}
			and create_time &lt; #{weeklyEnd,jdbcType=VARCHAR}
			group by aid
)z on y.aid = z.aid
left join (
select
			aid,
			count(0) as dayFollowNo,
			ifnull(sum(case when contact_type = 0 then 1 else 0 end) ,0)as dayVisitNo, 
			ifnull(sum(case when contact_type = 1 then 1 else 0 end) ,0)as dayTelNo
			from user_follow where 1=1
			and create_time &gt; #{dayStart,jdbcType=VARCHAR}
			and create_time &lt; #{dayEnd,jdbcType=VARCHAR}
			group by aid
)w on y.aid = w.aid
)t2 on t1.id = t2.aid
 </select>
  
  <select id="marketingDepStatisticsList" parameterType="String" resultType="com.goafanti.report.bo.marketingESBo">
	select t1.departmentId,t1.departmentName,t1.departmentManagerName,t1.privateCustomersCount,t1.departmentNumber,
			t1.dataCount,
			t2.yearFollowNo,t2.yearVisitNo,t2.yearTelNo,
			 t2.monthFollowNo,t2.monthVisitNo,t2.monthTelNo,
			 t2.weeklyFollowNo,t2.weeklyVisitNo,t2.weeklyTelNo,
			 t2.dayFollowNo,t2.dayVisitNo,t2.dayTelNo ,
			 t2.dayFollowNo/t1.departmentNumber as dayAVG
		from(
select
	a.department_id as departmentId,
	b.name as departmentName,
	c.name as departmentManagerName,
	a.count2 as privateCustomersCount,
	d.count3 as departmentNumber,
	a.dataCount
from ( select
			z.department_id,
			sum( y.count1 ) as count2
			,ifnull(sum(y2.dataCount),0) as  dataCount 
			from
			( select aid, count( aid ) as count1
				from user 
				where aid is not null and aid !=''
				and share_type=0
				and status != 1
				group by aid
			) y left join admin z on
			y.aid = z.id left join (select aid,count(0) as dataCount from user
        where create_time &gt; date_sub(curdate(),interval 2 day)
        and create_time &lt; curdate()
        group by aid) y2 on y2.aid=z.id
	group by z.department_id
	) a left join department b 
	on a.department_id = b.id left join admin c
	on b.manager_id=c.id left join (select department_id,count(0) as count3 from admin   	group by department_id)d
	on a.department_id = d.department_id
	where a.department_id is not null
	<if test="depId != null">
		and a.department_id = #{depId,jdbcType=VARCHAR}
	</if>
)t1
left join (
select m.department_id as departmentId,sum(n.dayFollowNo)as dayFollowNo,sum(n.dayVisitNo)as dayVisitNo,sum(n.dayTelNo) as dayTelNo
					,sum(n.weeklyFollowNo)as weeklyFollowNo,sum(n.weeklyVisitNo)as weeklyVisitNo,sum(n.weeklyTelNo) as weeklyTelNo
					,sum(n.monthFollowNo)as monthFollowNo,sum(n.monthVisitNo)as monthVisitNo,sum(n.monthTelNo) as monthTelNo
					,sum(n.yearFollowNo)as yearFollowNo,sum(n.yearVisitNo)as yearVisitNo,sum(n.yearTelNo) as yearTelNo
from admin m left join 
(select x.aid,x.yearFollowNo,x.yearVisitNo,x.yearTelNo,
			 y.monthFollowNo,y.monthVisitNo,y.monthTelNo,
			 z.weeklyFollowNo,z.weeklyVisitNo,z.weeklyTelNo,
			 w.dayFollowNo,w.dayVisitNo,dayTelNo
			 from (
select
			aid,
			count(0) as yearFollowNo,
			ifnull(sum(case when contact_type = 0 then 1 else 0 end) ,0)as yearVisitNo, 
			ifnull(sum(case when contact_type = 1 then 1 else 0 end) ,0)as yearTelNo
			from user_follow where 1=1
			and create_time &gt; #{yearStart,jdbcType=VARCHAR}
			and create_time &lt; #{yearEnd,jdbcType=VARCHAR}
			group by aid
)x  left join (
select
			aid,
			count(0) as monthFollowNo,
			ifnull(sum(case when contact_type = 0 then 1 else 0 end) ,0)as monthVisitNo, 
			ifnull(sum(case when contact_type = 1 then 1 else 0 end) ,0)as monthTelNo
			from user_follow where 1=1
			and create_time &gt; #{monthStart,jdbcType=VARCHAR}
			and create_time &lt; #{monthEnd,jdbcType=VARCHAR}
			group by aid
)y on x.aid = y.aid
left join (
select
			aid,
			count(0) as weeklyFollowNo,
			ifnull(sum(case when contact_type = 0 then 1 else 0 end) ,0)as weeklyVisitNo, 
			ifnull(sum(case when contact_type = 1 then 1 else 0 end) ,0)as weeklyTelNo
			from user_follow where 1=1
			and create_time &gt; #{weeklyStart,jdbcType=VARCHAR}
			and create_time &lt; #{weeklyEnd,jdbcType=VARCHAR}
			group by aid
)z on y.aid = z.aid
left join (
select
			aid,
			count(0) as dayFollowNo,
			ifnull(sum(case when contact_type = 0 then 1 else 0 end) ,0)as dayVisitNo, 
			ifnull(sum(case when contact_type = 1 then 1 else 0 end) ,0)as dayTelNo
			from user_follow where 1=1
			and create_time &gt; #{dayStart,jdbcType=VARCHAR}
			and create_time &lt; #{dayEnd,jdbcType=VARCHAR}
			group by aid
)w on y.aid = w.aid) n on m.id=n.aid
where m.department_id is not null and m.department_id !=''
group by m.department_id
)t2 on t1.departmentId = t2.departmentId
ORDER by t1.departmentName
	</select>
  <select id="privateCustomersCount" parameterType="string" resultType="com.goafanti.report.bo.CountMarketingStatisticsBo">
	select SUM( n.privateCustomersCount )as privateCustomersCount,
	SUM( m.dayFollowNo )as dayFollowNo,
	SUM( m.dayVisitNo )as dayVisitNo,
	SUM( m.dayTelNo )as dayTelNo,
	SUM( m.weeklyFollowNo )as weeklyFollowNo,
	SUM( m.weeklyVisitNo )as weeklyVisitNo,
	SUM( m.weeklyTelNo )as weeklyTelNo,
	SUM( m.monthFollowNo )as monthFollowNo,
	SUM( m.monthVisitNo )as monthVisitNo,
	SUM( m.monthTelNo )as monthTelNo,
	SUM( m.yearFollowNo )as yearFollowNo,
	SUM( m.yearVisitNo )as yearVisitNo,
	SUM( m.yearTelNo )as yearTelNo
	
from( 
select a.id,sum(e.privateCustomersCount) as privateCustomersCount
	from admin a left join user_role b
	on a.id=b.uid left join role c
	on b.rid=c.id left join (select aid,count(0) as privateCustomersCount from user where aid is not null and aid !=''
				and share_type=0 and status != 1 group by aid)e
	on a.id=e.aid  where c.role_name in ('营销员','营销经理','营销管理员')
	<if test="depId != null">
	and a.department_id = #{depId,jdbcType=VARCHAR}
	</if>
	GROUP by a.id
) n left join (
select x.aid,x.yearFollowNo,x.yearVisitNo,x.yearTelNo,
			 y.monthFollowNo,y.monthVisitNo,y.monthTelNo,
			 z.weeklyFollowNo,z.weeklyVisitNo,z.weeklyTelNo,
			 w.dayFollowNo,w.dayVisitNo,w.dayTelNo
			 from (
select
			aid,
			count(0) as yearFollowNo,
			ifnull(sum(case when contact_type = 0 then 1 else 0 end) ,0)as yearVisitNo, 
			ifnull(sum(case when contact_type = 1 then 1 else 0 end) ,0)as yearTelNo
			from user_follow where 1=1
			and create_time &gt;  #{yearStart,jdbcType=VARCHAR}
			and create_time &lt;  #{yearEnd,jdbcType=VARCHAR}
			group by aid
)x  left join (
select
			aid,
			count(0) as monthFollowNo,
			ifnull(sum(case when contact_type = 0 then 1 else 0 end) ,0)as monthVisitNo, 
			ifnull(sum(case when contact_type = 1 then 1 else 0 end) ,0)as monthTelNo
			from user_follow where 1=1
			and create_time &gt;  #{monthStart,jdbcType=VARCHAR}
			and create_time &lt;  #{monthEnd,jdbcType=VARCHAR}
			group by aid
)y on x.aid = y.aid
left join (
select
			aid,
			count(0) as weeklyFollowNo,
			ifnull(sum(case when contact_type = 0 then 1 else 0 end) ,0)as weeklyVisitNo, 
			ifnull(sum(case when contact_type = 1 then 1 else 0 end) ,0)as weeklyTelNo
			from user_follow where 1=1
			and create_time &gt;  #{weeklyStart,jdbcType=VARCHAR}
			and create_time &lt;  #{weeklyEnd,jdbcType=VARCHAR}
			group by aid
)z on y.aid = z.aid
left join (
select
			aid,
			count(0) as dayFollowNo,
			ifnull(sum(case when contact_type = 0 then 1 else 0 end) ,0)as dayVisitNo, 
			ifnull(sum(case when contact_type = 1 then 1 else 0 end) ,0)as dayTelNo
			from user_follow where 1=1
			and create_time &gt;  #{dayStart,jdbcType=VARCHAR}
			and create_time &lt;  #{dayEnd,jdbcType=VARCHAR}
			group by aid
)w on y.aid = w.aid
)m on n.id=m.aid	
	</select>
  
  <select id="privateCustomersDepCount" parameterType="String" resultType="com.goafanti.report.bo.CountMarketingStatisticsBo">
	select sum(t1.privateCustomersCount) as privateCustomersCount,sum(t1.departmentNumber) as departmentNumber,
			sum(t1.dataCount) as dataCount,
			sum(t2.yearFollowNo) as yearFollowNo,sum(t2.yearVisitNo) as yearVisitNo,sum(t2.yearTelNo) as yearTelNo,
			 sum(t2.monthFollowNo) as monthFollowNo,sum(t2.monthVisitNo) as monthVisitNo,sum(t2.monthTelNo) as monthTelNo,
			 sum(t2.weeklyFollowNo) as weeklyFollowNo,sum(t2.weeklyVisitNo) as weeklyVisitNo,sum(t2.weeklyTelNo) as weeklyTelNo,
			 sum(t2.dayFollowNo) as dayFollowNo,sum(t2.dayVisitNo) as dayVisitNo,sum(t2.dayTelNo) as dayTelNo
			,sum(t2.dayFollowNo)/sum(t1.departmentNumber) as dayAVG
		from(select
	a.department_id as departmentId,
	b.name as departmentName,
	c.name as departmentManagerName,
	a.count2 as privateCustomersCount,
	a.dataCount ,
	d.count3 as departmentNumber
from ( select
			z.department_id,
			sum( y.count1 ) as count2 
			,ifnull(sum(y2.dataCount),0) as  dataCount 
			from
			( select aid, count( aid ) as count1
				from user 
				where aid is not null and aid !=''
				and share_type=0 and status != 1
				group by aid
			) y left join admin z on
			y.aid = z.id left join (select aid,count(0) as dataCount from user
        where create_time &gt; date_sub(curdate(),interval 2 day)
        and create_time &lt; curdate()
        group by aid) y2 on y2.aid=z.id 
		group by
			z.department_id
	) a left join department b 
	on a.department_id = b.id left join admin c
	on b.manager_id=c.id left join (select department_id,count(0) as count3 from admin   	group by department_id)d
	on a.department_id = d.department_id
	where a.department_id is not null
	<if test="depId != null">
	and a.department_id = #{depId,jdbcType=VARCHAR}
	</if>
)t1
left join (
select m.department_id as departmentId,sum(n.dayFollowNo)as dayFollowNo,sum(n.dayVisitNo)as dayVisitNo,sum(n.dayTelNo) as dayTelNo
					,sum(n.weeklyFollowNo)as weeklyFollowNo,sum(n.weeklyVisitNo)as weeklyVisitNo,sum(n.weeklyTelNo) as weeklyTelNo
					,sum(n.monthFollowNo)as monthFollowNo,sum(n.monthVisitNo)as monthVisitNo,sum(n.monthTelNo) as monthTelNo
					,sum(n.yearFollowNo)as yearFollowNo,sum(n.yearVisitNo)as yearVisitNo,sum(n.yearTelNo) as yearTelNo
from admin m left join 
(select x.aid,x.yearFollowNo,x.yearVisitNo,x.yearTelNo,
			 y.monthFollowNo,y.monthVisitNo,y.monthTelNo,
			 z.weeklyFollowNo,z.weeklyVisitNo,z.weeklyTelNo,
			 w.dayFollowNo,w.dayVisitNo,w.dayTelNo
			 from (
select
			aid,
			count(0) as yearFollowNo,
			ifnull(sum(case when contact_type = 0 then 1 else 0 end) ,0)as yearVisitNo, 
			ifnull(sum(case when contact_type = 1 then 1 else 0 end) ,0)as yearTelNo
			from user_follow where 1=1
			and create_time &gt; #{yearStart,jdbcType=VARCHAR}
			and create_time &lt; #{yearEnd,jdbcType=VARCHAR}
			group by aid
)x  left join (
select
			aid,
			count(0) as monthFollowNo,
			ifnull(sum(case when contact_type = 0 then 1 else 0 end) ,0)as monthVisitNo, 
			ifnull(sum(case when contact_type = 1 then 1 else 0 end) ,0)as monthTelNo
			from user_follow where 1=1
			and create_time &gt; #{monthStart,jdbcType=VARCHAR}
			and create_time &lt; #{monthEnd,jdbcType=VARCHAR}
			group by aid
)y on x.aid = y.aid
left join (
select
			aid,
			count(0) as weeklyFollowNo,
			ifnull(sum(case when contact_type = 0 then 1 else 0 end) ,0)as weeklyVisitNo, 
			ifnull(sum(case when contact_type = 1 then 1 else 0 end) ,0)as weeklyTelNo
			from user_follow where 1=1
			and create_time &gt; #{weeklyStart,jdbcType=VARCHAR}
			and create_time &lt; #{weeklyEnd,jdbcType=VARCHAR}
			group by aid
)z on y.aid = z.aid
left join (
select
			aid,
			count(0) as dayFollowNo,
			ifnull(sum(case when contact_type = 0 then 1 else 0 end) ,0)as dayVisitNo, 
			ifnull(sum(case when contact_type = 1 then 1 else 0 end) ,0)as dayTelNo
			from user_follow where 1=1
			and create_time &gt; #{dayStart,jdbcType=VARCHAR}
			and create_time &lt; #{dayEnd,jdbcType=VARCHAR}
			group by aid
)w on y.aid = w.aid) n on m.id=n.aid
where m.department_id is not null and m.department_id !=''
group by m.department_id
)t2 on t1.departmentId = t2.departmentId
</select>


<select id="sometimeMarketingStatistics" parameterType="String" resultType="com.goafanti.report.bo.SomeTimeMarketingBo">
select a.id,a.name,d.id as departmentId,d.name as departmentName,role_name as roleName,ifnull(e.telNo,0)as countTelNo,
	ifnull(e.visitNo,0)as countVisitNo,ifnull(e.followNo,0)as countFollowNo
	<if test="startTime !=null and endTime !=null">
	,e.followNo/datediff(#{endTime,jdbcType=VARCHAR},#{startTime,jdbcType=VARCHAR}) as dayAVG
	</if>
	from admin a left join user_role b on
	a.id = b.uid left join role c on
	b.rid = c.id left join department d on
	a.department_id = d.id left join (		select
		a.aid,
		count( 0 ) as followNo,
		ifnull(
			sum( case when a.contact_type = 0 then 1 else 0 end ),
			0
		) as visitNo,
		ifnull(
			sum( case when a.contact_type = 1 then 1 else 0 end ),
			0
			) as telNo
	from user_follow a 
	where
		1 = 1
	<if test="startTime !=null">
	and a.create_time &gt;  #{startTime,jdbcType=VARCHAR}
	</if>
	<if test="endTime !=null">
	and a.create_time &lt;  #{endTime,jdbcType=VARCHAR}
	</if>
		GROUP BY a.aid)e on a.id=e.aid
	where c.role_name in ('营销员','营销经理','营销管理员')
	<if test="depId != null">
	and a.department_id= #{depId,jdbcType=VARCHAR}
	</if>
	order by a.name 
</select>
<select id="sometimeMarketingDepStatistics" parameterType="String" resultType="com.goafanti.report.bo.SomeTimeMarketingBo">
	select z.id,z.name as departmentName,y.name as departmentManagerName,
 		x.followNo as countFollowNo,x.visitNo as countVisitNo,x.telNo as countTelNo
 		<if test="startTime !=null and endTime !=null">
			,x.followNo/datediff(#{endTime,jdbcType=VARCHAR},#{startTime,jdbcType=VARCHAR}) as dayAVG
		</if>
	 from (select a.id, a.name,a.manager_id
	from department a left join admin b on a.id=b.department_id
	left join user_role c on b.id = c.uid
	 left join role d on c.rid = d.id
	  where d.role_name in ('营销员','营销经理','营销管理员')
	  	<if test="depId != null">
	and a.id=#{depId,jdbcType=VARCHAR}
		</if>
	  group by a.id) z  left join (select b.department_id as departmentId,sum(a.followNo) as followNo,sum(a.visitNo)as visitNo,sum(a.telNo)as telNo
			from (select
		aid,
		count( 0 ) as followNo,
		ifnull(
			sum( case when contact_type = 0 then 1 else 0 end ),
			0
		) as visitNo,
		ifnull(
			sum( case when contact_type = 1 then 1 else 0 end ),
			0
			) as telNo
	from user_follow a where 1 = 1
	<if test="startTime !=null">
	and a.create_time &gt;  #{startTime,jdbcType=VARCHAR}
	</if>
	<if test="endTime !=null">
	and a.create_time &lt;  #{endTime,jdbcType=VARCHAR}
	</if>
	GROUP BY aid)a left join admin b on a.aid=b.id
	left join department c on b.department_id=c.id
	where b.department_id is not null  and b.department_id !=''
	GROUP by b.department_id
) x  on x.departmentId=z.id
	left join admin y on z.manager_id=y.id
	</select>
	<select id="countsometimeMarketingStatistics" parameterType="String" resultType="com.goafanti.report.bo.SomeTimeMarketingBo">
		select
			count( 0 ) as countFollowNo,
			ifnull(
				sum( case when a.contact_type = 0 then 1 else 0 end ),
				0
			) as countVisitNo,
			ifnull(
				sum( case when a.contact_type = 1 then 1 else 0 end ),
				0
			) as countTelNo
			<if test="startTime !=null and endTime !=null">
			,count( 0 )/datediff(#{endTime,jdbcType=VARCHAR},#{startTime,jdbcType=VARCHAR}) as dayAVG
			</if>
		from
			user_follow a left join admin b on
			a.aid = b.id left join department c on
			b.department_id = c.id left join user_role d on
			b.id = d.uid left join role e on
			d.rid = e.id
		where 1 = 1
		and( e.role_name = '营销员' or e.role_name = '营销经理' or e.role_name = '营销管理员')
		<if test="startTime !=null">
		and a.create_time &gt;  #{startTime,jdbcType=VARCHAR}
		</if>
		<if test="endTime !=null">
		and a.create_time &lt;  #{endTime,jdbcType=VARCHAR}
		</if>
		<if test="depId != null">
		and b.department_id= #{depId,jdbcType=VARCHAR}
		</if>
	</select>
	
	<select id="countsometimeMarketingDepStatistics" parameterType="String" resultType="com.goafanti.report.bo.SomeTimeMarketingBo">
		select
			count( 0 ) as countFollowNo,
			ifnull(
				sum( case when a.contact_type = 0 then 1 else 0 end ),
				0
			) as countVisitNo,
			ifnull(
				sum( case when a.contact_type = 1 then 1 else 0 end ),
				0
			) as countTelNo
			<if test="startTime !=null and endTime !=null">
			,count( 0 )/datediff(#{endTime,jdbcType=VARCHAR},#{startTime,jdbcType=VARCHAR}) as A
			</if>
		from
			user_follow a left join admin b on
			a.aid = b.id left join department c on
			b.department_id = c.id left join user_role d on
			b.id = d.uid left join role e on
			d.rid = e.id
		where 1 = 1
		and( e.role_name = '营销员' or e.role_name = '营销经理' or e.role_name = '营销管理员')
		<if test="depId != null">
		and a.department_id= #{depId,jdbcType=VARCHAR}
		</if>
	</select>
	<select id="technicalDepStatistics" parameterType='String' resultType="com.goafanti.report.bo.technicalDepStatisticsListBo">
		select
	d.nickname as OrgName,f.name as orderDep,c.sign_total_amount as signTotalAmount,
	date_format( c.sign_time, '%Y-%m-%d' ) as signTime,c.contract_no as contractNo,
	c.order_remarks as orderRemarks, g.contacts, g.contact_mobile as contactMobile,
	a.commodity_name as commodityName, date_format( a.task_distribution_time, '%Y-%m-%d %H:%i:%S' ) as taskDistributionTime,
	i.name as dutyDep, h.name as dutyName, h.contact_mobile as dutyContactMobile,
	(case when a.task_status = 0 then '未分配' when a.task_status = 1 then '已派单'
	when a.task_status = 2 then '材料已启动' when a.task_status = 3 then '材料已完成'end ) as taskStatus,
	a.finish_time as finishTime, a.task_comment as taskComment,
	(case when a.publish_status = 0 then '已公示' when a.publish_status = 1 then '未公示' 
	when a.publish_status = 2 then '待公示' when a.publish_status = 3 then '抽查中' end) as publishStatus,
	a.publish_time as publishTime, e.name as salesmanName
from
	contract_task a left join contract b on
	a.contract_id = b.id left join user d on
	b.uid = d.id left join t_order c on
	b.order_no = c.order_no left join admin e on
	c.salesman_id = e.id left join department f on
	e.department_id = f.id left join organization_identity g on
	d.id = g.uid left join admin h on
	a.task_receiver = h.id left join department i on
	h.department_id = i.id
		where 1=1
		<if test="depId != null">
		and e.department_id= #{depId,jdbcType=VARCHAR}
		</if>
		<if test="taskStatus != null">
		and a.task_status = #{taskStatus,jdbcType=VARCHAR}
		</if>
		<if test="publishStatus != null">
		and a.publish_status= #{publishStatus,jdbcType=VARCHAR}
		</if>
		<if test="startTime !=null">
		and c.sign_time &gt;  #{startTime,jdbcType=VARCHAR}
		</if>
		<if test="endTime !=null">
		and c.sign_time &lt;  #{endTime,jdbcType=VARCHAR}
		</if>
		order by c.sign_time desc,f.name
	</select>
</mapper>