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,
bai.unit_id,bai.project_id,bu.unit_name, bp.pro_name,bai.agreement_id, bai.agreement_code, tt.task_status as taskStatus,
case tt.task_status
when 0 then '待审核'
when 1 then '待审核'
when 2 then '审核中'
when 3 then '已完成'
when 4 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
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_agreement_info bai on tta.agreement_id = bai.agreement_id
left join bm_unit bu on bu.unit_id = bai.unit_id
left join bm_project bp on bp.pro_id = bai.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'
select
lad.id, lad.parent_id, mt.type_id, mt.type_name, mt2.type_name as ma_type_name,
CASE mt.manage_type
WHEN 0 THEN
IFNULL(subquery0.num, 0)
ELSE
IFNULL(mt.storage_num, 0)
END as storage_num,
mt.manage_type as manageType,
(lad.pre_num - IF(lad.al_num IS NULL,'0',lad.al_num)) AS outNum,
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, lad.company_id
from
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'
left join (SELECT mt.type_id,
mt2.type_name AS typeName,
mt.type_name AS typeModelName,
count(mm.ma_id) num
FROM ma_machine mm
LEFT JOIN ma_type mt ON mt.type_id = mm.type_id
LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
WHERE mm.ma_code is not null and mm.ma_status in (1)
GROUP BY mt.type_id) AS subquery0 ON subquery0.type_id = mt.type_id
insert into 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,apply_code,supplier_time,supplier_place,unit_id,project_id,#{code},#{taskId},#{leasePerson},#{phone},#{type},#{companyAuditBy},#{companyAuditTime},#{companyAuditRemark},#{deptAuditBy},#{deptAuditTime},#{deptAuditRemark},#{directAuditBy},#{directAuditTime},#{directAuditRemark},#{createBy},#{createTime},#{updateBy},#{updateTime},#{remark},#{companyId},#{directId},#{leaseType},#{estimateLeaseTime},#{costBearingParty},#{applyCode},#{supplierTime},#{supplierPlace},#{unitId},#{projectId},
insert into lease_apply_details
(parent_id, type_id, pre_num, al_num, `status`, create_by, create_time, update_by,
update_time, remark, company_id,ma_type_ids,is_manual)
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},#{item.maTypeIdsStr,jdbcType=VARCHAR},
#{item.isManual,jdbcType=INTEGER})
insert into lease_publish_details
parent_id,type_id,new_type,num,lease_person,phone,create_by,create_time,update_by,update_time,remark,unit_id,project_id,month_order,publish_task,code,#{parentId},#{typeId},#{newTypeId},#{num},#{leasePerson},#{phone},#{createBy},#{createTime},#{updateBy},#{updateTime},#{remark},#{unitId},#{projectId},#{monthOrder},#{publishTask},#{code},
update 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},apply_code = #{applyCode},supplier_time = #{supplierTime},supplier_place = #{supplierPlace},
update_time = now(),
remark = #{remark},company_id = #{companyId},direct_id = #{directId},lease_type = #{leaseType},estimate_lease_time = #{estimateLeaseTime},cost_bearing_party = #{costBearingParty},
where id = #{id}
update tm_task
task_status = #{taskStatus},
where task_Id = #{taskId}
update lease_apply_details
publish_num = IFNULL(publish_num, 0) + #{num},new_type = #{newTypeId},
where parent_id = #{parentId} and type_id = #{typeId}
update
lease_apply_details
set
publish_num = IFNULL(publish_num, 0) - #{outNum}
where
parent_id = #{parentId}
and type_id = #{typeId}
insert into uni_org.sys_information_people
create_time,update_time,information_id,read_state,handle_state,receiver_username,update_user,is_examine,#{parentId},now(),now(),#{informationId},#{readState},#{handleState},#{receiverUsername},#{updateUser},#{isExample},
insert into uni_org.sys_information
id,create_time,create_user_name,title,content,web_app_id,type,extend,send_user_name,web_url,type_id,business_id,flow_state,rout,next_status,extend_name,#{id},now(),#{createUserName},#{title},#{content},#{webAppId},#{type},#{extend},#{sendUserName},#{webUrl},#{typeId},#{businessId},#{flowState},#{rout},#{nextStatus},#{extendName},
insert into uni_org.sys_information_people
id,create_time,update_time,information_id,read_state,handle_state,receiver_username,update_user,is_examine,#{id},now(),#{updateTime},#{informationId},#{readState},#{handleState},#{receiverUsername},#{updateUser},#{isExamine},
insert into lease_back_record
parent_id,type_id,new_type,ma_id,num,create_by,
create_time,
#{parentId},#{typeId},#{newTypeId},#{maId},#{num},#{createBy},
now(),
UPDATE uni_org.sys_information_people
SET
read_state = 1,
handle_state = 1,
is_examine =1,
update_user = #{receiverUsername},
update_time = now()
WHERE information_id = #{informationId}
UPDATE ma_machine
SET ma_status = '1',
update_time = now()
WHERE ma_id = #{maId}
update ma_type
SET storage_num = IFNULL(storage_num, 0 ) + #{alNum}
WHERE type_id = #{typeId}
update lease_apply_details
set al_num = al_num - #{outNum},
update_time = now(),
status = '1'
where parent_id = #{parentId}
and new_type = #{newTypeId}
update lease_apply_details
set al_num = al_num - #{outNum},
update_time = now(),
status = '1'
where parent_id = #{parentId}
and type_id = #{typeId}
DELETE
FROM
lease_publish_details
WHERE
parent_id = #{parentId}
and new_type = #{newTypeId}
and publish_task = #{publishTask}
DELETE
FROM
lease_apply_details
WHERE
type_id = #{typeId}
and parent_id = #{parentId}
DELETE
FROM
lease_apply_info
WHERE
id = #{parentId}
DELETE
FROM
lease_apply_details
WHERE
parent_id = #{parentId}
and type_id = #{typeId}
delete from lease_out_details where parent_id = #{parentId} and type_id = #{typeId}
and ma_id = #{maId}
DELETE FROM slt_agreement_info
WHERE
lease_id = #{parentId}
and type_id = #{typeId}
AND ma_id = #{maId}
DELETE FROM uni_org.sys_information_people
WHERE
information_id = #{informationId}
DELETE FROM uni_org.sys_information
WHERE
id = #{informationId}
DELETE FROM sys_workflow_record_history
WHERE
record_id = #{recordId}
DELETE FROM sys_workflow_record
WHERE
id = #{recordId}