<?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.worksinglewage.dao.WorkSingleWageDao">
    
	<sql id="workSingleWageColumns">
		a.id AS "id",
		a.process_instance_id AS "processInstanceId",
		a.company_id AS "companyId",
		a.status AS "status",
		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.name AS "name",
		a.month AS "month",
		a.wage_id AS "wageId",
		a.money AS "money",
		a.branch_count AS "branchCount",
		a.grant_count AS "grantCount",
		a.now_month AS "nowMonth",
		a.branch_office AS "branchOffice",
		o.name AS "office.name",
		u.name AS "createBy.name",
		r.id AS "workSalaryItem.id",
		r.name AS "workSalaryItem.name"
	</sql>
	
	<sql id="workSingleWageJoins">
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		LEFT JOIN sys_user u ON u.id = a.create_by
		LEFT JOIN work_salary_item r ON r.id = a.wage_id
	</sql>
	
    
	<select id="get" resultType="WorkSingleWage" >
		SELECT 
			<include refid="workSingleWageColumns"/>
		FROM work_single_wage a
		<include refid="workSingleWageJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkSingleWage" >
		SELECT 
			<include refid="workSingleWageColumns"/>
		FROM work_single_wage a
		<include refid="workSingleWageJoins"/>
		<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="startDate != null and startDate != ''">
				AND a.create_date &gt;= #{startDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND a.create_date &lt;= #{endDate}
			</if>
			<if test="minmoney != null and minmoney != ''">
				AND a.money &gt;= #{minmoney}
			</if>
			<if test="maxmoney != null and maxmoney != ''">
				AND a.money &lt;= #{maxmoney}
			</if>
			<if test="workSalaryItem != null and workSalaryItem.id != null and workSalaryItem.id != ''">
				AND a.wage_id = #{workSalaryItem.id}
			</if>
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			<if test="branchOffice != null and branchOffice != ''">
				AND a.branch_office = #{branchOffice}
			</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="WorkSingleWage" >
		SELECT 
			<include refid="workSingleWageColumns"/>
		FROM work_single_wage a
		<include refid="workSingleWageJoins"/>
		<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>

	<!-- 获取流程ID -->
	<select id="getByProcessInstanceId" resultType="WorkSingleWage">
		SELECT
		<include refid="workSingleWageColumns"/>
		FROM work_single_wage a
		<include refid="workSingleWageJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>

	<update id="updateProcessInstanceId">
		UPDATE work_single_wage SET
		process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>
	
	<insert id="insert">
		INSERT INTO work_single_wage(
			id,
			company_id,
			status,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			office_id,
			name,
			month,
			wage_id,
			money,
			branch_count,
			now_month,
			branch_office,
			grant_count
		) VALUES (
			#{id},
			#{companyId},
			#{status},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{office.id},
			#{name},
			#{month},
			#{workSalaryItem.id},
			#{money},
			#{branchCount},
			#{nowMonth},
			#{branchOffice},
			#{grantCount}
		)
	</insert>
	
	<update id="update">
		UPDATE work_single_wage SET 	
			company_id = #{companyId},
			status = #{status},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			office_id = #{office.id},
			name = #{name},
			month = #{month},
			wage_id = #{workSalaryItem.id},
			money = #{money},
			branch_count = #{branchCount},
			now_month = #{nowMonth},
			branch_office = #{branchOffice},
			grant_count = #{grantCount}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_single_wage
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_single_wage SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkSingleWage" statementType="STATEMENT">
		select * FROM work_single_wage  where ${propertyName} = '${value}'
	</select>

</mapper>