CONVERT (IFNULL(common_position,0) / (IFNULL(common_position,0) + common_funding),DECIMAL (15, 2)) AS stockPosition,
CONVERT ((IFNULL(common_position,0) + common_funding),DECIMAL (15, 2)) AS common_total_price,
CONVERT ((IFNULL(common_position,0) + common_funding)-initial_funding,DECIMAL (15, 2)) AS profitLossPrice,
CONVERT (((IFNULL(common_position,0) + common_funding)-initial_funding)/ initial_funding,DECIMAL (15, 4)) AS common_proportion
FROM
(SELECT
a.id,
a.customer_id,
e.`name`,
p.product_name,
p.product_type,
a.initial_funding,
a.status,
a.simulated_market_id as market_id,
a.real_market_id AS real_market_id,
a.simulated_market_id AS simulated_market_id,
a.simulated_funding as common_funding,
a.created_at,
a.expired_at,
(SELECT sum((select recent_closing_price from stocks_code as sc where sc.symbol = b.code ) * count) FROM high_stock_position b WHERE a.simulated_market_id = b.market_id and a.is_deleted='0') AS common_position,
(SELECT count(b.code) FROM high_stock_position b WHERE a.simulated_market_id = b.market_id) AS common_position_count,
(SELECT count(b.code) FROM high_stock_position b WHERE a.simulated_market_id = b.market_id and b.is_deleted = '0') AS
position_count,
(SELECT count(spl.code) FROM high_stock_profit_log spl WHERE a.simulated_market_id = spl.market_id and
spl.profit_price>0) AS profit_count,
e.investment_style,
e.account_limit,
e.created_time
FROM
high_account_product_relation a
INNER JOIN us_member_base e ON a.customer_id = e.id
INNER JOIN high_product p ON a.product_id = p.id
left join us_team_user as t on a.sales_id = t.admin_user_id
<where>
<iftest="salesDirector != null">
AND t.team_leader_id = #{salesDirector}
</if>
<iftest="salesManager != null">
AND t.parent_leader_id = #{salesManager}
</if>
<iftest="analystId != null">
AND p.analyst = #{analystId}
</if>
<iftest="companyId != null">
AND e.company_id = #{companyId}
</if>
<iftest="customerName != null">
AND e.name = #{customerName}
</if>
<iftest="productStatus != null">
AND a.status = #{productStatus}
</if>
<iftest="productId != null">
AND p.id = #{productId}
</if>
<iftest="buyStartTime != null">
AND a.created_at <![CDATA[>=]]> #{buyStartTime}
</if>
<iftest="buyEndTime != null">
AND a.created_at <![CDATA[<=]]> #{buyEndTime}
</if>
<iftest="expStartTime != null">
AND a.expired_at <![CDATA[>=]]> #{expStartTime}
</if>
<iftest="expEndTime != null">
AND a.expired_at <![CDATA[<=]]> #{expEndTime}
</if>
</where>
) f
) g
<where>
<iftest="positionUpper != null">
AND g.stockPosition <![CDATA[<=]]> #{positionUpper}
</if>
<iftest="positionLower != null">
AND g.stockPosition <![CDATA[>=]]> #{positionLower}
</if>
<iftest="profitAndLossUpper != null">
AND g.common_proportion <![CDATA[<=]]> #{profitAndLossUpper}
</if>
<iftest="profitAndLossLower != null">
AND g.common_proportion <![CDATA[>=]]> #{profitAndLossLower}
</if>
<iftest="sumAssetsUpper != null">
AND g.common_total_price <![CDATA[<=]]> #{sumAssetsUpper}
</if>
<iftest="sumAssetsLower != null">
AND g.common_total_price <![CDATA[>=]]> #{sumAssetsLower}
</if>
</where>
union
select * from (select
id,
"real" as accountType,
customer_id,
name,
product_name,
product_type,
status,
market_id,
real_market_id,
simulated_market_id,
common_funding,
initial_funding,
created_at,
expired_at,
common_position,
common_position_count,
position_count,
profit_count,
investment_style,
account_limit,
created_time,
CONVERT (common_position / (common_position + common_funding),DECIMAL (15, 2)) AS stockPosition,
CONVERT ((common_position + common_funding),DECIMAL (15, 2)) AS common_total_price,
CONVERT ((common_position + common_funding)-initial_funding,DECIMAL (15, 2)) AS profitLossPrice,
CONVERT (((common_position + common_funding)-initial_funding)/ initial_funding,DECIMAL (15, 4)) AS common_proportion
FROM
(SELECT
a.id,
a.customer_id,
e.`name`,
p.product_name,
p.product_type,
a.initial_funding,
a.status,
a.real_market_id as market_id,
a.real_market_id AS real_market_id,
a.simulated_market_id AS simulated_market_id,
a.real_funding as common_funding,
a.created_at,
a.expired_at,
(SELECT sum((select recent_closing_price from stocks_code as sc where sc.symbol = b.code ) * count) FROM high_stock_position b WHERE a.real_market_id = b.market_id and a.is_deleted='0') AS common_position,
(SELECT count(b.code) FROM high_stock_position b WHERE a.real_market_id = b.market_id) AS common_position_count,
(SELECT count(b.code) FROM high_stock_position b WHERE a.real_market_id = b.market_id and b.is_deleted = '0') AS
position_count,
(SELECT count(spl.code) FROM high_stock_profit_log spl WHERE a.real_market_id = spl.market_id and
spl.profit_price>0) AS profit_count,
e.investment_style,
e.account_limit,
e.created_time
FROM
high_account_product_relation a
INNER JOIN us_member_base e ON a.customer_id = e.id
INNER JOIN high_product p ON a.product_id = p.id
left join us_team_user as t on a.sales_id = t.admin_user_id
<where>
<iftest="salesDirector != null">
AND t.team_leader_id = #{salesDirector}
</if>
<iftest="salesManager != null">
AND t.parent_leader_id = #{salesManager}
</if>
<iftest="analystId != null">
AND p.analyst = #{analystId}
</if>
<iftest="companyId != null">
AND e.company_id = #{companyId}
</if>
<iftest="customerName != null">
AND e.name = #{customerName}
</if>
<iftest="productStatus != null">
AND a.status = #{productStatus}
</if>
<iftest="productId != null">
AND p.id = #{productId}
</if>
<iftest="buyStartTime != null">
AND a.created_at <![CDATA[>=]]> #{buyStartTime}
</if>
<iftest="buyEndTime != null">
AND a.created_at <![CDATA[<=]]> #{buyEndTime}
</if>
<iftest="expStartTime != null">
AND a.expired_at <![CDATA[>=]]> #{expStartTime}
</if>
<iftest="expEndTime != null">
AND a.expired_at <![CDATA[<=]]> #{expEndTime}
</if>
</where>
) f
) g
<where>
<iftest="positionUpper != null">
AND g.stockPosition <![CDATA[<=]]> #{positionUpper}
</if>
<iftest="positionLower != null">
AND g.stockPosition <![CDATA[>=]]> #{positionLower}
</if>
<iftest="profitAndLossUpper != null">
AND g.common_proportion <![CDATA[<=]]> #{profitAndLossUpper}
</if>
<iftest="profitAndLossLower != null">
AND g.common_proportion <![CDATA[>=]]> #{profitAndLossLower}
</if>
<iftest="sumAssetsUpper != null">
AND g.common_total_price <![CDATA[<=]]> #{sumAssetsUpper}
</if>
<iftest="sumAssetsLower != null">
AND g.common_total_price <![CDATA[>=]]> #{sumAssetsLower}
CONVERT (IFNULL(common_position,0) / (IFNULL(common_position,0) + common_funding),DECIMAL (15, 2)) AS stockPosition,
CONVERT ((IFNULL(common_position,0) + common_funding),DECIMAL (15, 2)) AS common_total_price,
CONVERT ((IFNULL(common_position,0) + common_funding)-initial_funding,DECIMAL (15, 2)) AS profitLossPrice,
CONVERT (((IFNULL(common_position,0) + common_funding)-initial_funding)/ initial_funding,DECIMAL (15, 4)) AS common_proportion,
data_source
FROM
(SELECT
a.id,
a.customer_id,
e.`name`,
p.product_name,
p.product_type,
a.initial_funding,
a.status,
a.simulated_market_id as market_id,
a.real_market_id AS real_market_id,
a.simulated_market_id AS simulated_market_id,
a.simulated_funding as common_funding,
a.created_at,
a.expired_at,
(SELECT sum((select recent_closing_price from stocks_code as sc where sc.symbol = b.code ) * count) FROM high_stock_position b WHERE a.simulated_market_id = b.market_id and a.is_deleted='0') AS common_position,
(SELECT count(b.code) FROM high_stock_position b WHERE a.simulated_market_id = b.market_id) AS common_position_count,
(SELECT count(b.code) FROM high_stock_position b WHERE a.simulated_market_id = b.market_id and b.is_deleted = '0') AS
position_count,
(SELECT count(spl.code) FROM high_stock_profit_log spl WHERE a.simulated_market_id = spl.market_id and
spl.profit_price>0) AS profit_count,
e.investment_style,
e.account_limit,
e.created_time,
e.data_source
FROM
high_account_product_relation a
INNER JOIN us_member_base e ON a.customer_id = e.id
INNER JOIN high_product p ON a.product_id = p.id
left join us_team_user as t on a.sales_id = t.admin_user_id
<where>
<iftest="salesDirector != null">
AND (
t.team_leader_id = #{salesDirector}
<iftest="dataSource != null">
or e.data_source in
<foreachcollection="dataSource"item="d2"
index="index"open="("close=")"separator=",">
#{d2}
</foreach>
</if>
)
</if>
<iftest="salesManager != null">
AND (
t.parent_leader_id = #{salesManager}
<iftest="dataSource != null">
or e.data_source in
<foreachcollection="dataSource"item="d2"
index="index"open="("close=")"separator=",">
#{d2}
</foreach>
</if>
)
</if>
<iftest="clientDataSource != null">
AND e.data_source = #{clientDataSource}
</if>
<iftest="analystId != null">
AND p.analyst = #{analystId}
</if>
<iftest="companyId != null">
AND (
e.company_id in
<foreachcollection="companyId"item="cid2"
index="index"open="("close=")"separator=",">
#{cid2}
</foreach>
<iftest="dataSource != null">
or e.data_source in
<foreachcollection="dataSource"item="d2"
index="index"open="("close=")"separator=",">
#{d2}
</foreach>
</if>
)
</if>
<iftest="customerName != null">
AND e.name = #{customerName}
</if>
<iftest="productStatus != null">
AND a.status = #{productStatus}
</if>
<iftest="productId != null">
AND p.id = #{productId}
</if>
<iftest="buyStartTime != null">
AND a.created_at <![CDATA[>=]]> #{buyStartTime}
</if>
<iftest="buyEndTime != null">
AND a.created_at <![CDATA[<=]]> #{buyEndTime}
</if>
<iftest="expStartTime != null">
AND a.expired_at <![CDATA[>=]]> #{expStartTime}
</if>
<iftest="expEndTime != null">
AND a.expired_at <![CDATA[<=]]> #{expEndTime}
</if>
</where>
) f
) g
<where>
<iftest="positionUpper != null">
AND g.stockPosition <![CDATA[<=]]> #{positionUpper}
</if>
<iftest="positionLower != null">
AND g.stockPosition <![CDATA[>=]]> #{positionLower}
</if>
<iftest="profitAndLossUpper != null">
AND g.common_proportion <![CDATA[<=]]> #{profitAndLossUpper}
</if>
<iftest="profitAndLossLower != null">
AND g.common_proportion <![CDATA[>=]]> #{profitAndLossLower}
</if>
<iftest="sumAssetsUpper != null">
AND g.common_total_price <![CDATA[<=]]> #{sumAssetsUpper}
</if>
<iftest="sumAssetsLower != null">
AND g.common_total_price <![CDATA[>=]]> #{sumAssetsLower}
</if>
</where>
union
select * from (select
id,
"real" as accountType,
customer_id,
name,
product_name,
product_type,
status,
market_id,
real_market_id,
simulated_market_id,
common_funding,
initial_funding,
created_at,
expired_at,
common_position,
common_position_count,
position_count,
profit_count,
investment_style,
account_limit,
created_time,
CONVERT (common_position / (common_position + common_funding),DECIMAL (15, 2)) AS stockPosition,
CONVERT ((common_position + common_funding),DECIMAL (15, 2)) AS common_total_price,
CONVERT ((common_position + common_funding)-initial_funding,DECIMAL (15, 2)) AS profitLossPrice,
CONVERT (((common_position + common_funding)-initial_funding)/ initial_funding,DECIMAL (15, 4)) AS common_proportion,
data_source
FROM
(SELECT
a.id,
a.customer_id,
e.`name`,
p.product_name,
p.product_type,
a.initial_funding,
a.status,
a.real_market_id as market_id,
a.real_market_id AS real_market_id,
a.simulated_market_id AS simulated_market_id,
a.real_funding as common_funding,
a.created_at,
a.expired_at,
(SELECT sum((select recent_closing_price from stocks_code as sc where sc.symbol = b.code ) * count) FROM high_stock_position b WHERE a.real_market_id = b.market_id and a.is_deleted='0') AS common_position,
(SELECT count(b.code) FROM high_stock_position b WHERE a.real_market_id = b.market_id) AS common_position_count,
(SELECT count(b.code) FROM high_stock_position b WHERE a.real_market_id = b.market_id and b.is_deleted = '0') AS
position_count,
(SELECT count(spl.code) FROM high_stock_profit_log spl WHERE a.real_market_id = spl.market_id and
spl.profit_price>0) AS profit_count,
e.investment_style,
e.account_limit,
e.created_time,
e.data_source
FROM
high_account_product_relation a
INNER JOIN us_member_base e ON a.customer_id = e.id
INNER JOIN high_product p ON a.product_id = p.id
left join us_team_user as t on a.sales_id = t.admin_user_id
<where>
<iftest="salesDirector != null">
AND (
t.team_leader_id = #{salesDirector}
<iftest="dataSource != null">
or e.data_source in
<foreachcollection="dataSource"item="d2"
index="index"open="("close=")"separator=",">
#{d2}
</foreach>
</if>
)
</if>
<iftest="salesManager != null">
AND (
t.parent_leader_id = #{salesManager}
<iftest="dataSource != null">
or e.data_source in
<foreachcollection="dataSource"item="d2"
index="index"open="("close=")"separator=",">
#{d2}
</foreach>
</if>
)
</if>
<iftest="analystId != null">
AND p.analyst = #{analystId}
</if>
<iftest="clientDataSource != null">
AND e.data_source = #{clientDataSource}
</if>
<iftest="companyId != null">
AND (
e.company_id in
<foreachcollection="companyId"item="c1"
index="index"open="("close=")"separator=",">
#{c1}
</foreach>
<iftest="dataSource != null">
or e.data_source in
<foreachcollection="dataSource"item="d2"
index="index"open="("close=")"separator=",">
#{d2}
</foreach>
</if>
)
</if>
<iftest="customerName != null">
AND e.name = #{customerName}
</if>
<iftest="productStatus != null">
AND a.status = #{productStatus}
</if>
<iftest="productId != null">
AND p.id = #{productId}
</if>
<iftest="buyStartTime != null">
AND a.created_at <![CDATA[>=]]> #{buyStartTime}
</if>
<iftest="buyEndTime != null">
AND a.created_at <![CDATA[<=]]> #{buyEndTime}
</if>
<iftest="expStartTime != null">
AND a.expired_at <![CDATA[>=]]> #{expStartTime}
</if>
<iftest="expEndTime != null">
AND a.expired_at <![CDATA[<=]]> #{expEndTime}
</if>
</where>
) f
) g
<where>
<iftest="positionUpper != null">
AND g.stockPosition <![CDATA[<=]]> #{positionUpper}
</if>
<iftest="positionLower != null">
AND g.stockPosition <![CDATA[>=]]> #{positionLower}
</if>
<iftest="profitAndLossUpper != null">
AND g.common_proportion <![CDATA[<=]]> #{profitAndLossUpper}
</if>
<iftest="profitAndLossLower != null">
AND g.common_proportion <![CDATA[>=]]> #{profitAndLossLower}
</if>
<iftest="sumAssetsUpper != null">
AND g.common_total_price <![CDATA[<=]]> #{sumAssetsUpper}
</if>
<iftest="sumAssetsLower != null">
AND g.common_total_price <![CDATA[>=]]> #{sumAssetsLower}
LEFT JOIN stocks_code b1 ON b.`code` = b1.`symbol`
WHERE
a.simulated_market_id = b.market_id
and b.is_deleted = '0'
),0) AS simulated_position,
(select profit_loss_ratio as profitLossRatio from stock_position as sp where sp.market_id = a.simulated_market_id AND sp.code = #{code} AND sp.is_deleted = "0") as profitLossRatio,
(select DATEDIFF(now(),sp.created_time) as holdDay from stock_position as sp where sp.market_id = a.simulated_market_id AND sp.code = #{code} AND sp.is_deleted = "0") as holdDay,
(select stock_position as stockPosition from stock_position as sp where sp.market_id = a.simulated_market_id AND sp.code = #{code} AND sp.is_deleted = "0") as stockPosition
FROM
high_account_product_relation a
LEFT JOIN us_member_base e ON a.customer_id = e.id
LEFT JOIN high_product p ON a.product_id = p.id
<where>
<iftest="isSelling == false">
AND a.status in(1,2)
</if>
<iftest="customerIds!=null">
AND a.customer_id IN
<foreachcollection="customerIds"item="customerId"
index="index"open="("close=")"separator=",">
#{customerId}
</foreach>
</if>
<iftest="productIds!=null">
AND a.product_id IN
<foreachcollection="productIds"item="productId"
index="index"open="("close=")"separator=",">
#{productId}
</foreach>
</if>
<iftest="investmentStyle != null">
AND e.investment_style = #{investmentStyle}
</if>
<iftest="companyId != null">
AND e.company_id = #{companyId}
</if>
<iftest="analyst != null">
AND p.analyst = #{analyst}
</if>
<iftest="accountLimit != null">
AND e.account_limit = #{accountLimit}
</if>
<iftest="isHolding == true and isHolding == true and isSelling == false">
AND a.simulated_market_id IN (
<includerefid="commSql"/>
)
</if>
<iftest="productType != null">
AND a.product_type = #{productType}
</if>
<iftest="isHolding == true and isHolding == true and isSelling == true">
AND a.simulated_market_id IN (
<includerefid="commSql2"/>
)
</if>
<iftest="isHolding == false">
AND a.simulated_market_id NOT IN (
<includerefid="commSql"/>
)
</if>
</where>
) f
) g
)h
<where>
<iftest="customerNames != null and customerNames.size()>0">