<?xml version="1.0" encoding="UTF-8"?>

<!--
  ~
  ~      Copyright (c) 2018-2025, hnqz All rights reserved.
  ~
  ~  Redistribution and use in source and binary forms, with or without
  ~  modification, are permitted provided that the following conditions are met:
  ~
  ~ Redistributions of source code must retain the above copyright notice,
  ~  this list of conditions and the following disclaimer.
  ~  Redistributions in binary form must reproduce the above copyright
  ~  notice, this list of conditions and the following disclaimer in the
  ~  documentation and/or other materials provided with the distribution.
  ~  Neither the name of the pig4cloud.com developer nor the names of its
  ~  contributors may be used to endorse or promote products derived from
  ~  this software without specific prior written permission.
  ~  Author: hnqz
  ~
  -->

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.qunzhixinxi.hnqz.admin.mapper.WmScorePackageStatusMapper">

  <resultMap id="wmScorePackageStatusMap" type="com.qunzhixinxi.hnqz.admin.entity.WmScorePackageStatus">
                  <id property="id" column="id"/>
                        <result property="userId" column="user_id"/>
                        <result property="packageId" column="package_id"/>
                        <result property="status" column="status"/>
                        <result property="deptId" column="dept_id"/>
                        <result property="delFlag" column="del_flag"/>
                        <result property="enableFlag" column="enable_flag"/>
                        <result property="tenantId" column="tenant_id"/>
                        <result property="createTime" column="create_time"/>
                        <result property="createUser" column="create_user"/>
                        <result property="updateTime" column="update_time"/>
                        <result property="updateUser" column="update_user"/>
	  					<result property="phone" column="phone"/>
	  					<result property="userScore" column="user_score"/>
	 				 	<result property="enableType" column="enable_type"/>
	  <result property="taskAddFlag" column="task_add_flag"/>
	  <result property="taskNum" column="task_num"/>

            </resultMap>

	<select id="getPage" resultMap="wmScorePackageStatusMap">
		select u.realname as user_id,u.phone as phone,
		CONCAT(u.province,'-',u.city ) as address,
		p.score_package_name as package_id,
		s.id,s.`status`,s.dept_id,s.del_flag,s.enable_flag,s.create_time
		,s.enable_type,s.user_id as task_user_id, s.task_num
		from wm_score_package_status s
		left join sys_user u on u.user_id=s.user_id
		left join wm_score_package p on p.id=s.package_id
		left join sys_user u1 on u1.user_id = s.user_id
		where s.del_flag=0

		<if test="query.id!=null and query.id!=''">
			and s.id=#{query.id}
		</if>
		<if test="query.userId!=null and query.userId!=''">
			and s.user_id=#{query.userId}
		</if>
		<if test="query.userRealName != null and query.userRealName != ''">
			and u1.realname like concat('%',#{query.userRealName},'%')
		</if>
		<if test="query.phone != null and query.phone != ''">
			and u1.phone like concat('%',#{query.phone},'%')
		</if>
		<if test="query.status!=null and query.status!=''">
			and s.`status` = #{query.status}
		</if>
		<if test="query.packageId!=null and query.packageId!=''">
			and s.package_id=#{query.packageId}
		</if>
		order by s.create_time
	</select>


	<select id="getByPackageId" resultMap="wmScorePackageStatusMap">
		select s.id, s.task_num, s.user_score, u.realname as user_id, s.user_id as task_user_id
		from wm_score_package_status s
		left join sys_user u on u.user_id=s.user_id
		where s.del_flag = '0' and s.enable_flag = '0'
		<if test="query.id!=null and query.id!=''">
			and s.id=#{query.id}
		</if>
		<if test="query.userId!=null and query.userId!=''">
			and s.user_id=#{query.userId}
		</if>
		<if test="query.packageId!=null and query.packageId!=''">
			and s.package_id=#{query.packageId}
		</if>
		<if test="query.status!=null and query.status!=''">
			and s.status=#{query.status}
		</if>
		order by s.create_time
	</select>

	<select id="getReceivedPackageList" resultType="com.qunzhixinxi.hnqz.admin.entity.input.WmScorePackageApiOutput">
		SELECT s.enable_flag, s.id statusId, p.id, p.score_package_name, p.send_package_dept_id, s.task_add_flag,
		p.create_time, p.package_type1, p.user_num, p.task_num, p.description, p.belong_date,
		p.score, s.enable_type, s.status, s.task_num taskNum2 , s.create_time receiveTime, d.name packageEntName
		FROM
		wm_score_package_status s, wm_score_package p , sys_dept d
		WHERE s.package_id = p.id AND p.send_package_dept_id = d.dept_id
		AND s.del_flag = '0'
		<if test="query.scorePackageName != null and query.scorePackageName != ''">
			AND p.score_package_name like CONCAT('%',#{query.scorePackageName},'%')
		</if>
		<if test="query.selDate != null and query.selDate != ''">
			AND p.belong_date= #{query.selDate}
		</if>
		<if test="query.typeid != null and query.typeid != ''">
			AND p.typeid= #{query.typeid}
		</if>
		<if test="query.packageType2 != null and query.packageType2 != ''">
			AND p.package_type2= #{query.packageType2}
		</if>
		<if test="query.userId != null and query.userId != ''">
			AND s.user_id= #{query.userId}
		</if>
		<if test="query.status != null and query.status != ''">
			AND s.status= #{query.status}
		</if>
		<if test="query.taskAddFlag != null and query.taskAddFlag != ''">
			AND s.task_add_flag= #{query.taskAddFlag}
		</if>
		order by s.create_time desc
	</select>



	<select id="getWmScorePackageStatusPage" resultMap="wmScorePackageStatusMap">
		select s.*,p.score_package_name from wm_score_package_status s
		left join  wm_score_package p on p.id=s.package_id
		left join sys_user u on u.dept_id=p.send_package_dept_id
		left join sys_user u1 on u1.user_id = s.user_id
		where  s.del_flag=0  and p.del_flag=0
		  and p.typeid=4
		  and p.source_type = '0'
		<if test="query.id!=null and query.id!=''">
			and s.id=#{query.id}
		</if>
		<if test="query.userRealName != null and query.userRealName != ''">
			and u1.realname like concat('%',#{query.userRealName},'%')
		</if>
		<if test="query.scorePackageName != null and query.scorePackageName !=''">
			and p.score_package_name like concat('%',#{query.scorePackageName},'%')
		</if>
		<if test="query.userId!=null and query.userId!=''">
			and u.user_id=#{query.userId}
		</if>
		<if test="query.packageId!=null and query.packageId!=''">
			and s.package_id=#{query.packageId}
		</if>
		<if test="query.status !=null">
			and s.status = #{query.status}
		</if>
		<if test="query.teamIdList != null">
			and p.package_user_scope in
			<foreach item="teamId" index="index" collection="query.teamIdList" open="(" separator="," close=")">
				#{teamId}
			</foreach>
		</if>
		order by s.create_time desc
	</select>

	<!-- 获取当前部门已接单人的列表 -->
	<select id="getDeptReceiveUserList" resultType="java.util.Map">
		SELECT DISTINCT
			s.user_id as userId,
			u.realname as realName
		FROM
			wm_score_package_status s
				LEFT JOIN wm_score_package p ON p.id = s.package_id
				LEFT JOIN sys_user u ON u.user_id = s.user_id
		WHERE
			s.del_flag = 0
		  AND s.enable_flag = 0
		  AND s.dept_id = #{deptId}
		  AND p.package_type1 = #{packageType1}
		  AND p.package_type2 = #{packageType2}
	</select>
</mapper>
