select result_id, taking_id, item_id, item_code, item_name, specification, unit_of_measure, unit_name, quantity, taking_quantity, taking_status, remark, attr1, attr2, attr3, attr4, create_by, create_time, update_by, update_time from wm_stock_taking_result insert into wm_stock_taking_result (taking_id,item_id,item_code,item_name,specification,taking_quantity,quantity,taking_status) select sl.taking_id,sl.item_id,sl.item_code,sl.item_name,sl.specification,taking_quantity,quantity, case when ISNULL(taking_quantity,0) > ISNULL(quantity,0) then 'PROFIT' when ISNULL(taking_quantity,0) < ISNULL(quantity,0) then 'LOSS' else 'NORMAL' end as taking_status from wm_stock_taking_line sl where sl.taking_id = #{takingId} insert into wm_stock_taking_result (item_id,item_code,item_name,specification,taking_quantity,quantity,taking_status) select ISNULL(m.item_id,t.item_id) as item_id, ISNULL(m.item_code,t.item_code) as item_code,ISNULL(m.item_name,t.item_name) as item_name,ISNULL(m.specification,t.specification) as specification, ISNULL(t.taking_quantity,0) as taking_quantity,ISNULL(m.onhand,0) as quantity, case when ISNULL(t.taking_quantity,0) > ISNULL(m.onhand,0) then 'PROFIT' when ISNULL(t.taking_quantity,0) < ISNULL(m.onhand,0) then 'LOSS' else 'NORMAL' end as taking_status from ( select sl.item_id,sl.item_code,sl.item_name,sl.specification,sum(sl.taking_quantity) as taking_quantity from wm_stock_taking_line sl where sl.taking_id = #{takingId} group by sl.item_id,sl.item_code,sl.item_name,sl.specification ) t left join ( select ms.item_id,ms.item_code,ms.item_name,ms.specification,sum(ms.quantity_onhand) as onhand from wm_material_stock ms where (ms.warehouse_id,ms.location_id,ms.area_id) in ( select warehouse_id,location_id,area_id from wm_stock_taking where taking_id = #{takingId} ) group by ms.item_id,ms.item_code,ms.item_name,ms.specification ) m on t.item_id = m.item_id and t.item_code = m.item_code where t.taking_quantity !=0 union select ISNULL(t.item_id,m.item_id) as item_id,ISNULL(t.item_code,m.item_code) as item_code,ISNULL(t.item_name,m.item_name) as item_name,ISNULL(t.specification,m.specification) as specification, ISNULL(t.taking_quantity,0) as taking_quantity,ISNULL(m.onhand,0) as quantity, case when ISNULL(t.taking_quantity,0) > ISNULL(m.onhand,0) then 'PROFIT' when ISNULL(t.taking_quantity,0) < ISNULL(m.onhand,0) then 'LOSS' else 'NORMAL' end as taking_status from ( select ms.item_id,ms.item_code,ms.item_name,ms.specification,sum(ms.quantity_onhand) as onhand from wm_material_stock ms where (ms.warehouse_id,ms.location_id,ms.area_id) in ( select warehouse_id,location_id,area_id from wm_stock_taking where taking_id = #{takingId} ) group by ms.item_id,ms.item_code,ms.item_name,ms.specification ) m left join ( select sl.item_id,sl.item_code,sl.item_name,sl.specification,sum(sl.taking_quantity) as taking_quantity from wm_stock_taking_line sl where sl.taking_id = #{takingId} group by sl.item_id,sl.item_code,sl.item_name,sl.specification ) t on t.item_id = m.item_id and t.item_code = m.item_code where m.onhand !=0 insert into wm_stock_taking_result taking_id, item_id, item_code, item_name, specification, unit_of_measure, unit_name, quantity, taking_quantity, taking_status, remark, attr1, attr2, attr3, attr4, create_by, create_time, update_by, update_time, #{takingId}, #{itemId}, #{itemCode}, #{itemName}, #{specification}, #{unitOfMeasure}, #{unitName}, #{quantity}, #{takingQuantity}, #{takingStatus}, #{remark}, #{attr1}, #{attr2}, #{attr3}, #{attr4}, #{createBy}, #{createTime}, #{updateBy}, #{updateTime}, update wm_stock_taking_result taking_id = #{takingId}, item_id = #{itemId}, item_code = #{itemCode}, item_name = #{itemName}, specification = #{specification}, unit_of_measure = #{unitOfMeasure}, unit_name = #{unitName}, quantity = #{quantity}, taking_quantity = #{takingQuantity}, taking_status = #{takingStatus}, remark = #{remark}, attr1 = #{attr1}, attr2 = #{attr2}, attr3 = #{attr3}, attr4 = #{attr4}, create_by = #{createBy}, create_time = #{createTime}, update_by = #{updateBy}, update_time = #{updateTime}, where result_id = #{resultId} delete from wm_stock_taking_result where result_id = #{resultId} delete from wm_stock_taking_result where result_id in #{resultId} delete from wm_stock_taking_result where taking_id = #{takingId}