trigger CaseTrigger on Case (after insert,before insert,before update,before delete) {
Case c=new Case();
if(trigger.isDelete){
c = trigger.old[0];
}else{
c = trigger.new[0];
}
if(trigger.isInsert&&trigger.isBefore){
List<Case> caselist =trigger.new;
List<String> phoneids = new List<String>();
for(Case c1:caselist){
if(c1.contactphone__c!=null&&c1.contactname__c!=null&&c1.AccountId!=null){
phoneids.add(c1.contactphone__c);
}
}
List<Contact> cons=[SELECT id,Phone_number2__c FROM Contact WHERE Phone_number2__c IN :phoneids];
for(Case c1:caselist){
Boolean isExistCon = false;
for(Contact c2:cons){
if(c2.Phone_number2__c==c1.contactphone__c){
c1.ContactId = c2.Id;
isExistCon = true;
}
}
if(!isExistCon){
Contact c3 = new Contact();
c3.LastName = c1.contactname__c;
c3.AccountId = c1.AccountId;
c3.Phone_number2__c = c1.contactphone__c;
insert c3;
c1.ContactId = c3.Id;
}
}
}
if(Trigger.isAfter&&Trigger.isInsert){
if(c.shiporder__c!=null){
consignment__c consignment =[SELECT id,Dispatchingorder__c FROM consignment__c WHERE id=:c.shiporder__c];
consignment.Dispatchingorder__c = true;
update consignment;
}
}
if(c.shiporder__c!=null&&trigger.isInsert&&trigger.isAfter){
List<consignment_detail__c> cDetails =[SELECT id,product__c,machineID__c,number__c,materiel_number__c,product_name__c,
ERPproductmodel__c,remark__c,partSpec__c FROM consignment_detail__c WHERE consignment__c =:c.shiporder__c];
List<installationdetail__c> iDetails = new List<installationdetail__c>();
for(consignment_detail__c cd:cDetails){
installationdetail__c temp = new installationdetail__c();
temp.consignment_detail__c = cd.Id;
temp.machinecode1__c = cd.machineID__c;
temp.product__c = cd.product__c;
temp.casenumber__c = c.Id;
temp.quantity1__c = cd.number__c;
temp.materialnumber1__c = cd.materiel_number__c;
temp.materialname1__c = cd.product_name__c;
temp.type1__c = cd.ERPproductmodel__c;
temp.statue1__c = cd.remark__c;
temp.style1__c = cd.partSpec__c;
iDetails.add(temp);
}
insert iDetails;
}
List<RecordType> records =[SELECT Id, Name, DeveloperName, SobjectType FROM RecordType WHERE SobjectType='Case'];
Map<String,String> recordName_Id = new Map<String,String>();
for(RecordType r:records){
recordName_Id.put(r.Name,r.Id);
}
if(trigger.isBefore&&trigger.isInsert){
if(c.AccountId!=null&&(recordName_Id.get('新机装机')==c.RecordTypeId||recordName_Id.get('客户报修')==c.RecordTypeId)){
List<caseassignmentrule__c> asign = new List<caseassignmentrule__c>();
asign = [SELECT Id,skillman__c FROM caseassignmentrule__c WHERE customername__c=:c.AccountId];
if(asign.size()>0){
c.firstowner__c = asign[0].skillman__c;
}else{
Boolean step2 = false;
if(c.shiporder__c!=null){
List<consignment_detail__c> cDetails =[SELECT id,product__c FROM consignment_detail__c WHERE consignment__c =:c.shiporder__c];
for(consignment_detail__c cd:cDetails){
asign =[SELECT Id,skillman__c FROM caseassignmentrule__c WHERE producttype__c=:cd.product__c];
if(asign.size()>0){
c.firstowner__c = asign[0].skillman__c;
step2 = true;
}
}
}
if(!step2&&c.area__c!=null){
Region__c reg =[SELECT Name FROM Region__c WHERE id=:c.area__c];
asign =[SELECT Id,skillman__c,area__r.Name FROM caseassignmentrule__c ];
if(asign.size()>0){
for(caseassignmentrule__c cg:asign){
if(reg.Name.Contains(cg.area__r.Name)){
c.firstowner__c = asign[0].skillman__c;
}
}
}
}
}
}
}
if(trigger.isBefore&&trigger.isUpdate){
List<ProcessInstance> pInstance =[SELECT TargetObjectId, Id FROM ProcessInstance WHERE TargetObjectId=:c.Id ORDER BY CreatedDate DESC];
system.debug('pInstance'+pInstance);
if(pInstance.size()>0){
List<ProcessInstanceStep> step=[SELECT Id,Comments,StepNodeId FROM ProcessInstanceStep WHERE ProcessInstanceId=:pInstance[0].Id AND StepStatus!='Started' AND Comments!=''];
system.debug('step'+step);
if(step.size()>0){
for(ProcessInstanceStep ps:step){
ProcessInstanceNode pid=[SELECT Id, NodeStatus, ProcessNodeName FROM ProcessInstanceNode WHERE ProcessNodeId=:ps.StepNodeId limit 1];
if(pid.ProcessNodeName=='投诉内容核查'){
c.Complaintsverification__c = ps.Comments;
}else if(pid.ProcessNodeName=='责任部门核实'){
c.responsibilitydepartment__c = ps.Comments;
}else if(pid.ProcessNodeName=='客服管理中心处理意见及纠正预防措施'){
c.Customermeasures__c = ps.Comments;
}else if(pid.ProcessNodeName=='公司主管领导意见'){
c.Companymanager__c = ps.Comments;
}else if(pid.ProcessNodeName=='修改投诉单处理意见及预防纠正措施'){
c.Customermeasures__c = ps.Comments;
}
}
}
}
if(trigger.old[0].Status!='已结案'&&c.Status=='已结案'){
c.Maintenance__c = true;
system.debug('caseTrigger-Maintenance__c:'+c.Maintenance__c);
List<customerservicelist__c> cusSers =[SELECT id,state1__c FROM customerservicelist__c WHERE servicearr__c=:c.Id];
for(customerservicelist__c cu:cusSers){
cu.state1__c ='已审核';
}
update cusSers;
}
if(recordName_Id.get('客户报修')==c.RecordTypeId){
if(c.firstTime2Hours__c){
if(c.secondTime2Hours__c){
String msg ='单号 '+c.CaseNumber+' 的派工单距离分配时间已达2小时,请电话联系客户';
ChatterSender.send(c.secondowner__c,msg,c.Id);
c.secondTime2Hours__c = false;
c.firstTime2Hours__c = false;
User u=[SELECT Phone FROM User WHERE id=:c.secondowner__c];
SMS.doFutureSend(u.Phone,msg);
}else{
String msg ='单号 '+c.CaseNumber+' 的派工单距离分配时间已达2小时,请电话联系客户';
ChatterSender.send(c.firstowner__c,msg,c.Id);
c.firstTime2Hours__c = false;
User u=[SELECT Phone FROM User WHERE id=:c.firstowner__c];
SMS.doFutureSend(u.Phone,msg);
}
}
}
if(recordName_Id.get('客户报修')==c.RecordTypeId&&c.Reason=='等待配件'&&c.Status=='已结案'&&c.ParentId==null&&trigger.old[0].Status!='已结案'){
String msg ='您的派工单号【'+c.CaseNumber+'】已进行特殊结案,请选择特殊结案关联派工单';
c.SpecialcaseTime__c = DateTime.now();
if(c.firstowner__c!=null&&c.secondowner__c!=null){
ChatterSender.send(c.secondowner__c,msg,c.Id);
}else if(c.firstowner__c!=null){
ChatterSender.send(c.firstowner__c,msg,c.Id);
}
}
if(c.Status=='审批中'&&trigger.old[0].Status=='已接受(技术员)'&&recordName_Id.get('新机装机')==c.RecordTypeId){
if(c.costprice__c==null){
c.addError('车旅费为空,不允许提交!');
}else{
List<installationdetail__c> iDetail =[SELECT id,Name FROM installationdetail__c WHERE casenumber__c=:c.Id];
List<String> iids = new List<String>();
for(installationdetail__c i:iDetail){
iids.add(i.Id);
}
List<checkinglist__c> checks=[SELECT id,Name,installationdetail__c FROM checkinglist__c WHERE installationdetail__c IN :iids];
Set<String> iDetailNames = new Set<String>();
for(installationdetail__c i:iDetail){
Boolean haveCheck = false;
for(checkinglist__c c1:checks){
if(c1.installationdetail__c==i.Id){
haveCheck = true;
}
}
if(!haveCheck){
iDetailNames.add(i.Name);
}
}
if(iDetailNames.size()>0){
c.addError(iDetailNames+'的设备安装验收单为空,不允许提交审批!');
}
List<String> cids = new List<String>();
for(checkinglist__c c1:checks){
cids.add(c1.Id);
}
system.debug('cids:'+cids);
if(cids.size()>0){
List<Attachment> att=[SELECT Id, ParentId, Name FROM Attachment WHERE ParentId IN:cids];
List<ContentDocumentLink> documents =[SELECT Id, LinkedEntityId, ContentDocumentId FROM ContentDocumentLink where LinkedEntityId IN:cids];
Set<String> NotDocNames =new Set<String>();
for(checkinglist__c c2:checks){
Boolean thisCheckHasAtt = false;
Integer thisNum =0;
for(Attachment a:att){
if(a.ParentId==c2.Id){
thisNum ++;
}
}
for(ContentDocumentLink d:documents){
if(d.LinkedEntityId==c2.Id){
thisNum ++;
}
}
if(thisNum>=3){
thisCheckHasAtt = true;
}
if(!thisCheckHasAtt){
NotDocNames.add(c2.Name);
}
}
if(NotDocNames.size()>0){
c.addError('设备安装验收单'+NotDocNames+'附件少于三个,不允许提交!');
}
}
}
}
if(c.Status=='审批中'&&trigger.old[0].Status=='已接受(技术员)'&&recordName_Id.get('客户报修')==c.RecordTypeId){
if(c.costprice__c==null){
c.addError('车旅费为空,不允许提交!');
}else{
List<customerservicelist__c> cusSer=[SELECT id,Name FROM customerservicelist__c WHERE servicearr__c=:c.Id];
if(cusSer.size()==0){
c.addError('客户服务单为空,不允许提交审批!');
}
List<String> csids = new List<String>();
for(customerservicelist__c cs:cusSer){
csids.add(cs.Id);
}
System.debug('===================1===================');
List<Attachment> att=[SELECT Id, ParentId, Name FROM Attachment WHERE ParentId IN:csids];
System.debug('==================2===================='+csids);
if(csids.size()>0){
List<ContentDocumentLink> documents =[SELECT Id, LinkedEntityId, ContentDocumentId FROM ContentDocumentLink where LinkedEntityId IN:csids];
System.debug('==================3====================');
Set<String> NotDocNames =new Set<String>();
for(customerservicelist__c cs2:cusSer){
Boolean thisCheckHasAtt = false;
for(Attachment a:att){
if(a.ParentId==cs2.Id){
thisCheckHasAtt = true;
}
}
for(ContentDocumentLink d:documents){
if(d.LinkedEntityId==cs2.Id){
thisCheckHasAtt = true;
}
}
if(!thisCheckHasAtt){
NotDocNames.add(cs2.Name);
}
}
if(NotDocNames.size()>0){
String names='';
for(String s:NotDocNames){
if(names==''){
names = s;
}else{
names = names +'、'+s;
}
}
c.addError('客户服务单'+names+'附件为空,不允许提交!');
}
}
}
}
}
if(trigger.isDelete){
if(c.Status!='未派工'&&(recordName_Id.get('新机装机')==c.RecordTypeId||recordName_Id.get('客户报修')==c.RecordTypeId)){
c.addError('未派工状态下才可以删除!');
}
}
if(trigger.isBefore&&trigger.isUpdate){
system.debug('shares:'+1);
Case c1 = trigger.old[0];
Case c2 = trigger.new[0];
if(c1.firstowner__c!=c2.firstowner__c){
system.debug('shares:'+2);
List<CaseShare> shares=[SELECT Id, CaseId, UserOrGroupId, CaseAccessLevel, RowCause FROM CaseShare WHERE CaseId=:c1.Id AND UserOrGroupId=:c1.firstowner__c AND RowCause='Manual'];
system.debug('shares:'+shares);
delete shares;
}
if(c1.secondowner__c!=c2.secondowner__c){
List<CaseShare> shares=[SELECT Id, CaseId, UserOrGroupId, CaseAccessLevel, RowCause FROM CaseShare WHERE CaseId=:c1.Id AND UserOrGroupId=:c1.secondowner__c AND RowCause='Manual'];
system.debug('shares:'+shares);
delete shares;
}
}
if(trigger.isUpdate&&(recordName_Id.get('新机装机')==c.RecordTypeId||recordName_Id.get('客户报修')==c.RecordTypeId)){
if(c.firstowner__c!=null&&c.secondowner__c==null&&c.Status=='已派工'&&trigger.old[0].Status!='已派工'){
String msg='单号:'+c.CaseNumber+'的派工单已分配给您,请按照派工单中的内容执行,谢谢!';
User u=[SELECT Name,Phone FROM User WHERE id=:c.firstowner__c];
SMS.doFutureSend(u.Phone, msg);
}
if(c.Status=='未派工'&&c.refuse__c=='1'){
User u=[SELECT Alias FROM User WHERE id=:c.firstowner__c];
String msg='单号:'+c.CaseNumber+'的派工单已被维修人员'+u.Alias+'拒绝,请重新进行派工,谢谢!';
sendToServer(msg);
}
if(c.X2HoursSms__c==true&&trigger.old[0].X2HoursSms__c!=true){
String msg='单号:'+c.CaseNumber+'的派工截止目前已超过2小时一线维修人员还未接收,请话务人员致电维修人员询问情况,谢谢!';
sendToServer(msg);
}
if(c.X24SendOne__c&&trigger.old[0].X24SendOne__c!=true){
String msg='单号:'+c.CaseNumber+'的派工单距离客户预约上门时间即将达24小时,请及时到达客户现场,谢谢!';
User u=[SELECT Phone FROM User WHERE id=:c.firstowner__c];
SMS.doFutureSend(u.Phone,msg);
}
if(c.X24SendTwo__c&&trigger.old[0].X24SendTwo__c!=true){
String msg='单号:'+c.CaseNumber+'的派工单距离客户预约上门时间即将达24小时,请及时到达客户现场,谢谢!';
User u=[SELECT Phone FROM User WHERE id=:c.secondowner__c];
SMS.doFutureSend(u.Phone,msg);
}
}
public void sendToServer(String msg){
if(recordName_Id.get('客户报修')==c.RecordTypeId){
List<Group> g=[SELECT Id, Name, DeveloperName, RelatedId, Type, Email, OwnerId FROM Group WHERE Name='客服话务组' limit 1];
List<String> Uids = new List<String>();
if(g.size()>0){
List<GroupMember> members =[SELECT Id, UserOrGroupId, GroupId FROM GroupMember WHERE GroupId=:g[0].Id];
for(GroupMember m:members){
Uids.add(m.UserOrGroupId);
}
}
List<User> users=[SELECT id,Name,Phone FROM User WHERE id IN :Uids];
for(User u2:users){
SMS.doFutureSend(u2.Phone,msg);
}
}else{
List<Group> g=[SELECT Id, Name, DeveloperName, RelatedId, Type, Email, OwnerId FROM Group WHERE Name='客服话务组长' limit 1];
List<String> Uids = new List<String>();
if(g.size()>0){
List<GroupMember> members =[SELECT Id, UserOrGroupId, GroupId FROM GroupMember WHERE GroupId=:g[0].Id];
for(GroupMember m:members){
Uids.add(m.UserOrGroupId);
}
}
List<User> users=[SELECT id,Name,Phone FROM User WHERE id IN :Uids];
for(User u2:users){
SMS.doFutureSend(u2.Phone,msg);
}
}
}
}