<?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.jxy.windminer.mapper.TicketMapper">

    <select id="getPrivateTicketList" resultType="com.jxy.windminer.dto.TicketListDto">
        select
            id,model,create_time `date`,serial_no serialNo,user_name userName,email,status
        from
            ticket
        <where>
            <if test="userName != null and userName != ''">
               user_name = #{userName}
            </if>
        </where>
    </select>

    <select id="getPrivateTicketListByStatus" resultType="com.jxy.windminer.dto.TicketListDto">
        select
            id,
            model,
            create_time `date`,
            serial_no serialNo,
            user_name userName,
            email,
            status
        from
            ticket
        <where>
            <if test="userName != null and userName != ''">
                and  user_name = #{userName}
            </if>
            <if test="status != null and status != 0">
                and  status = #{status}
            </if>
        </where>
    </select>

    <select id="getTicketById" resultType="com.jxy.windminer.dto.TicketDto">
		select
			id,
			user_name userName,
			email,
			model,
			serial_no serialNo,
			buy_date buyDate,
			tracking_id trackingId,
			fault,
			`desc`,
			create_time submitTime,
			status
		from
			ticket
		where
		   id = #{id}
		ORDER BY create_time
	</select>

    <select id="getBackendTicketList" resultType="com.jxy.windminer.dto.TicketListDto">
        select
            id,
            model,
            create_time `date`,
            serial_no serialNo,
            user_name userName,
            email,
            status
        from
        ticket
        <where>
            <if test="status != null and status != 0">
                and t.status = #{status}
            </if>
        </where>
        order by t.create_time,t.user_name
    </select>

    <select id="getDailyCountList" resultType="com.jxy.windminer.dto.TicketDailyCountDto">
		select
			DATE_FORMAT(create_time,'%Y-%m-%d') as date,
			sum(case when 1 then 1 else 0 end) submitNum,
			sum(case when status = 4 then 1 else 0 end) solvdNum,
			sum(case when status = 8 then 1 else 0 end) checkingNum,
			sum(case when status = 11 then 1 else 0 end) checkedNum
		FROM
			ticket
		GROUP BY date
	</select>


    <select id="getTicketResponListByTicketId" resultType="com.jxy.windminer.dto.TicketContentDto">
        select
            create_time `time`,
            respon content,
            responer name,
            files
        from
            respon
        <where>
            <if test="id != null and id != 0">
                ticket_id = #{id}
            </if>
        </where>
        order BY create_time
    </select>

    <select id="getFileUrlsByTicketId" resultType="java.lang.String">
		SELECT url FROM file where id =#{id};
	</select>

    <select id="getTicketSupplementCountByTicketId" resultType="java.lang.Integer">
		select count(*) from ticket_supplement where ticket_id = #{id};
	</select>

    <select id="getTicketSupplementListByTicketId" resultType="com.jxy.windminer.dto.TicketContentDto">
        select
            s.create_time `time`,
            s.content content,
            s.files files,
            t.user_name name
        from
        ticket_supplement s left join ticket t on s.ticket_id = t.id
        <where>
            <if test="id != null and id != 0">
                s.ticket_id = #{id}
            </if>
        </where>
        order by s.create_time,s.id
    </select>
    <select id="checkTicketExist" resultType="java.lang.Integer">
		select count(1) from ticket where id = #{id} limit 1
	</select>

    <select id="getTicketAuditByTicketId" resultType="com.jxy.windminer.dto.TicketAuditDto">
        select create_time `time`,user_name name,content
        from audit
        <where>
            <if test="id != null and id != 0">
                and ticket_id = #{id}
            </if>
        </where>
        limit 1
    </select>
    <select id="getTicketListByTime" resultType="com.jxy.windminer.dto.TicketListDto">
        select
        id,create_time `date`,user_name userName,status
        from
        ticket
        <where>
            <if test="vo.start != null and vo.start != ''">
                and create_time &gt;= #{vo.start}
            </if>
            <if test="vo.end != null and vo.end != ''">
                and create_time &lt;= #{vo.end}
            </if>
        </where>
        order by t.user_name,t.create_time
    </select>

    <select id="getFileInfoById" resultType="com.jxy.windminer.entity.JXYFile">
			select id,file_name fileName,url,user_name userName,create_time createTime  from file where id = #{id}
	</select>

    <update id="changeTicketStatus">
		UPDATE ticket SET status=#{status} WHERE id = #{id}
	</update>


    <insert id="insertTicket" useGeneratedKeys="true" keyProperty="id">
        insert into ticket(
        <if test="data.userName != null and data.userName != ''">user_name,</if>
        <if test="data.email != null and data.email !=''">email,</if>
        <if test="data.model != null and data.model !=''">model,</if>
        <if test="data.serialNo != null and data.serialNo != ''">serial_no,</if>
        <if test="data.buyDate != null and data.buyDate != ''">buy_date,</if>
        <if test="data.trackingId != null and data.trackingId != ''">tracking_id,</if>
        <if test="data.fault != null and data.fault != ''">fault,</if>
        <if test="data.desc != null and data.desc != ''">`desc`,</if>
        <if test="data.status != null and data.status != 0">status,</if>
        create_time
        )values(
        <if test="data.userName != null and data.userName != ''">#{data.userName},</if>
        <if test="data.email != null and data.email != ''">#{data.email},</if>
        <if test="data.model != null and data.model != ''">#{data.model},</if>
        <if test="data.serialNo != null and data.serialNo != ''">#{data.serialNo},</if>
        <if test="data.buyDate != null and data.buyDate != ''">#{data.buyDate},</if>
        <if test="data.trackingId != null and data.trackingId != ''">#{data.trackingId},</if>
        <if test="data.fault != null and data.fault != ''">#{data.fault},</if>
        <if test="data.desc != null and data.desc != ''">#{data.desc},</if>
        <if test="data.status != null and data.status != 0">1,</if>
        sysdate()
        )
    </insert>

    <insert id="insertTicketSupplement">
		insert into ticket_supplement(ticket_id,content,files,create_time) values (
			#{vo.id},#{vo.desc},#{vo.files},sysdate()
		)
	</insert>


    <insert id="insertRespon">
		insert into  respon(ticket_id,respon,responer,files,create_time) values(
		#{vo.id},#{vo.respon},#{name},#{vo.files},sysdate()
		)
	</insert>

    <insert id="insertFileInfo" useGeneratedKeys="true" keyProperty="id">
		insert into `file`(url,file_name,user_name,create_time) values(
		#{vo.url},#{vo.fileName},#{vo.userName},sysdate()
		)
	</insert>
</mapper>
