<?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.ssm.manage.mapper.BaseProjectMapper">
  <resultMap id="BaseResultMap" type="com.ssm.manage.entity.BaseProject">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="project_num" jdbcType="VARCHAR" property="projectNum" />
    <result column="project_name" jdbcType="VARCHAR" property="projectName" />
    <result column="state" jdbcType="INTEGER" property="state" />
    <result column="label" jdbcType="VARCHAR" property="label" />
    <result column="gold_interval" jdbcType="INTEGER" property="goldInterval" />
    <result column="long_operation_time" jdbcType="INTEGER" property="longOperationTime" />
    <result column="moderate_operation_time" jdbcType="INTEGER" property="moderateOperationTime" />
    <result column="short_operation_time" jdbcType="INTEGER" property="shortOperationTime" />
    <result column="founder_staff" jdbcType="VARCHAR" property="founderStaff" />
    <result column="Founder_datetime" jdbcType="TIMESTAMP" property="founderDatetime" />
    <result column="modify_by" jdbcType="VARCHAR" property="modifyBy" />
    <result column="modify_datetime" jdbcType="TIMESTAMP" property="modifyDatetime" />
    <result column="minumum_times" jdbcType="INTEGER" property="minumumTimes" />
    <result column="direct_consumption" jdbcType="INTEGER" property="directConsumption" />
    <result column="price" jdbcType="DECIMAL" property="price" />
    <result column="minumum_price" jdbcType="DECIMAL" property="minumumPrice" />
    <result column="price_member" jdbcType="DECIMAL" property="priceMember" />
    <result column="minumum_price_member" jdbcType="DECIMAL" property="minumumPriceMember" />
    <result column="sales_achievement" jdbcType="DECIMAL" property="salesAchievement" />
    <result column="sales_commission" jdbcType="DECIMAL" property="salesCommission" />
    <result column="card_sales_achievement" jdbcType="DECIMAL" property="cardSalesAchievement" />
    <result column="card_sales_commission" jdbcType="DECIMAL" property="cardSalesCommission" />
    <result column="achievement_large" jdbcType="DECIMAL" property="achievementLarge" />
    <result column="achievement_zhonggong" jdbcType="DECIMAL" property="achievementZhonggong" />
    <result column="achievement_technician" jdbcType="DECIMAL" property="achievementTechnician" />
    <result column="achievement_small" jdbcType="DECIMAL" property="achievementSmall" />
    <result column="commission_large" jdbcType="DECIMAL" property="commissionLarge" />
    <result column="commission_zhonggong" jdbcType="DECIMAL" property="commissionZhonggong" />
    <result column="commission_technician" jdbcType="DECIMAL" property="commissionTechnician" />
    <result column="commission_small" jdbcType="DECIMAL" property="commissionSmall" />
    <result column="card_achievement_large" jdbcType="DECIMAL" property="cardAchievementLarge" />
    <result column="card_achievement_zhonggong" jdbcType="DECIMAL" property="cardAchievementZhonggong" />
    <result column="card_achievement_technician" jdbcType="DECIMAL" property="cardAchievementTechnician" />
    <result column="card_achievement_small" jdbcType="DECIMAL" property="cardAchievementSmall" />
    <result column="card_commission_large" jdbcType="DECIMAL" property="cardCommissionLarge" />
    <result column="card_commission_zhonggong" jdbcType="DECIMAL" property="cardCommissionZhonggong" />
    <result column="card_commission_technician" jdbcType="DECIMAL" property="cardCommissionTechnician" />
    <result column="card_commission_small" jdbcType="DECIMAL" property="cardCommissionSmall" />
    <result column="project_type_id" jdbcType="BIGINT" property="projectTypeId" />
    <result column="project_type_name" jdbcType="VARCHAR" property="projectTypeName" />
    <result column="store_id" jdbcType="BIGINT" property="storeId" />
    <result column="store_name" jdbcType="VARCHAR" property="storeName" />
    <result column="valid_time" jdbcType="INTEGER" property="validTime" />
    <result column="brokerage" jdbcType="DECIMAL" property="brokerage" />
    <result column="is_cooperation" jdbcType="INTEGER" property="isCooperation" />
    <result column="no_discount" jdbcType="INTEGER" property="noDiscount" />
    <result column="new_id" jdbcType="BIGINT" property="newId" />
    <result column="project_type_num" jdbcType="VARCHAR" property="projectTypeNum" />
    <result column="top_price" jdbcType="DECIMAL" property="topPrice" />
    <result column="top_price_member" jdbcType="DECIMAL" property="topPriceMember" />
    <result column="sales_deadline" jdbcType="TIMESTAMP" property="salesDeadline" />
    <result column="unified_due" jdbcType="TIMESTAMP" property="unifiedDue" />
    <result column="servicing_time" jdbcType="VARCHAR" property="servicingTime" />
    <result column="overtime" jdbcType="VARCHAR" property="overtime" />
    <result column="overtime_money" jdbcType="DECIMAL" property="overtimeMoney" />
    <result property="marketComm" column="market_comm" jdbcType="NUMERIC"/>
    <result property="clockComm" column="clock_comm" jdbcType="NUMERIC"/>
    <result property="roundClockComm" column="round_clock_comm" jdbcType="NUMERIC"/>
    <result property="cardMarketComm" column="card_market_comm" jdbcType="NUMERIC"/>
    <result property="cardClockComm" column="card_clock_comm" jdbcType="NUMERIC"/>
    <result property="cardRoundClockComm" column="card_round_clock_comm" jdbcType="NUMERIC"/>
    <result property="projectNature" column="project_nature" jdbcType="INTEGER"/>

    <result property="commodityCash" column="commodity_cash" jdbcType="NUMERIC"/>
    <result property="commodityCard" column="commodity_card" jdbcType="NUMERIC"/>
    <result property="deptName" column="dept_name" jdbcType="INTEGER"/>
    <result property="isCardPay" column="is_card_pay" jdbcType="INTEGER"/>
    <result property="smokeCommissionWay" column="smoke_commission_way" jdbcType="INTEGER"/>
    <result property="cooperateId" column="cooperate_id" jdbcType="BIGINT"/>
  </resultMap>

  <select id="selectPageNew" resultMap="BaseResultMap" parameterType="com.ssm.manage.entity.BaseProject">
    select * from base_project
    <where>(
      1=1
      <if test="projectNum !=null and projectNum.trim().length() >0 ">
        and   project_num like #{projectNum}
      </if>

      <if test="projectName !=null and  projectName.trim().length() >0 ">
        and project_name like #{projectName}
      </if>
      <if test="projectTypeName !=null">
        and project_type_name like #{projectTypeName}
      </if>

      <if test="state !=null">
        and state
        = #{state}
      </if>

      <if test="projectTypeId !=null">
        and project_typeId = #{projectTypeId}
      </if>

      <if test="storeId !=null">
        and store_id = #{storeId}
      </if>

      <if test="directConsumption !=null">
        and direct_consumption = #{directConsumption}
      </if>

      <if test="type !=null">
        and founderDatetime between #{newdate1} and #{newdate2}
      </if>

      <if test="

      isCooperation !=null">
        and  is_cooperation = #{isCooperation}
      </if>

      <if test="projectNature !=null">
        and  project_nature = #{projectNature}
      </if>
      )
      <if test="prices !=null">
        and  (price =  #{prices} or price_member = #{prices} or price*minumum_times = #{prices})
      </if>

--       and  (unified_due is null
--       or (unified_due is null and sales_deadline is null )
--       or (unified_due is not null and unified_due>NOW() and sales_deadline is null)
--       or(unified_due  is not null and unified_due>NOW() and sales_deadline  is not null and sales_deadline > NOW() )
--       )

      order by project_num ASC


      <!-- <if test="projectName == null  or  projectName.trim().length() == 0 ">-->
      LIMIT #{page},#{pageSize}
      <!--</if>-->



    </where>
  </select>


  <!--  LIMIT #{pageNum},#{pageSize}-->
  <select id="selelctCount"  resultType ="java.lang.Integer">
    select count(0) from base_project
    <where>(
      1=1
      <if test="projectNum !=null and projectNum.trim().length() >0">
        and   project_num like #{projectNum}
      </if>

      <if test="projectName !=null and  projectName.trim().length() >0">
        and project_name like #{projectName}
      </if>

      <if test="projectTypeName !=null">
        and project_type_name like #{projectTypeName}
      </if>

      <if test="state !=null">
        and state
        = #{state}
      </if>

      <if test="projectTypeId !=null">
        and project_typeId = #{projectTypeId}
      </if>

      <if test="storeId !=null">
        and store_id = #{storeId}
      </if>

      <if test="directConsumption !=null">
        and direct_consumption = #{directConsumption}
      </if>

      <if test="type !=null">
        and founderDatetime between #{newdate1} and #{newdate2}
      </if>

      <if test="isCooperation !=null">
        and  is_cooperation = #{isCooperation}
      </if>

      <if test="projectNature !=null">
        and  project_nature = #{projectNature}
      </if>
      )
      <if test="prices !=null">
        and  (price =  #{prices} or price_member = #{prices})
      </if>

      and  (unified_due is null
      or (unified_due is null and sales_deadline is null )
      or (unified_due is not null and unified_due>NOW() and sales_deadline is null)
      or(unified_due  is not null and unified_due>NOW() and sales_deadline  is not null and sales_deadline > NOW() )
      )
    </where>
  </select>

  <sql id="baseColumnList">
    project_num,
    project_name,
    state,
    label,
    gold_interval,
    long_operation_time,
    moderate_operation_time,
    short_operation_time,
    founder_staff,
    founder_datetime,
    modify_by,
    modify_datetime,
    minumum_times,
    direct_consumption,
    price,
    minumum_price,
    price_member,
    minumum_price_member,
    sales_achievement,
    sales_commission,
    card_sales_achievement,
    card_sales_commission,
    achievement_large,
    achievement_zhonggong,
    achievement_technician,
    achievement_small,
    commission_large,
    commission_zhonggong,
    commission_technician,
    commission_small,
    card_achievement_large,
    card_achievement_zhonggong,
    card_achievement_technician,
    card_achievement_small,
    card_commission_large,
    card_commission_zhonggong,
    card_commission_technician,
    card_commission_small,
    project_type_id,
    project_type_name,
    store_id,
    store_name,
    id,
    valid_time,
    brokerage,
    is_cooperation,
    no_discount,
    new_id,
    project_type_num,
    top_price,
    top_price_member,
    sales_deadline,
    unified_due,
    servicing_time,
    overtime,
    overtime_money,
    market_comm,
    clock_comm,
    round_clock_comm,card_market_comm,card_clock_comm,card_round_clock_comm,project_nature,
    commodity_cash,commodity_card,dept_name,is_card_pay,smoke_commission_way,cooperate_id
  </sql>
  <insert id="insertBatch" parameterType="java.util.ArrayList">
    insert into base_project (
    project_num,
    project_name,
    state,
    label,
    gold_interval,
    long_operation_time,
    moderate_operation_time,
    short_operation_time,
    founder_staff,
    founder_datetime,
    modify_by,
    modify_datetime,
    minumum_times,
    direct_consumption,
    price,
    minumum_price,
    price_member,
    minumum_price_member,
    sales_achievement,
    sales_commission,
    card_sales_achievement,
    card_sales_commission,
    achievement_large,
    achievement_zhonggong,
    achievement_technician,
    achievement_small,
    commission_large,
    commission_zhonggong,
    commission_technician,
    commission_small,
    card_achievement_large,
    card_achievement_zhonggong,
    card_achievement_technician,
    card_achievement_small,
    card_commission_large,
    card_commission_zhonggong,
    card_commission_technician,
    card_commission_small,
    project_type_id,
    project_type_name,
    store_id,
    store_name,
    id
    ,valid_time
    ,brokerage
    ,is_cooperation
    ,no_discount
    ,project_type_num
    ,top_price
    ,top_price_member
    ,sales_deadline
    ,unified_due
    ,servicing_time
    ,overtime
    ,overtime_money
    ,market_comm
    ,clock_comm
    ,round_clock_comm
    ,card_market_comm
    ,card_clock_comm
    ,card_round_clock_comm
    ,project_nature
    ,commodity_cash
    ,commodity_card
    ,dept_name
    ,is_card_pay
    ,smoke_commission_way,cooperate_id
    ) values
    <foreach collection="list" index="index" item="item" separator=",">
      (
      #{item.projectNum,jdbcType=VARCHAR},
      #{item.projectName,jdbcType=VARCHAR},
      #{item.state,jdbcType=INTEGER},
      #{item.label,jdbcType=VARCHAR},
      #{item.goldInterval,jdbcType=INTEGER},
      #{item.longOperationTime,jdbcType=INTEGER},
      #{item.moderateOperationTime,jdbcType=INTEGER},
      #{item.shortOperationTime,jdbcType=INTEGER},
      #{item.founderStaff,jdbcType=VARCHAR},
      #{item.founderDatetime,jdbcType=TIMESTAMP},
      #{item.modifyBy,jdbcType=VARCHAR},
      #{item.modifyDatetime,jdbcType=TIMESTAMP},
      #{item.minumumTimes,jdbcType=INTEGER},
      #{item.directConsumption,jdbcType=INTEGER},
      #{item.price,jdbcType=DECIMAL},
      #{item.minumumPrice,jdbcType=DECIMAL},
      #{item.priceMember,jdbcType=DECIMAL},
      #{item.minumumPriceMember,jdbcType=DECIMAL},
      #{item.salesAchievement,jdbcType=DECIMAL},
      #{item.salesCommission,jdbcType=DECIMAL},
      #{item.cardSalesAchievement,jdbcType=DECIMAL},
      #{item.cardSalesCommission,jdbcType=DECIMAL},
      #{item.achievementLarge,jdbcType=DECIMAL},
      #{item.achievementZhonggong,jdbcType=DECIMAL},
      #{item.achievementTechnician,jdbcType=DECIMAL},
      #{item.achievementSmall,jdbcType=DECIMAL},
      #{item.commissionLarge,jdbcType=DECIMAL},
      #{item.commissionZhonggong,jdbcType=DECIMAL},
      #{item.commissionTechnician,jdbcType=DECIMAL},
      #{item.commissionSmall,jdbcType=DECIMAL},
      #{item.cardAchievementLarge,jdbcType=DECIMAL},
      #{item.cardAchievementZhonggong,jdbcType=DECIMAL},
      #{item.cardAchievementTechnician,jdbcType=DECIMAL},
      #{item.cardAchievementSmall,jdbcType=DECIMAL},
      #{item.cardCommissionLarge,jdbcType=DECIMAL},
      #{item.cardCommissionZhonggong,jdbcType=DECIMAL},
      #{item.cardCommissionTechnician,jdbcType=DECIMAL},
      #{item.cardCommissionSmall,jdbcType=DECIMAL},
      #{item.projectTypeId,jdbcType=BIGINT},
      #{item.projectTypeName,jdbcType=VARCHAR},
      #{item.storeId,jdbcType=BIGINT},
      #{item.storeName,jdbcType=VARCHAR},
      #{item.id,jdbcType=BIGINT}
      ,#{item.validTime,jdbcType=INTEGER}
      ,#{item.brokerage,jdbcType=DECIMAL}
      , #{item.isCooperation,jdbcType=INTEGER}
      ,#{item.noDiscount,jdbcType=INTEGER}
      , #{item.projectTypeNum,jdbcType=VARCHAR}
      ,#{item.topPrice,jdbcType=DECIMAL}
      ,#{item.topPriceMember,jdbcType=DECIMAL}
      ,#{item.salesDeadline,jdbcType=TIMESTAMP}
      ,#{item.unifiedDue,jdbcType=TIMESTAMP}
      ,#{item.servicingTime,jdbcType=VARCHAR}
      ,#{item.overtime,jdbcType=VARCHAR}
      ,#{item.overtimeMoney,jdbcType=DECIMAL}
      ,#{item.marketComm,jdbcType=NUMERIC}
      ,#{item.clockComm,jdbcType=NUMERIC}
      ,#{item.roundClockComm,jdbcType=NUMERIC}
      ,#{item.cardMarketComm,jdbcType=NUMERIC}
      ,#{item.cardClockComm,jdbcType=NUMERIC}
      ,#{item.cardRoundClockComm,jdbcType=NUMERIC}
      ,#{item.projectNature,jdbcType=INTEGER}
      ,#{item.commodityCash,jdbcType=NUMERIC}
      ,#{item.commodityCard,jdbcType=NUMERIC}
      ,#{item.deptName,jdbcType=INTEGER}
      ,#{item.isCardPay,jdbcType=INTEGER}
      ,#{item.smokeCommissionWay,jdbcType=INTEGER}
      ,#{item.cooperateId,jdbcType=BIGINT}
      )
    </foreach>
  </insert>




  <select id="selectUpdate" resultMap="BaseResultMap" parameterType="java.util.ArrayList">
   select * from base_project where
    <foreach close=")" collection="list" index="index" item="item" open="(" separator="or">
      (store_id = #{item.storeId} and project_num = #{item.projectNum})
      </foreach>
  </select>
    <select id="selectList" resultMap="BaseResultMap" parameterType="collection">
      select * from base_project where id in
                                              <foreach collection="collect" open="(" close=")" separator="," item="item">
                                                #{item}
                                              </foreach>
    </select>
    <select id="selectRepeatPage" resultMap="BaseResultMap" parameterType="com.ssm.manage.query.ProjectRequest">
      SELECT * from base_project b1 where store_id = #{storeId} and project_name in (
        SELECT p.project_name
        FROM base_project p
        where p.store_id = #{storeId}
        GROUP BY p.project_name
        HAVING COUNT(p.project_name) > 1 )
        order by b1.project_name
    </select>


</mapper>