<?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.jeeplus.modules.workstaff.dao.WorkStaffBasicInfoDao">
    
	<sql id="workStaffBasicInfoColumns">
		a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.office_id AS "office.id",
		a.company_id AS "company.id",
		a.no AS "no",
		a.name AS "name",
		a.gender AS "gender",
		a.nation AS "nation",
		a.id_card AS "idCard",
		a.age AS "age",
		a.birthday AS "birthday",
		a.calendar AS "calendar",
		a.email AS "email",
		a.mobile AS "mobile",
		a.phone AS "phone",
		a.marriage AS "marriage",
		a.politics AS "politics",
		a.ex_soldier AS "exSoldier",
		a.address AS "address",
		a.native_place AS "nativePlace.id",
		a.household AS "household",
		a.duty AS "duty",
		a.job_grade AS "jobGrade.id",
		a.status AS "status",
		a.entry_date AS "entryDate",
		a.work_date AS "workDate",
		a.bank_name AS "bankName",
		a.bank_card AS "bankCard",
		a.industry_date AS "industryDate",
		a.ts AS "ts",
		a.picture AS "picture",
		o.top_company AS "office.name",
		a.user_id AS "userId",
		a.branch_office AS "branchOffice.id",
		branch.name AS "branchOffice.name",
		s.name AS "company.name",
		jg.name AS "jobGrade.name",
		jg.pay AS "salary",
		a.achive_id AS "achiveId",
		nativePlace.name AS "nativePlace.name",
		a.try_end_time AS "tryEndTime"
<!--		ur.role_id AS "roleId",-->
<!--		r.name AS "roleName"-->
	</sql>
	
	<sql id="workStaffBasicInfoJoins">
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office branch ON branch.id = a.branch_office
		LEFT JOIN sys_office s ON s.id = a.company_id
		LEFT JOIN sys_user u ON u.id = a.create_by
		LEFT JOIN sys_area nativePlace ON nativePlace.id = a.native_place
		LEFT JOIN work_job_grade jg ON jg.id = a.job_grade
-- 		LEFT JOIN sys_user_role ur ON  a.user_id = ur.user_id
-- 		LEFT JOIN sys_role r ON ur.role_id = r.id
	</sql>

	<select id="get" resultType="WorkStaffBasicInfo" >
		SELECT
			<include refid="workStaffBasicInfoColumns"/>
		FROM work_staff_basic_info a
		<include refid="workStaffBasicInfoJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="getNo" resultType="WorkStaffBasicInfo" >
		SELECT
		<include refid="workStaffBasicInfoColumns"/>
		FROM work_staff_basic_info a
		<include refid="workStaffBasicInfoJoins"/>
		WHERE a.no = #{no}
	</select>
	<select id="getByCompany" resultType="java.lang.Integer" >
		select count(*) FROM work_staff_basic_info
		where
			user_id = #{userId}
			AND company_id = #{company.id}
			AND `status` IN ('试用','正式','劳务')
			AND del_flag = '0'
	</select>
	<select id="getByCompanyInfo" resultType="WorkStaffBasicInfo" >
		select
			<include refid="workStaffBasicInfoColumns"/>
		FROM work_staff_basic_info
		<include refid="workStaffBasicInfoJoins"/>
		where
			a.user_id = #{userId}
			AND a.company_id = #{company.id}
			AND a.`status` IN ('试用','正式','劳务')
			AND a.del_flag = '0'
	</select>

	<select id="findListByRoleList" resultType="WorkStaffBasicInfo">
		select
		<include refid="workStaffBasicInfoColumns"/>
		FROM work_staff_basic_info a
		<include refid="workStaffBasicInfoJoins"/>
		LEFT JOIN sys_user_role ur ON ur.user_id = a.user_id
		LEFT JOIN sys_role r ON ur.role_id = r.id
		WHERE
        a.del_flag = #{DEL_FLAG_NORMAL} AND r.id = #{roleId}
        <if test="company != null and company.id != null and company.id != ''">
            AND a.company_id = #{company.id}
        </if>
        <if test="office != null and office.id != null and office.id != ''">
            AND a.office_id = #{office.id}
        </if>
        <if test="no != null and no != ''">
            AND a.no = #{no}
        </if>
        <if test="name != null and name != ''">
            AND a.name LIKE
            <if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
            <if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
            <if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
        </if>
        <if test="idCard != null and idCard != ''">
            AND a.id_card = #{idCard}
        </if>
        <if test="mobile != null and mobile != ''">
            AND a.mobile = #{mobile}
        </if>
        <if test="jobGrade != null and jobGrade.id !=null and jobGrade.id != ''">
            AND a.job_grade = #{jobGrade.id}
        </if>
        <if test="status != null and status != ''">
            AND a.status = #{status}
        </if>
        <if test="beginEntryDate != null">
            AND a.entry_date >= #{beginEntryDate}
        </if>
        <if test="endEntryDate != null">
            AND a.entry_date &lt;= #{endEntryDate}
        </if>
        <if test="achiveId != null and achiveId !=''">
            AND a.achive_id = #{achiveId}
        </if>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findList" resultType="WorkStaffBasicInfo" >
		SELECT 
			<include refid="workStaffBasicInfoColumns"/>
		FROM work_staff_basic_info a
		<include refid="workStaffBasicInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="company != null and company.id != null and company.id != ''">
				AND a.company_id = #{company.id}
			</if>
			<if test="office != null and office.id != null and office.id != ''">
				AND a.office_id = #{office.id}
			</if>
			<if test="no != null and no != ''">
				AND a.no = #{no}
			</if>
			<if test="name != null and name != ''">
				AND a.name LIKE 
					<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<if test="idCard != null and idCard != ''">
				AND a.id_card = #{idCard}
			</if>
			<if test="mobile != null and mobile != ''">
				AND a.mobile = #{mobile}
			</if>
			<if test="jobGrade != null and jobGrade.id !=null and jobGrade.id != ''">
				AND a.job_grade = #{jobGrade.id}
			</if>
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			<if test="beginEntryDate != null">
				AND a.entry_date >= #{beginEntryDate}
			</if>
			<if test="endEntryDate != null">
				AND a.entry_date &lt;= #{endEntryDate}
			</if>
			<if test="achiveId != null and achiveId !=''">
				AND a.achive_id = #{achiveId}
			</if>
            <if test="sqlMap.dsf !=null and sqlMap.dsf !=''">
                AND ((a.user_id = #{currentUser.id} AND a.branch_office = #{currentUser.branchOffice.id})${sqlMap.dsf} )
            </if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.create_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findAddressBook" resultType="WorkStaffBasicInfo" >
		SELECT
		<include refid="workStaffBasicInfoColumns"/>
		FROM work_staff_basic_info a
		<include refid="workStaffBasicInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="company != null and company.id != null and company.id != ''">
				AND a.company_id = #{company.id}
			</if>
			<if test="office != null and office.id != null and office.id != ''">
				AND a.office_id = #{office.id}
			</if>
			<if test="no != null and no != ''">
				AND a.no = #{no}
			</if>
			<if test="name != null and name != ''">
				AND a.name LIKE
				<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<if test="idCard != null and idCard != ''">
				AND a.id_card = #{idCard}
			</if>
			<if test="mobile != null and mobile != ''">
				AND a.mobile = #{mobile}
			</if>
			<if test="jobGrade != null and jobGrade.id !=null and jobGrade.id != ''">
				AND a.job_grade = #{jobGrade.id}
			</if>
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			<if test="beginEntryDate != null">
				AND a.entry_date >= #{beginEntryDate}
			</if>
			<if test="endEntryDate != null">
				AND a.entry_date &lt;= #{endEntryDate}
			</if>
			<if test="achiveId != null and achiveId !=''">
				AND a.achive_id = #{achiveId}
			</if>
			<!--<if test="sqlMap.dsf !=null and sqlMap.dsf !=''">-->
				<!--AND ((a.user_id = #{currentUser.id} AND a.branch_office = #{currentUser.branchOffice.id})${sqlMap.dsf} )-->
			<!--</if>-->
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.create_date
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="WorkStaffBasicInfo" >
		SELECT 
			<include refid="workStaffBasicInfoColumns"/>
		FROM work_staff_basic_info a
		<include refid="workStaffBasicInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO work_staff_basic_info(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			office_id,
			company_id,
			no,
			name,
			gender,
			nation,
			id_card,
			age,
			birthday,
			calendar,
			email,
			mobile,
			phone,
			marriage,
			politics,
			ex_soldier,
			address,
			native_place,
			household,
			duty,
			job_grade,
			status,
			entry_date,
			work_date,
			bank_name,
			bank_card,
			industry_date,
			ts,
			picture,
			user_id,
			branch_office,
			achive_id,
			try_end_time
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{office.id},
			#{company.id},
			#{no},
			#{name},
			#{gender},
			#{nation},
			#{idCard},
			#{age},
			#{birthday},
			#{calendar},
			#{email},
			#{mobile},
			#{phone},
			#{marriage},
			#{politics},
			#{exSoldier},
			#{address},
			#{nativePlace.id},
			#{household},
			#{duty},
			#{jobGrade.id},
			#{status},
			#{entryDate},
			#{workDate},
			#{bankName},
			#{bankCard},
			#{industryDate},
			#{ts},
			#{picture},
			#{userId},
			#{branchOffice.id},
			#{achiveId},
			#{tryEndTime}
		)
	</insert>
	
	<update id="update">
		UPDATE work_staff_basic_info SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			office_id = #{office.id},
			company_id = #{company.id},
			no = #{no},
			name = #{name},
			gender = #{gender},
			nation = #{nation},
			id_card = #{idCard},
			age = #{age},
			birthday = #{birthday},
			calendar = #{calendar},
			email = #{email},
			phone = #{phone},
			mobile = #{mobile},
			marriage = #{marriage},
			politics = #{politics},
			ex_soldier = #{exSoldier},
			address = #{address},
			native_place = #{nativePlace.id},
			household = #{household},
			duty = #{duty},
			job_grade = #{jobGrade.id},
			status = #{status},
			entry_date = #{entryDate},
			work_date = #{workDate},
			bank_name = #{bankName},
			bank_card = #{bankCard},
			industry_date = #{industryDate},
			ts = #{ts},
			picture = #{picture},
			user_id = #{userId},
			branch_office = #{branchOffice.id},
			achive_id = #{achiveId},
			try_end_time = #{tryEndTime}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_staff_basic_info
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_staff_basic_info SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkStaffBasicInfo" statementType="STATEMENT">
		select * FROM work_staff_basic_info  where ${propertyName} = '${value}'
	</select>
    <!--根据userId获取入职信息-->
	<select id="getWorkStaffBasicInfo" resultType="WorkStaffBasicInfo" parameterType="java.lang.String">
		select a.* ,
		o.name as "office.name",
		jg.name AS "jobGrade.name",
		jg.pay AS "salary"
		FROM work_staff_basic_info  a
		LEFT JOIN sys_office o on o.id=a.office_id
		LEFT JOIN work_job_grade jg ON jg.id = a.job_grade
		where a.id=#{id}
	</select>

<!--	根据userId，获取workStaffBasicInfo信息-->
	<select id="getWorkStaffBasicInfoByUserId" resultType="WorkStaffBasicInfo" parameterType="java.lang.String">
		select a.* ,
		o.name as "office.name",
		jg.name AS "jobGrade.name",
		jg.id  AS "jobGrade.id",
		jg.pay AS "salary"
		FROM work_staff_basic_info  a
		LEFT JOIN sys_office o on o.id=a.office_id
		LEFT JOIN work_job_grade jg ON jg.id = a.job_grade
		where a.user_id=#{userId,jdbcType=VARCHAR}
		and a.status = "试用"
	</select>

    <select id="queryMaxNoWithCompany" resultType="String">
        SELECT max(SUBSTR(no,5)+0) FROM work_staff_achives where branch_office=#{branchOffice.id} and del_flag='0'
    </select>

	<select id="findUserList" resultType="WorkStaffBasicInfo" >
		SELECT
		<include refid="workStaffBasicInfoColumns"/>
		FROM work_staff_basic_info a
		<include refid="workStaffBasicInfoJoins"/>
		/*left join work_relationship wrp on a.id = wrp.staff_info_id*/
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="name != null and name != ''">
				AND a.name LIKE
				<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<if test="office != null and office.id != null and office.id !=''">
				AND a.office_id= #{office.id}
			</if>
			<if test="no != null and no != ''">
				AND a.no = #{no}
			</if>
			<!--<if test="relationshipStatus != null and relationshipStatus != ''">
				AND (wrp.del_flag ='0' or wrp.del_flag is null) AND (wrp.status = #{relationshipStatus} or wrp.status is null)
			</if>-->
			<if test="gender != null and gender != ''">
				AND a.gender = #{gender}
			</if>
			<if test="jobGrade != null and jobGrade != ''">
				AND a.job_grade = #{jobGrade}
			</if>
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			and a.office_id in (${sqlStr})
		</where>

	</select>

    <select id="findListByIdCard" resultType="WorkStaffBasicInfo">
        SELECT
        a.*
        FROM work_staff_basic_info a
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            <if test="company != null and company.id != null and company.id != ''">
                AND a.company_id = #{company.id}
            </if>
            <if test="idCard != null and idCard != ''">
                AND a.id_card = #{idCard}
            </if>
        </where>
    </select>

    <select id="findListByAchiveId" resultType="WorkStaffBasicInfo">
        SELECT
        a.*
        FROM work_staff_basic_info a
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            <if test="achiveId != null and achiveId != ''">
                AND a.achive_id = #{achiveId}
            </if>
            <if test="branchOffice != null and branchOffice.id != null and branchOffice.id != ''">
                AND a.branch_office = #{branchOffice.id}
            </if>
        </where>
    </select>

    <select id="findListByUserId" resultType="WorkStaffBasicInfo">
        SELECT
        a.*
        FROM work_staff_basic_info a
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            <if test="achiveId != null and achiveId != ''">
                AND a.user_id = #{userId}
            </if>
            <if test="branchOffice != null and branchOffice.id != null and branchOffice.id != ''">
                AND a.branch_office = #{branchOffice.id}
            </if>
        </where>
    </select>

	<update id="updateStatus">
		update work_staff_basic_info set
		status = #{status}
		where user_id = #{id}
	</update>

	<select id="findByMob" parameterType="WorkStaffBasicInfo" resultType="WorkStaffBasicInfo">
		select a.id
		from work_staff_basic_info a
		<where>
			a.mobile=#{mobile}
			<if test="id != null and id != ''">
				and a.id != #{id}
			</if>
		</where>
	</select>

	<select id="findByCard" parameterType="String" resultType="WorkStaffBasicInfo">
		select a.mobile
		from work_staff_basic_info a
		<where>
			id_card = #{cardNo}
		</where>
	</select>

	<select id="findByName" parameterType="String" resultType="WorkStaffBasicInfo">
		select a.name, a.id_card AS "idCard"
		from work_staff_basic_info a
		<where>
			name = #{name}
		</where>
	</select>
	<update id="updateWorkStaffBasicInfo" parameterType="WorkStaffBasicInfo">
		update work_staff_basic_info
		<set>
			<if test="name != null and name != ''">
				name = #{name},
			</if>
			<if test="email != null and email != ''">
				email = #{email},
			</if>
			<if test="phone != null and phone != ''">
				phone = #{phone},
			</if>
			<if test="mobile != null and mobile != ''">
				mobile = #{mobile},
			</if>
			<if test="remarks != null and remarks != ''">
				remarks = #{remarks},
			</if>
			<if test="address != null and address != ''">
				address = #{address},
			</if>
			<if test="household != null and household != ''">
				household = #{household},
			</if>
			<if test="politics != null and politics != ''">
				politics = #{politics},
			</if>
			<if test="marriage != null and marriage != ''">
				marriage = #{marriage},
			</if>
			<if test="ts != null and ts != ''">
				ts = #{ts}
			</if>
		</set>
		where user_id = #{userId}
	</update>

	<select id="getByAchieveId" parameterType="String" resultType="WorkStaffBasicInfo">
		select w.*
		from work_staff_basic_info w left join work_staff_achives a
		on w.user_id = a.user_id
		where a.id = #{achieveId}
	</select>

	<update id="updateJobGradeByUserId">
		update work_staff_basic_info
		set job_grade = #{jobGradeId}
		where user_id = #{userId}
	</update>
	<update id="updateOfficeByUserId">
		update work_staff_basic_info
		set office_id = #{officeId}
		where user_id = #{userId}
	</update>

</mapper>