<?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.pz.merchant.mapper.CompanyMapper">

    <resultMap type="com.pz.merchant.domain.Company" id="CompanyResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="cityId" column="city_id"/>
        <result property="tel" column="tel"/>
        <result property="phone" column="phone"/>
        <result property="address" column="address"/>
        <result property="status" column="status"/>
        <result property="businessLicense" column="business_license"/>
        <result property="isCashDeposit" column="is_cash_deposit"/>
        <result property="cashDeposit" column="cash_deposit"/>
        <result property="uid" column="uid"/>
        <result property="totalRevenue" column="total_revenue"/>
        <result property="balance" column="balance"/>
        <result property="freezeBalance" column="freeze_balance"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="delFlag" column="del_flag"/>
    </resultMap>

    <select id="selectCompanyDataForApp" resultType="com.pz.merchant.domain.vo.CompanyAppVo">
        select company.name,
               company.phone,
               company.address,
               company.status,
               company.business_license,
               user.avatar
        from company
                 left join sys_user as user on user.user_id = company.uid
        where company.id = #{id}
    </select>

    <select id="selectFinanceStatistic" resultType="java.math.BigDecimal">
        select ifnull(sum(ifnull(divide_price, 0)),0)
        from employees_divide
        where company_id = #{id} and to_days(current_date) = to_days(create_time);
    </select>

    <select id="selectAllOrder" resultType="com.pz.merchant.domain.vo.FinanceOrderInfoVo">
        select total_order.id as orderId,
        total_order.status as orderStatus,
        business.name as businessName,
        services.name as project,
        employees.name as emName,
        services.cover,
        CONVERT((1 - services.fenmo / 100), decimal(10, 2)) *
        CONVERT(services.price, decimal(10, 2)) as commission,
        services.bid as businessId
        from total_order
        left join business on total_order.business_id = business.id
        left join employees on employees.id = total_order.em_id
        left join services on total_order.service_id = services.id
        <where>
            and total_order.business_id != 0
            <if test="ew.emptyOfWhere == false">
                and ${ew.sqlSegment}
            </if>
        </where>
    </select>
    <select id="selectDataView" resultType="com.pz.system.domain.vo.DataViewVo">
        SELECT count(id) as dataSum
        FROM company
        WHERE `status` = 1
        <if test="dataViewBo.condition == 2">
            and create_time >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) -- 近7天
        </if>
        <if test="dataViewBo.condition == 3">
            and create_time >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) -- 近30天
        </if>
        <if test="dataViewBo.condition == 1">
            and DATE(create_time) = CURDATE()
        </if>
    </select>


</mapper>