<?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.sgzb.app.mapper.AppBackApplyAppMapper">
  <resultMap id="BaseResultMap" type="com.bonus.sgzb.app.domain.BmAgreementInfo">
    <!--@mbg.generated-->
    <!--@Table bm_agreement_info-->
    <id column="agreement_id" jdbcType="INTEGER" property="agreementId" />
    <result column="agreement_code" jdbcType="VARCHAR" property="agreementCode" />
    <result column="sign_time" jdbcType="VARCHAR" property="signTime" />
    <result column="unit_id" jdbcType="INTEGER" property="unitId" />
    <result column="project_id" jdbcType="INTEGER" property="projectId" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy" />
    <result column="lease_day" jdbcType="INTEGER" property="leaseDay" />
    <result column="plan_start_time" jdbcType="TIMESTAMP" property="planStartTime" />
    <result column="contract_code" jdbcType="VARCHAR" property="contractCode" />
    <result column="auth_person" jdbcType="VARCHAR" property="authPerson" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="company_id" jdbcType="INTEGER" property="companyId" />
    <result column="unit_id" jdbcType="INTEGER" property="unitId" />
    <result column="unit_name" jdbcType="VARCHAR" property="unitName" />
    <result column="lot_id" jdbcType="INTEGER" property="lotId" />
    <result column="lot_name" jdbcType="VARCHAR" property="lotName" />

  </resultMap>
  <insert id="insertTask" keyColumn="task_id" keyProperty="taskId" parameterType="com.bonus.sgzb.app.domain.TmTask" useGeneratedKeys="true">
    insert into tm_task
    (
    <if test="taskType != null">
      task_type,
    </if>
    <if test="taskStatus != null">
      task_status,
    </if>
    <if test="code != null and code != ''">
      code,
    </if>
    <if test="createBy != null and createBy != ''">
      create_by,
    </if>
    <if test="updateBy != null and updateBy != ''">
      update_by,
    </if>
    <if test="updateTime != null">
      update_time,
    </if>
    <if test="remark != null and remark != ''">
      remark,
    </if>
    <if test="companyId != null">
      company_id,
    </if>
      create_time
    ) values (
    <if test="taskType != null">
      #{taskType},
    </if>
    <if test="taskStatus != null">
      #{taskStatus},
    </if>
    <if test="code != null and code != ''">
      #{code},
    </if>
    <if test="createBy != null and createBy != ''">
      #{createBy},
    </if>
    <if test="updateBy != null and updateBy != ''">
      #{updateBy},
    </if>
    <if test="updateTime != null">
      #{updateTime},
    </if>
    <if test="remark != null and remark != ''">
      #{remark},
    </if>
    <if test="companyId != null">
      #{companyId},
    </if>
    NOW()
    )
  </insert>
  <insert id="insertTaskAgreement">
    insert into tm_task_agreement
    (
    <if test="taskId != null">
      task_id,
    </if>
    <if test="agreementId != null">
      agreement_id,
    </if>
    <if test="createBy != null and createBy != ''">
      create_by,
    </if>
    <if test="updateBy != null and updateBy != ''">
      update_by,
    </if>
    <if test="updateTime != null">
      update_time,
    </if>
    <if test="remark != null and remark != ''">
      remark,
    </if>
    <if test="companyId != null">
      company_id,
    </if>
    create_time
    ) values (
    <if test="taskId != null">
      #{taskId},
    </if>
    <if test="agreementId != null">
      #{agreementId},
    </if>
    <if test="createBy != null and createBy != ''">
      #{createBy},
    </if>
    <if test="updateBy != null and updateBy != ''">
      #{updateBy},
    </if>
    <if test="updateTime != null">
      #{updateTime},
    </if>
    <if test="remark != null and remark != ''">
      #{remark},
    </if>
    <if test="companyId != null">
      #{companyId},
    </if>
    NOW()
    )
  </insert>
  <insert id="insertBackApply">
    insert into back_apply_info
    (
    <if test="code != null">
      code,
    </if>
    <if test="taskId != null">
      task_id,
    </if>
    <if test="backPerson != null and backPerson != ''">
      back_person,
    </if>
    <if test="backTime != null">
      back_time,
    </if>
    <if test="phone != null and phone != ''">
      phone,
    </if>
    <if test="directAuditBy != null">
      direct_audit_by,
    </if>
    <if test="directAuditTime != null">
      direct_audit_time,
    </if>
    <if test="directAuditRemark != null">
      direct_audit_remark,
    </if>
    <if test="createBy != null and createBy != ''">
      create_by,
    </if>
    <if test="updateBy != null and updateBy != ''">
      update_by,
    </if>
    <if test="updateTime != null">
      update_time,
    </if>
    <if test="remark != null and remark != ''">
      remark,
    </if>
    <if test="companyId != null">
      company_id,
    </if>
    create_time
    ) values (
    <if test="code != null">
      #{code},
    </if>
    <if test="taskId != null">
      #{taskId},
    </if>
    <if test="backPerson != null and backPerson != ''">
      #{backPerson},
    </if>
    <if test="backTime != null">
      #{backTime},
    </if>
    <if test="phone != null and phone != ''">
      #{phone},
    </if>
    <if test="directAuditBy != null">
      #{directAuditBy},
    </if>
    <if test="directAuditTime != null">
      #{directAuditTime},
    </if>
    <if test="directAuditRemark != null">
      #{directAuditRemark},
    </if>
    <if test="createBy != null and createBy != ''">
      #{createBy},
    </if>
    <if test="updateBy != null and updateBy != ''">
      #{updateBy},
    </if>
    <if test="updateTime != null">
      #{updateTime},
    </if>
    <if test="remark != null and remark != ''">
      #{remark},
    </if>
    <if test="companyId != null">
      #{companyId},
    </if>
    NOW()
    )
  </insert>

  <insert id="upload">
    insert into back_apply_details
    (
    <if test="parentId != null">
      parent_id,
    </if>
    <if test="typeId != null">
      type_id,
    </if>
    <if test="preNum != null">
      pre_num,
    </if>
    status,
    <if test="createBy != null and createBy != ''">
      create_by,
    </if>
    <if test="createBy != null and createBy != ''">
      update_by,
    </if>
      update_time,
    <if test="remark != null and remark != ''">
      remark,
    </if>
    <if test="companyId != null">
      company_id,
    </if>
    create_time
    )
    values (
    <if test="parentId != null">
      #{parentId},
    </if>
    <if test="typeId != null">
      #{typeId},
    </if>
    <if test="preNum != null">
      #{preNum},
    </if>
    '0',
    <if test="createBy != null and createBy != ''">
      #{createBy},
    </if>
    <if test="createBy != null and createBy != ''">
      #{createBy},
    </if>
      NOW(),
    <if test="remark != null and remark != ''">
      #{remark},
    </if>
    <if test="companyId != null">
      #{companyId},
    </if>
    NOW()
           )
  </insert>
    <update id="audit">
      UPDATE tm_task tt
        LEFT JOIN back_apply_info bai ON bai.task_id = tt.task_id
        LEFT JOIN back_apply_details bad on bad.parent_id=bai.id
        SET bai.status = #{status},bad.audit_num=bad.pre_num,bai.direct_audit_by=#{createBy},bai.direct_audit_time=NOW()
      WHERE
        bai.id = #{id}
    </update>

  <update id="refuse">
    UPDATE tm_task tt
      LEFT JOIN back_apply_info bai ON bai.task_id = tt.task_id
      LEFT JOIN back_apply_details bad on bad.parent_id=bai.id
      SET bai.status = #{status},bai.direct_audit_remark=#{directAuditRemark},bad.audit_num=bad.pre_num,bai.direct_audit_by=#{createBy},bai.direct_audit_time=NOW()
    WHERE
      bai.id = #{id}
  </update>
  <select id="examineListByUser">
    SELECT
      bai.id,
      bai.status,
      bai.back_person as backPerson,
      bai.phone,
      bpl.lot_name as lotName,
      bui.unit_name as unitName,
      bai.back_time as backTime,
      bagi.plan_start_time as planStartTime,
      tt.task_status as taskStatus,
      GROUP_CONCAT(DISTINCT bad.type_id) as typeId,
      GROUP_CONCAT(CONCAT_WS('/',  IFNULL(mt2.type_name, ''))) AS typeName,
      SUM(DISTINCT bad.pre_num) AS num,
      bai.direct_audit_remark as directAuditRemark
    FROM
      back_apply_info bai
        LEFT JOIN back_apply_details bad on bad.parent_id=bai.id
        LEFT JOIN tm_task tt on tt.task_id=bai.task_id
        LEFT JOIN tm_task_agreement tta on tta.task_id=tt.task_id
        LEFT JOIN bm_agreement_info bagi on bagi.agreement_id=tta.agreement_id
        LEFT JOIN bm_project_lot bpl on bpl.lot_id=bagi.project_id
        LEFT JOIN bm_unit_info bui on bui.unit_id=bagi.unit_id
        LEFT JOIN sys_user us on us.user_id=bai.create_by
        LEFT JOIN ma_type mt1 ON mt1.type_id=bad.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id=mt1.parent_id
    WHERE
      bad.type_id is not null and tt.create_by = #{createBy}
    GROUP BY bai.id, us.user_name, bai.phone, bpl.lot_name, bui.unit_name, bagi.plan_start_time
    ORDER BY bai.create_time desc
  </select>
  <update id="updateBackApply">
    UPDATE tm_task tt
      LEFT JOIN back_apply_info bai ON bai.task_id = tt.task_id
      LEFT JOIN back_apply_details bad on bad.parent_id=bai.id
      SET tt.task_status = 38
    WHERE
      bai.id = #{id}
  </update>
  <update id="updateTmTask">
    UPDATE tm_task tt
      INNER JOIN back_apply_info bai ON bai.task_id = tt.task_id
      SET tt.task_status = #{taskStatus}
    WHERE
      bai.id = #{id}
  </update>
  <update id="updateBackInfo">
    update back_apply_info set company_id = #{companyId} where id = #{parentId}
  </update>

  <delete id="del">
      DELETE
      FROM tm_task_agreement
      WHERE task_id IN (SELECT task_id FROM back_apply_info WHERE id = #{id});

      DELETE
      FROM tm_task
      WHERE task_id IN (SELECT task_id FROM back_apply_info WHERE id = #{id});

      DELETE
      FROM back_apply_details
      WHERE parent_id IN (SELECT id FROM back_apply_info WHERE id = #{id});

      DELETE
      FROM back_apply_info
      WHERE id = #{id};
  </delete>

  <select id="getbackUnit" resultType="com.bonus.sgzb.app.domain.BmAgreementInfo">
    SELECT bui.unit_id,
           bui.unit_name,
           bai.agreement_id,
           bai.agreement_code,
           bpl.lot_id,
           bpl.lot_name
    FROM bm_unit_info bui
           LEFT JOIN bm_unit_person bup on bup.unit_id = bui.unit_id
           LEFT JOIN bm_agreement_info bai on bai.unit_id=bui.unit_id
           LEFT JOIN bm_project_lot bpl on bpl.lot_id=bai.project_id
    WHERE bup.user_id = #{userId}
      and bui.del_flag = '0'
  </select>

  <select id="getbackPro" resultType="com.bonus.sgzb.app.domain.BmAgreementInfo">
    SELECT
      bpl.lot_id,
      bpl.lot_name
    FROM
      bm_unit_info bui
        LEFT JOIN bm_agreement_info bai ON bai.unit_id = bui.unit_id
        LEFT JOIN bm_project_lot bpl on bai.project_id=bpl.lot_id
    WHERE
      bui.unit_id=#{unitId}
      and bui.del_flag='0'
  </select>
  <select id="getbackList" resultType="com.bonus.sgzb.base.api.domain.BackApplyInfo">
    SELECT
      bai.id,
      bai.`code`,
      bai.back_person as backPerson,
      bai.phone,
      bpl.lot_id,
      bpl.lot_name as lotName,
      bui.unit_id as unitId,
      bui.unit_name as unitName,
      bai.back_time as backTime,
      tt.task_status as taskStatus,
      tta.agreement_id as agreementId,
      GROUP_CONCAT(DISTINCT bad.type_id) as typeId,
      GROUP_CONCAT(CONCAT_WS('/',  IFNULL(mt2.type_name, ''))) AS typeName
    FROM
      back_apply_info bai
        LEFT JOIN back_apply_details bad on bad.parent_id=bai.id
        LEFT JOIN tm_task tt on tt.task_id=bai.task_id
        LEFT JOIN tm_task_agreement tta on tta.task_id=tt.task_id
        LEFT JOIN bm_agreement_info bagi on bagi.agreement_id=tta.agreement_id
        LEFT JOIN bm_project_lot bpl on bpl.lot_id=bagi.project_id
        LEFT JOIN bm_unit_info bui on bui.unit_id=bagi.unit_id
        LEFT JOIN sys_user us on us.user_id=bai.create_by
        LEFT JOIN ma_type mt1 ON mt1.type_id=bad.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id=mt1.parent_id
    WHERE 1=1
    <if test="createBy != 1 ">
      and tt.create_by = #{createBy}
    </if>
    GROUP BY bai.id, us.user_name, bai.phone, bpl.lot_name, bui.unit_name, bagi.plan_start_time
    ORDER BY bai.create_time desc
  </select>

  <select id="materialList" resultType="com.bonus.sgzb.base.api.domain.BackApplyInfo">
    SELECT
      mt.type_id,
      mt.type_name as typeCode,
      mt.parent_id,
      mt.unit_name,
      mt.company_id,
      mt2.type_name,
      SUM(CASE WHEN sai.agreement_id = #{agreementId} AND sai.status = '0' THEN sai.num ELSE 0 END) AS num,
      mt.level
    FROM
      ma_type mt
        LEFT JOIN ma_type mt2 on mt.parent_id = mt2.type_id
        LEFT JOIN slt_agreement_info sai ON mt.type_id = sai.type_id
    WHERE
      EXISTS (
              SELECT 1
              FROM slt_agreement_info sai2
              WHERE sai2.type_id = mt.type_id AND sai2.agreement_id = #{agreementId} AND sai2.status = '0'
        )
    GROUP BY
      mt.type_id
  </select>

  <select id="view" resultType="com.bonus.sgzb.base.api.domain.BackApplyInfo">
    SELECT
      mt.type_name typeCode,
      CONCAT_WS('/',  IFNULL(mt2.type_name, '')) AS typeName,
      bad.pre_num as num
    FROM
      back_apply_details bad
        LEFT JOIN back_apply_info bai on bai.id=bad.parent_id
        LEFT JOIN ma_type mt on mt.type_id=bad.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id=mt.parent_id
    WHERE
      bai.id=#{id}
  </select>
  <select id="examineList" resultType="com.bonus.sgzb.base.api.domain.BackApplyInfo">
    SELECT
      bai.id,
      bai.code,
      bai.status,
      bai.back_person as backPerson,
      bai.phone,
      bpl.lot_name as lotName,
      bui.unit_name as unitName,
      bai.back_time as backTime,
      bagi.plan_start_time as planStartTime,
      tt.task_status as taskStatus,
      GROUP_CONCAT(bai.company_id) as companyId,
      GROUP_CONCAT(DISTINCT bad.type_id) as typeId,
      GROUP_CONCAT(CONCAT_WS('/',  IFNULL(mt2.type_name, ''))) AS typeName,
      SUM(bad.pre_num) AS num,
      bai.direct_audit_remark as directAuditRemark
    FROM
      back_apply_info bai
        LEFT JOIN back_apply_details bad on bad.parent_id=bai.id
        LEFT JOIN tm_task tt on tt.task_id=bai.task_id
        LEFT JOIN tm_task_agreement tta on tta.task_id=tt.task_id
        LEFT JOIN bm_agreement_info bagi on bagi.agreement_id=tta.agreement_id
        LEFT JOIN bm_project_lot bpl on bpl.lot_id=bagi.project_id
        LEFT JOIN bm_unit_info bui on bui.unit_id=bagi.unit_id
        LEFT JOIN sys_user us on us.user_id=bai.create_by
        LEFT JOIN ma_type mt1 ON mt1.type_id=bad.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id=mt1.parent_id
    WHERE
          bad.type_id is not null
    GROUP BY bai.id, us.user_name, bai.phone, bpl.lot_name, bui.unit_name, bagi.plan_start_time
    ORDER BY bai.create_time desc
  </select>
  <select id="examineView" resultType="com.bonus.sgzb.base.api.domain.BackApplyInfo">
    SELECT
      mt.type_name typeCode,
      CONCAT_WS('/',  IFNULL(mt2.type_name, '')) AS typeName,
      bad.type_id as typeId,
      mt.manage_type as manageType,
      bad.pre_num as preNum,
      bad.pre_num as num
    FROM
      back_apply_details bad
        LEFT JOIN back_apply_info bai on bai.id=bad.parent_id
        LEFT JOIN ma_type mt on mt.type_id=bad.type_id
        LEFT JOIN ma_type mt1 ON mt1.type_id=bad.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id=mt1.parent_id
    WHERE
      bai.id=#{id}
  </select>
  <select id="selectBackApplyInfo" resultType="com.bonus.sgzb.base.api.domain.BackApplyInfo">
    SELECT * FROM tm_task tt
        LEFT JOIN back_apply_info bai ON bai.task_id = tt.task_id
        LEFT JOIN back_apply_details bad on bad.parent_id=bai.id
    WHERE
      bai.id = #{id}
  </select>
  <select id="getTaskIdById" resultType="java.lang.Integer">
     SELECT bai1.status FROM  back_apply_info bai1
     INNER JOIN
     (SELECT * FROM back_apply_info bai2 WHERE bai2.id = #{id}) dd
     on bai1.task_id = dd.task_id
  </select>
  <select id="getMaTypeDetails" resultType="com.bonus.sgzb.app.domain.TmTask">
    SELECT
      mws2.type_id AS typeId,
      mws2.part_num AS partNum
    FROM
      ma_type mt1
        LEFT JOIN ma_whole_set mws2 ON mt1.type_id = mws2.parent_id
    WHERE
      mt1.type_id = #{typeId}
  </select>
  <select id="getMachineParts" resultType="com.bonus.sgzb.base.api.domain.MachinePart">
    SELECT
      mt.type_name AS typeModelName,
      mt.unit_name as unitName,
      m2.type_name As typeName
    FROM
      ma_type mt
        LEFT JOIN ma_type m2 ON mt.parent_id = m2.type_id
    WHERE
      mt.type_id = #{typeId}
  </select>


</mapper>