<?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.yifu.cloud.plus.v1.yifu.salary.mapper.SysMessageSalaryMapper">
    <resultMap id="sysMessageSalaryMap" type="com.yifu.cloud.plus.v1.yifu.salary.entity.SysMessageSalary">
        <id property="id" column="id"/>
        <result property="actualSalary" column="actual_salary"/>
        <result property="salaryBase" column="salary_base"/>
        <result property="salaryMonth" column="salary_month"/>
        <result property="settleMonth" column="settle_month"/>
        <result property="employeeId" column="employee_id"/>
        <result property="employeeName" column="employee_name"/>
        <result property="employeeIdnum" column="employee_idnum"/>
        <result property="salaryMakeMan" column="salary_make_man"/>
        <result property="province" column="province"/>
        <result property="city" column="city"/>
        <result property="town" column="town"/>
        <result property="departId" column="DEPART_ID"/>
        <result property="departName" column="DEPART_NAME"/>
        <result property="departNo" column="DEPART_NO"/>
        <result property="type" column="TYPE"/>
        <result property="customerId" column="CUSTOMER_ID"/>
        <result property="customerName" column="CUSTOMER_NAME"/>
		<result property="reasonType" column="REASON_TYPE"/>
		<result property="feedBack" column="FEED_BACK"/>
    </resultMap>
	<resultMap id="sysMessageSalaryExportMap" type="com.yifu.cloud.plus.v1.yifu.salary.vo.SysMessageSalaryExportVo">
		<result property="actualSalary" column="actual_salary"/>
		<result property="salaryBase" column="salary_base"/>
		<result property="salaryMonth" column="salary_month"/>
		<result property="employeeName" column="employee_name"/>
		<result property="employeeIdnum" column="employee_idnum"/>
		<result property="province" column="province"/>
		<result property="city" column="city"/>
		<result property="town" column="town"/>
		<result property="departName" column="DEPART_NAME"/>
		<result property="customerName" column="CUSTOMER_NAME"/>
	</resultMap>
    <sql id="Base_Column_List">
		a.id,
		a.actual_salary,
		a.relay_salary,
		a.salary_base,
		a.salary_month,
		a.settle_month,
		a.employee_id,
		a.employee_name,
		a.employee_idnum,
		a.salary_make_man,
		a.province,
		a.city,
		a.town,
		a.DEPART_ID,
		a.DEPART_NAME,
		a.DEPART_NO,
		a.type,
		a.FEED_BACK,
		a.CUSTOMER_NAME,
		a.CUSTOMER_ID,
		a.REASON_TYPE,
		a.CREATE_BY,
		a.CREATE_NAME
		,a.CREATE_NAME,
		a.CREATE_TIME
         </sql>
    <sql id="sysMessageSalary_where">
        <if test="sysMessageSalary != null">
            <if test="sysMessageSalary.id != null and sysMessageSalary.id.trim() != ''">
                AND a.id = #{sysMessageSalary.id}
            </if>
            <if test="sysMessageSalary.actualSalary != null">
                AND a.actual_salary = #{sysMessageSalary.actualSalary}
            </if>
			<if test="sysMessageSalary.createName != null">
				AND a.CREATE_NAME = #{sysMessageSalary.createName}
			</if>
            <if test="sysMessageSalary.salaryBase != null">
                AND a.salary_base = #{sysMessageSalary.salaryBase}
            </if>
            <if test="sysMessageSalary.salaryMonth != null and sysMessageSalary.salaryMonth.trim() != ''">
                AND a.salary_month = #{sysMessageSalary.salaryMonth}
            </if>
            <if test="sysMessageSalary.settleMonth != null and sysMessageSalary.settleMonth.trim() != ''">
                AND a.settle_month = #{sysMessageSalary.settleMonth}
            </if>
            <if test="sysMessageSalary.employeeId != null and sysMessageSalary.employeeId.trim() != ''">
                AND a.employee_id = #{sysMessageSalary.employeeId}
            </if>
            <if test="sysMessageSalary.employeeName != null and sysMessageSalary.employeeName.trim() != ''">
                AND a.employee_name = #{sysMessageSalary.employeeName}
            </if>
            <if test="sysMessageSalary.employeeIdnum != null and sysMessageSalary.employeeIdnum.trim() != ''">
                AND a.employee_idnum = #{sysMessageSalary.employeeIdnum}
            </if>
            <if test="sysMessageSalary.salaryMakeMan != null and sysMessageSalary.salaryMakeMan.trim() != ''">
                AND a.salary_make_man = #{sysMessageSalary.salaryMakeMan}
            </if>
            <if test="sysMessageSalary.province != null">
                AND a.province = #{sysMessageSalary.province}
            </if>
            <if test="sysMessageSalary.city != null">
                AND a.city = #{sysMessageSalary.city}
            </if>
            <if test="sysMessageSalary.town != null">
                AND a.town = #{sysMessageSalary.town}
            </if>
            <if test="sysMessageSalary.departId != null and sysMessageSalary.departId.trim() != ''">
                AND a.DEPART_ID = #{sysMessageSalary.departId}
            </if>
            <if test="sysMessageSalary.departName != null and sysMessageSalary.departName.trim() != ''">
                AND a.DEPART_NAME = #{sysMessageSalary.departName}
            </if>
            <if test="sysMessageSalary.departNo != null and sysMessageSalary.departNo.trim() != ''">
                AND a.DEPART_NO LIKE CONCAT('%',    #{sysMessageSalary.departNo},'%')
            </if>
            <if test="sysMessageSalary.type != null">
                AND a.type = #{sysMessageSalary.type}
            </if>
            <if test="sysMessageSalary.customerId != null and sysMessageSalary.customerId.trim() != ''">
                AND a.CUSTOMER_ID = #{sysMessageSalary.customerId}
            </if>
            <if test="sysMessageSalary.customerName != null and sysMessageSalary.customerName.trim() != ''">
                AND a.CUSTOMER_NAME LIKE CONCAT('%',   #{sysMessageSalary.customerName},'%')
            </if>
        </if>
    </sql>
    <!--sysMessageSalary简单分页查询-->
    <select id="getSysMessageSalaryPage" resultMap="sysMessageSalaryMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys_message_salary a
        left join t_feed_back b  on  a.ID=b.RELATE_ID
        <where>
			<include refid="sysMessageSalarySql"/>
			<include refid="sysMessageSalary_where"/>
			<if test="sysMessageSalary.authSql != null and sysMessageSalary.authSql.trim() != ''">
				${sysMessageSalary.authSql}
			</if>
		  order by  a.CREATE_TIME desc
        </where>
    </select>

    <!--sysMessageSalary简单分页查询-->
    <select id="getSysMessageSalaryHandlePage" resultMap="sysMessageSalaryMap">
        SELECT
        <include refid="Base_Column_List"/>,
        b.*
        FROM sys_message_salary a
        left join t_feed_back b  on  a.ID=b.RELATE_ID
        <where>
            1=1 and ((b.IGNORE_FLAG=1 and b.TYPE=2) or (b.id is null))
            <include refid="sysMessageSalary_where"/>
        </where>
    </select>

    <sql id="sysMessageSalarySql">
        1=1 and ((b.IGNORE_FLAG=1 and b.TYPE=2) or (b.id is null))
        <!--数据权限判断-->
        <if test="settleDomainVos != null">
            <if test="settleDomainVos.size() > 0">
                and a.DEPART_ID in
                <foreach collection="settleDomainVos" item="param" index="index" open="(" close=")" separator=",">
                    #{param.id}
                </foreach>
            </if>
            <if test="settleDomainVos.size() == 0">
                and 1=2
            </if>
        </if>
        <if test="settleDomainVos == null">
            and 1=1
        </if>
    </sql>

    <!--普通工资-->
    <select id="insertSalaryBySettleMonth" resultType="com.yifu.cloud.plus.v1.yifu.salary.entity.SysMessageSalary">
        SELECT
            id,
            actual_salary,
            relay_salary,
            salary_base,
            salary_month,
            settle_month,
            employee_id,
            employee_name,
            employee_idnum,
            create_by,
            province,
            city,
            town,
			DEPT_ID as DEPART_ID,
			DEPT_NAME as DEPART_NAME,
			DEPT_NO as DEPART_NO,
            0 as TYPE,
			CREATE_NAME,
			CREATE_TIME
            FROM
                (
                    SELECT
                        a.ID,
                        a.EMP_ID employee_id,
                        a.EMP_IDCARD employee_idnum,
                        a.EMP_NAME employee_name,
						s.DEPT_ID,
						s.DEPT_NAME,
						s.DEPT_NO,
                        a.SALARY_MONTH salary_month,
                        a.SETTLEMENT_MONTH settle_month,
                        sum(i.SALARY_MONEY) AS actual_salary,
                        sum(l.SALARY_MONEY) AS relay_Salary,
						s.CREATE_BY AS CREATE_BY,
						s.CREATE_NAME AS CREATE_NAME,
						s.CREATE_TIME AS CREATE_TIME,
                        min(IFNULL(t.salary_base,c.salary_base)) AS salary_base,
                      a.PROVINCE,
                      a.CITY,
                      a.TOWN
                    FROM
                        t_salary_account a
                    LEFT JOIN t_salary_standard s ON s.id = a.SALARY_FORM_ID
                    LEFT JOIN t_min_salary t ON t.TOWN = a.TOWN AND t.town IS NOT NULL
                    LEFT JOIN t_min_salary c ON c.city = a.city AND c.town IS NULL
                    LEFT JOIN t_salary_account_item i ON i.SALARY_ACCOUNT_ID = a.id and i.JAVA_FIED_NAME = 'actualSalarySum'
                    LEFT JOIN t_salary_account_item l ON l.SALARY_ACCOUNT_ID = a.id and l.JAVA_FIED_NAME = 'relaySalary'
                    WHERE
					(t.`STATUS`=1 or t.id is null) and (c.`STATUS`=1 or c.id is null) and
                    a.DELETE_FLAG = 0
					and a.FORM_TYPE='0'
                    and	a.SALARY_MONTH = #{settleMonth}
                    AND s.STATUS IN (3, 4)
                    GROUP BY a.EMP_IDCARD,a.SALARY_MONTH
                ) b
            WHERE b.salary_base > b.actual_salary
    </select>
    <!--工程工资-->
    <select id="insertEngineerBySettleMonth" resultType="com.yifu.cloud.plus.v1.yifu.salary.entity.SysMessageSalary">
        SELECT
            id,
            actual_salary,
            relay_salary,
            salary_base,
            salary_month,
            settle_month,
            employee_id,
            employee_name,
            employee_idnum,
            salary_make_man,
            province,
            city,
            town,
            DEPART_ID,
            DEPART_NAME,
            DEPART_NO,
            1 as TYPE
            FROM
                (
                    SELECT
                        a.id,
                        a.EMP_ID employee_id,
                        a.EMP_IDCARD employee_idnum,
                        a.EMP_NAME employee_name,
                        s.DEPART_ID,
                        s.DEPART_NAME,
                        s.DEPART_NO,
                        a.SALARY_DATE salary_month,
                        a.SETTLEMENT_MONTH settle_month,
                        sum(i.SALARY_MONEY) AS actual_salary,
                        sum(l.SALARY_MONEY) AS relay_Salary,
                        s.CREATE_USER AS salary_make_man,
                        min(IFNULL(t.salary_base,c.salary_base)) AS salary_base,
                      a.PROVINCE,
                      a.CITY,
                      a.TOWN
                    FROM
                        t_engineer_account a
                    LEFT JOIN t_salary_engineering s ON s.id = a.SALARY_FORM_ID
                    LEFT JOIN t_min_salary t ON t.TOWN = a.TOWN AND t.town IS NOT NULL
                    LEFT JOIN t_min_salary c ON c.city = a.city AND c.town IS NULL
                    LEFT JOIN t_engineer_account_item i ON i.SALARY_ACCOUNT_ID = a.id and i.JAVA_FIED_NAME = 'actualSalarySum'
                    LEFT JOIN t_engineer_account_item l ON l.SALARY_ACCOUNT_ID = a.id and l.JAVA_FIED_NAME = 'relaySalary'
                    WHERE
                    a.DELETE_FLAG = 0
                    and	a.SETTLEMENT_MONTH = #{settleMonth}
                    AND s.STATUS IN (3, 4)
                    GROUP BY a.EMP_IDCARD,a.SALARY_DATE,a.DEPT_ID
                ) b
            WHERE b.salary_base > b.actual_salary
    </select>

    <!--生成前，先删除老数据-->
    <delete id="deleteAllBySettleMonth">
        delete from sys_message_salary where SALARY_MONTH = #{settleMonth}
    </delete>


	<!--noPageDiy查询-->
	<select id="noPageDiy" resultMap="sysMessageSalaryExportMap">
		SELECT
		<include refid="Base_Column_List"/>,
		b.*
		FROM sys_message_salary a
		left join t_feed_back b  on  a.ID=b.RELATE_ID
		<where>
			1=1 and ((b.IGNORE_FLAG=1 and b.TYPE=2) or (b.id is null))
			<include refid="export_sql"/>
			<if test="sysMessageSalary != null">
				<if test="sysMessageSalary.authSql != null and sysMessageSalary.authSql.trim() != ''">
					${sysMessageSalary.authSql}
				</if>
				<if test="sysMessageSalary.limitStart != null">
					limit #{sysMessageSalary.limitStart},#{sysMessageSalary.limitEnd}
				</if>
			</if>
		</where>
	</select>

	<!--noPageCountDiy查询-->
	<select id="noPageCountDiy" resultType="java.lang.Integer">
		SELECT
		count(1)
		FROM sys_message_salary a
		left join t_feed_back b  on  a.ID=b.RELATE_ID
		<where>
			1=1 and ((b.IGNORE_FLAG=1 and b.TYPE=2) or (b.id is null))
			<include refid="export_sql"/>
			<if test="sysMessageSalary.authSql != null and sysMessageSalary.authSql.trim() != ''">
				${sysMessageSalary.authSql}
			</if>
		</where>
	</select>
	<sql id="export_sql">
		<include refid="sysMessageSalary_where"/>
		<if test="idsStr != null and idsStr.size > 0">
			AND a.ID in
			<foreach item="items" index="index" collection="idsStr" open="(" separator="," close=")">
				#{items}
			</foreach>
		</if>
		<!--数据权限判断-->
		<if test="settleDomainIds != null">
			<if test="settleDomainIds.size() > 0">
				and a.DEPART_ID in
				<foreach collection="settleDomainIds" item="param" index="index" open="(" close=")" separator=",">
					#{param}
				</foreach>
			</if>
			<if test="settleDomainIds.size() == 0">
				and 1=2
			</if>
		</if>
		<if test="settleDomainIds == null">
			and 1=1
		</if>
		<if test='sql != null and sql != ""'>
			and <![CDATA[sql]]>
		</if>
	</sql>

</mapper>
