<?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.web.work.mapper.CompanyMapper">
    
    <select id="selectList" resultType="com.ruoyi.web.work.domain.Company">
        SELECT
        c.id,
        c.company_name,
        c.bank_name,
        c.bank_account,
        c.address,
        c.director,
        c.money,
        c.phone,
        c.rate,
        c.create_time,
        c.state,
        c.email,
        ( SELECT COUNT( r.id ) FROM tb_relate r WHERE r.company_id = c.id ) AS peoples,
        ( SELECT SUM( r.money ) FROM tb_statement r WHERE r.company_id = c.id AND r.give=2) AS giveMoney,
        ( SELECT SUM( r.service_money ) FROM tb_statement r WHERE r.company_id = c.id AND r.give=2) AS serviceMoney,
        ( SELECT SUM( r.peoples ) FROM tb_statement r WHERE r.company_id = c.id AND r.give=2) AS givePeoples
        FROM
        tb_company c
        <where>  
            <if test="companyName != null  and companyName != ''"> and c.company_name like concat('%', #{companyName}, '%')</if>
            <if test="director != null  and director != ''"> and c.director like concat('%', #{director}, '%')</if>
            <if test="phone != null  and phone != ''"> and c.phone = #{phone}</if>
            <if test="state != null "> and c.state = #{state}</if>
            <if test="dateBegin != null  and dateBegin != ''"> AND c.create_time BETWEEN #{dateBegin} AND #{dateEnd} + INTERVAL 1 DAY</if>
        </where>
    </select>

    <select id="selectCompanyList" resultType="com.ruoyi.web.work.domain.vo.CompanyListVo">
        SELECT * FROM tb_company
        WHERE state=0
        <if test="companyName != null  and companyName != ''">AND company_name like concat('%', #{companyName}, '%')</if>
        ORDER BY id DESC
    </select>

    <select id="selectCompanyById" resultType="com.ruoyi.web.work.domain.Company">
        SELECT
            c.*,
            u.user_name AS userName
        FROM
            tb_company c
                LEFT JOIN sys_user u ON u.company_id = c.id
        WHERE
            c.id =#{id}
          AND u.create_by = ''
    </select>

</mapper>