<?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.yeejoin.equipmanage.mapper.EquipmentAccountMapper">
    <select id="getNameList" resultType="com.yeejoin.equipmanage.common.vo.EquipmentAccountFilterVo">
           select * from
                     (
                     select
                    (select code from wl_equipment_category where id =( select category_id from wl_equipment where equipment_id = wl_equipment.id)) as id,
                    car.name as name
                    from
                    wl_car as car
                    group by car.name
                    union all
                    select
                    (select code from wl_equipment_category where id =( select category_id from wl_equipment where equipment_id = wl_equipment.id)) as id,
                    eq.name as name
                    from
                    wl_equipment_detail as eq
                    group by eq.name) data
                    where id like CONCAT(#{eType},'%')

    </select>
    <select id="getAccounList" resultType="com.yeejoin.equipmanage.common.vo.EquipmentAccountVo">
         select * from
                 ( select
                    id,
                    (select name from wl_equipment where equipment_id = wl_equipment.id) as equipmentType,
                    (select code from wl_equipment_category where id =( select category_id from wl_equipment where equipment_id = wl_equipment.id)) as equipmentTypeId,
                    car.name as equipmentName,
                    '辆' unit,
                    count(case when car.car_state= 1 then 1  else null end ) inLibCount,
                    0 onCarCount,
                    count(case when car.car_state= 3 then 1  else null end ) maintainCount,
                    count(case when car.car_state= 7 then 1  else null end ) badCount,
                    0 wasteCount,
                    'car' as dataType
                    from
                    wl_car as car
                    group by car.name
                    union all
                    select
                    eq.id,
                    (select name from wl_equipment where equipment_id = wl_equipment.id) as equipmentType,
                    (select code from wl_equipment_category where id =( select category_id from wl_equipment where equipment_id = wl_equipment.id)) as equipmentTypeId,
                    eq.name as equipmentName,
                    (select name from wl_unit where wleq.unit_id = wl_unit.id) as unit,
                    sum(if(sto.status= 1,sto.amount,0)) as inLibCount,
                    sum(if(sto.status= 10,sto.amount,0)) as onCarCount,
                    sum(if(sto.status= 3,sto.amount,0)) as maintainCount,
                    sum(if(sto.status= 7,sto.amount,0)) as badCount,
                    sum(if(sto.status= 11,sto.amount,0)) as wasteCount,
                   'acc' as dataType
                    from
                    wl_equipment_detail as eq
                    left join
                    wl_stock_detail as sto on
                    eq.id = sto.equipment_detail_id
                    left join
                    wl_equipment as wleq on
                    eq.equipment_id = wleq.id
                    group by eq.name) data
                    where 1=1
                    <if test="eType != null and eType != ''  ">
                        AND equipmentTypeId like CONCAT(#{eType},'%')
                    </if>
                    <if test="eName != null and eName != '' ">
                        AND equipmentName = #{eName}
                    </if>
                    LIMIT #{spage},#{pageSize}
    </select>
    <select id="getAccounListCount" resultType="int">
             select count(1) from
                     (
                    select
                    (select code from wl_equipment_category where id =( select category_id from wl_equipment where equipment_id = wl_equipment.id)) as equipmentTypeId,
                    car.name as equipmentName
                    from
                    wl_car as car
                    group by car.name
                    union all
                    select
                    (select code from wl_equipment_category where id =( select category_id from wl_equipment where equipment_id = wl_equipment.id)) as equipmentTypeId,
                    eq.name as equipmentName
                    from
                    wl_equipment_detail as eq
                    group by eq.name
                    ) data
                    where 1=1
                    <if test="eType != null and eType != ''  ">
                        AND equipmentTypeId like CONCAT(#{eType},'%')
                    </if>
                    <if test="eName != null and eName != '' ">
                        AND equipmentName = #{eName}
                    </if>
    </select>
    <select id="getMoreCarList"  resultType="com.yeejoin.equipmanage.common.vo.EquipmentAccountCarVo">
        select
            id,
            name,
            (select name from wl_equipment where id = equipment_id) as type,
            car_num as carNum,
            if(is_import=0,'进口','国产') as isImport,
            brand,
            (select name from wl_manufacturer_info where id = manufacturer_id ) as facturer,
            standard,
            chassis_brand as carBrand
            from wl_car
            where
            name =#{equipmentName}
            and car_state = #{accType}
            <if test="carNum != null and carNum != '' ">
                and car_num like CONCAT(#{carNum},'%')
            </if>
            LIMIT #{spage},#{pageSize}
    </select>

    <select id="getMoreCarListCount"  resultType="int">
        select
        count(1)
        from wl_car
        where
        name =#{equipmentName}
        and car_state = #{accType}
        <if test="carNum != null and carNum != '' ">
            and car_num like CONCAT(#{carNum},'%')
        </if>
     </select>
    <select id="getMoreAccList"  resultType="com.yeejoin.equipmanage.common.vo.EquipmentAccountAccVo">
        select
        sto.id,
        eq.name,
        (select name from wl_manufacturer_info where id = eq.manufacturer_id) facturer,
        date_format(eq.production_date,'%Y-%m-%d') AS proDate,
        eq.standard,
        eq.maintenance_cycle as cycle,
        eq.expiry_date AS expDate,
        sto.qr_code as code,
        amount
        from wl_equipment_detail as eq
        left join
        wl_stock_detail as sto on
        eq.id = sto.equipment_detail_id
        where
        name =#{equipmentName}
        and status = #{accType}
        <if test="code != null and code != '' ">
            and qr_code like CONCAT(#{code},'%')
        </if>
        LIMIT #{spage},#{pageSize}
    </select>

    <select id="getMoreAccListCount"  resultType="int">
        select
        count(1)
        from wl_equipment_detail as eq
        left join
        wl_stock_detail as sto on
        eq.id = sto.equipment_detail_id
        where
        name =#{equipmentName}
        and status = #{accType}
        <if test="code != null and code != '' ">
            and qr_code like CONCAT(#{code},'%')
        </if>
    </select>
    <select id="selectHistoryById" resultType="com.yeejoin.equipmanage.common.vo.EquipmentHistoryVo">
            select
            remark ,
            date_format(create_date,'%Y-%m-%d') as date,
            user_name as user,
            department_name  as deptName
            from
            wl_journal
            where
            car_id =#{id}
    </select>
    <select id="getaccData" resultType="String">
             select
            date_format(production_date,'%Y-%m-%d') AS proDate
            from
            wl_stock_detail as sto
            left join wl_equipment_detail as eq on sto.equipment_detail_id = eq.id
             where
               sto.id=#{id}
    </select>
    <select id="selectAccHistoryById" resultType="com.yeejoin.equipmanage.common.vo.EquipmentHistoryVo">
            select
            remark ,
            date_format(create_date,'%Y-%m-%d') as date,
            user_name as user,
            department_name  as deptName
            from
            wl_journal
            where
            stock_detail_id =#{id}
    </select>
    <select id="getaccId" resultType="String">
            select equipment_detail_id from wl_stock_detail where id =#{id}
    </select>
</mapper>
