<?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.workOrder.mapper.BizWorkOrderMapper">

    <resultMap type="cn.ezhizao.project.business.workOrder.domain.BizWorkOrder" id="BizWorkOrderResult">
        <id column="id" property="id"/>
        <result column="company_id" jdbcType="BIGINT" property="companyId"/>
        <collection property="records"   select="getRecords" column="id"/>
    </resultMap>

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

    <select id="getList" parameterType="BizWorkOrder" 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.alter_type,t6.address_style,t6.fiction_address_id,t6.province as inputProvince,t6.city as inputCity,t6.district as  inputDistrict
        FROM biz_work_order 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_detail t6 ON t6.id = t1.contract_detail_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 and t6.from_id = 0
            <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="type != null ">AND t1.type = #{type}</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>
            <if test="status != null and status > 0">
                and exists (select 1 from biz_work_order_record t6 where t6.deleted = 0 and t6.work_order_id = t1.id
                <if test="year != null">
                and t6.year = #{year}
                </if>
                <if test="month != null">
                and t6.month = #{month}
                </if>
                and t6.status = #{status} and t6.from_id = 0)
            </if>
            <if test="status != null and status == 0">
                and not exists (select 1 from biz_work_order_record t6 where t6.deleted = 0 and t6.work_order_id = t1.id
                <if test="year != null">
                    and t6.year = #{year}
                </if>
                <if test="month != null">
                    and t6.month = #{month}
                </if>
                and t6.status > 0 and t6.from_id = 0)
            </if>
        </trim>
    </select>

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

    <update id="setContinue" >
        UPDATE  biz_work_order SET is_continue =#{state}
        where id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
             #{id}
        </foreach>
    </update>


    <select id="getIndexList" parameterType="BizWorkOrder" 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
        FROM biz_work_order 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
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0
            <if test="id != null "> AND t1.id = #{id}</if>
            <if test="tenantId != null "> AND t1.tenant_id = #{tenantId}</if>
            <if test="companyId != null "> AND t1.company_id = #{companyId}</if>
            <if test="tenantId != null "> AND t1.tenant_id = #{tenantId}</if>
            <if test=" serviceId != null">AND t1.service_id = #{serviceId}</if>
            <if test="workOrderNo != null  and workOrderNo != ''">AND t1.work_order_no = #{workOrderNo}</if>
            <if test="companyName != null and companyName != '' "> AND t2.name like concat('%', #{companyName}, '%')</if>
            AND NOT EXISTS (
            SELECT 1 FROM biz_entrust t7 WHERE t7.work_order_id =t1.id
            )
        </trim>

    </select>
    <select id="getList2" parameterType="BizWorkOrder" 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 FROM biz_work_order 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
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 and t1.is_stop = 0
            <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="type != null ">AND t1.type = #{type}</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>

    <delete id="physicalDelete">
        DELETE FROM biz_work_order
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="id != null">
                id = #{id} AND
            </if>
            <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>
    <insert id="insertTasks">
        <foreach collection="tableValues" item="item" index="index" separator=";">
            insert into ${item.tableName} (
                id,
                company_id,
                work_order_id,
                tenant_id,
                create_time,
                creator_id
            ) values (
                #{item.id},
                #{item.companyId},
                #{item.workOrderId},
                #{item.tenantId},
                sysdate(),
                #{item.creatorId}
            )
<!--            select-->
<!--                #{item.id},-->
<!--                #{item.workOrderId},-->
<!--                company_id,-->
<!--                tenant_id,-->
<!--                sysdate(),-->
<!--                #{item.creatorId}-->
<!--            from biz_work_order where id = #{item.workOrderId}-->
<!--            and not exists (select 1 from ${item.tableName} where ${item.tableName}.company_id = biz_work_order.company_id and is_stop = 0
            )-->
        </foreach>
    </insert>

    <update id="stopTasks">
        <foreach collection="tableValues" item="item" index="index" separator=";">
            update ${item.tableName} set is_stop = 1 where work_order_id = #{item.workOrderId}
        </foreach>
    </update>
<!--    20241114 设置是否延续的时候用的-->
    <update id="stopLoopTasks">
        <foreach collection="tableValues" item="item" index="index" separator=";">
            update ${item.tableName} set is_stop = 1,deleted = 1 where work_order_id = #{item.workOrderId}
        </foreach>
    </update>
    <update id="updateByTenantId">
        update biz_work_order set contract_id = 0,contract_detail_id = 0  where tenant_id = #{tenantId}
    </update>
    <update id="updateByWorkOrderId"
            parameterType="java.util.List">
        update biz_work_order set is_stop = 1,no_contract = 1,is_continue=0
        WHERE id in
        <foreach collection="ids" item="id" index="index" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>
