<?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.TOrderMonthReportMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.TOrderMonthReport">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 14:53:14 CST 2018.
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="type" jdbcType="INTEGER" property="type" />
    <result column="month" jdbcType="INTEGER" property="month" />
    <result column="amount" jdbcType="DECIMAL" property="amount" />
    <result column="proof_count" jdbcType="REAL" property="proofCount" />
    <result column="salemans_id" jdbcType="VARCHAR" property="salemansId" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 14:53:14 CST 2018.
    -->
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 14:53:14 CST 2018.
    -->
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 14:53:14 CST 2018.
    -->
    id, type, month, amount, proof_count, salemans_id, update_time
  </sql>
  <select id="selectByExample" parameterType="com.goafanti.common.model.TOrderMonthReportExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 14:53:14 CST 2018.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from t_order_month_report
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 14:53:14 CST 2018.
    -->
    select 
    <include refid="Base_Column_List" />
    from t_order_month_report
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 14:53:14 CST 2018.
    -->
    delete from t_order_month_report
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="com.goafanti.common.model.TOrderMonthReportExample">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 14:53:14 CST 2018.
    -->
    delete from t_order_month_report
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.TOrderMonthReport">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 14:53:14 CST 2018.
    -->
    insert into t_order_month_report (id, type, month, 
      amount, proof_count, salemans_id, 
      update_time)
    values (#{id,jdbcType=INTEGER}, #{type,jdbcType=INTEGER}, #{month,jdbcType=INTEGER}, 
      #{amount,jdbcType=DECIMAL}, #{proofCount,jdbcType=REAL}, #{salemansId,jdbcType=VARCHAR}, 
      #{updateTime,jdbcType=TIMESTAMP})
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.TOrderMonthReport">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 14:53:14 CST 2018.
    -->
    insert into t_order_month_report
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="type != null">
        type,
      </if>
      <if test="month != null">
        month,
      </if>
      <if test="amount != null">
        amount,
      </if>
      <if test="proofCount != null">
        proof_count,
      </if>
      <if test="salemansId != null">
        salemans_id,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="type != null">
        #{type,jdbcType=INTEGER},
      </if>
      <if test="month != null">
        #{month,jdbcType=INTEGER},
      </if>
      <if test="amount != null">
        #{amount,jdbcType=DECIMAL},
      </if>
      <if test="proofCount != null">
        #{proofCount,jdbcType=REAL},
      </if>
      <if test="salemansId != null">
        #{salemansId,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.goafanti.common.model.TOrderMonthReportExample" resultType="java.lang.Integer">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 14:53:14 CST 2018.
    -->
    select count(*) from t_order_month_report
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 14:53:14 CST 2018.
    -->
    update t_order_month_report
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.type != null">
        type = #{record.type,jdbcType=INTEGER},
      </if>
      <if test="record.month != null">
        month = #{record.month,jdbcType=INTEGER},
      </if>
      <if test="record.amount != null">
        amount = #{record.amount,jdbcType=DECIMAL},
      </if>
      <if test="record.proofCount != null">
        proof_count = #{record.proofCount,jdbcType=REAL},
      </if>
      <if test="record.salemansId != null">
        salemans_id = #{record.salemansId,jdbcType=VARCHAR},
      </if>
      <if test="record.updateTime != null">
        update_time = #{record.updateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 14:53:14 CST 2018.
    -->
    update t_order_month_report
    set id = #{record.id,jdbcType=INTEGER},
      type = #{record.type,jdbcType=INTEGER},
      month = #{record.month,jdbcType=INTEGER},
      amount = #{record.amount,jdbcType=DECIMAL},
      proof_count = #{record.proofCount,jdbcType=REAL},
      salemans_id = #{record.salemansId,jdbcType=VARCHAR},
      update_time = #{record.updateTime,jdbcType=TIMESTAMP}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.TOrderMonthReport">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 14:53:14 CST 2018.
    -->
    update t_order_month_report
    <set>
      <if test="type != null">
        type = #{type,jdbcType=INTEGER},
      </if>
      <if test="month != null">
        month = #{month,jdbcType=INTEGER},
      </if>
      <if test="amount != null">
        amount = #{amount,jdbcType=DECIMAL},
      </if>
      <if test="proofCount != null">
        proof_count = #{proofCount,jdbcType=REAL},
      </if>
      <if test="salemansId != null">
        salemans_id = #{salemansId,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.TOrderMonthReport">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 14:53:14 CST 2018.
    -->
    update t_order_month_report
    set type = #{type,jdbcType=INTEGER},
      month = #{month,jdbcType=INTEGER},
      amount = #{amount,jdbcType=DECIMAL},
      proof_count = #{proofCount,jdbcType=REAL},
      salemans_id = #{salemansId,jdbcType=VARCHAR},
      update_time = #{updateTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>
   <!-- 定时插入上月的签单数据 -->
  <insert id="insertLastMonthOrderData" parameterType="java.lang.String">
  	REPLACE INTO t_order_month_report(`type`,`month`,amount,proof_count,salemans_id) 
	select o.order_type, date_format(DATE_ADD(NOW(),INTERVAL -1 MONTH),'%Y%m'),sum(o.total_amount), sum(o.proof_count), o.salesman_id 
	from t_order_new o
	where 1 = 1
	  and o.proof_status = 1 and o.delete_sign = 0 and o.order_status &lt;&gt; 7
	  and PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( o.sign_time, '%Y%m' ) ) = 1
	group by o.salesman_id , o.order_type
  </insert>
  <!-- 定时插入上一年的数据 -->
  <insert id="insertLastYearOrderData" parameterType="java.lang.String">
  	REPLACE INTO t_order_year_report(`type`,`year`,amount,proof_count,salemans_id) 
	select m.`type`, year(date_sub(now(),interval 1 year)), sum(amount), sum(proof_count), salemans_id from t_order_month_report m
	where CAST(`month` as CHAR) like concat (year(date_sub(now(),interval 1 year)),'%') 
	group by m.salemans_id , m.`type`
  </insert>
  <!-- 查看订单统计数据 -->
  <select id="getOrderReportDataList" parameterType="Map" resultType="com.goafanti.order.bo.OrderReportBo">
  	select @rowno:= @rowno+1 as id, d.name as deptName, a.name as saleName, m.`type`, sum(proof_count) as proofCount, sum(amount) as amount,
  	m.id as mid
	from t_order_month_report m
	left join admin a on m.salemans_id = a.id
	left join department d on d.id = a.department_id
	,(select @rowno:=0)c
	where 
	`month` between #{r.month,jdbcType=INTEGER} and  #{r.month1,jdbcType=INTEGER}
	<if test='r.departmentId != null and r.departmentId != ""'>
	 and a.department_id = #{r.departmentId,jdbcType=VARCHAR}
	</if>
	<if test='r.type != null and r.type != ""'><!-- 装数据是0变成null 加一处理后减一 -->
	 and m.type= #{r.type,jdbcType=INTEGER} -1
	</if>
	<if test='r.saleName != null and r.saleName != ""'>
	<bind name="n" value=" '%' + r.saleName + '%'"/>
	 and a.name like #{n,jdbcType=VARCHAR}
	</if>
	group by
	a.department_id, a.id, m.`type`
	<if test="page_sql != null">
		${page_sql}
	</if>
  </select>
  <select id="getOrderReportDataCount" parameterType="Map" resultType="java.lang.Integer">
  select count(*) from (
  	select count(*)
	from t_order_month_report m
	left join admin a on m.salemans_id = a.id
	left join department d on d.id = a.department_id
	where 
	`month` between #{r.month,jdbcType=INTEGER} and  #{r.month1,jdbcType=INTEGER}
	<if test='r.departmentId != null and r.departmentId != ""'>
	 and a.department_id = #{r.departmentId,jdbcType=VARCHAR}
	</if>
	<if test='r.type != null and r.type != ""'>
	 and m.type= #{r.type,jdbcType=INTEGER} -1
	</if>
	<if test='r.saleName != null and r.saleName != ""'>
	<bind name="n" value=" '%' + r.saleName + '%'"/>
	 and a.name like #{n,jdbcType=VARCHAR}
	</if>
	group by
	a.department_id, a.id, m.`type`
	)ff
  </select>
 <!-- 导出月统计 -->
 <select id="exportMonthData" parameterType="com.goafanti.order.bo.OrderReportBo" resultType="com.goafanti.order.bo.OrderReportBo">
 	select  @rowno:= @rowno+1 as id, d.name as deptName, a.name as saleName, m.`type`, sum(proof_count) as proofCount, sum(amount) as amount
	from t_order_month_report m
	left join admin a on m.salemans_id = a.id
	left join department d on d.id = a.department_id
	,(select @rowno:=0)c
	where 
	`month` between #{month,jdbcType=INTEGER} and  #{month1,jdbcType=INTEGER}
	<if test='departmentId != null and departmentId != ""'>
	 and a.department_id = #{departmentId,jdbcType=VARCHAR}
	</if>
	<if test='type != null and type != ""'>
	 and m.type= #{type,jdbcType=INTEGER} -1
	</if>
	<if test='saleName != null and saleName != ""'>
	<bind name="n" value=" '%' + saleName + '%'"/>
	 and a.name like #{n,jdbcType=VARCHAR}
	</if>
	group by
	a.department_id, a.id, m.`type`
 </select>
 <!-- 查看某人某年某月某类型的数据是否存在 -->
 <select id="checkData" parameterType="com.goafanti.order.bo.OrderReportBo" resultType="java.lang.Integer">
 	select count(*) from
	t_order_month_report
	where salemans_id = #{salemansId,jdbcType=VARCHAR} and `month` = #{month,jdbcType=INTEGER}
	and type = #{type,jdbcType=INTEGER}
 </select>
 <!-- 查看某时间范围内的月统计 -->
 <select id="getMonthReportByMonth" parameterType="com.goafanti.order.bo.OrderReportBo" resultType="com.goafanti.order.bo.OrderReportBo">
 	select salemans_id as salemansId, sum(case `type` when 0 then proof_count else 0 end) renzm ,sum(case `type` when 1 then proof_count else 0 end) kejim
	from 
	t_order_month_report
	where `month` between  #{month,jdbcType=INTEGER} and #{month1,jdbcType=INTEGER}
	<if test="salemansId != null">
		and salemans_id = #{salemansId,jdbcType=VARCHAR}
	</if>
	<if test="salemansId == null">
		group by salemans_id
	</if>
 </select>
 <!-- 查看某时间范围内的年统计 -->
 <select id="getYearReportByYear" parameterType="com.goafanti.order.bo.OrderReportBo" resultType="com.goafanti.order.bo.OrderReportBo">
 	select sum(case `type` when 0 then proof_count else 0 end) renzy ,sum(case `type` when 1 then proof_count else 0 end) kejiy
	from 
	t_order_year_report
	where `year` between #{month,jdbcType=INTEGER} and #{month1,jdbcType=INTEGER}
	<if test="salemansId != null">
		and salemans_id = #{salemansId,jdbcType=VARCHAR}
	</if>
	<if test="salemansId == null">
		group by salemans_id
	</if>
 </select>
 <!-- 更新某人某月份某类型的数据 -->
 <update id="proofDataBySomeBody" parameterType="com.goafanti.common.model.TOrderNew">
 UPDATE t_order_month_report m,
	(select sum(proof_count) as proof_count, sum(total_amount) as amount , order_type, salesman_id  from t_order_new 
	  	where  
	  	proof_status = 1 
	  	and salesman_id = #{salesmanId,jdbcType=VARCHAR} 
	  	and order_type = #{orderType,jdbcType=INTEGER}
	  	and date_format( sign_time , '%Y%m' )  = date_format( #{signTime,jdbcType=DATE}, '%Y%m' )) as o
  	
  	SET m.proof_count = o.proof_count, m.amount = o.amount
  	
  	WHERE  m.type = o.order_type and m.salemans_id = o.salesman_id and m.month =  date_format( #{signTime,jdbcType=DATE}, '%Y%m' )
 </update>
 
 <!-- 更新年数据 -->
 <update id="updateYearReportData" parameterType="java.lang.String">
 update t_order_year_report y,(

	SELECT salemans_id, `type`, sum(amount) amount, sum(proof_count) proof_count FROM t_order_month_report 
	
	WHERE CAST(`month` as CHAR) like  concat (date_format(now(),'%Y'),'%')
	
	GROUP BY salemans_id, type) m 
	
	SET y.proof_count = m.proof_count, y.amount = m.amount
	  	
	WHERE y.salemans_id = m.salemans_id and y.type = m.type and year = date_format(now(),'%Y')
 </update>
 
 <!-- 新增某人某月某类型的统计数据 -->
 <insert id="insertBySome" parameterType="com.goafanti.common.model.TOrderNew">
 	INSERT INTO t_order_month_report(`type`,`month`,amount,proof_count,salemans_id) 
 	select order_type,  date_format( sign_time , '%Y%m' ), sum(total_amount) as amount, sum(proof_count) as proof_count,   salesman_id  
	from t_order_new 
	  	where  
	  	proof_status = 1 and o.delete_sign = 0 and o.order_status &lt;&gt; 7
	  	and salesman_id = #{salesmanId,jdbcType=VARCHAR} 
	  	and order_type = #{orderType,jdbcType=INTEGER}
	  	and date_format( sign_time , '%Y%m' )  = date_format( #{signTime,jdbcType=DATE}, '%Y%m' )
 </insert>
 <!-- 更新指定月份的统计数据 -->
 <insert id="updateByMonth" parameterType="java.lang.Integer">
 	REPLACE INTO t_order_month_report(`type`,`month`,amount,proof_count,salemans_id) 
	select o.order_type, #{month,jdbcType=INTEGER},sum(o.total_amount), sum(o.proof_count), o.salesman_id
	from t_order_new o
	where 1 = 1
	  and o.proof_status = 1 and o.delete_sign = 0 and o.order_status &lt;&gt; 7
	  and date_format( o.sign_time, '%Y%m' ) = #{month,jdbcType=INTEGER}
	group by o.salesman_id , o.order_type
 </insert>
 <!-- 更新本年的年统计数据 -->
 <insert id="updateByYear">
 	REPLACE INTO t_order_year_report(`type`,`year`,amount,proof_count,salemans_id) 
	select m.`type`, year(now()), sum(amount), sum(proof_count), salemans_id from t_order_month_report m
	where CAST(`month` as CHAR) like concat (year(now()),'%') 
	group by m.salemans_id , m.`type`
 </insert>
</mapper>