<?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.bonus.material.materialStation.mapper.ProAuthorizeMapper">

    <resultMap type="com.bonus.common.biz.domain.lease.LeasePublishInfo" id="LeasePublishInfoResult">
        <result property="id" column="id"/>
        <result property="code" column="code"/>
        <result property="taskId" column="task_id"/>
        <result property="leasePerson" column="lease_person"/>
        <result property="phone" column="phone"/>
        <result property="type" column="type"/>
        <result property="companyAuditBy" column="company_audit_by"/>
        <result property="companyAuditTime" column="company_audit_time"/>
        <result property="companyAuditRemark" column="company_audit_remark"/>
        <result property="deptAuditBy" column="dept_audit_by"/>
        <result property="deptAuditTime" column="dept_audit_time"/>
        <result property="deptAuditRemark" column="dept_audit_remark"/>
        <result property="directAuditBy" column="direct_audit_by"/>
        <result property="directAuditTime" column="direct_audit_time"/>
        <result property="directAuditRemark" column="direct_audit_remark"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="companyId" column="company_id"/>
        <result property="directId" column="direct_id"/>
        <result property="leaseType" column="lease_type"/>
        <result property="estimateLeaseTime" column="estimate_lease_time"/>
        <result property="costBearingParty" column="cost_bearing_party"/>
        <result property="leaseProject" column="pro_name"/>
        <result property="leaseProjectId" column="project_id"/>
        <result property="leaseUnit" column="unit_name"/>
        <result property="leaseUnitId" column="unit_id"/>
        <result property="agreementId" column="agreement_id"/>
        <result property="agreementCode" column="agreement_code"/>
        <result property="leaseSignUrl" column="lease_sign_url"/>
        <result property="leaseSignType" column="lease_sign_type"/>
        <result property="applyCode" column="apply_code"/>
    </resultMap>
    <insert id="insertProAuthorizeInfo" useGeneratedKeys="true" keyProperty="id">
        insert into pro_authorize_info(lease_id, team_id,team_name,url,file_name, status, create_by, create_time)
        values (#{leaseId}, #{teamId},#{teamName}, #{url}, #{fileName}, 1, #{createBy}, NOW())
    </insert>

    <insert id="insertProAuthorizeDetails">
        insert into pro_authorize_user(parent_id, name, id_number, phone,front_url, back_url, create_by,
                                          create_time, update_by, update_time)
        values (#{parentId}, #{name}, #{idNumber},#{phone}, #{frontUrl}, #{backUrl}, #{createBy}, NOW(),#{createBy},NOW())
    </insert>
    <update id="updateAuthorizeInfoSign">
        update pro_authorize_user set sign_url = #{signName}, sign_type = #{signType}
        where parent_id = #{parentId}
    </update>
    <update id="updateAuthorizeInfoSignStatus">
        update pro_authorize_info set sign_status = 1
        where id = #{parentId}
    </update>

    <delete id="deleteProAuthorizeDetails">
        delete
        from pro_authorize_user
        where parent_id = #{authId}
    </delete>

    <select id="getPublishList" resultType="com.bonus.common.biz.domain.lease.LeasePublishInfo"
            resultMap="LeasePublishInfoResult">
        select
        lai.id, lai.code, lai.task_id, lai.lease_person, lai.phone, lai.type, lai.company_audit_by,lai.apply_code,
        lai.company_audit_time, lai.company_audit_remark, lai.dept_audit_by, lai.dept_audit_time,
        lai.dept_audit_remark, lai.direct_audit_by, lai.direct_audit_time, lai.direct_audit_remark,
        lai.create_by, lai.create_time, lai.update_by, lai.update_time, lai.remark, lai.company_id,
        lai.direct_id, lai.lease_type, lai.estimate_lease_time, lai.cost_bearing_party, lai.lease_sign_url,
        lai.lease_sign_type,tt.task_id as taskId,
        lai.unit_id,lai.project_id,bu.unit_name, bp.pro_name, tt.task_status as taskStatus,bp.external_id as externalId,

        case tt.task_status
        when 1 then '未完成'
        when 2 then '已终止'
        when 3 then '已完成'
        end as taskStatusName,
        IFNULL(sum(lad.pre_num),0) as preCountNum,
        IFNULL(sum(lad.al_num),0) as alNum,
        GROUP_CONCAT(DISTINCT mt1.type_name) AS maTypeNames,
        bp.contract_part as contractPart,
        sd.dept_name as impUnitName,
        pai.id as authId
        from
        lease_apply_info lai
        left join tm_task tt on lai.task_id = tt.task_id
        left join lease_apply_details lad on lai.id = lad.parent_id
        left join tm_task_agreement tta on lai.task_id = tta.task_id
        left join bm_unit bu on bu.unit_id = lai.unit_id
        left join bm_project bp on bp.pro_id = lai.project_id
        left join sys_dept sd on sd.dept_id = bp.imp_unit
        left join sys_dict_data sda on tt.task_status = sda.dict_value
        and sda.dict_type = 'lease_task_status'
        left join ma_type mt on lad.type_id = mt.type_id and mt.del_flag = '0'
        left join ma_type mt1 on mt.parent_id = mt1.type_id and mt1.del_flag = '0'
        LEFT JOIN pro_authorize_info pai on pai.lease_id=lai.id and pai.del_flag='0'
        where tt.task_type = '19'
        and tt.task_status in (1, 2, 3)
        <if test="taskStatus != null">
            <if test="taskStatus==0">
                and pai.id is null
            </if>
            <if test="taskStatus==1">
                and pai.id is not null
            </if>
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND DATE_FORMAT( lai.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime}
        </if>
        <if test="projectIdList != null  and projectIdList.size() > 0">
            and bp.external_id in
            <foreach item="item" collection="projectIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY lai.id
        ORDER BY tt.task_status,tt.create_time desc
    </select>
    <select id="selectProAuthorizeInfo"
            resultType="com.bonus.material.materialStation.domain.ProAuthorizeInfo">
        SELECT pai.id,
               pai.lease_id as leaseId,
               pai.team_id  as teamId,
               pai.team_name as teamName,
               pai.url,
               pai.file_name as fileName
        FROM pro_authorize_info pai
        WHERE pai.lease_id = #{leaseId}
    </select>
    <select id="getDetailsList" resultType="com.bonus.material.materialStation.domain.ProAuthorizeDetails">
        SELECT id,
               `name`,
               id_number as idNumber,
               phone,
               front_url as frontUrl,
               back_url  as backUrl,
               sign_url as signName,
               sign_type as signType
        FROM pro_authorize_user
        WHERE parent_id = #{id}
    </select>
    <select id="selectProAuthorizeInfoView" resultType="com.bonus.material.materialStation.domain.ProAuthorizeInfo">
        SELECT pai.id,
               pai.lease_id as leaseId,
               pai.team_id  as teamId,
               pai.team_name as teamName,
               bu.unit_name as leaseUnit,
               bp.pro_name as leaseProject,
               su.nick_name as userName,
               DATE_FORMAT(pai.create_time, '%Y-%m-%d') as createTime
        FROM pro_authorize_info pai
                 left join lease_apply_info lai on pai.lease_id = lai.id
                 left join tm_task tt on lai.task_id = tt.task_id
                 left join tm_task_agreement tta on lai.task_id = tta.task_id
                 left join bm_unit bu on bu.unit_id = lai.unit_id
                 left join bm_project bp on bp.pro_id = lai.project_id
                 left join sys_user su on pai.create_by = su.user_id
        WHERE pai.lease_id = #{leaseId}
    </select>
    <select id="getAuthorList" resultType="com.bonus.material.materialStation.domain.ProAuthorizeInfo">
        SELECT pai.id,
               pai.lease_id as leaseId,
               pai.team_id  as teamId,
               pai.team_name as teamName,
               bu.unit_name as leaseUnit,
               bp.pro_name as leaseProject,
               su.nick_name as userName,
               lai.code as code,
               lai.create_by as createBy,
               lai.create_time createTime,
               pai.sign_status as signStatus
        FROM pro_authorize_info pai
                 left join lease_apply_info lai on pai.lease_id = lai.id
                 left join tm_task tt on lai.task_id = tt.task_id
                 left join tm_task_agreement tta on lai.task_id = tta.task_id
                 left join bm_unit bu on bu.unit_id = lai.unit_id
                 left join bm_project bp on bp.pro_id = lai.project_id
                 left join sys_user su on pai.create_by = su.user_id
        WHERE
            pai.status = 1
            <if test="keyWord != null and keyWord != ''">
                AND (
                pai.team_name LIKE concat('%', #{keyWord}, '%') or
                lai.code LIKE concat('%', #{keyWord}, '%')
                )
            </if>
            <if test="signStatus != null">
                    and pai.sign_status = #{signStatus}
            </if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                AND DATE_FORMAT( lai.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime}
            </if>
        order by pai.create_time desc
    </select>
</mapper>