<?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.maintain.business.mapper.ErpRepairFormMapper">

    <resultMap type="com.maintain.business.domain.ErpRepairForm" id="ErpRepairFormResult">
        <result property="id" column="id"/>
        <result property="carId" column="car_id"/>
        <result property="clientId" column="client_id"/>
        <result property="clientName" column="client_name"/>
        <result property="vehicleModelId" column="vehicle_model_id"/>
        <result property="repairNumber" column="repair_number"/>
        <result property="repairType" column="repair_type"/>
        <result property="isRebuild" column="is_rebuild"/>
        <result property="repairCause" column="repair_cause"/>
        <result property="vehicleKilometers" column="vehicle_kilometers"/>
        <result property="isOldPart" column="is_old_part"/>
        <result property="isByoPart" column="is_byo_part"/>
        <result property="byoPartRemark" column="byo_part_remark"/>
        <result property="byoPartUrl" column="byo_part_url"/>
        <result property="dashboardUrl" column="dashboard_url"/>
        <result property="bodyConditionUrl" column="body_condition_url"/>
        <result property="trailerDocumentNumber" column="trailer_document_number"/>
        <result property="trailerCompany" column="trailer_company"/>
        <result property="trailerPlateNumber" column="trailer_plate_number"/>
        <result property="trailerDriver" column="trailer_driver"/>
        <result property="trailerPhone" column="trailer_phone"/>
        <result property="trailerVehicleKilometers" column="trailer_vehicle_kilometers"/>
        <result property="trailerMoney" column="trailer_money"/>
        <result property="trailerStartTime" column="trailer_start_time"/>
        <result property="trailerEndTime" column="trailer_end_time"/>
        <result property="trailerRemark" column="trailer_remark"/>
        <result property="rescueLocation" column="rescue_location"/>
        <result property="destinationLocation" column="destination_location"/>
        <result property="repairerCompany" column="repairer_company"/>
        <result property="repairerDeptId" column="repairer_dept_id"/>
        <result property="repairerGroupId" column="repairer_group_id"/>
        <result property="malfunctionItem" column="malfunction_item"/>
        <result property="examineItem" column="examine_item"/>
        <result property="state" column="state"/>
        <result property="maintainEndTime" column="maintain_end_time"/>
        <result property="isDelivery" column="is_delivery"/>
        <result property="deliveryTime" column="delivery_time"/>
        <result property="settlementTime" column="settlement_time"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <resultMap type="com.maintain.business.domain.vo.ErpRepairFormVo" id="ErpRepairFormResultVo">
        <result property="id" column="id"/>
        <result property="carId" column="car_id"/>
        <result property="clientId" column="client_id"/>
        <result property="clientName" column="client_name"/>
        <result property="vehicleModelId" column="vehicle_model_id"/>
        <result property="repairNumber" column="repair_number"/>
        <result property="repairType" column="repair_type"/>
        <result property="isRebuild" column="is_rebuild"/>
        <result property="repairCause" column="repair_cause"/>
        <result property="vehicleKilometers" column="vehicle_kilometers"/>
        <result property="isOldPart" column="is_old_part"/>
        <result property="isByoPart" column="is_byo_part"/>
        <result property="byoPartRemark" column="byo_part_remark"/>
        <result property="byoPartUrl" column="byo_part_url"/>
        <result property="dashboardUrl" column="dashboard_url"/>
        <result property="bodyConditionUrl" column="body_condition_url"/>
        <result property="trailerDocumentNumber" column="trailer_document_number"/>
        <result property="trailerCompany" column="trailer_company"/>
        <result property="trailerPlateNumber" column="trailer_plate_number"/>
        <result property="trailerDriver" column="trailer_driver"/>
        <result property="trailerPhone" column="trailer_phone"/>
        <result property="trailerVehicleKilometers" column="trailer_vehicle_kilometers"/>
        <result property="trailerMoney" column="trailer_money"/>
        <result property="trailerStartTime" column="trailer_start_time"/>
        <result property="trailerEndTime" column="trailer_end_time"/>
        <result property="trailerRemark" column="trailer_remark"/>
        <result property="rescueLocation" column="rescue_location"/>
        <result property="destinationLocation" column="destination_location"/>
        <result property="repairerCompany" column="repairer_company"/>
        <result property="repairerDeptId" column="repairer_dept_id"/>
        <result property="repairerDeptName" column="repairer_dept_name"/>
        <result property="repairerGroupId" column="repairer_group_id"/>
        <result property="repairerGroupName" column="repairer_group_name"/>
        <result property="malfunctionItem" column="malfunction_item"/>
        <result property="examineItem" column="examine_item"/>
        <result property="state" column="state"/>
        <result property="settlementState" column="settlement_state"/>
        <result property="maintainEndTime" column="maintain_end_time"/>
        <result property="isDelivery" column="is_delivery"/>
        <result property="isFindMaintain" column="is_find_maintain"/>
        <result property="isTrailer" column="is_trailer"/>
        <result property="deliveryTime" column="delivery_time"/>
        <result property="settlementTime" column="settlement_time"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="totalActualTime" column="totalActualTime"/>
        <result property="totalActualTimeMoney" column="totalActualTimeMoney"/>
        <result property="totalMaterialMoney" column="totalMaterialMoney"/>
        <result property="totalMoney" column="totalMoney"/>
        <association property="car" javaType="com.maintain.business.domain.vo.ErpCarVo">
            <result property="id" column="ec_id"/>
            <result property="plateNumber" column="plate_number"/>
            <result property="carBrand" column="car_brand"/>
            <result property="carModel" column="car_model"/>
            <result property="clientContact" column="contact"/>
            <result property="phone" column="ec_phone"/>
            <result property="clientId" column="client_id"/>
            <result property="clientName" column="client_name"/>
            <result property="carVin" column="car_vin"/>
            <result property="drivingLicenseUrl" column="driving_license_url"/>
            <result property="carImgUrl" column="car_img_url"/>
            <result property="carTypeId" column="car_type_id"/>
            <result property="createBy" column="create_by"/>
            <result property="createTime" column="create_time"/>
        </association>
        <association property="vehicleModelVo" javaType="com.maintain.business.domain.vo.ErpVehicleModelVo">
            <result property="id" column="evm_id"/>
            <result property="carTypeCode" column="car_type_code"/>
            <result property="carBrand" column="car_brand"/>
            <result property="carModel" column="car_model"/>
            <result property="carConfiguration" column="car_configuration"/>
            <result property="carColour" column="car_colour"/>
            <result property="type" column="evm_type"/>
            <result property="expendType" column="expend_type"/>
            <result property="chassisNumber" column="chassis_number"/>
            <result property="motorNumber" column="motor_number"/>
            <result property="engineNumber" column="engine_number"/>
            <result property="axleNumber" column="axle_number"/>
            <result property="createBy" column="create_by"/>
            <result property="createTime" column=" create_time"/>
        </association>
        <association property="client" javaType="com.maintain.business.domain.vo.ErpClientVo">
            <result property="id" column="ec2_id"/>
            <result property="name" column="name"/>
            <result property="contact" column="contact"/>
            <result property="phone" column="ec2_phone"/>
            <result property="type" column="ec2_type"/>
            <result property="certificateType" column="certificate_type"/>
            <result property="certificateNumber" column="certificate_number"/>
            <result property="birthDate" column="birth_date"/>
            <result property="address" column="address"/>
            <result property="occupation" column="occupation"/>
            <result property="mailbox" column="mailbox"/>
            <result property="remark" column="remark"/>
            <result property="state" column="state"/>
        </association>
    </resultMap>

    <sql id="repairFormResultVoSql">
        SELECT erf.id, erf.repair_number, erf.client_id, erf.car_id, erf.repair_type, erf.is_rebuild, erf.repair_cause, erf.vehicle_kilometers, erf.is_old_part,
               erf.is_byo_part, erf.byo_part_remark, erf.byo_part_url, erf.is_find_maintain, erf.dashboard_url, erf.body_condition_url,
               erf.trailer_document_number, erf.trailer_company, erf.trailer_plate_number, erf.trailer_driver,erf.trailer_phone,
               erf.trailer_vehicle_kilometers, erf.trailer_money, erf.trailer_start_time, erf.trailer_end_time, erf.trailer_remark,
               erf.rescue_location, erf.destination_location, erf.repairer_company, erf.repairer_dept_id, erf.repairer_dept_name, erf.repairer_group_id, erf.repairer_group_name,
               erf.malfunction_item, erf.examine_item, erf.state, erf.settlement_state, erf.maintain_end_time, erf.is_delivery, erf.delivery_time,
               erf.settlement_time, erf.create_by, erf.create_time, erf.vehicle_model_id, erf.is_trailer, ec.id as ec_id,ec.plate_number, ec.phone as ec_phone, ec.client_name, ec.car_vin,
               evm.id as evm_id, evm.car_type_code, evm.car_brand, evm.car_model, evm.car_configuration, evm.car_colour, evm.type as evm_type, evm.expend_type,
               evm.chassis_number, evm.motor_number, evm.engine_number, evm.axle_number, ec2.id as ec2_id, ec2.name, ec2.sex, ec2.contact, ec2.phone as ec2_phone,
               ec2.type as ec2_type, totalActualTime, IFNULL(erfpd.totalActualTimeMoney,0) AS totalActualTimeMoney, IFNULL(empi.totalMaterialMoney,0) AS totalMaterialMoney,
               erf.trailer_money + IFNULL(erfpd.totalActualTimeMoney,0) + IFNULL(empi.totalMaterialMoney,0) AS totalMoney
        FROM erp_repair_form erf
                 LEFT JOIN erp_car ec ON erf.car_id = ec.id
                 LEFT JOIN erp_vehicle_model evm on erf.vehicle_model_id = evm.id
                 LEFT JOIN erp_client ec2 on erf.client_id = ec2.id
                 LEFT JOIN (
            SELECT
                repair_form_id,
                SUM(actual_time) AS totalActualTime,
                SUM(actual_time * standard_price) AS totalActualTimeMoney
            FROM erp_repair_form_project
            GROUP BY repair_form_id) erfpd ON erf.id = erfpd.repair_form_id
                 LEFT JOIN (
            SELECT
                repair_form_id,
                SUM(selling_price * collect_number) AS totalMaterialMoney
            FROM erp_material_picking_info
            GROUP BY repair_form_id
        ) empi ON erf.id = empi.repair_form_id
    </sql>
    <select id="customQueryPageList" resultMap="ErpRepairFormResultVo">
        <include refid="repairFormResultVoSql" />
        ${ew.getCustomSqlSegment}
    </select>

    <select id="customQueryList" resultMap="ErpRepairFormResultVo">
        <include refid="repairFormResultVoSql" />
        ${ew.getCustomSqlSegment}
    </select>

    <select id="customQueryListByIds" resultMap="ErpRepairFormResultVo">
        <include refid="repairFormResultVoSql" />
        <where>
            and erf.id in
            <foreach collection="ids" item="item" open="(" close=")" separator="," >
                #{item}
            </foreach>
        </where>
    </select>

    <select id="customQueryById" resultMap="ErpRepairFormResultVo">
        <include refid="repairFormResultVoSql" />
        <where>
            and erf.id = #{id}
        </where>
    </select>


</mapper>
