<?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.smartfactory.dal.mysql.staff.StaffMapper">

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

    <select id="getStaffData" resultType="cn.iocoder.yudao.module.smartfactory.dal.dataobject.staff.StaffDO">
        select
            a.*,
            t3.label AS postName
        from sf_staff as a
        LEFT JOIN system_dict_data t3 ON
            t3.dict_type = 'user_work_type'
            AND a.work_type_id = t3.value
            AND t3.deleted = 0
        <where>
            a.status = 1
            and a.deleted = 0
            and a.factory_id != 1
            <if test="factoryId != null">
                and a.factory_id = #{factoryId}
            </if>
        </where>
    </select>
    <select id="getStaffDataV2" resultType="cn.iocoder.yudao.module.smartfactory.dal.dataobject.staff.StaffDO">
        SELECT
        a.id as id,
        a.id as staffNo,
        a.nickname as nickName,
        a.age as age,
        a.sex as sex,
        ext.work_type as workType,
        b.factory_id as factoryId,
        a.idcard as idCard,
        a.status as status
        FROM
        system_users AS a
        LEFT JOIN system_users_ext AS ext ON a.id = ext.user_id
        LEFT JOIN system_dept AS b ON a.dept_id = b.id
        LEFT JOIN sf_factory_info AS c ON b.factory_id = c.id
        <where>
            a.user_type = 2
            and a.deleted = 0
            <if test="factoryId != null">
                and b.factory_id = #{factoryId}
            </if>
        </where>
    </select>

    <select id="selectStaffByUserId" resultType="cn.iocoder.yudao.module.smartfactory.dal.dataobject.staff.StaffDO">
        SELECT
            a.*
        FROM sf_staff a
        JOIN system_users u ON
            u.id = #{userId}
            AND u.idcard = a.id_card
            AND u.deleted = 0
--         JOIN system_dept d ON
--             d.id = u.dept_id
--             AND d.factory_id = a.factory_id
--             AND d.deleted = 0
        WHERE
            a.status = 1
            and a.deleted = 0
    </select>
</mapper>
