<?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="cn.ezhizao.project.business.upgrade.mapper.BizWorkOrderUpdateMapper">

    <resultMap type="cn.ezhizao.project.business.upgrade.domain.BizWorkOrderUpdate" id="BizWorkOrderUpdateResult">
        <id column="id" property="id"/>
    </resultMap>
    <resultMap type="cn.ezhizao.project.business.upgrade.domain.BizWorkOrderUpdate" id="BizWorkOrderResult">
        <id column="id" property="id"/>
        <result column="company_id" jdbcType="BIGINT" property="companyId"/>
        <collection property="records"   select="getRecords" column="id"/>
        <collection property="entrusts"   select="getEntrusts" column="id"/>
    </resultMap>

    <resultMap type="cn.ezhizao.project.business.workOrder.domain.BizWorkOrderRecord"
               id="BizWorkOrderRecordResult">
        <id column="id" property="id"/>
    </resultMap>
    <resultMap type="cn.ezhizao.project.business.entrust.domain.BizEntrust"
               id="BizEntrustResult">
        <id column="id" property="id"/>
    </resultMap>

    <select id="getList" parameterType="BizWorkOrderUpdate" resultMap="BizWorkOrderUpdateResult">
        SELECT * FROM biz_work_order_update
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            <if test="type != null "> AND type = #{type}</if>
            <if test="noContract != null "> AND no_contract = #{noContract}</if>
            <if test="workOrderNo != null  and workOrderNo != ''"> AND work_order_no = #{workOrderNo}</if>
            <if test="startMonth != null "> AND start_month = #{startMonth}</if>
            <if test="endMonth != null "> AND end_month = #{endMonth}</if>
            <if test="monthNum != null "> AND month_num = #{monthNum}</if>
            <if test="isStop != null "> AND is_stop = #{isStop}</if>
            <if test="provinceCode != null  and provinceCode != ''"> AND province_code = #{provinceCode}</if>
            <if test="province != null  and province != ''"> AND province = #{province}</if>
            <if test="cityCode != null  and cityCode != ''"> AND city_code = #{cityCode}</if>
            <if test="city != null  and city != ''"> AND city = #{city}</if>
            <if test="districtCode != null  and districtCode != ''"> AND district_code = #{districtCode}</if>
            <if test="district != null  and district != ''"> AND district = #{district}</if>
            <if test="isContinue != null "> AND is_continue = #{isContinue}</if>
            <if test="hasImportTasks != null "> AND has_import_tasks = #{hasImportTasks}</if>
        </trim>
    </select>
    <select id="getList2" parameterType="BizWorkOrderUpdate" resultMap="BizWorkOrderResult">
        SELECT t1.*, t2.name as company_name, t2.social_credit_code, t3.name as task_type_name, t4.name as
        task_type_detail_name, t5.nick_name as service_name, t2.is_zero,t2.tax_type,t6.contract_no FROM biz_work_order_update t1
        left join biz_company t2 on t2.id = t1.company_id
        left join biz_task_type t3 on t3.id = t1.task_type_id
        left join biz_task_type_detail t4 on t4.id = t1.task_type_detail_id
        left join sys_user t5 on t5.user_id = t1.service_id
        left join biz_archive_input_update t6 on t6.id = t1.contract_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0  and t1.type = 1
            <if test="id != null "> AND t1.id = #{id}</if>
            <if test="contractDetailId != null "> AND t1.contract_detail_id = #{contractDetailId}</if>
            <if test="tenantId != null "> AND t1.tenant_id = #{tenantId}</if>
            <if test="companyId != null "> AND t1.company_id = #{companyId}</if>
            <if test="serviceId != null">AND t1.service_id = #{serviceId}</if>
            <if test="noContract != null ">AND t1.no_contract = #{noContract}</if>
            <if test="workOrderNo != null  and workOrderNo != ''">AND t1.work_order_no = #{workOrderNo}</if>
            <if test="startMonth != null ">AND t1.start_month = #{startMonth}</if>
            <if test="endMonth != null ">AND t1.end_month = #{endMonth}</if>
            <if test="monthNum != null ">AND t1.month_num = #{monthNum}</if>
            <if test="companyName != null and companyName != '' "> AND t2.name like concat('%', #{companyName}, '%')</if>
            <if test="isStop != null ">AND t1.is_stop = #{isStop}</if>
            <if test="year != null ">
                AND t1.start_month > 0
                <if test="month != null and month != ''">
                    AND concat(#{year}, '-', #{month}, '-01') &gt;= (case when t1.task_type_id = 1 then DATE_ADD(t1.start_month, interval 1 month) else t1.start_month end) and concat(#{year}, '-', #{month}, '-01') &lt;= (case when t1.task_type_id = 1 then DATE_ADD(t1.end_month,interval 1 month) else t1.end_month end)
                </if>
                <if test="month == null or month == ''">
                    AND #{year} >= (case when t1.task_type_id = 1 then DATE_FORMAT(DATE_ADD(t1.start_month,interval 1 month), '%Y') else DATE_FORMAT(t1.start_month, '%Y') end) AND #{year} &lt;= (case when t1.end_month = 0 then #{year} when t1.task_type_id = 1 then DATE_FORMAT(DATE_ADD(t1.end_month,interval 1 month), '%Y') else DATE_FORMAT(t1.end_month, '%Y') end)
                </if>
            </if>


        </trim>
    </select>
    <select id="gettoUpLoadList" parameterType="BizWorkOrderUpdate" resultMap="BizWorkOrderResult">
        SELECT t1.*, t2.name as company_name, t2.social_credit_code, t3.name as task_type_name, t4.name as
        task_type_detail_name, t5.nick_name as service_name, t2.is_zero,t2.tax_type,t6.contract_no FROM biz_work_order t1
        left join biz_company t2 on t2.id = t1.company_id
        left join biz_work_order_update orderupdate on orderupdate.id = t1.id and orderupdate.deleted = 0
        left join biz_task_type t3 on t3.id = t1.task_type_id
        left join biz_task_type_detail t4 on t4.id = t1.task_type_detail_id
        left join sys_user t5 on t5.user_id = t1.service_id
        left join biz_archive_input_update t6 on t6.id = t1.contract_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            orderupdate.id is null and t1.type = 1 and t1.deleted = 0
            <if test="tenantId != null "> AND t1.tenant_id = #{tenantId}</if>
            <if test="isStop != null "> AND t1.is_stop = #{isStop}</if>
            <if test="companyName != null and companyName != '' "> AND t2.name like concat('%', #{companyName}, '%')</if>
        </trim>
    </select>
    <select id="getToUpLoadCount" parameterType="BizWorkOrderUpdate" resultType="java.lang.Long">
        SELECT count(*) FROM biz_work_order t1
        left join biz_work_order_update orderupdate on orderupdate.id = t1.id and orderupdate.deleted = 0
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            orderupdate.id is null and t1.type = 1 and t1.deleted = 0 and t1.is_stop = 0
            <if test="tenantId != null "> AND t1.tenant_id = #{tenantId}</if>
        </trim>
    </select>
    <select id="selectByUpgradeId" resultType="cn.ezhizao.project.business.workOrder.domain.BizWorkOrder">
        select * from biz_work_order_update t1
        <where>
            <if test="tenantId != null "> and t1.tenant_id = #{tenantId}</if>
        </where>

    </select>
    <select id="getRecords" resultMap="BizWorkOrderRecordResult">
        SELECT * FROM biz_work_order_record WHERE deleted = 0 AND work_order_id = #{id}
    </select>
    <select id="getEntrusts" resultMap="BizEntrustResult">
        SELECT * FROM biz_entrust WHERE deleted = 0 AND work_order_id = #{id}
    </select>

    <delete id="physicalDelete">
        DELETE FROM biz_work_order_update


        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            id in
            <foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
                #{id}
            </foreach>
       <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>
    <select id="getNewOrder" resultType="cn.ezhizao.project.business.workOrder.domain.BizWorkOrder">
        select *from biz_work_order_update where is_new = 1;
    </select>
    <select id="getStopTaskOrder" resultType="cn.ezhizao.project.business.workOrder.domain.BizWorkOrder">
        select *from biz_work_order_update where is_stop = 1;
    </select>
</mapper>
