<?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="cn.iocoder.yudao.module.bpm.dal.mysql.oa.BpmOALoanMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="selectSumByStaffId" resultType="cn.iocoder.yudao.module.bpm.api.oa.vo.loan.BpmOALoanSumDTO">
        SELECT
            sf_user_id AS sfUserId,
            SUM(total_money) AS totalAmount
        FROM
            bpm_oa_loan
        WHERE
            deleted = 0
            AND result = 2
            AND return_date = #{month}
            AND sf_user_id IN
            <foreach item="item" index="index" collection="staffId" open="(" separator="," close=")">
                #{item}
            </foreach>
        GROUP BY
            sf_user_id
    </select>

    <select id="selectReturnList" resultType="cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.loan.BpmOAReturnVO">
        <if test="pageReqVO.loanType == 1">
            SELECT
            s.staff_id AS staffId,
            st.nick_name AS staffName,
            s.factory_id AS factoryId,
            sf.short_name AS factoryName,
            s.month AS month,
            s.return_amount AS repaymentAmount,
            2 AS type
            FROM
            sf_staff_salary s
            LEFT JOIN sf_staff st ON s.staff_id = st.id
            LEFT JOIN sf_factory_info sf ON s.factory_id = sf.id
            WHERE
            s.staff_id = #{pageReqVO.staffId}
            AND s.return_amount &gt; 0
            AND s.deleted = 0
            AND st.deleted = 0
            AND sf.deleted = 0
        </if>
        <if test="pageReqVO.loanType == 2">
            SELECT
            e.user_id AS staffId,
            u.nickname AS staffName,
            d.factory_id AS factoryId,
            d.name AS factoryName,
            DATE_FORMAT( p.end_time, '%Y-%m' ) AS month,
            e.amount_paid AS repaymentAmount,
            1 AS type
            FROM
            bpm_oa_expenses e
            LEFT JOIN system_users u ON e.user_id = u.id
            LEFT JOIN system_dept d ON u.dept_id = d.id
            LEFT JOIN bpm_process_instance_ext p ON e.process_instance_id = p.process_instance_id
            WHERE
            e.deleted = 0
            AND e.result = 2
            AND e.status = 2
            AND u.deleted = 0
            AND d.deleted = 0
            AND p.deleted = 0
        </if>
    </select>
</mapper>
