select
bp.pro_center AS departName,
lai.id, lai.code, lai.task_id, lai.lease_person, lai.phone, lai.type, lai.company_audit_by,
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,
lai.project_id as proId, bp.pro_name as projectName, tt.task_status as taskStatus,
sda.dict_label as taskStatusName,
lai.team_id as teamId,
bt.unit_name as teamName,
IFNULL(sum(lad.pre_num),0) as preCountNum,
IFNULL(lod.num,0) as alNum,
IFNULL(sum(lad.pre_num),0) - IFNULL(lod.num,0) as waitCountNum,
GROUP_CONCAT(DISTINCT mt1.type_name) as maTypeNames,
bp.contract_part as contractPart,
sd.dept_name as impUnitName,
tta.agreement_id,
bp.external_id,
bp.imp_unit,
bt.bzz_idcard,
bai.unit_id,
bai.project_id as projectId,
bt.link_man as relName,
CASE WHEN lai.lease_sign_url IS NOT NULL
THEN 0
ELSE 1
END as isElectronicSign,
lai.sub_unit_name as subUnitName
from
clz_lease_apply_info lai
left join tm_task tt on lai.task_id = tt.task_id
left join tm_task_agreement tta ON tt.task_id = tta.task_id
left join clz_bm_agreement_info bai ON tta.agreement_id = bai.agreement_id
left join clz_lease_apply_details lad on lai.id = lad.parent_id
LEFT JOIN (SELECT IFNULL( sum(out_num ), 0) AS num, parent_id from clz_lease_out_details
WHERE is_finished = 1 GROUP BY parent_id) lod ON lai.id = lod.parent_id
left join bm_project bp on bp.pro_id = lai.project_id
left join bm_unit bt on bt.unit_id = lai.team_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 = 'clz_lease_apply_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'
select
lad.id, lad.parent_id, mt.type_id, mt.type_name, mt2.type_name as ma_type_name,
mt.manage_type as manageType,
IFNULL(lad.pre_num,0) as pre_num,
IFNULL(lad.audit_num,0) as audit_num,
IFNULL(lad.al_num,0) as al_num,
IFNULL(lad.status,0) as status, mt.unit_name ,mt.unit_value,
lad.create_by, lad.create_time, lad.update_by, lad.update_time, lad.remark as remark, lad.company_id
from
clz_lease_apply_details lad
left join
ma_type mt on lad.type_id = mt.type_id and mt.`level` = '4' and mt.del_flag = '0'
left join
ma_type mt2 on mt2.type_id = mt.parent_id and mt2.`level` = '3' and mt2.del_flag = '0'
JOIN ma_type_keeper mtk ON mtk.type_id = lad.type_id AND mtk.user_id = #{userId}
insert into clz_lease_apply_info
code,
task_id,
lease_person,
phone,
type,
company_audit_by,
company_audit_time,
company_audit_remark,
dept_audit_by,
dept_audit_time,
dept_audit_remark,
direct_audit_by,
direct_audit_time,
direct_audit_remark,
create_by,
create_time,
update_by,
update_time,
remark,
company_id,
direct_id,
lease_type,
estimate_lease_time,
cost_bearing_party,
team_id,
project_id,
lease_style,
pick_type,
sub_unit_name,
#{code},
#{taskId},
#{leasePerson},
#{phone},
#{type},
#{companyAuditBy},
#{companyAuditTime},
#{companyAuditRemark},
#{deptAuditBy},
#{deptAuditTime},
#{deptAuditRemark},
#{directAuditBy},
#{directAuditTime},
#{directAuditRemark},
#{createBy},
#{createTime},
#{updateBy},
#{updateTime},
#{remark},
#{companyId},
#{directId},
#{leaseType},
#{estimateLeaseTime},
#{costBearingParty},
#{teamId},
#{proId},
#{leaseStyle},
#{pickType},
#{subUnitName},
insert into clz_lease_out_details
parent_id,
type_id,
ma_id,
out_num,
out_type,
create_by,
update_by,
remark,
company_id,
car_code,
publish_task,
is_finished,
create_time,
update_time
#{parentId},
#{typeId},
#{maId},
#{outNum},
#{outType},
#{createBy},
#{updateBy},
#{remark},
#{companyId},
#{carCode},
#{publishTask},
#{isFinished},
NOW(),
NOW()
insert into clz_lease_apply_details
(parent_id, type_id, pre_num, al_num, `status`, create_by, create_time, update_by,
update_time, remark, company_id)
values
(#{item.parentId,jdbcType=INTEGER}, #{item.typeId,jdbcType=INTEGER}, #{item.preNum,jdbcType=INTEGER},
#{item.alNum,jdbcType=INTEGER}, #{item.status,jdbcType=VARCHAR}, #{item.createBy,jdbcType=VARCHAR},
NOW(), #{item.updateBy,jdbcType=VARCHAR}, NOW(),
#{item.remark,jdbcType=VARCHAR}, #{item.companyId,jdbcType=INTEGER})
insert into clz_slt_agreement_info (agreement_id,type_id,ma_id,num,start_time,status,lease_id,lease_price,buy_price,is_slt,company_id,lease_type,create_time,publish_task, lease_style)
values (#{agreementId},#{record.typeId},#{record.maId},#{record.outNum},now(),0,#{record.parentId},#{ma.finalPrice},#{ma.buyPrice},'0',#{record.companyId},#{record.leaseType},now(),#{record.publishTask}, #{record.leaseStyle});
insert into clz_lease_apply_details
(parent_id, type_id, pre_num, al_num, `status`, create_by, create_time, update_by,
update_time, remark, company_id)
values (#{parentId}, #{typeId}, #{preNum},#{alNum}, #{status}, #{createBy},NOW(), #{updateBy}, NOW(),
#{remark}, #{companyId})
update clz_lease_apply_info
code = #{code},
task_id = #{taskId},
lease_person = #{leasePerson},
phone = #{phone},
`type` = #{type},
company_audit_by = #{companyAuditBy},
company_audit_time = #{companyAuditTime},
company_audit_remark = #{companyAuditRemark},
dept_audit_by = #{deptAuditBy},
dept_audit_time = #{deptAuditTime},
dept_audit_remark = #{deptAuditRemark},
direct_audit_by = #{directAuditBy},
direct_audit_time = #{directAuditTime},
direct_audit_remark = #{directAuditRemark},
update_by = #{updateBy},
update_time = now(),
remark = #{remark},
company_id = #{companyId},
direct_id = #{directId},
lease_type = #{leaseType},
estimate_lease_time = #{estimateLeaseTime},
cost_bearing_party = #{costBearingParty},
team_id = #{teamId},
project_id = #{projectId},
sub_unit_name = #{subUnitName},
where id = #{id}
update clz_lease_apply_info
lease_sign_url = #{leaseSignUrl},
lease_sign_type = #{leaseSignType},
update_by = #{updateBy},
update_time = #{updateTime},
where id = #{id}
update clz_slt_agreement_info
set num = #{num},
update_time = now()
where id = #{id}
UPDATE
clz_lease_apply_details
SET
pre_num = pre_num + #{record.outNum},
al_num = al_num + #{record.outNum},
update_time = now()
WHERE
parent_id = #{record.parentId} and type_id = #{record.typeId}
UPDATE
clz_lease_out_details
SET
out_num = out_num + #{record.outNum},
update_time = now()
WHERE
parent_id = #{record.parentId} and type_id = #{record.typeId}
delete from clz_lease_apply_details where parent_id = #{id}
delete from clz_lease_out_details where parent_id = #{id}
delete from clz_lease_apply_info where id = #{id}
delete from clz_slt_agreement_info where lease_id = #{id}