<?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.marketall.mapper.UserHelpMapper">

	<select id="getTypeSelect" resultType="com.jxy.marketall.dto.SelectDto">
		select
			id,`type`
		from
			question_type
	</select>

	<select id="getPrivateTicketList" resultType="com.jxy.marketall.dto.TicketListDto">
		select
			t.id id,q.type type,t.create_time date,t.title,t.user_name userName,t.status
		from
			ticket t left join question_type q on t.question_type_id = q.id
		<where>
			<if test="userName != null and userName != ''">
				t.user_name = #{userName}
			</if>
		</where>
	</select>

	<select id="getPrivateTicketListByStatus" resultType="com.jxy.marketall.dto.TicketListDto">
		select
			t.id id,q.type type,t.create_time date,t.title,t.user_name userName,t.status
		from
			ticket t left join question_type q on t.question_type_id = q.id
		<where>
			<if test="userName != null and userName != ''">
				and  t.user_name = #{userName}
			</if>
			<if test="status != null and status != 0">
				and  t.status = #{status}
			</if>
		</where>
	</select>

	<select id="getTicketById" resultType="com.jxy.marketall.dto.TicketDto">
		select
			t.id id,
			q.type type,
			t.title title,
			t.user_name userName,
			t.create_time submitTime,
			t.status status
		from
			ticket t left join  question_type q on t.question_type_id = q.id
		where
		   t.id = #{id}
		ORDER BY t.create_time
	</select>

	<select id="getBackendTicketList" resultType="com.jxy.marketall.dto.TicketListDto">
		select
			t.id id,q.type type,t.create_time `date`,t.title,t.user_name userName,t.status
		from
			ticket t left join question_type q on t.question_type_id = q.id
		<where>
			<if test="status != null and status != 0">
				and t.status = #{status}
			</if>
		</where>
		order by t.user_name,t.create_time
	</select>

	<select id="getDailyCountList" resultType="com.jxy.marketall.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.marketall.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>
		GROUP 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.marketall.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>
		GROUP 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.marketall.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.marketall.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>
		group by create_time,user_name
	</select>

	<select id="getFileInfoById" resultType="com.jxy.marketall.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.typeId != null and data.typeId != 0">question_type_id,</if>
		<if test="data.userName != null and data.userName != ''">user_name,</if>
		<if test="data.title != null and data.title != ''">title,</if>
		<if test="data.status != null and data.status != 0">status,</if>
		create_time
		)values(
		<if test="data.typeId != null and data.typeId != 0">#{data.typeId},</if>
		<if test="data.userName != null and data.userName != ''">#{data.userName},</if>
		<if test="data.title != null and data.title != ''">#{data.title},</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="insertAudit">
		insert into  audit(ticket_id,user_name,content,create_time) values(
		#{vo.id},#{name},#{vo.msg},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>
