<?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.dayou.mapper.BusinessOpportunityMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.BusinessOpportunity">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="customer_id" property="customerId" />
        <result column="opportunity_name" property="opportunityName" />
        <result column="estimate_amount" property="estimateAmount" />
        <result column="estimate_date" property="estimateDate" />
        <result column="remark" property="remark" />
        <result column="state" property="state" />
        <result column="off_reason" property="offReason" />
        <result column="user_id" property="userId" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        business_opportunity.id,
	business_opportunity.deleted,
	business_opportunity.created,
	business_opportunity.modified,
	business_opportunity.customer_id,
	business_opportunity.opportunity_name,
	business_opportunity.estimate_amount,
	business_opportunity.estimate_date,
	business_opportunity.remark,
	business_opportunity.state,
	business_opportunity.off_reason,
    business_opportunity.user_id
    </sql>

    <select id="getPage" parameterType="com.dayou.vo.BusinessOpportunityVO" resultType="com.dayou.vo.BusinessOpportunityVO">
        select <include refid="Base_Column_List"/> ,c.name as customerName,u.name as userName
        from business_opportunity left join customer c on business_opportunity.customer_id = c.id left join user u on u.id = business_opportunity.user_id
        where c.deleted =0 and business_opportunity.deleted = 0
        <if test="keyword!=null and keyword!='' ">
            and(
            c.name like concat ('%',#{keyword},'%')
            or u.name like concat('%',#{keyword},'%')
            )
        </if>
        <if test="dto!=null and dto.customerName!=null and dto.customerName!=''">
            and c.name like concat('%',#{dto.customerName},'%')
        </if>
        <if test="dto!=null and dto.opportunityName!=null and dto.opportunityName!=''">
            and business_opportunity.opportunity_name like concat('%',#{dto.opportunityName},'%')
        </if>
        <if test="dto!=null and dto.state!=null and dto.state!=''">
            and business_opportunity.state = #{dto.state}
        </if>
        <if test="dto!=null and dto.userIds!=null and dto.userIds.size!=0">
            and business_opportunity.user_id in
            <foreach collection="dto.userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
        order by business_opportunity.state desc, business_opportunity.estimate_date DESC
    </select>
</mapper>
