<?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"/>
    </resultMap>


    <select id="selectStarMonthList" 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, t7.account_name as from_company_name
        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 biz_tenant t7 on t7.id = t1.tenant_id
        left join sys_user t5 on t5.user_id = t1.service_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            (t1.deleted = 0 or t1.deleted = 1)
            <if test="id != null ">AND t1.id = #{id}</if>
            <if test="ids != null and ids.size() > 0">
                AND t1.id IN
                <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
                    #{item,jdbcType=BIGINT}
                </foreach>
            </if>
            <if test="companyName != null and companyName!=''">AND t2.name like concat('%', #{companyName}, '%')</if>
            <if test="tenantId != null ">
                <!--                AND exists (select 1 from biz_entrust t1.tenant_id = tenant_id where )-->
                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="noContract != null ">AND t1.no_contract = #{noContract}</if>
            <if test="workOrderNo != null  and workOrderNo != ''">AND t1.work_order_no = #{workOrderNo}</if>
            <if test="taskTypeId != null ">AND t1.task_type_id = #{taskTypeId}</if>
            <if test="startMonth != null ">AND t1.start_month &lt;= #{startMonth}</if>
            <if test="startMonth != null ">AND t1.end_month &gt;= #{startMonth}</if>
            <if test="monthNum != null ">AND t1.month_num = #{monthNum}</if>
            <if test="isStop != null ">AND t1.is_stop = #{isStop}</if>
            <if test="currentMonth != null">AND t1.start_month != 0 AND DATE_FORMAT(#{currentMonth}, '%Y-%m-%d') &gt;=
                (case when t1.task_type_id
                = 1 then DATE_ADD(t1.start_month,interval 1 month) else t1.start_month end) and
                DATE_FORMAT(#{currentMonth}, '%Y-%m-%d') &lt;=
                (case when t1.end_month = 0 then DATE_FORMAT(#{currentMonth}, '%Y-%m-%d') when t1.task_type_id = 1 then
                DATE_ADD(t1.end_month,interval 1 month) else t1.end_month end)
            </if>
            <if test="toTenantId != null and currentMonth != null">
                AND exists (select 1 from biz_entrust t6 where t6.deleted = 0 and t6.to_tenant_id = #{toTenantId} and
                t6.work_month = #{currentMonth} and t1.id = t6.work_order_id )
            </if>
            <if test="toTenantId != null">
                AND exists (select 1 from biz_entrust t6 where t6.deleted = 0 and t6.to_tenant_id = #{toTenantId} and
                t1.id = t6.work_order_id )
            </if>
        </trim>
    </select>

    <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, t7.account_name as
        from_company_name,t6.alter_type,t6.address_style,
        t6.fiction_address_id,t6.province as inputProvince,t6.city as inputCity,t6.district as inputDistrict,
        COALESCE(t8.account_name, t5.nick_name) as toAccountName

        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 biz_tenant t7 on t7.id = t1.tenant_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
        left join (select tt1.*,tt2.account_name from biz_entrust tt1 LEFT JOIN biz_tenant tt2
        on tt1.to_tenant_id =tt2.id and tt1.deleted = 0) t8 on t8.work_order_id = t1.id and t8.deleted = 0 and
        t8.task_type_id = t1.task_type_id and t8.work_month = #{workDate}
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 and t2.deleted = 0
            <if test="id != null ">AND t1.id = #{id}</if>
            <if test="ids != null and ids.size() > 0">
                AND t1.id IN
                <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
                    #{item,jdbcType=BIGINT}
                </foreach>
            </if>
            <if test="toAccountName != null and toAccountName!=''">AND (COALESCE ( t8.account_name, t5.nick_name ) LIKE
                concat( '%', #{toAccountName}, '%' ))
            </if>

            <if test="fromCompanyName != null and fromCompanyName!=''">AND t7.account_name LIKE
                concat( '%', #{fromCompanyName}, '%' )
            </if>
            <if test="companyName != null and companyName!=''">AND t2.name like concat('%', #{companyName}, '%')</if>
            <if test="tenantId != null ">
                <!--                AND exists (select 1 from biz_entrust t1.tenant_id = tenant_id where )-->
                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="noContract != null ">AND t1.no_contract = #{noContract}</if>
            <if test="workOrderNo != null  and workOrderNo != ''">AND t1.work_order_no = #{workOrderNo}</if>
            <if test="taskTypeId != null ">AND t1.task_type_id = #{taskTypeId}</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="isStop != null ">AND t1.is_stop = #{isStop}</if>
            <if test="currentMonth != null">AND t1.start_month != 0 AND DATE_FORMAT(#{currentMonth}, '%Y-%m-%d') &gt;=
                (case when t1.task_type_id
                = 1 then DATE_ADD(t1.start_month,interval 1 month) else t1.start_month end) and
                DATE_FORMAT(#{currentMonth}, '%Y-%m-%d') &lt;=
                (case when t1.is_continue=1 then #{currentMonth} when t1.end_month = 0 then DATE_FORMAT(#{currentMonth},
                '%Y-%m-%d') when t1.task_type_id = 1 then
                DATE_ADD(t1.end_month,interval 1 month) else t1.end_month end)
            </if>
            <if test="toTenantId != null and currentMonth != null">
                AND exists (select 1 from biz_entrust t6 where t6.deleted = 0 and t6.to_tenant_id = #{toTenantId} and
                t6.work_month = #{currentMonth} and t1.id = t6.work_order_id )
            </if>
            <if test="toTenantId != null">
                AND exists (select 1 from biz_entrust t6 where t6.deleted = 0 and t6.to_tenant_id = #{toTenantId} and
                t1.id = t6.work_order_id )
            </if>
        </trim>
        ORDER BY t1.create_time DESC
    </select>
    <select id="getHistoryList" resultType="cn.ezhizao.project.business.workOrder.domain.vo.EntrustRes">
        SELECT
        t1.id,t1.work_order_id,t2.account_name as toAccountName,
        t3.name as companyName,t3.social_credit_code,
        t4.name as task_type_name,t5.type,t5.start_month,
        t5.end_month,t5.month_num,t7.account_name as
        fromCompanyName
        FROM
        biz_entrust t1
        LEFT JOIN biz_tenant t2 on t1.to_tenant_id =t2.id
        LEFT Join biz_company t3 on t3.id = t1.company_id
        left join biz_task_type t4 on t4.id = t1.task_type_id
        LEFT JOIN biz_work_order t5 on t5.id = t1.work_order_id
        LEFT JOIN biz_tenant t7 on t1.from_tenant_id =t7.id
        <where>
            t1.deleted = 0 and t5.id is not null
            AND t5.type = 1
            AND t5.start_month != 0
            and t5.deleted = 0
            <if test="fromTenantId != null and fromTenantId != ''">
                AND t1.from_tenant_id = #{fromTenantId}
            </if>
            <if test="toTenantId != null and toTenantId != ''">
                AND t1.to_tenant_id = #{toTenantId}
            </if>
            <if test="fromCompanyName != null and fromCompanyName != ''">
                AND t7.account_name like concat('%', #{fromCompanyName}, '%')
            </if>
            <if test="date == null">
                and t5.start_month &lt; DATE_FORMAT(CURRENT_DATE (), '%Y-%m-01')
                AND MONTH ( t1.work_month ) &lt;MONTH(CURRENT_DATE ())
            </if>
            <if test="date != null">
                AND t1.work_month = #{date}
            </if>
            <if test="companyName != null and companyName != ''">
                AND t3.name like concat('%', #{companyName}, '%')
            </if>
            <if test="toAccountName != null and toAccountName!=''">
                AND t2.account_name like concat( '%', #{toAccountName}, '%' )
            </if>
        </where>
        ORDER BY
        t5.start_month DESC

    </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,jdbcType=BIGINT},
            #{item.companyId,jdbcType=BIGINT},
            #{item.workOrderId,jdbcType=BIGINT},
            #{item.tenantId,jdbcType=BIGINT},
            sysdate(),
            #{item.creatorId,jdbcType=BIGINT}
            )
        </foreach>
    </insert>

    <update id="stopTasks">
        <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="updateTasks">
        <foreach collection="tableValues" item="item" index="index" separator=";">
            update ${item.tableName} set is_stop = 0,deleted = 0 where work_order_id = #{item.workOrderId}
        </foreach>
    </update>
    <update id="updateByIdAndDeleted">
        update biz_work_order set deleted = #{deleted},is_continue = #{isContinue},is_stop=#{isStop}
        where id = #{id}
    </update>
</mapper>
