t.org_id_int,t.property,t.org_name,t.org_code,t.type_id,t.type_name,t.size_id,t.size_name,t.location_type,t.location_id,max(t.location_name) as location_name,sum(t.number) as number,t.unit_price,sum(t.price)as price,sum(t.stock_number)as stock_number,sum(t.outbound_number)as outbound_number,
sum(t.destruction_number)as destruction_number,sum(t.expire_number)as expire_number,sum(t.broken_number)as broken_number,sum(t.fix_number)as fix_number,sum(t.use_number)as use_number FROM (
SELECT
bi.org_id_int as org_id_int,
COALESCE(bi.property, 0) as property,
po.org_name as org_name,
po.org_code as org_code,
bi.type_id as type_id,
t.name as type_name,
bi.size_id as size_id,
s.name as size_name,
COALESCE(bcb.cabinet_id, bw.id) as location_id,
COALESCE(MAX(bc.name), MAX(bw.name)) as location_name,
bi.location_type as location_type,
SUM(CASE WHEN bi.location_type=0 and location_state = "in" and bi.state !="broken" THEN 1
when bi.location_type=1 and location_state="in" then 1 ELSE 0 END) + SUM(CASE WHEN location_state = "out" and bi.state != "destory" THEN 1 ELSE 0 END)+sum(case when bi.location_type=0 and bi.state = "broken" and bi.location_state = "in" then 1 else 0 end) as number,
bi.price as unit_price,
(SUM(CASE WHEN bi.location_type=0 and location_state = "in" and bi.state !="broken" THEN 1
when bi.location_type=1 and location_state="in" then 1 ELSE 0 END) + SUM(CASE WHEN location_state = "out" and bi.state != "destory" THEN 1 ELSE 0 END)+sum(case when bi.location_type=0 and bi.state = "broken" and bi.location_state = "in" then 1 else 0 end))*bi.price as price,
SUM(CASE WHEN bi.location_type=0 and location_state = "in" and bi.state !="broken" THEN 1
when bi.location_type=1 and location_state="in" then 1 ELSE 0 END) AS stock_number,
SUM(CASE WHEN location_state = "out" and bi.state != "destory" THEN 1 ELSE 0 END) AS outbound_number,
sum(case when bi.state = "destory" then 1 else 0 end) as destruction_number,
sum(case when bi.warranty_period = 0 then 0 when DATE_ADD(bi.production_date, INTERVAL bi.warranty_period MONTH) < NOW() and bi.state!="broken" then 1 else 0 end) as expire_number,
sum(case when bi.location_type=0 and bi.state = "broken" and bi.location_state = "in" then 1 else 0 end) as broken_number,
sum(case when bi.state = "repair" and bi.location_state = "out" then 1 else 0 end) as fix_number,
sum(case when bi.bussiness_state = "use" and bi.location_state = "out" then 1 else 0 end) as use_number
FROM base_inventory bi
LEFT JOIN base_cabinet_box bcb ON bi.location_id = bcb.id and bi.location_type=1
LEFT JOIN base_cabinet bc ON bcb.cabinet_id = bc.id
LEFT JOIN base_warehouse bw ON bi.location_id = bw.id AND bi.location_type = 0
JOIN base_equipment_type t ON t.id = bi.type_id
JOIN base_equipment_size s ON s.id = bi.size_id
JOIN pub_org po ON bi.org_id_int = po.org_id
and po.org_id=#{orgId}
where bi.location_id=#{locationId}
GROUP BY bi.org_id_int, location_id, type_id, size_id, bi.price, bi.location_type,bi.property
ORDER BY org_id_int, location_id) as t
GROUP BY t.org_id_int,t.location_id,t.type_id,t.size_id,t.location_type,t.unit_price,t.property
<iftest="req.typeName != null and req.typeName != ''">
AND vw.type_name like concat('%',#{req.typeName},'%')
</if>
</where>
GROUP BY
vw.id,
vw.size_id,
vw.type_id,
vw.warranty_type,
vw.location_id,
vw.org_id,
vw.location_type,
vw.price,
vw.property) as a
inner join base_inventory_summary as ins on a.org_id=ins.org_id_int and a.location_id=ins.location_id and a.type_id=ins.type_id and a.size_id=ins.size_id and a.price=ins.unit_price and a.property=ins.property
t.org_id_int,t.property,t.org_name,t.org_code,t.type_id,t.type_name,t.size_id,t.size_name,t.location_type,t.location_id,max(t.location_name) as location_name,sum(t.number) as number,t.unit_price,sum(t.price)as price,sum(t.stock_number)as stock_number,sum(t.outbound_number)as outbound_number,
sum(t.destruction_number)as destruction_number,sum(t.expire_number)as expire_number,sum(t.broken_number)as broken_number,sum(t.fix_number)as fix_number,sum(t.use_number)as use_number FROM (
SELECT
bi.org_id_int as org_id_int,
COALESCE(bi.property, 0) as property,
po.org_name as org_name,
po.org_code as org_code,
bi.type_id as type_id,
t.name as type_name,
bi.size_id as size_id,
s.name as size_name,
COALESCE(bcb.cabinet_id, bw.id) as location_id,
COALESCE(MAX(bc.name), MAX(bw.name)) as location_name,
bi.location_type as location_type,
SUM(CASE WHEN bi.location_type=0 and location_state = "in" and bi.state !="broken" THEN 1
when bi.location_type=1 and location_state="in" then 1 ELSE 0 END) + SUM(CASE WHEN location_state = "out" and bi.state != "destory" THEN 1 ELSE 0 END)+sum(case when bi.location_type=0 and bi.state = "broken" and bi.location_state = "in" then 1 else 0 end) as number,
bi.price as unit_price,
(SUM(CASE WHEN bi.location_type=0 and location_state = "in" and bi.state !="broken" THEN 1
when bi.location_type=1 and location_state="in" then 1 ELSE 0 END) + SUM(CASE WHEN location_state = "out" and bi.state != "destory" THEN 1 ELSE 0 END)+sum(case when bi.location_type=0 and bi.state = "broken" and bi.location_state = "in" then 1 else 0 end))*bi.price as price,
SUM(CASE WHEN bi.location_type=0 and location_state = "in" and bi.state !="broken" THEN 1
when bi.location_type=1 and location_state="in" then 1 ELSE 0 END) AS stock_number,
SUM(CASE WHEN location_state = "out" and bi.state != "destory" THEN 1 ELSE 0 END) AS outbound_number,
sum(case when bi.state = "destory" then 1 else 0 end) as destruction_number,
sum(case when bi.warranty_period = 0 then 0 when DATE_ADD(bi.production_date, INTERVAL bi.warranty_period MONTH) < NOW() and bi.state!="broken" then 1 else 0 end) as expire_number,
sum(case when bi.location_type=0 and bi.state = "broken" and bi.location_state = "in" then 1 else 0 end) as broken_number,
sum(case when bi.state = "repair" and bi.location_state = "out" then 1 else 0 end) as fix_number,
sum(case when bi.bussiness_state = "use" and bi.location_state = "out" then 1 else 0 end) as use_number
FROM base_inventory bi
LEFT JOIN base_cabinet_box bcb ON bi.location_id = bcb.id and bi.location_type=1
LEFT JOIN base_cabinet bc ON bcb.cabinet_id = bc.id
LEFT JOIN base_warehouse bw ON bi.location_id = bw.id AND bi.location_type = 0
JOIN base_equipment_type t ON t.id = bi.type_id
JOIN base_equipment_size s ON s.id = bi.size_id
JOIN pub_org po ON bi.org_id_int = po.org_id
and po.org_id=#{orgId}
where bi.location_id=#{locationId}
GROUP BY bi.org_id_int, location_id, type_id, size_id, bi.price, bi.location_type,bi.property
ORDER BY org_id_int, location_id) as t
GROUP BY t.org_id_int,t.location_id,t.type_id,t.size_id,t.location_type,t.unit_price,t.property
<iftest="req.typeName != null and req.typeName != ''">
AND vw.type_name like concat('%',#{req.typeName},'%')
</if>
</where>
GROUP BY
vw.id,
vw.size_id,
vw.type_id,
vw.warranty_type,
vw.location_id,
vw.org_id,
vw.location_type,
vw.price,
vw.property) as a
inner join base_inventory_summary as ins on a.org_id=ins.org_id_int and a.location_id=ins.location_id and a.type_id=ins.type_id and a.size_id=ins.size_id and a.price=ins.unit_price and a.property=ins.property