<?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.workperson.dao.WorkPersonDao">
    
	<sql id="workPersonColumns">
		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.company_id AS "companyId",
		a.office_id AS "officeId",
		a.basic_id AS "basicId"
	</sql>
	
	<sql id="workPersonJoins">
	</sql>
	
    
	<select id="get" resultType="WorkPerson" >
		SELECT 
			<include refid="workPersonColumns"/>
		FROM work_person a
		<include refid="workPersonJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkPerson" >
		SELECT 
			<include refid="workPersonColumns"/>
		,ah.id AS "workStaffBasicInfo.id",
		ah.create_by AS "workStaffBasicInfo.createBy.id",
		ah.create_date AS "workStaffBasicInfo.createDate",
		ah.update_by AS "workStaffBasicInfo.updateBy.id",
		ah.update_date AS "workStaffBasicInfo.updateDate",
		ah.remarks AS "workStaffBasicInfo.remarks",
		ah.del_flag AS "workStaffBasicInfo.delFlag",
		ah.office_id AS "workStaffBasicInfo.office.id",
		ah.company_id AS "workStaffBasicInfo.company.id",
		ah.no AS "workStaffBasicInfo.no",
		ah.dimission_date AS "workStaffBasicInfo.dimissionDate",
		ah.workEnd_date AS "workStaffBasicInfo.workEndDate",
		ah.name AS "workStaffBasicInfo.name",
		ah.gender AS "workStaffBasicInfo.gender",
		ah.nation AS "workStaffBasicInfo.nation",
		ah.id_card AS "workStaffBasicInfo.idCard",
		ah.age AS "workStaffBasicInfo.age",
		ah.birthday AS "workStaffBasicInfo.birthday",
		ah.calendar AS "workStaffBasicInfo.calendar",
		ah.email AS "workStaffBasicInfo.email",
		ah.mobile AS "workStaffBasicInfo.mobile",
		ah.marriage AS "workStaffBasicInfo.marriage",
		ah.politics AS "workStaffBasicInfo.politics",
		ah.ex_soldier AS "workStaffBasicInfo.exSoldier",
		ah.address AS "workStaffBasicInfo.address",
		ah.native_place AS "workStaffBasicInfo.nativePlace.id",
		ah.household AS "workStaffBasicInfo.household",
		ah.duty AS "workStaffBasicInfo.duty",
		ah.job_grade AS "workStaffBasicInfo.jobGrade.id",
		ah.status AS "workStaffBasicInfo.status",
		ah.entry_date AS "workStaffBasicInfo.entryDate",
		ah.work_date AS "workStaffBasicInfo.workDate",
		ah.bank_name AS "workStaffBasicInfo.bankName",
		ah.bank_card AS "workStaffBasicInfo.bankCard",
		ah.industry_date AS "workStaffBasicInfo.industryDate",
		ah.ts AS "workStaffBasicInfo.ts",
		ah.picture AS "workStaffBasicInfo.picture",
		o.name AS "workStaffBasicInfo.office.name",
		o.top_company AS "workStaffBasicInfo.office.topCompany",
		ah.user_id AS "workStaffBasicInfo.userId",
		ah.branch_office AS "workStaffBasicInfo.branchOffice.id",
		branch.name AS "workStaffBasicInfo.branchOffice.name",
		s.name AS "workStaffBasicInfo.company.name",
		jg.name AS "workStaffBasicInfo.jobGrade.name",
		jg.pay AS "workStaffBasicInfo.salary",
		nativePlace.name AS "workStaffBasicInfo.nativePlace.name",
		ah.percent AS "workStaffBasicInfo.percent",
		ah.try_end_time AS "workStaffBasicInfo.tryEndTime"

		FROM work_person a
		LEFT JOIN work_staff_achives ah on a.basic_id = ah.id
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office branch ON branch.id = ah.branch_office
		LEFT JOIN sys_office s ON s.id = a.company_id
		LEFT JOIN sys_user u ON u.id = ah.create_by
		LEFT JOIN sys_area nativePlace ON nativePlace.id = ah.native_place
		LEFT JOIN work_job_grade jg ON jg.id = ah.job_grade

		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="workStaffBasicInfo!=null and workStaffBasicInfo.office != null and workStaffBasicInfo.office.id != null and workStaffBasicInfo.office.id != ''">
				AND ah.office_id = #{workStaffBasicInfo.office.id}
			</if>
			<if test="workStaffBasicInfo!=null and workStaffBasicInfo.no != null and workStaffBasicInfo.no != ''">
				AND ah.no = #{workStaffBasicInfo.no}
			</if>
			<if test="workStaffBasicInfo!=null and workStaffBasicInfo.name != null and workStaffBasicInfo.name != ''">
				AND ah.name LIKE
				<if test="dbName == 'oracle'">'%'||#{workStaffBasicInfo.name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{workStaffBasicInfo.name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{workStaffBasicInfo.name},'%')</if>
			</if>
			<if test="workStaffBasicInfo!=null and workStaffBasicInfo.idCard != null and workStaffBasicInfo.idCard != ''">
				AND ah.id_card = #{workStaffBasicInfo.idCard}
			</if>
			<if test="workStaffBasicInfo!=null and workStaffBasicInfo.gender != null and workStaffBasicInfo.gender !=''">
				AND ah.gender = #{workStaffBasicInfo.gender}
			</if>
			<if test="sqlMap.dsf !=null and sqlMap.dsf !=''">
				AND ((ah.user_id = #{currentUser.id} AND ah.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.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="WorkPerson" >
		SELECT 
			<include refid="workPersonColumns"/>
		FROM work_person a
		<include refid="workPersonJoins"/>
		<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_person(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			company_id,
			office_id,
			basic_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{companyId},
			#{officeId},
			#{basicId}
		)
	</insert>
	
	<update id="update">
		UPDATE work_person SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			company_id = #{companyId},
			office_id = #{officeId},
			basic_id = #{basicId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_person
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_person SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkPerson" statementType="STATEMENT">
		select * FROM work_person  where ${propertyName} = '${value}'
	</select>
	
</mapper>