<?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.ruoyi.system.mapper.DepartmentMapper">

    <resultMap type="com.ruoyi.system.domain.Department" id="DepartmentMap">
        <result property="id" column="Id" jdbcType="INTEGER"/>
        <result property="name" column="Name" jdbcType="VARCHAR"/>
        <result property="codeid" column="CodeId" jdbcType="INTEGER"/>
        <result property="company" column="Company" jdbcType="VARCHAR"/>
        <result property="companyid" column="CompanyId" jdbcType="INTEGER"/>
        <result property="price" column="Price" jdbcType="NUMERIC"/>
        <result property="prefix" column="Prefix" jdbcType="VARCHAR"/>
        <result property="para1" column="Para1" jdbcType="VARCHAR"/>
        <result property="para2" column="Para2" jdbcType="VARCHAR"/>
        <result property="para3" column="Para3" jdbcType="VARCHAR"/>
        <result property="para4" column="Para4" jdbcType="VARCHAR"/>
        <result property="para5" column="Para5" jdbcType="VARCHAR"/>
        <result property="para6" column="Para6" jdbcType="VARCHAR"/>
        <result property="para7" column="Para7" jdbcType="VARCHAR"/>
        <result property="para8" column="Para8" jdbcType="VARCHAR"/>
        <result property="para9" column="Para9" jdbcType="VARCHAR"/>
        <result property="para10" column="Para10" jdbcType="VARCHAR"/>
    </resultMap>
    <resultMap id="vUserDepartmentMap" type="com.ruoyi.system.domain.dto.DepartMentDto">
        <result property="companyId" column="CompanyId" jdbcType="INTEGER"/>
        <result property="departmentId" column="DepartmentId" jdbcType="INTEGER"/>
        <result property="company" column="Company" jdbcType="VARCHAR"/>
        <result property="name" column="Name" jdbcType="VARCHAR"/>
        <result property="personName" column="personName" jdbcType="VARCHAR"/>
        <result property="dutyId" column="dutyId" jdbcType="INTEGER"/>
        <result property="location" column="Localtion" jdbcType="VARCHAR"/>
        <result property="telPhone" column="Telefon" jdbcType="VARCHAR"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultMap="DepartmentMap">
        select Id,
               Name,
               CodeId,
               Company,
               CompanyId,
               Price,
               Prefix,
               Para1,
               Para2,
               Para3,
               Para4,
               Para5,
               Para6,
               Para7,
               Para8,
               Para9,
               Para10

        from Department
        where Id = #{id}
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="DepartmentMap">
        select
        Id,
        Name,
        CodeId,
        Company,
        CompanyId,
        Price,
        Prefix,
        Para1,
        Para2,
        Para3,
        Para4,
        Para5,
        Para6,
        Para7,
        Para8,
        Para9,
        Para10

        from Department
        <where>
            <if test="id != null">
                and Id = #{id}
            </if>
            <if test="name != null and name != ''">
                and Name = #{name}
            </if>
            <if test="codeid != null">
                and CodeId = #{codeid}
            </if>
            <if test="company != null and company != ''">
                and Company = #{company}
            </if>
            <if test="companyid != null">
                and CompanyId = #{companyid}
            </if>
            <if test="price != null">
                and Price = #{price}
            </if>
            <if test="prefix != null and prefix != ''">
                and Prefix = #{prefix}
            </if>
            <if test="para1 != null and para1 != ''">
                and Para1 = #{para1}
            </if>
            <if test="para2 != null and para2 != ''">
                and Para2 = #{para2}
            </if>
            <if test="para3 != null and para3 != ''">
                and Para3 = #{para3}
            </if>
            <if test="para4 != null and para4 != ''">
                and Para4 = #{para4}
            </if>
            <if test="para5 != null and para5 != ''">
                and Para5 = #{para5}
            </if>
            <if test="para6 != null and para6 != ''">
                and Para6 = #{para6}
            </if>
            <if test="para7 != null and para7 != ''">
                and Para7 = #{para7}
            </if>
            <if test="para8 != null and para8 != ''">
                and Para8 = #{para8}
            </if>
            <if test="para9 != null and para9 != ''">
                and Para9 = #{para9}
            </if>
            <if test="para10 != null and para10 != ''">
                and Para10 = #{para10}
            </if>
        </where>
        limit #{pageable.offset}, #{pageable.pageSize}
    </select>

    <!--统计总行数-->
    <select id="count" resultType="java.lang.Long">
        select count(1)
        from Department
        <where>
            <if test="id != null">
                and Id = #{id}
            </if>
            <if test="name != null and name != ''">
                and Name = #{name}
            </if>
            <if test="codeid != null">
                and CodeId = #{codeid}
            </if>
            <if test="company != null and company != ''">
                and Company = #{company}
            </if>
            <if test="companyid != null">
                and CompanyId = #{companyid}
            </if>
            <if test="price != null">
                and Price = #{price}
            </if>
            <if test="prefix != null and prefix != ''">
                and Prefix = #{prefix}
            </if>
            <if test="para1 != null and para1 != ''">
                and Para1 = #{para1}
            </if>
            <if test="para2 != null and para2 != ''">
                and Para2 = #{para2}
            </if>
            <if test="para3 != null and para3 != ''">
                and Para3 = #{para3}
            </if>
            <if test="para4 != null and para4 != ''">
                and Para4 = #{para4}
            </if>
            <if test="para5 != null and para5 != ''">
                and Para5 = #{para5}
            </if>
            <if test="para6 != null and para6 != ''">
                and Para6 = #{para6}
            </if>
            <if test="para7 != null and para7 != ''">
                and Para7 = #{para7}
            </if>
            <if test="para8 != null and para8 != ''">
                and Para8 = #{para8}
            </if>
            <if test="para9 != null and para9 != ''">
                and Para9 = #{para9}
            </if>
            <if test="para10 != null and para10 != ''">
                and Para10 = #{para10}
            </if>
        </where>
    </select>
    <select id="getDepartments" resultMap="vUserDepartmentMap">
        SELECT
        vudmt.CompanyId,vudmt.DepartmentId,vudmt.Company,vudmt.Name, mmb.Name as personName,vudmt.AccountId AS
        dutyId,(SELECT TOP 1 Name FROM Location WHERE CompanyId = vudmt.CompanyId ORDER BY Id DESC)AS Localtion,
        mmb.Telefon
        FROM
        v_UserDepartment AS vudmt
        LEFT JOIN Member AS mmb ON mmb.Id = vudmt.AccountId
        <where>
            AND vudmt.CompanyId IS NOT NULL
            AND vudmt.ParentId IS NOT NULL
            AND mmb.Name IS NOT NULL
            <if test="departmentName != null and departmentName != ''">
                and vudmt.Name LIKE '%'+#{departmentName}+'%'
            </if>
        </where>
        order by vudmt.DepartmentId DESC
    </select>

    <select id="getDepartmentsV2" resultType="Map">
        SELECT d.Id AS departmentId, d.Name AS departmentName, d.CompanyId AS companyId, d.Company AS company,
               d.Address AS location, d.Para10 AS photo
        FROM Department d
        <where>
            <if test="departmentName != null and departmentName != ''">
                AND d.Name LIKE '%'+#{departmentName}+'%'
            </if>
        </where>
        ORDER BY d.Id DESC
    </select>

    <select id="getMemberByDepartment" resultType="Map">
        SELECT m.Id AS memberId, m.Name AS memberName, m.Telefon AS phone
        FROM Member m
        WHERE m.DepartmentId = #{departmentId}
        ORDER BY m.Name ASC
    </select>

    <select id="getLocationByCompany" resultType="Map">
        SELECT TOP 1 l.Id AS locationId, l.Name AS locationName
        FROM Location l
        WHERE l.CompanyId = #{companyId}
        ORDER BY l.Id DESC
    </select>

    <select id="getDepartmentAssetNum" resultType="Long">
        SELECT COUNT (1) AS total
        FROM AssetLog
        WHERE AssetType IS NOT NULL AND (SaveDepartmentId = #{departmentId} OR DepartmentId = #{departmentId})
          AND state != 3 AND Guid = 0 AND Gone = 0
        <if test="goodsName != null and goodsName != ''">
            AND Name LIKE '%'+#{goodsName}+'%'
        </if>
        <if test="assetType != null and assetType != ''">
            AND AssetType = #{assetType}
        </if>
    </select>

    <select id="sumDepartmentAssetNum" resultType="Long">
        SELECT SUM(Quantity) AS total
        FROM AssetLog
        WHERE AssetType IS NOT NULL AND (SaveDepartmentId = #{departmentId} OR DepartmentId = #{departmentId})
          AND Guid = 0 AND Gone = 0
        <if test="assetStatus == null">
            AND state != 3
        </if>
        <if test="assetStatus != null">
            AND state = #{assetStatus}
        </if>
        <if test="goodsName != null and goodsName != ''">
            AND Name LIKE '%'+#{goodsName}+'%'
        </if>
        <if test="assetType != null and assetType != ''">
            AND AssetType = #{assetType}
        </if>
    </select>

</mapper>

