<?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.zcloud.mapper.datasource.relatedparty.OutSourcedMapper">

	<!--表名 -->
	<sql id="tableName">
		BUS_OUTSOURCED
	</sql>

	<!--数据字典表名 -->
	<sql id="dicTableName">
		SYS_DICTIONARIES
	</sql>

	<!-- 字段 -->
	<sql id="Field">
		f.DEPARTMENT_ID,
		f.CONTRACT_PIC,
		f.OUTSOURCED_NAME,
		f.STARTTIME,
		f.ENDTIME,
		f.CONTRACT_STIME,
		f.CONTRACT_ETIME,
		f.UNITS_ID,
		f.CONTRACT_NUM,
		f.UNITS_APTITUDE,
		f.UNITS_PIC,
		f.UNITS_PHONE,
		f.MANAGE_NAME,
		f.MANAGE_APTITUDE,
		f.MANAGE_PIC,
		f.MANAGE_PHONE,
		f.DESIGE,
		f.MEASURES,
		f.ATTACHMENTS,
		f.REMARKS,
		f.TRAINING_NAME,
		f.TRAINING_PIC,
		f.TRAINING_STIME,
		f.TRAINING_ETIME,
		f.RECORDS,
		f.SHEET,
		f.EXAMINATION,
		f.OATTACHMENTS,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.CORPINFO_ID,
		f.APPLICANT,
		f.STATE,
		f.OUTSOURCED_ID,
		f.UNITS_APTITUDEPATH,
		f.MANAGE_APTITUDEPATH,
		f.MEASURESPATH,
		f.DESIGEPATH,
		f.ATTACHMENTSPATH,
		f.RECORDSPATH,
		f.SHEETPATH,
		f.EXAMINATIONPATH,
		f.OATTACHMENTSPATH,
		f.PUNISH_THE_PERSON
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		IS_SMS,
		PUNISH_THE_PERSON,
		INVOLVING_CORPS_DEPART_ID,
		COMPETENT_DEPT_ID,
		DEPARTMENT_ID,
		CONTRACT_PIC,
		OUTSOURCED_NAME,
		STARTTIME,
		ENDTIME,
		CONTRACT_STIME,
		CONTRACT_ETIME,
		UNITS_ID,
		CONTRACT_NUM,
		UNITS_APTITUDE,
		UNITS_PIC,
		UNITS_PHONE,
		MANAGE_NAME,
		MANAGE_APTITUDE,
		MANAGE_PIC,
		MANAGE_PHONE,
		DESIGE,
		MEASURES,
		ATTACHMENTS,
		REMARKS,
		TRAINING_NAME,
		TRAINING_PIC,
		TRAINING_STIME,
		TRAINING_ETIME,
		RECORDS,
		SHEET,
		EXAMINATION,
		OATTACHMENTS,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		ISDELETE,
		CORPINFO_ID,
		APPLICANT,
		STATE,
		OUTSOURCED_ID,
		UNITS_APTITUDEPATH,
		MANAGE_APTITUDEPATH,
		MEASURESPATH,
		DESIGEPATH,
		ATTACHMENTSPATH,
		RECORDSPATH,
		SHEETPATH,
		EXAMINATIONPATH,
		OATTACHMENTSPATH
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{IS_SMS},
		#{PUNISH_THE_PERSON},
		#{INVOLVING_CORPS_DEPART_ID},
		#{COMPETENT_DEPT_ID},
		#{DEPARTMENT_ID},
		#{CONTRACT_PIC},
		#{OUTSOURCED_NAME},
		#{STARTTIME},
		#{ENDTIME},
		#{CONTRACT_STIME},
		#{CONTRACT_ETIME},
		#{UNITS_ID},
		#{CONTRACT_NUM},
		#{UNITS_APTITUDE},
		#{UNITS_PIC},
		#{UNITS_PHONE},
		#{MANAGE_NAME},
		#{MANAGE_APTITUDE},
		#{MANAGE_PIC},
		#{MANAGE_PHONE},
		#{DESIGE},
		#{MEASURES},
		#{ATTACHMENTS},
		#{REMARKS},
		#{TRAINING_NAME},
		#{TRAINING_PIC},
		#{TRAINING_STIME},
		#{TRAINING_ETIME},
		#{RECORDS},
		#{SHEET},
		#{EXAMINATION},
		#{OATTACHMENTS},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{ISDELETE},
		#{CORPINFO_ID},
		#{APPLICANT},
		#{STATE},
		#{OUTSOURCED_ID},
		#{UNITS_APTITUDEPATH},
		#{MANAGE_APTITUDEPATH},
		#{MEASURESPATH},
		#{DESIGEPATH},
		#{ATTACHMENTSPATH},
		#{RECORDSPATH},
		#{SHEETPATH},
		#{EXAMINATIONPATH},
		#{OATTACHMENTSPATH}
	</sql>

	<!-- 新增-->
	<insert id="save" parameterType="pd">
		insert into
	<include refid="tableName"></include>
		(
	<include refid="Field2"></include>
		) values (
	<include refid="FieldValue"></include>
		)
	</insert>

	<!-- 删除-->
	<delete id="delete" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			OUTSOURCED_ID = #{OUTSOURCED_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		DEPARTMENT_ID = #{DEPARTMENT_ID},
		PUNISH_THE_PERSON = #{PUNISH_THE_PERSON},
			CONTRACT_PIC = #{CONTRACT_PIC},
			OUTSOURCED_NAME = #{OUTSOURCED_NAME},
			STARTTIME = #{STARTTIME},
			ENDTIME = #{ENDTIME},
			CONTRACT_STIME = #{CONTRACT_STIME},
			CONTRACT_ETIME = #{CONTRACT_ETIME},
			UNITS_ID = #{UNITS_ID},
			CONTRACT_NUM = #{CONTRACT_NUM},
			UNITS_APTITUDE = #{UNITS_APTITUDE},
			UNITS_PIC = #{UNITS_PIC},
			UNITS_PHONE = #{UNITS_PHONE},
			MANAGE_NAME = #{MANAGE_NAME},
			DESIGE = #{DESIGE},
			MANAGE_APTITUDE = #{MANAGE_APTITUDE},
			MANAGE_PIC = #{MANAGE_PIC},
			MANAGE_PHONE = #{MANAGE_PHONE},
			MEASURES = #{MEASURES},
			ATTACHMENTS = #{ATTACHMENTS},
			REMARKS = #{REMARKS},
			TRAINING_NAME = #{TRAINING_NAME},
			TRAINING_PIC = #{TRAINING_PIC},
			TRAINING_STIME = #{TRAINING_STIME},
			TRAINING_ETIME = #{TRAINING_ETIME},
			RECORDS = #{RECORDS},
			SHEET = #{SHEET},
			EXAMINATION = #{EXAMINATION},
			OATTACHMENTS = #{OATTACHMENTS},
			APPLICANT = #{APPLICANT},
			STATE = #{STATE},
			OUTSOURCED_ID = OUTSOURCED_ID,
		UNITS_APTITUDEPATH = #{UNITS_APTITUDEPATH},
		MANAGE_APTITUDEPATH = #{MANAGE_APTITUDEPATH},
		MEASURESPATH = #{MEASURESPATH},
		DESIGEPATH = #{DESIGEPATH},
		ATTACHMENTSPATH = #{ATTACHMENTSPATH},
		RECORDSPATH = #{RECORDSPATH},
		SHEETPATH = #{SHEETPATH},
		EXAMINATIONPATH = #{EXAMINATIONPATH},
		OATTACHMENTSPATH = #{OATTACHMENTSPATH}
		where
			OUTSOURCED_ID = #{OUTSOURCED_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		f.*,
		a.NAME as DEPARTMENT_NAME,
		b.UNITS_NAME as UNITS_NAME,
		pcij.NAME Q_DEPARTMENT_NAME,
		pci.NAME Q_COMPETENT_DEPT_NAME,
		vua.NAME OUTSOURCED_CREATOR_NAME,
		isdd.NAME MANAGER_DEPARTMENT_NAME,
		ifnull(vac.CORP_NAME,'秦港股份有限公司') CORP_NAME,
		( SELECT GROUP_CONCAT( t.NAME ) FROM oa_department t WHERE FIND_IN_SET( t.DEPARTMENT_ID, f.DEPARTMENT_ID ) ) AS DEPARTMENT_NAME,
		( select count(1) from BUS_KEYPROJECTCHECK c where c.OUTSOURCED_ID = f.OUTSOURCED_ID) as CHECK_COUNT,
		( select GROUP_CONCAT(t.CORP_NAME) from BUS_CORP_INFO t where FIND_IN_SET(t.CORPINFO_ID, f.MANAGE_NAME)) as MANAGE_CORPS_NAME,
		( SELECT GROUP_CONCAT( t.NAME ) FROM oa_department t WHERE FIND_IN_SET( t.DEPARTMENT_ID, f.INVOLVING_CORPS_DEPART_ID ) ) AS INVOLVING_CORPS_DEPART_NAME,
		(select GROUP_CONCAT(t.NAME) from ( SELECT CONCAT( CONCAT( c.CORP_NAME, '-' ), t.NAME ) NAME  ,t.DEPARTMENT_ID FROM OA_DEPARTMENT t LEFT JOIN bus_corp_info c ON c.CORPINFO_ID = t.CORPINFO_ID) t where FIND_IN_SET(t.DEPARTMENT_ID, f.JURISDICTION_UNIT)) as JURISDICTION_UNIT_NAME,
		(select NAME FROM bus_personnelmanagement p where p.PERSONNELMANAGEMENT_ID = f.UNITS_PIC) as UNITS_PIC_NAME,
		c.NAME as CONTRACT_NAME
		from
		<include refid="tableName"></include> f
		LEFT JOIN vi_user_all vua on f.CREATOR = vua.USER_ID
		LEFT JOIN vi_all_corp vac on vua.CORPINFO_ID = vac.CORPINFO_ID
		LEFT JOIN oa_department pci ON pci.DEPARTMENT_ID = f.COMPETENT_DEPT_ID
		LEFT JOIN oa_department isdd ON isdd.DEPARTMENT_ID = f.MANAGER_DEPARTMENT_ID
		LEFT JOIN oa_department pcij ON pcij.DEPARTMENT_ID = f.DEPARTMENT_ID
		left join oa_department a on a.DEPARTMENT_ID = f.DEPARTMENT_ID
		left join bus_units b using (UNITS_ID)
		left join sys_user c on c.USER_ID= f.CONTRACT_PIC
		where
			f.OUTSOURCED_ID = #{OUTSOURCED_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		f.CONTRACT_PIC,
		f.STARTTIME,
		f.ENDTIME,
		f.OUTSOURCED_NAME,
		f.CONTRACT_STIME,
		f.CONTRACT_ETIME,
		f.CONTRACT_NUM,
		f.UNITS_APTITUDE,
		f.UNITS_PIC,
		f.UNITS_PHONE,
		f.MANAGE_NAME,
		f.DESIGE,
		f.MANAGE_APTITUDE,
		f.MANAGE_PIC,
		f.MANAGE_PHONE,
		f.MEASURES,
		f.ATTACHMENTS,
		f.REMARKS,
		f.TRAINING_NAME,
		f.TRAINING_PIC,
		f.TRAINING_STIME,
		f.TRAINING_ETIME,
		f.RECORDS,
		f.SHEET,
		f.EXAMINATION,
		f.OATTACHMENTS,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.CORPINFO_ID,
		f.APPLICANT,
		f.STATE,
		f.OUTSOURCED_ID,
		f.UNITS_APTITUDEPATH,
		f.MANAGE_APTITUDEPATH,
		f.MEASURESPATH,
		f.DESIGEPATH,
		f.ATTACHMENTSPATH,
		f.RECORDSPATH,
		f.SHEETPATH,
		f.EXAMINATIONPATH,
		f.OATTACHMENTSPATH,
		a.NAME as DEPARTMENT_ID,
		pcij.NAME Q_DEPARTMENT_NAME,
		isdd.NAME MANAGER_DEPARTMENT_NAME,
		b.UNITS_NAME as UNITS_NAME,
		vua.NAME OUTSOURCED_CREATOR_NAME,
		pci.NAME Q_COMPETENT_DEPT_NAME,
		( SELECT GROUP_CONCAT( t.NAME ) FROM oa_department t WHERE FIND_IN_SET( t.DEPARTMENT_ID, f.DEPARTMENT_ID ) ) AS DEPARTMENT_NAME,
		( select count(1) from BUS_KEYPROJECTCHECK c where c.OUTSOURCED_ID = f.OUTSOURCED_ID) as CHECK_COUNT,
		( select GROUP_CONCAT(t.CORP_NAME) from BUS_CORP_INFO t where FIND_IN_SET(t.CORPINFO_ID, f.MANAGE_NAME)) as MANAGE_CORPS_NAME,
		( SELECT GROUP_CONCAT( t.NAME ) FROM oa_department t WHERE FIND_IN_SET( t.DEPARTMENT_ID, f.INVOLVING_CORPS_DEPART_ID ) ) AS INVOLVING_CORPS_DEPART_NAME,
		(select GROUP_CONCAT(t.NAME) from ( SELECT CONCAT( CONCAT( c.CORP_NAME, '-' ), t.NAME ) NAME  ,t.DEPARTMENT_ID FROM OA_DEPARTMENT t LEFT JOIN bus_corp_info c ON c.CORPINFO_ID = t.CORPINFO_ID) t where FIND_IN_SET(t.DEPARTMENT_ID, f.JURISDICTION_UNIT)) as JURISDICTION_UNIT_NAME,
		vuad.NAME CORP_NAME,
		p.`NAME` as UNITS_PIC_NAME
		from
		<include refid="tableName"></include> f
		LEFT JOIN oa_department pci ON pci.DEPARTMENT_ID = f.COMPETENT_DEPT_ID
		LEFT JOIN oa_department isdd ON isdd.DEPARTMENT_ID = f.MANAGER_DEPARTMENT_ID
		LEFT JOIN oa_department pcij ON pcij.DEPARTMENT_ID = f.DEPARTMENT_ID
		left join oa_department a on a.DEPARTMENT_ID = f.DEPARTMENT_ID
		left join bus_units b using (UNITS_ID)
		LEFT JOIN sys_user vua on f.CREATOR = vua.USERNAME and vua.ISDELETE = '0'
		LEFT JOIN oa_department vuad ON vuad.DEPARTMENT_ID = vua.DEPARTMENT_ID
		LEFT JOIN vi_all_corp vac on vua.CORPINFO_ID = vac.CORPINFO_ID
		LEFT JOIN bus_personnelmanagement p on p.PERSONNELMANAGEMENT_ID = f.UNITS_PIC
		where f.ISDELETE = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			<!--	根据需求自己加检索条件-->
			f.OUTSOURCED_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			b.UNITS_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			p.`NAME` LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')

			)
		</if>
		<if test="pd.STATE != null and pd.STATE !=''">
			and f.STATE = #{pd.STATE}
		</if>
		<if test="pd.UNITS_ID != null and pd.UNITS_ID !=''">
			and f.UNITS_ID = #{pd.UNITS_ID}
		</if>
		<if test="pd.CONTRACT_STIME  != null and pd.CONTRACT_STIME   !=''">
			and (f.CONTRACT_STIME between #{pd.CONTRACT_STIME} and #{pd.CONTRACT_ETIME}
			         or f.CONTRACT_ETIME between #{pd.CONTRACT_STIME} and #{pd.CONTRACT_ETIME}
			or (f.CONTRACT_STIME &lt; #{pd.CONTRACT_STIME} and f.CONTRACT_ETIME &gt; #{pd.CONTRACT_ETIME}))
		</if>
		<if test="pd.CORPINFO_ID != null and pd.ORPINFO_ID != ''">
			and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		<if test="pd.CORP_NAME != null and pd.CORP_NAME != ''">
			and vuad.NAME LIKE CONCAT(CONCAT('%', #{pd.CORP_NAME}),'%')
		</if>
		ORDER BY f.OPERATTIME DESC
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
	</select>

	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			OUTSOURCED_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>

	<select id="homepagedata" parameterType="pd" resultType="pd">
		select
		count(1) as INSPECTION_COUNT,
		(select count(1) from bus_keyproject_hidden where RECTIFICATIONOR = #{PERSONNELMANAGEMENT_ID} and state = '1'  and ISDELETE = '0') as HIDDEN_COUNT,
		(select count(1) from bus_keyprojectpunish where HANDLED = '0' and PERSONNELMANAGEMENT_ID = #{PERSONNELMANAGEMENT_ID}) as PUNISH_COUNT
		from bus_keyprojectcheck f
		where
		f.PERSONNELMANAGEMENT_ID = #{PERSONNELMANAGEMENT_ID}
		and INSPECTION_STATUS = '0'
		<if test="UNITS_ID != null and UNITS_ID !=''">
			and f.UNITS_ID = #{UNITS_ID}
		</if>
	</select>

	<select id="getDeptId" resultType="string">
		SELECT
			DEPARTMENT_ID
			FROM
			`sys_user`
			WHERE
			`USER_ID` =#{userId}
	</select>

	<select id="getCorpInfoByOutsourcesCreator" parameterType="pd" resultType="string">
		SELECT
			v.CORPINFO_ID
		FROM
			bus_outsourced o
				LEFT JOIN vi_user_all v on v.USER_ID=o.CREATOR
		WHERE
			OUTSOURCED_ID = #{OUTSOURCED_ID}
	</select>

	<!-- APP工程申请列表 -->
	<select id="applylistPage" parameterType="page" resultType="pd">
		select
		f.*,
		(select GROUP_CONCAT(t.CORP_NAME) from BUS_CORP_INFO t where FIND_IN_SET(t.CORPINFO_ID, f.INVOLVING_CORPS)) as INVOLVING_CORPS_NAME,
		(select GROUP_CONCAT(t.CORP_NAME) from BUS_CORP_INFO t where FIND_IN_SET(t.CORPINFO_ID, f.MANAGE_NAME)) as MANAGE_CORPS_NAME,
		( SELECT GROUP_CONCAT( t.NAME ) FROM oa_department t WHERE FIND_IN_SET( t.DEPARTMENT_ID, f.INVOLVING_CORPS_DEPART_ID ) ) AS INVOLVING_CORPS_DEPART_NAME,
		(select GROUP_CONCAT(t.CORP_NAME) from BUS_CORP_INFO t where FIND_IN_SET(t.CORPINFO_ID, f.GROUP_UNIT)) as GROUP_UNIT_NAME,
		isu.NAME USER_NAME,
		b.UNITS_NAME as UNITS_NAME,
		isu.NAME as AIWARNING_PEOPLE,
		pcij.NAME Q_DEPARTMENT_NAME,
		vua.NAME as OUTSOURCED_CREATOR_NAME,
		ifnull(vac.CORP_NAME,'秦港股份有限公司') CORP_NAME,
		p.`NAME` as UNITS_PIC_NAME
		from
		<include refid="tableName"></include> f
		left join bus_units b using (UNITS_ID)
		LEFT JOIN oa_department pcij ON pcij.DEPARTMENT_ID = f.DEPARTMENT_ID
		LEFT JOIN SYS_USER isu ON isu.USER_ID = f.CONTRACT_PIC
		LEFT JOIN vi_user_all vua on f.CREATOR = vua.USER_ID
		LEFT JOIN vi_all_corp vac on vua.CORPINFO_ID = vac.CORPINFO_ID
		LEFT JOIN bus_personnelmanagement p on p.PERSONNELMANAGEMENT_ID = f.UNITS_PIC
		where f.ISDELETE = '0'  and f.STATE in ('0','1')
		<!-- 目前必须有 根据创建人的公司查询 -->
		<if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''">
			and vac.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			<!--	根据需求自己加检索条件-->
			f.OUTSOURCED_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			b.UNITS_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			f.UNITS_PIC LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			pcij.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
		</if>
		<if test="pd.PERSONNELMANAGEMENT_ID != null and pd.PERSONNELMANAGEMENT_ID !=''">
			and f.UNITS_PIC = #{pd.PERSONNELMANAGEMENT_ID}
		</if>
		<if test="pd.OUTSOURCED_NAME != null and pd.OUTSOURCED_NAME !=''">
			and f.OUTSOURCED_NAME = #{pd.OUTSOURCED_NAME}
		</if>
		<if test="pd.UNITS_ID != null and pd.UNITS_ID !=''">
			and f.UNITS_ID = #{pd.UNITS_ID}
		</if>
		<if test="pd.INVOLVING_CORP != null and pd.INVOLVING_CORP !=''">
			and f.INVOLVING_CORPS LIKE CONCAT(CONCAT('%', #{pd.INVOLVING_CORP}),'%')
		</if>
		<if test="pd.DEPARTMENT_ID != null and pd.DEPARTMENT_ID !=''">
			and f.DEPARTMENT_ID LIKE CONCAT(CONCAT('%', #{pd.DEPARTMENT_ID}),'%')
		</if>
		<if test="pd.COMPETENT_DEPT_ID != null and pd.COMPETENT_DEPT_ID !=''">
			and f.COMPETENT_DEPT_ID LIKE CONCAT(CONCAT('%', #{pd.COMPETENT_DEPT_ID}),'%')
		</if>
		<if test="pd.GROUP_UNIT != null and pd.GROUP_UNIT !=''">
			and f.GROUP_UNIT LIKE CONCAT(CONCAT('%', #{pd.GROUP_UNIT}),'%')
		</if>
		<if test="pd.CORP_NAME != null and pd.CORP_NAME !=''">
			and vac.CORP_NAME LIKE CONCAT(CONCAT('%', #{pd.CORP_NAME}),'%')
		</if>
		<if test="pd.CONTRACT_STIME  != null and pd.CONTRACT_STIME   !=''">
			and (f.CONTRACT_STIME between #{pd.CONTRACT_STIME} and #{pd.CONTRACT_ETIME}
			or f.CONTRACT_ETIME between #{pd.CONTRACT_STIME} and #{pd.CONTRACT_ETIME}
			or (f.CONTRACT_STIME &lt; #{pd.CONTRACT_STIME} and f.CONTRACT_ETIME &gt; #{pd.CONTRACT_ETIME}))
		</if>
		ORDER BY
		f.STATE ASC,
		f.OPERATTIME DESC
	</select>

	<update id="editDingWei" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		WORK_LONGITUDE = #{WORK_LONGITUDE},
		WORK_LATITUDE = #{WORK_LATITUDE},
		LOCATION = #{LOCATION}
		where
		OUTSOURCED_ID = #{OUTSOURCED_ID}
	</update>

	<select id="jieshuOutSourced" parameterType="pd" resultType="pd">
		SELECT
			o.OUTSOURCED_ID,
			IFNULL( QR.qr, 0 ) qr,
			IFNULL( ZG.zg, 0 ) zg,
			IFNULL( YS.ys, 0 ) ys,
			IFNULL( CF.cf, 0 ) cf,
			IFNULL( FK.fk, 0 ) fk,
			IFNULL( AI.ai, 0 ) ai
		FROM
			bus_outsourced o
		<!-- 检查待确认 -->
		LEFT JOIN (
		SELECT
			COUNT( 1 ) qr,
			c.OUTSOURCED_ID
		FROM
			bus_keyprojectcheck c
		WHERE
			c.INSPECTION_STATUS = 0
			AND c.ISDELETE = 0
			GROUP BY
			c.OUTSOURCED_ID
			) QR ON QR.OUTSOURCED_ID = o.OUTSOURCED_ID
		<!-- 隐患待整改 -->
		LEFT JOIN (
		SELECT
			COUNT( f.HIDDEN_ID ) zg,
			f.OUTSOURCED_ID
		FROM
			(
			SELECT
				h.HIDDEN_ID,
				IFNULL( c.OUTSOURCED_ID, h.FOREIGN_ID ) OUTSOURCED_ID
			FROM
				bus_keyproject_hidden h
			LEFT JOIN bus_keyprojectcheck c ON h.FOREIGN_ID = c.KEYPROJECTCHECK_ID
			WHERE
			h.STATE = 1
			AND h.ISDELETE = 0
			) f
		GROUP BY
			f.OUTSOURCED_ID
		) ZG ON ZG.OUTSOURCED_ID = o.OUTSOURCED_ID
		<!-- 隐患待验收 -->
		LEFT JOIN (
		SELECT
		COUNT( DISTINCT t.HIDDEN_ID ) ys,
		t.OUTSOURCED_ID
		FROM
		(
		SELECT
		f.HIDDEN_ID,
		IFNULL( c.OUTSOURCED_ID, f.FOREIGN_ID ) OUTSOURCED_ID
		FROM
			BUS_KEYPROJECT_HIDDEN f
		LEFT JOIN bus_keyprojectcheck c ON c.KEYPROJECTCHECK_ID = f.FOREIGN_ID
		WHERE
		f.ISDELETE = '0'
		AND f.SOURCE IN ( '4', '5', '1' )
		AND f.STATE = '2'
		) t
		GROUP BY
		t.OUTSOURCED_ID
		) YS ON YS.OUTSOURCED_ID = o.OUTSOURCED_ID
		<!-- 隐患待处罚 -->
		LEFT JOIN (
		SELECT
		COUNT( DISTINCT t.HIDDEN_ID ) cf,
		t.OUTSOURCED_ID
		FROM
		(
		SELECT
		f.HIDDEN_ID,
		IFNULL( c.OUTSOURCED_ID, f.FOREIGN_ID ) OUTSOURCED_ID
		FROM
		BUS_KEYPROJECT_HIDDEN f
		LEFT JOIN bus_keyprojectcheck c ON c.KEYPROJECTCHECK_ID = f.FOREIGN_ID
		WHERE
		f.ISDELETE = '0'
		AND f.SOURCE IN ( '4', '5', '1' )
		AND (f.ISPUNISH is NULL OR f.ISPUNISH = '')
		) t
		GROUP BY
		t.OUTSOURCED_ID
		) CF on CF.OUTSOURCED_ID = o.OUTSOURCED_ID
		<!-- 处罚待反馈 -->
		LEFT JOIN (
		SELECT
		COUNT( DISTINCT t.HIDDEN_ID ) fk,
		t.OUTSOURCED_ID
		FROM
		(
		SELECT
		p.HIDDEN_ID,
		IFNULL( c.OUTSOURCED_ID, f.FOREIGN_ID ) OUTSOURCED_ID
		FROM
		bus_keyprojectpunish p
		LEFT JOIN BUS_KEYPROJECT_HIDDEN f on p.HIDDEN_ID = f.HIDDEN_ID
		LEFT JOIN bus_keyprojectcheck c ON c.KEYPROJECTCHECK_ID = f.FOREIGN_ID
		WHERE
		p.ISDELETE = '0'
		AND p.HANDLED = '0'
		) t
		GROUP BY
		t.OUTSOURCED_ID
		) FK ON FK.OUTSOURCED_ID = o.OUTSOURCED_ID
		<!-- ai报警待处置 -->
		LEFT JOIN (
		SELECT
		count( 1 ) AS ai ,t.OUTSOURCED_ID
		FROM
		bus_aiwarning f
		LEFT JOIN (
		SELECT DISTINCT
		vi.VIDEOMANAGER_ID,
		aip.OUTSOURCED_ID
		FROM
		bus_aiwarning_people aip
		LEFT JOIN bus_videomanager vi ON vi.OUTSOURCED_ID = aip.OUTSOURCED_ID
		WHERE
		aip.ISDELETE = 0
		AND vi.VIDEOMANAGER_ID IS NOT NULL
		) t ON t.VIDEOMANAGER_ID = f.VIDEOMANAGER_ID
		WHERE
		f.ISDELETE = 0
		AND f.STATUS = 0
		GROUP BY t.OUTSOURCED_ID
		) AI ON AI.OUTSOURCED_ID = o.OUTSOURCED_ID
		WHERE o.OUTSOURCED_ID=#{OUTSOURCED_ID}
	</select>

	<!-- 列表 -->
	<select id="outSouceslistPage" parameterType="page" resultType="pd">
		select
		f.WORK_LONGITUDE,
		f.WORK_LATITUDE,
		f.CONTRACT_PIC,
		f.STARTTIME,
		f.ENDTIME,
		f.OUTSOURCED_NAME,
		f.CONTRACT_STIME,
		f.CONTRACT_ETIME,
		f.CONTRACT_NUM,
		f.UNITS_APTITUDE,
		f.UNITS_PIC,
		f.UNITS_PHONE,
		f.MANAGE_NAME,
		f.DESIGE,
		f.MANAGE_APTITUDE,
		f.MANAGE_PIC,
		f.MANAGE_PHONE,
		f.MEASURES,
		f.ATTACHMENTS,
		f.REMARKS,
		f.TRAINING_NAME,
		f.TRAINING_PIC,
		f.TRAINING_STIME,
		f.TRAINING_ETIME,
		f.RECORDS,
		f.SHEET,
		f.EXAMINATION,
		f.OATTACHMENTS,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.CORPINFO_ID,
		f.APPLICANT,
		f.STATE,
		f.OUTSOURCED_ID,
		f.UNITS_APTITUDEPATH,
		f.MANAGE_APTITUDEPATH,
		f.MEASURESPATH,
		f.DESIGEPATH,
		f.ATTACHMENTSPATH,
		f.RECORDSPATH,
		f.SHEETPATH,
		f.EXAMINATIONPATH,
		f.OATTACHMENTSPATH,
		a.NAME as DEPARTMENT_ID,
		pcij.NAME Q_DEPARTMENT_NAME,
		isdd.NAME MANAGER_DEPARTMENT_NAME,
		b.UNITS_NAME as UNITS_NAME,
		vua.NAME OUTSOURCED_CREATOR_NAME,
		pci.NAME Q_COMPETENT_DEPT_NAME,
		( SELECT GROUP_CONCAT( t.NAME ) FROM oa_department t WHERE FIND_IN_SET( t.DEPARTMENT_ID, f.DEPARTMENT_ID ) ) AS DEPARTMENT_NAME,
		( select count(1) from BUS_KEYPROJECTCHECK c where c.OUTSOURCED_ID = f.OUTSOURCED_ID) as CHECK_COUNT,
		( select GROUP_CONCAT(t.CORP_NAME) from BUS_CORP_INFO t where FIND_IN_SET(t.CORPINFO_ID, f.MANAGE_NAME)) as MANAGE_CORPS_NAME,
		( SELECT GROUP_CONCAT( t.NAME ) FROM oa_department t WHERE FIND_IN_SET( t.DEPARTMENT_ID, f.INVOLVING_CORPS_DEPART_ID ) ) AS INVOLVING_CORPS_DEPART_NAME,
		(select GROUP_CONCAT(t.NAME) from ( SELECT CONCAT( CONCAT( c.CORP_NAME, '-' ), t.NAME ) NAME  ,t.DEPARTMENT_ID FROM OA_DEPARTMENT t LEFT JOIN bus_corp_info c ON c.CORPINFO_ID = t.CORPINFO_ID) t where FIND_IN_SET(t.DEPARTMENT_ID, f.JURISDICTION_UNIT)) as JURISDICTION_UNIT_NAME,
		ifnull(vac.CORP_NAME,'秦港股份有限公司') CORP_NAME,
		p.`NAME` as UNITS_PIC_NAME
		from
		<include refid="tableName"></include> f
		LEFT JOIN oa_department pci ON pci.DEPARTMENT_ID = f.COMPETENT_DEPT_ID
		LEFT JOIN oa_department isdd ON isdd.DEPARTMENT_ID = f.MANAGER_DEPARTMENT_ID
		LEFT JOIN oa_department pcij ON pcij.DEPARTMENT_ID = f.DEPARTMENT_ID
		left join oa_department a on a.DEPARTMENT_ID = f.DEPARTMENT_ID
		left join bus_units b using (UNITS_ID)
		LEFT JOIN vi_user_all vua on f.CREATOR = vua.USERNAME
		LEFT JOIN vi_all_corp vac on vua.CORPINFO_ID = vac.CORPINFO_ID
		LEFT JOIN bus_personnelmanagement p on p.PERSONNELMANAGEMENT_ID = f.UNITS_PIC
		where f.ISDELETE = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			<!--	根据需求自己加检索条件-->
			f.OUTSOURCED_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			b.UNITS_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			f.UNITS_PIC LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			pcij.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
		</if>
		<if test="pd.OUTSOURCED_NAME != null and pd.OUTSOURCED_NAME !=''">
			and f.OUTSOURCED_NAME = #{pd.OUTSOURCED_NAME}
		</if>
		<if test="pd.UNITS_ID != null and pd.UNITS_ID !=''">
			and f.UNITS_ID = #{pd.UNITS_ID}
		</if>
		<if test="pd.INVOLVING_CORP != null and pd.INVOLVING_CORP !=''">
			and f.INVOLVING_CORPS LIKE CONCAT(CONCAT('%', #{pd.INVOLVING_CORP}),'%')
		</if>
		<if test="pd.DEPARTMENT_ID != null and pd.DEPARTMENT_ID !=''">
			and f.DEPARTMENT_ID LIKE CONCAT(CONCAT('%', #{pd.DEPARTMENT_ID}),'%')
		</if>
		<if test="pd.COMPETENT_DEPT_ID != null and pd.COMPETENT_DEPT_ID !=''">
			and f.COMPETENT_DEPT_ID LIKE CONCAT(CONCAT('%', #{pd.COMPETENT_DEPT_ID}),'%')
		</if>
		<if test="pd.GROUP_UNIT != null and pd.GROUP_UNIT !=''">
			and f.GROUP_UNIT LIKE CONCAT(CONCAT('%', #{pd.GROUP_UNIT}),'%')
		</if>
		<if test="pd.CORP_NAME != null and pd.CORP_NAME !=''">
			and vac.CORP_NAME LIKE CONCAT(CONCAT('%', #{pd.CORP_NAME}),'%')
		</if>
		<if test="pd.STATE != null and pd.STATE !=''">
			and f.STATE = #{pd.STATE}
		</if>
		<if test="pd.CONTRACT_STIME  != null and pd.CONTRACT_STIME   !=''">
			and (f.CONTRACT_STIME between #{pd.CONTRACT_STIME} and #{pd.CONTRACT_ETIME}
			or f.CONTRACT_ETIME between #{pd.CONTRACT_STIME} and #{pd.CONTRACT_ETIME}
			or (f.CONTRACT_STIME &lt; #{pd.CONTRACT_STIME} and f.CONTRACT_ETIME &gt; #{pd.CONTRACT_ETIME}))
		</if>
		<if test="pd.DeptId  != null and pd.DeptId   !=''">
			and ( f.DEPARTMENT_ID LIKE CONCAT(CONCAT('%', #{pd.DeptId}),'%')
			or f.COMPETENT_DEPT_ID LIKE CONCAT(CONCAT('%',  #{pd.DeptId}),'%')
			or f.INVOLVING_CORPS LIKE CONCAT(CONCAT('%',  #{pd.DeptId}),'%')
			or f.INVOLVING_CORPS_DEPART_ID LIKE CONCAT(CONCAT('%', #{pd.DeptId}),'%')
			or f.JURISDICTION_UNIT LIKE CONCAT(CONCAT('%', #{pd.DeptId}),'%')
			or f.PUNISH_THE_PERSON LIKE CONCAT(CONCAT('%', #{pd.UserId}),'%')
			or f.CREATOR = #{pd.UserId}
			)
		</if>
		ORDER BY
		f.STATE,
		f.OPERATTIME DESC
	</select>

	<!-- 修改 -->
	<update id="jie" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		DEPARTMENT_ID = #{DEPARTMENT_ID},
		CONTRACT_PIC = #{CONTRACT_PIC},
		OUTSOURCED_NAME = #{OUTSOURCED_NAME},
		INVOLVING_CORPS = #{INVOLVING_CORPS},
		STARTTIME = #{STARTTIME},
		ENDTIME = #{ENDTIME},
		CONTRACT_STIME = #{CONTRACT_STIME},
		CONTRACT_ETIME = #{CONTRACT_ETIME},
		UNITS_ID = #{UNITS_ID},
		CONTRACT_NUM = #{CONTRACT_NUM},
		UNITS_APTITUDE = #{UNITS_APTITUDE},
		UNITS_PIC = #{UNITS_PIC},
		UNITS_PHONE = #{UNITS_PHONE},
		MANAGE_NAME = #{MANAGE_NAME},
		DESIGE = #{DESIGE},
		MANAGE_APTITUDE = #{MANAGE_APTITUDE},
		MANAGE_PIC = #{MANAGE_PIC},
		MANAGE_PHONE = #{MANAGE_PHONE},
		MEASURES = #{MEASURES},
		ATTACHMENTS = #{ATTACHMENTS},
		REMARKS = #{REMARKS},
		TRAINING_NAME = #{TRAINING_NAME},
		TRAINING_PIC = #{TRAINING_PIC},
		TRAINING_STIME = #{TRAINING_STIME},
		TRAINING_ETIME = #{TRAINING_ETIME},
		RECORDS = #{RECORDS},
		SHEET = #{SHEET},
		EXAMINATION = #{EXAMINATION},
		OATTACHMENTS = #{OATTACHMENTS},
		APPLICANT = #{APPLICANT},
		PUNISH_THE_PERSON = #{PUNISH_THE_PERSON},
		STATE = '2',
		OUTSOURCED_ID = OUTSOURCED_ID
		where
		OUTSOURCED_ID = #{OUTSOURCED_ID}
	</update>
	<update id="start">
		update
		<include refid="tableName"></include>
		set
		STATE = '1'
		where
		OUTSOURCED_ID = #{OUTSOURCED_ID}
	</update>

	<update id="updateState" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		STATE = #{STATE},
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME}
		where
		OUTSOURCED_ID = #{OUTSOURCED_ID}
	</update>

	<select id="getDingWeiSum" parameterType="pd" resultType="int">
		SELECT
			sum( c.co )
		FROM
			(
			SELECT
				COUNT( OUTSOURCED_ID ) co
			FROM
				bus_outsourced
			WHERE
				ISDELETE = '0'
				AND OUTSOURCED_ID = #{OUTSOURCED_ID}
				AND (WORK_LATITUDE IS  NULL or WORK_LATITUDE = '' )
				AND (WORK_LONGITUDE IS  NULL or WORK_LONGITUDE = '' )
				) c
	</select>

	<select id="getPUNISHlistPage" parameterType="page" resultType="pd">
		select
		f.WORK_LONGITUDE,
		f.WORK_LATITUDE,
		f.CONTRACT_PIC,
		f.STARTTIME,
		f.ENDTIME,
		f.OUTSOURCED_NAME,
		f.CONTRACT_STIME,
		f.CONTRACT_ETIME,
		f.CONTRACT_NUM,
		f.UNITS_APTITUDE,
		f.UNITS_PIC,
		f.UNITS_PHONE,
		f.MANAGE_NAME,
		f.DESIGE,
		f.MANAGE_APTITUDE,
		f.MANAGE_PIC,
		f.MANAGE_PHONE,
		f.MEASURES,
		f.ATTACHMENTS,
		f.REMARKS,
		f.TRAINING_NAME,
		f.TRAINING_PIC,
		f.TRAINING_STIME,
		f.TRAINING_ETIME,
		f.RECORDS,
		f.SHEET,
		f.EXAMINATION,
		f.OATTACHMENTS,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.CORPINFO_ID,
		f.APPLICANT,
		f.STATE,
		f.OUTSOURCED_ID,
		f.UNITS_APTITUDEPATH,
		f.MANAGE_APTITUDEPATH,
		f.MEASURESPATH,
		f.DESIGEPATH,
		f.ATTACHMENTSPATH,
		f.RECORDSPATH,
		f.SHEETPATH,
		f.EXAMINATIONPATH,
		f.OATTACHMENTSPATH,
		a.NAME as DEPARTMENT_ID,
		pcij.NAME Q_DEPARTMENT_NAME,
		isdd.NAME MANAGER_DEPARTMENT_NAME,
		b.UNITS_NAME as UNITS_NAME,
		vua.NAME OUTSOURCED_CREATOR_NAME,
		pci.NAME Q_COMPETENT_DEPT_NAME,
		( SELECT GROUP_CONCAT( t.NAME ) FROM oa_department t WHERE FIND_IN_SET( t.DEPARTMENT_ID, f.DEPARTMENT_ID ) ) AS DEPARTMENT_NAME,
		( select count(1) from BUS_KEYPROJECTCHECK c where c.OUTSOURCED_ID = f.OUTSOURCED_ID) as CHECK_COUNT,
		( select GROUP_CONCAT(t.CORP_NAME) from BUS_CORP_INFO t where FIND_IN_SET(t.CORPINFO_ID, f.MANAGE_NAME)) as MANAGE_CORPS_NAME,
		( SELECT GROUP_CONCAT( t.NAME ) FROM oa_department t WHERE FIND_IN_SET( t.DEPARTMENT_ID, f.INVOLVING_CORPS_DEPART_ID ) ) AS INVOLVING_CORPS_DEPART_NAME,
		(select GROUP_CONCAT(t.NAME) from ( SELECT CONCAT( CONCAT( c.CORP_NAME, '-' ), t.NAME ) NAME  ,t.DEPARTMENT_ID FROM OA_DEPARTMENT t LEFT JOIN bus_corp_info c ON c.CORPINFO_ID = t.CORPINFO_ID) t where FIND_IN_SET(t.DEPARTMENT_ID, f.JURISDICTION_UNIT)) as JURISDICTION_UNIT_NAME,
		ifnull(vac.CORP_NAME,'秦港股份有限公司') CORP_NAME,
		p.`NAME` as UNITS_PIC_NAME
		,ifnull(cfsl.co,0) as cfcount
		from
		<include refid="tableName"></include> f
		LEFT JOIN oa_department pci ON pci.DEPARTMENT_ID = f.COMPETENT_DEPT_ID
		LEFT JOIN oa_department isdd ON isdd.DEPARTMENT_ID = f.MANAGER_DEPARTMENT_ID
		LEFT JOIN oa_department pcij ON pcij.DEPARTMENT_ID = f.DEPARTMENT_ID
		left join oa_department a on a.DEPARTMENT_ID = f.DEPARTMENT_ID
		left join bus_units b using (UNITS_ID)
		LEFT JOIN vi_user_all vua on f.CREATOR = vua.USERNAME
		LEFT JOIN vi_all_corp vac on vua.CORPINFO_ID = vac.CORPINFO_ID
		LEFT JOIN bus_personnelmanagement p on p.PERSONNELMANAGEMENT_ID = f.UNITS_PIC
		left join (SELECT PUN.OUTSOURCED_ID,COUNT( distinct PUN.HIDDEN_ID) co  FROM (
		SELECT
		f.HIDDEN_ID,ifnull(bo.OUTSOURCED_ID,boai.OUTSOURCED_ID) OUTSOURCED_ID
		FROM
		BUS_KEYPROJECT_HIDDEN f
		LEFT JOIN bus_keyprojectcheck keycheck ON keycheck.KEYPROJECTCHECK_ID = f.FOREIGN_ID
		LEFT JOIN BUS_OUTSOURCED bo ON keycheck.OUTSOURCED_ID = bo.OUTSOURCED_ID
		LEFT JOIN BUS_OUTSOURCED boai ON f.FOREIGN_ID = boai.OUTSOURCED_ID
		WHERE
		f.ISDELETE = '0'
		AND f.SOURCE IN ( '4', '5', '1' )
		AND ifnull(bo.OUTSOURCED_ID,boai.OUTSOURCED_ID) IS NOT NULL
		and f.ISPUNISH is null
		) PUN GROUP BY PUN.OUTSOURCED_ID ) cfsl on cfsl.OUTSOURCED_ID = f.OUTSOURCED_ID
		where f.ISDELETE = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			<!--	根据需求自己加检索条件-->
			f.OUTSOURCED_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			b.UNITS_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			f.UNITS_PIC LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			pcij.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
		</if>
		and f.PUNISH_THE_PERSON LIKE CONCAT( CONCAT( '%', #{pd.PUNISHUser} ), '%' )
		ORDER BY
		cfsl.co DESC,
		f.OPERATTIME DESC
	</select>

</mapper>
