<?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="org.linlinjava.jz.db.dao.CustomerOrderMapper">
    <resultMap id="BaseResultMap" type="org.linlinjava.jz.db.domain.CustomerOrder">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
        <result column="clue_no" jdbcType="VARCHAR" property="clueNo"/>
        <result column="customer_name" jdbcType="VARCHAR" property="customerName"/>
        <result column="customer_tel" jdbcType="VARCHAR" property="customerTel"/>
        <result column="customer_addr" jdbcType="VARCHAR" property="customerAddr"/>
        <result column="state" jdbcType="INTEGER" property="state"/>
        <result column="payable_amount" jdbcType="DOUBLE" property="payableAmount"/>
        <result column="actual_pay_amount" jdbcType="DOUBLE" property="actualPayAmount"/>
        <result column="sign_time" jdbcType="TIMESTAMP" property="signTime"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="create_user_id" jdbcType="BIGINT" property="createUserId"/>
        <result column="create_user" jdbcType="VARCHAR" property="createUser"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_user_id" jdbcType="BIGINT" property="updateUserId"/>
        <result column="update_user" jdbcType="VARCHAR" property="updateUser"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>
    <sql id="Example_Where_Clause">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        <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 - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        <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 - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        id, order_no, clue_no, customer_name, customer_tel, customer_addr, `state`, payable_amount,
        actual_pay_amount, sign_time, remark, create_user_id, create_user, create_time, update_user_id,
        update_user, update_time
    </sql>
    <select id="selectByExample" parameterType="org.linlinjava.jz.db.domain.CustomerOrderExample"
            resultMap="BaseResultMap">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        select
        <if test="distinct">
            distinct
        </if>
        <include refid="Base_Column_List"/>
        from customer_order
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
    </select>
    <select id="selectByExampleSelective" parameterType="map" resultMap="BaseResultMap">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        select
        <if test="example != null and example.distinct">
            distinct
        </if>
        <choose>
            <when test="selective != null and selective.length > 0">
                <foreach collection="selective" item="column" separator=",">
                    ${column.aliasedEscapedColumnName}
                </foreach>
            </when>
            <otherwise>
                <include refid="Base_Column_List"/>
            </otherwise>
        </choose>
        from customer_order
        <if test="example != null">
            <include refid="Update_By_Example_Where_Clause"/>
        </if>
        <if test="example != null and example.orderByClause != null">
            order by ${example.orderByClause}
        </if>
    </select>
    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        select
        <include refid="Base_Column_List"/>
        from customer_order
        where id = #{id,jdbcType=BIGINT}
    </select>
    <select id="selectByPrimaryKeySelective" parameterType="map" resultMap="BaseResultMap">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        select
        <choose>
            <when test="selective != null and selective.length > 0">
                <foreach collection="selective" item="column" separator=",">
                    ${column.aliasedEscapedColumnName}
                </foreach>
            </when>
            <otherwise>
                <include refid="Base_Column_List"/>
            </otherwise>
        </choose>
        from customer_order
        where id = #{id,jdbcType=BIGINT}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        delete from customer_order
        where id = #{id,jdbcType=BIGINT}
    </delete>
    <delete id="deleteByExample" parameterType="org.linlinjava.jz.db.domain.CustomerOrderExample">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        delete from customer_order
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
    </delete>
    <insert id="insert" parameterType="org.linlinjava.jz.db.domain.CustomerOrder">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
            SELECT LAST_INSERT_ID()
        </selectKey>
        insert into customer_order (order_no, clue_no, customer_name,
        customer_tel, customer_addr, `state`,
        payable_amount, actual_pay_amount, sign_time,
        remark, create_user_id, create_user,
        create_time, update_user_id, update_user,
        update_time)
        values (#{orderNo,jdbcType=VARCHAR}, #{clueNo,jdbcType=VARCHAR}, #{customerName,jdbcType=VARCHAR},
        #{customerTel,jdbcType=VARCHAR}, #{customerAddr,jdbcType=VARCHAR}, #{state,jdbcType=INTEGER},
        #{payableAmount,jdbcType=DOUBLE}, #{actualPayAmount,jdbcType=DOUBLE}, #{signTime,jdbcType=TIMESTAMP},
        #{remark,jdbcType=VARCHAR}, #{createUserId,jdbcType=BIGINT}, #{createUser,jdbcType=VARCHAR},
        #{createTime,jdbcType=TIMESTAMP}, #{updateUserId,jdbcType=BIGINT}, #{updateUser,jdbcType=VARCHAR},
        #{updateTime,jdbcType=TIMESTAMP})
    </insert>
    <insert id="insertSelective" parameterType="org.linlinjava.jz.db.domain.CustomerOrder">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
            SELECT LAST_INSERT_ID()
        </selectKey>
        insert into customer_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderNo != null">
                order_no,
            </if>
            <if test="clueNo != null">
                clue_no,
            </if>
            <if test="customerName != null">
                customer_name,
            </if>
            <if test="customerTel != null">
                customer_tel,
            </if>
            <if test="customerAddr != null">
                customer_addr,
            </if>
            <if test="state != null">
                `state`,
            </if>
            <if test="payableAmount != null">
                payable_amount,
            </if>
            <if test="actualPayAmount != null">
                actual_pay_amount,
            </if>
            <if test="signTime != null">
                sign_time,
            </if>
            <if test="remark != null">
                remark,
            </if>
            <if test="createUserId != null">
                create_user_id,
            </if>
            <if test="createUser != null">
                create_user,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateUserId != null">
                update_user_id,
            </if>
            <if test="updateUser != null">
                update_user,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderNo != null">
                #{orderNo,jdbcType=VARCHAR},
            </if>
            <if test="clueNo != null">
                #{clueNo,jdbcType=VARCHAR},
            </if>
            <if test="customerName != null">
                #{customerName,jdbcType=VARCHAR},
            </if>
            <if test="customerTel != null">
                #{customerTel,jdbcType=VARCHAR},
            </if>
            <if test="customerAddr != null">
                #{customerAddr,jdbcType=VARCHAR},
            </if>
            <if test="state != null">
                #{state,jdbcType=INTEGER},
            </if>
            <if test="payableAmount != null">
                #{payableAmount,jdbcType=DOUBLE},
            </if>
            <if test="actualPayAmount != null">
                #{actualPayAmount,jdbcType=DOUBLE},
            </if>
            <if test="signTime != null">
                #{signTime,jdbcType=TIMESTAMP},
            </if>
            <if test="remark != null">
                #{remark,jdbcType=VARCHAR},
            </if>
            <if test="createUserId != null">
                #{createUserId,jdbcType=BIGINT},
            </if>
            <if test="createUser != null">
                #{createUser,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateUserId != null">
                #{updateUserId,jdbcType=BIGINT},
            </if>
            <if test="updateUser != null">
                #{updateUser,jdbcType=VARCHAR},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
        </trim>
    </insert>
    <select id="countByExample" parameterType="org.linlinjava.jz.db.domain.CustomerOrderExample"
            resultType="java.lang.Long">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        select count(*) from customer_order
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
    </select>
    <update id="updateByExampleSelective" parameterType="map">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        update customer_order
        <set>
            <if test="record.id != null">
                id = #{record.id,jdbcType=BIGINT},
            </if>
            <if test="record.orderNo != null">
                order_no = #{record.orderNo,jdbcType=VARCHAR},
            </if>
            <if test="record.clueNo != null">
                clue_no = #{record.clueNo,jdbcType=VARCHAR},
            </if>
            <if test="record.customerName != null">
                customer_name = #{record.customerName,jdbcType=VARCHAR},
            </if>
            <if test="record.customerTel != null">
                customer_tel = #{record.customerTel,jdbcType=VARCHAR},
            </if>
            <if test="record.customerAddr != null">
                customer_addr = #{record.customerAddr,jdbcType=VARCHAR},
            </if>
            <if test="record.state != null">
                `state` = #{record.state,jdbcType=INTEGER},
            </if>
            <if test="record.payableAmount != null">
                payable_amount = #{record.payableAmount,jdbcType=DOUBLE},
            </if>
            <if test="record.actualPayAmount != null">
                actual_pay_amount = #{record.actualPayAmount,jdbcType=DOUBLE},
            </if>
            <if test="record.signTime != null">
                sign_time = #{record.signTime,jdbcType=TIMESTAMP},
            </if>
            <if test="record.remark != null">
                remark = #{record.remark,jdbcType=VARCHAR},
            </if>
            <if test="record.createUserId != null">
                create_user_id = #{record.createUserId,jdbcType=BIGINT},
            </if>
            <if test="record.createUser != null">
                create_user = #{record.createUser,jdbcType=VARCHAR},
            </if>
            <if test="record.createTime != null">
                create_time = #{record.createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="record.updateUserId != null">
                update_user_id = #{record.updateUserId,jdbcType=BIGINT},
            </if>
            <if test="record.updateUser != null">
                update_user = #{record.updateUser,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 - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        update customer_order
        set id = #{record.id,jdbcType=BIGINT},
        order_no = #{record.orderNo,jdbcType=VARCHAR},
        clue_no = #{record.clueNo,jdbcType=VARCHAR},
        customer_name = #{record.customerName,jdbcType=VARCHAR},
        customer_tel = #{record.customerTel,jdbcType=VARCHAR},
        customer_addr = #{record.customerAddr,jdbcType=VARCHAR},
        `state` = #{record.state,jdbcType=INTEGER},
        payable_amount = #{record.payableAmount,jdbcType=DOUBLE},
        actual_pay_amount = #{record.actualPayAmount,jdbcType=DOUBLE},
        sign_time = #{record.signTime,jdbcType=TIMESTAMP},
        remark = #{record.remark,jdbcType=VARCHAR},
        create_user_id = #{record.createUserId,jdbcType=BIGINT},
        create_user = #{record.createUser,jdbcType=VARCHAR},
        create_time = #{record.createTime,jdbcType=TIMESTAMP},
        update_user_id = #{record.updateUserId,jdbcType=BIGINT},
        update_user = #{record.updateUser,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="org.linlinjava.jz.db.domain.CustomerOrder">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        update customer_order
        <set>
            <if test="orderNo != null">
                order_no = #{orderNo,jdbcType=VARCHAR},
            </if>
            <if test="clueNo != null">
                clue_no = #{clueNo,jdbcType=VARCHAR},
            </if>
            <if test="customerName != null">
                customer_name = #{customerName,jdbcType=VARCHAR},
            </if>
            <if test="customerTel != null">
                customer_tel = #{customerTel,jdbcType=VARCHAR},
            </if>
            <if test="customerAddr != null">
                customer_addr = #{customerAddr,jdbcType=VARCHAR},
            </if>
            <if test="state != null">
                `state` = #{state,jdbcType=INTEGER},
            </if>
            <if test="payableAmount != null">
                payable_amount = #{payableAmount,jdbcType=DOUBLE},
            </if>
            <if test="actualPayAmount != null">
                actual_pay_amount = #{actualPayAmount,jdbcType=DOUBLE},
            </if>
            <if test="signTime != null">
                sign_time = #{signTime,jdbcType=TIMESTAMP},
            </if>
            <if test="remark != null">
                remark = #{remark,jdbcType=VARCHAR},
            </if>
            <if test="createUserId != null">
                create_user_id = #{createUserId,jdbcType=BIGINT},
            </if>
            <if test="createUser != null">
                create_user = #{createUser,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateUserId != null">
                update_user_id = #{updateUserId,jdbcType=BIGINT},
            </if>
            <if test="updateUser != null">
                update_user = #{updateUser,jdbcType=VARCHAR},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>
    <update id="updateByPrimaryKey" parameterType="org.linlinjava.jz.db.domain.CustomerOrder">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        update customer_order
        set order_no = #{orderNo,jdbcType=VARCHAR},
        clue_no = #{clueNo,jdbcType=VARCHAR},
        customer_name = #{customerName,jdbcType=VARCHAR},
        customer_tel = #{customerTel,jdbcType=VARCHAR},
        customer_addr = #{customerAddr,jdbcType=VARCHAR},
        `state` = #{state,jdbcType=INTEGER},
        payable_amount = #{payableAmount,jdbcType=DOUBLE},
        actual_pay_amount = #{actualPayAmount,jdbcType=DOUBLE},
        sign_time = #{signTime,jdbcType=TIMESTAMP},
        remark = #{remark,jdbcType=VARCHAR},
        create_user_id = #{createUserId,jdbcType=BIGINT},
        create_user = #{createUser,jdbcType=VARCHAR},
        create_time = #{createTime,jdbcType=TIMESTAMP},
        update_user_id = #{updateUserId,jdbcType=BIGINT},
        update_user = #{updateUser,jdbcType=VARCHAR},
        update_time = #{updateTime,jdbcType=TIMESTAMP}
        where id = #{id,jdbcType=BIGINT}
    </update>
    <select id="selectOneByExample" parameterType="org.linlinjava.jz.db.domain.CustomerOrderExample"
            resultMap="BaseResultMap">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        select
        <include refid="Base_Column_List"/>
        from customer_order
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
        limit 1
    </select>
    <select id="selectOneByExampleSelective" parameterType="map" resultMap="BaseResultMap">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        select
        <choose>
            <when test="selective != null and selective.length > 0">
                <foreach collection="selective" item="column" separator=",">
                    ${column.aliasedEscapedColumnName}
                </foreach>
            </when>
            <otherwise>
                <include refid="Base_Column_List"/>
            </otherwise>
        </choose>
        from customer_order
        <if test="example != null">
            <include refid="Update_By_Example_Where_Clause"/>
        </if>
        <if test="example != null and example.orderByClause != null">
            order by ${example.orderByClause}
        </if>
        limit 1
    </select>
    <select id="selectOrderList" resultType="java.util.Map">
        SELECT
				s.id,
				cli.channel_id, 
				s.order_no,
				( SELECT user_name FROM `sys_user` WHERE id = ( SELECT `charge_id` FROM `customer_order_charge` WHERE order_no = s.order_no AND state = 0 ) ) AS charge_name,
				s.`customer_name`,
				s.`customer_tel`,
				s.`state`,
				( SELECT city_name FROM `sys_city` WHERE id = c.`service_city` ) AS city_name,
				c.`service_model`,
				s.create_time,
				( SELECT contract_state FROM `contract_master` WHERE master_order_no = s.order_no ) AS contract_state 
			FROM
				`customer_order` s
				LEFT JOIN customer_clue c ON s.`clue_no` = c.`clue_no`
				LEFT JOIN customer_client cli ON cli.id = c.customer_client_id
			  	left join customer_order_charge coc on coc.order_no=s.order_no and coc.state=0 
			WHERE
			s.state != 0
			and( 
			( coc.charge_id=#{userId}
				<if test="customerName != null and customerName !=''">
		            and s.`customer_name` like  concat ('%',#{customerName},'%')
		        </if>
		        <if test="customerTel != null and customerTel !=''">
		            and s.`customer_tel` like concat ('%',#{customerTel},'%')
		        </if>
			)
		 or s.id   in( select  order_id From jz_ticket where 'handler' =#{userId} and ticket_type=2 )
        )
        order by s.id desc
    </select>
</mapper>