<?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.boco.nbd.wios.manage.mapper.def.ScoreSupplierMapper">

    <resultMap id="BaseResultMap" type="com.boco.nbd.wios.manage.entity.bo.ScoreSupplierDTO">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="supplier_id" jdbcType="INTEGER" property="supplierId" />
        <result column="supplier_name" jdbcType="VARCHAR" property="supplierName" />
        <result column="region_id" jdbcType="INTEGER" property="regionId" />
        <result column="region_name" jdbcType="VARCHAR" property="regionName" />
        <result column="month" jdbcType="VARCHAR" property="month" />
        <result column="final_score" jdbcType="INTEGER" property="finalScore" />
        <result column="final_purchasing" jdbcType="INTEGER" property="finalPurchasing" />
        <result column="final_engineering" jdbcType="INTEGER" property="finalEngineering" />
        <result column="online" jdbcType="INTEGER" property="online" />
        <result column="offline" jdbcType="INTEGER" property="offline" />
        <result column="customer" jdbcType="INTEGER" property="customer" />
        <result column="final_deduct" jdbcType="INTEGER" property="finalDeduct" />
        <result column="description" jdbcType="VARCHAR" property="description" />
        <result column="status" jdbcType="INTEGER" property="status" />
        <result column="create_account" jdbcType="VARCHAR" property="createAccount" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="modify_account" jdbcType="VARCHAR" property="modifyAccount" />
        <result column="modify_time" jdbcType="TIMESTAMP" property="modifyTime" />
    </resultMap>


    <sql id="Base_Column_List">
  ts.id, ts.supplier_id, ts.region_id, ts.month, ts.final_score, ts.final_purchasing, ts.final_engineering,
  ts.online, ts.offline, ts.customer, ts.final_deduct, ts.description, ts.status, ts.create_account,
  ts.create_time, ts.modify_account, ts.modify_time
</sql>


    <delete id="delete" parameterType="java.lang.Integer">
  delete from t_score_supplier
  where id = #{id,jdbcType=INTEGER}
</delete>


    <insert id="insert" parameterType="com.boco.nbd.wios.manage.entity.bo.ScoreSupplier">
  insert into t_score_supplier (id, supplier_id, region_id,
    month, final_score, final_purchasing,
    final_engineering, online, offline,
    customer, final_deduct, description, status,
    create_account, create_time, modify_account,
    modify_time)
  values (#{id,jdbcType=INTEGER}, #{supplierId,jdbcType=VARCHAR}, #{regionId,jdbcType=VARCHAR},
    #{month,jdbcType=VARCHAR}, #{finalScore,jdbcType=INTEGER}, #{finalPurchasing,jdbcType=INTEGER},
    #{finalEngineering,jdbcType=INTEGER}, #{online,jdbcType=INTEGER}, #{offline,jdbcType=INTEGER},
    #{customer,jdbcType=INTEGER}, #{finalDeduct,jdbcType=INTEGER},  #{description,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER},
    #{createAccount,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{modifyAccount,jdbcType=VARCHAR},
    #{modifyTime,jdbcType=TIMESTAMP})
</insert>


    <update id="update" parameterType="com.boco.nbd.wios.manage.entity.bo.ScoreSupplier">
        update t_score_supplier
        <set>
            <if test="finalScore != null">
                final_score = #{finalScore,jdbcType=INTEGER},
            </if>
            <if test="finalPurchasing != null">
                final_purchasing = #{finalPurchasing,jdbcType=INTEGER},
            </if>
            <if test="finalEngineering != null">
                final_engineering = #{finalEngineering,jdbcType=INTEGER},
            </if>
            <if test="online != null">
                online = #{online,jdbcType=INTEGER},
            </if>
            <if test="offline != null">
                offline = #{offline,jdbcType=INTEGER},
            </if>
            <if test="customer != null">
                customer = #{customer,jdbcType=INTEGER},
            </if>
            <if test="finalDeduct != null">
                final_deduct = #{finalDeduct,jdbcType=INTEGER},
            </if>
            <if test="description != null">
                description = #{description,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                status = #{status,jdbcType=INTEGER},
            </if>
            <if test="modifyAccount != null">
                modify_account = #{modifyAccount,jdbcType=VARCHAR},
            </if>
            <if test="modifyTime != null">
                modify_time = #{modifyTime,jdbcType=TIMESTAMP},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>



    <select id="selectById" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" /> ,
        sp.code as supplier_code,
        sp.name as supplier_name,
        sp.contact_name,
        sp.contact_phone,
        tr.name as region_name
        from  t_score_supplier ts
        left join t_supplier sp on ts.supplier_id = sp.id
        left join t_region tr on ts.region_id = tr.id
        where ts.id = #{id,jdbcType=INTEGER}
    </select>

    <select id="selectLastBySupplierId" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" /> ,
        sp.code as supplier_code,
        sp.name as supplier_name,
        sp.contact_name,
        sp.contact_phone,
        tr.name as region_name
        from  t_score_supplier ts
        left join t_supplier sp on ts.supplier_id = sp.id
        left join t_region tr on ts.region_id = tr.id
        where ts.supplier_id = #{supplierId,jdbcType=INTEGER}
            and final_score >0
        order by modify_time desc
        LIMIT 1
    </select>


    <sql id="findPageWithWhere">
        <where> 1=1
            <if test="id != null" >
                and ts.id = #{id,jdbcType=INTEGER}
            </if>
            <if test="supplierId != null" >
                and ts.supplier_id = #{supplierId,jdbcType=VARCHAR}
            </if>
            <if test="supplierCode != null and supplierCode != ''" >
                and sp.code like  CONCAT(CONCAT('%', #{supplierCode,jdbcType=VARCHAR}),'%')
            </if>
            <if test="supplierName != null and supplierName != ''" >
                and sp.name like  CONCAT(CONCAT('%', #{supplierName,jdbcType=VARCHAR}),'%')
            </if>
            <if test="regionId != null " >
                and ts.region_id = #{regionId,jdbcType=VARCHAR}
            </if>
            <if test="month != null and month != ''" >
                and ts.month = #{month,jdbcType=VARCHAR}
            </if>
            <if test="description != null and description != ''" >
                and ts.description = #{description,jdbcType=VARCHAR}
            </if>
            <if test="status != null" >
                and ts.status = #{status,jdbcType=INTEGER}
            </if>
            <if test="createAccount != null and createAccount != ''" >
                and ts.create_account = #{createAccount,jdbcType=VARCHAR}
            </if>
            <if test="createTime != null" >
                and ts.create_time = #{createTime,jdbcType=TIMESTAMP}
            </if>
            <if test="modifyAccount != null and modifyAccount != ''" >
                and ts.modify_account = #{modifyAccount,jdbcType=VARCHAR}
            </if>
            <if test="modifyTime != null" >
                and ts.modify_time = #{modifyTime,jdbcType=TIMESTAMP}
            </if>
        </where>
    </sql>


    <select id="selectByExample" parameterType="com.boco.nbd.wios.manage.entity.bo.ScoreSupplierDTO" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" /> ,
         sp.code as supplier_code,
         sp.name as supplier_name,
         sp.contact_name,
         sp.contact_phone,
         tr.name as region_name
        from  t_score_supplier ts
        left join t_supplier sp on ts.supplier_id = sp.id
        left join t_region tr on ts.region_id = tr.id
        <include refid="findPageWithWhere" />
        order by sp.name

    </select>

    <insert id="insertMonthDetail" parameterType="java.lang.Long">
      insert into t_score_supplier_detail ( score_supplier_id, code, name,
            status, type, parent_code,
            weight,score, source, description,
            create_account, create_time, modify_account,
            modify_time)
        SELECT #{scoreSupplierId,jdbcType=INTEGER}, sc.code, sc.name,
            1, sc.type, sc.parent_code,
            sc.weight,0, sc.source, '',
                    1,NOW(), 1, NOW()
        from  t_score_config sc
        where sc.status=1
    </insert>

    <select id="countOrder"  resultType="java.lang.Integer">
        select
        count(1) as cnt
        from  t_order od
        join t_operate_log ol on od.id = ol.third_party_id
        where
        od.install_supplier_id = #{supplierId,jdbcType=INTEGER}
        and od.region_id = #{regionId,jdbcType=INTEGER}
        and ol.name = #{orderStatus,jdbcType=VARCHAR}
        and (SELECT DATE_FORMAT(ol.create_time,'%Y-%m') = #{month,jdbcType=VARCHAR})

    </select>
    <select id="countChangeSupplier"  resultType="java.lang.Integer">
        select
        count(1) as cnt
        from t_operate_log ol
        where
        ol.type =3
        and ol.pre_name = #{supplierId,jdbcType=INTEGER}
        and (SELECT DATE_FORMAT(ol.create_time,'%Y-%m') &gt;= #{threeMonthBefore,jdbcType=VARCHAR})
        and (SELECT DATE_FORMAT(ol.create_time,'%Y-%m') &lt;= #{month,jdbcType=VARCHAR})
    </select>

    <select id="calcOrderFinishedTime"  resultType="java.lang.Integer">
        select avg(timestampdiff(hour, ol.create_time, od.install_finish_time))
        from  t_order od
        join t_operate_log ol on od.id = ol.third_party_id
        where
        od.install_supplier_id = #{supplierId,jdbcType=INTEGER}
        and od.region_id = #{regionId,jdbcType=INTEGER}
        and ol.name = '7'
        and (SELECT DATE_FORMAT(ol.create_time,'%Y-%m') = #{month,jdbcType=VARCHAR})

    </select>

    <select id="calcOrderResponseTime"  resultType="java.lang.Integer">
        select avg(timestampdiff(hour, ol.create_time, od.survey_reserve_time))
        from  t_order od
        join t_operate_log ol on od.id = ol.third_party_id
        where
        od.install_supplier_id = #{supplierId,jdbcType=INTEGER}
        and od.region_id = #{regionId,jdbcType=INTEGER}
        and ol.name = '7'
        and (SELECT DATE_FORMAT(ol.create_time,'%Y-%m') = #{month,jdbcType=VARCHAR})

    </select>

    <select id="calcOrderSurveyTime"  resultType="java.lang.Integer">
        select avg(timestampdiff(hour, ol.create_time, od.survey_finish_time))
        from  t_order od
        join t_operate_log ol on od.id = ol.third_party_id
        where
        od.install_supplier_id = #{supplierId,jdbcType=INTEGER}
        and od.region_id = #{regionId,jdbcType=INTEGER}
        and ol.name = '7'
        and (SELECT DATE_FORMAT(ol.create_time,'%Y-%m') = #{month,jdbcType=VARCHAR})

    </select>
    
    <select id="selectLastestScoreInfo" resultMap="BaseResultMap">
        select * from t_score_supplier
        where supplier_id=#{supplierId} and region_id=#{regionId}
        order by create_time desc
        limit 1
    </select>
    
    <select id="calcOrderScore"  resultType="java.lang.Double">
        select avg(od.score) as avgScore
        from  t_order od
        join t_operate_log ol on od.id = ol.third_party_id
        left join t_region r on r.id = od.region_id
        where
        od.install_supplier_id = #{supplierId,jdbcType=INTEGER}
        and r.id_tree like '${regionIdTree}%'
        and ol.name = '61'
        and (SELECT DATE_FORMAT(ol.create_time,'%Y-%m') &gt;= #{threeMonthBefore,jdbcType=VARCHAR})
        and (SELECT DATE_FORMAT(ol.create_time,'%Y-%m') &lt;= #{month,jdbcType=VARCHAR})

    </select>
    <select id="countOrderByCondition" resultType="java.lang.Integer">
        select
        count(1) as cnt
        from  t_order od
        left join t_region r on r.id = od.region_id
        where  (SELECT DATE_FORMAT(od.dispatch_time,'%Y-%m') &gt;= #{threeMonthBefore,jdbcType=VARCHAR})
        and (SELECT DATE_FORMAT(od.dispatch_time,'%Y-%m') &lt;=  #{month,jdbcType=VARCHAR})
        and od.install_supplier_id = #{supplierId,jdbcType=INTEGER}
        and r.id_tree like '${regionIdTree}%'
        <if test="status != null">
            and od.status != #{status}
        </if>
    </select>
    <select id="countOrderHasFinished" resultType="java.lang.Integer">
        select
        count(1) as cnt
        from  t_order od
        left join t_region r on r.id = od.region_id
        where  (SELECT DATE_FORMAT(od.dispatch_time,'%Y-%m') &gt;= #{threeMonthBefore,jdbcType=VARCHAR})
        and (SELECT DATE_FORMAT(od.dispatch_time,'%Y-%m') &lt;=  #{month,jdbcType=VARCHAR})
        and od.install_supplier_id = #{supplierId,jdbcType=INTEGER}
        and r.id_tree like '${regionIdTree}%'
        and od.status in (46,49,52,61)
    </select>
    <select id="countOrderFinished" resultType="java.lang.Integer">
        select count(1)
        from  t_order od
        left join t_region r on r.id = od.region_id
        where od.install_finish_time is not null
          and DATE_FORMAT(od.dispatch_time,'%Y-%m') &gt;= #{threeMonthBefore}
          and DATE_FORMAT(od.dispatch_time,'%Y-%m') &lt;=  #{month,jdbcType=VARCHAR}
          and od.install_supplier_id = #{supplierId,jdbcType=INTEGER}
          and r.id_tree like '${regionIdTree}%'
    </select>
    <select id="countOrderFinishedTime" resultType="java.lang.Integer">
    select sum(timestampdiff(second , od.dispatch_time, od.install_finish_time)/3600)
        from  t_order od
        left join t_region r on r.id = od.region_id
        where od.install_finish_time is not null
        and DATE_FORMAT(od.dispatch_time,'%Y-%m') &gt;= #{threeMonthBefore}
        and DATE_FORMAT(od.dispatch_time,'%Y-%m') &lt;=  #{month,jdbcType=VARCHAR}
        and od.install_supplier_id = #{supplierId,jdbcType=INTEGER}
        and r.id_tree like '${regionIdTree}%'
    </select>
    <select id="countOrderResponse" resultType="java.lang.Integer">
        select count(1)
        from  t_order od
        join t_operate_log ol on od.id = ol.third_party_id
        left join t_region r on r.id = od.region_id
        where ol.name = '13'
          and (SELECT DATE_FORMAT(od.dispatch_time,'%Y-%m') &gt;= #{threeMonthBefore,jdbcType=VARCHAR})
          and (SELECT DATE_FORMAT(od.dispatch_time,'%Y-%m') &lt;=  #{month,jdbcType=VARCHAR})
          and od.install_supplier_id = #{supplierId,jdbcType=INTEGER}
          and r.id_tree like '${regionIdTree}%'
    </select>
    <select id="countOrderResponseTime" resultType="java.lang.Integer">
        select sum(timestampdiff(second, od.dispatch_time, ol.create_time)/3600)
        from  t_order od
        join t_operate_log ol on od.id = ol.third_party_id
        left join t_region r on r.id = od.region_id
        where ol.name = '13'
          and (SELECT DATE_FORMAT(od.dispatch_time,'%Y-%m') &gt;= #{threeMonthBefore,jdbcType=VARCHAR})
          and (SELECT DATE_FORMAT(od.dispatch_time,'%Y-%m') &lt;=  #{month,jdbcType=VARCHAR})
          and od.install_supplier_id = #{supplierId,jdbcType=INTEGER}
          and r.id_tree like '${regionIdTree}%'
    </select>
    <select id="countOrderSurvey" resultType="java.lang.Integer">
        select count(1)
        from  t_order od
        left join t_region r on r.id = od.region_id
        where od.survey_finish_time is not null
          and (SELECT DATE_FORMAT(od.dispatch_time,'%Y-%m') &gt;= #{threeMonthBefore,jdbcType=VARCHAR})
          and (SELECT DATE_FORMAT(od.dispatch_time,'%Y-%m') &lt;= #{month,jdbcType=VARCHAR})
          and od.install_supplier_id = #{supplierId,jdbcType=INTEGER}
          and r.id_tree like '${regionIdTree}%'
    </select>
    <select id="countOrderSurveyTime" resultType="java.lang.Integer">
        select sum(timestampdiff(second, od.dispatch_time, od.survey_finish_time)/3600)
        from  t_order od
        left join t_region r on r.id = od.region_id
        where od.survey_finish_time is not null
          and (SELECT DATE_FORMAT(od.dispatch_time,'%Y-%m') &gt;= #{threeMonthBefore,jdbcType=VARCHAR})
          and (SELECT DATE_FORMAT(od.dispatch_time,'%Y-%m') &lt;= #{month,jdbcType=VARCHAR})
          and od.install_supplier_id = #{supplierId,jdbcType=INTEGER}
          and r.id_tree like '${regionIdTree}%'
    </select>
</mapper>