public with sharing class ConsumTrialUpdateController { public static final String NONE = system.label.StartTrading_None; private List assetFieldApiList; private List receivingNoteFieldApiList; public List outputFieldList {get; set;} public List inputFieldList {get; set;} public Boolean canEdit{get;set;} public Boolean isCenter{get;set;} public String parId {get; set;} public PageBean pageB {get; set;} public Boolean showSelectRnsButton{get;set;} private String case_OR_animal_organLabel{get;set;} public ConsumTrialUpdateController() { parId = System.currentPageReference().getParameters().get('parId'); canEdit = System.currentPageReference().getParameters().get('canedit') == 'true'; } public void init() { try { if (String.isBlank(parId)) { throw new ControllerUtil.myException('请传入正确的参数'); } List rnList = [SELECT Id // , DateOfDelivery__c // , ReceivingNoteNo__c , Status__c // , Asset_loaner_category__c , RA_Status__c , HP_received_sign_rich__c , demo_purpose2__c FROM Consum_Apply__c WHERE Id = :parId]; if (rnList.size() == 0) { throw new ControllerUtil.myException('耗材申请Id不存在'); } case_OR_animal_organLabel = '病例'; if (rnList[0].demo_purpose2__c == '动物实验' ) { case_OR_animal_organLabel = '动物脏器'; } Set assetFieldApiSet = new Set(); Set receivingNoteFieldApiSet = new Set(); outputFieldList = setFIeldSet('Consum_Apply_Equipment_Set_Detail__c', 'ConsumTrialUpdate_L', receivingNoteFieldApiSet); inputFieldList = setFIeldSet('Consum_Apply_Equipment_Set_Detail__c', 'ConsumTrialUpdate_R', receivingNoteFieldApiSet); addField(receivingNoteFieldApiSet, assetFieldApiSet); assetFieldApiList = new List(); receivingNoteFieldApiList = new List(); assetFieldApiList.addAll(assetFieldApiSet); receivingNoteFieldApiList.addAll(receivingNoteFieldApiSet); setEditable(); pageB = new PageBean(rnList[0], assetFieldApiList, receivingNoteFieldApiList, isCenter); } catch (Exception e) { ApexPages.addMessages(e); } } //public List getAsreceivedConditionOps() { // return getPlickList('Consum_Apply__c', 'Status__c'); //} //public List getcategoryOps() { // return getPlickList('Consum_Apply__c', 'Asset_loaner_category__c'); //} /** @ description 备品中心确认只能让备品中心或管理员修改 */ public void setEditable(){ Id profileId = userinfo.getProfileId(); Profile pf = [Select Id, Name from Profile where Id=:profileId]; String pfNo = pf.Name.split('_')[0]; isCenter = false; // OLY_OCM-942 if (pfNo.contains('2B1') || pfNo.contains('2B2') || pfNo.contains('2B3') || profileId == System.Label.ProfileId_SystemAdmin ){ isCenter = true; } } //public void search() { // System.debug(assetFieldApiList); // String wher = ''; // if (String.isNotBlank(pageB.rn.Status__c)) { // wher += ' AND RND_Status__c = \'' + String.escapeSingleQuotes(pageB.rn.Status__c) + '\''; // } // if (String.isNotBlank(pageB.fixture_Model_No)) { // wher += ' AND Fixture_Model_No_F__c Like \'%' + String.escapeSingleQuotes(pageB.fixture_Model_No) + '%\''; // } // pageB.search(wher, assetFieldApiList, receivingNoteFieldApiList, isCenter); //} public void changeFixModel() { if (String.isNotBlank(pageB.rn.HP_received_sign_rich__c)){ // 上传试用单后不可编辑 ApexPages.Message message = new ApexPages.Message(ApexPages.severity.ERROR, '试用单已上传,不可以修改明细'); ApexPages.addMessage(message); } else{ pageB.fixMode = pageB.fixMode == false; } } private void addField(Set receivingNoteFieldApiSet, Set assetFieldApiSet) { receivingNoteFieldApiSet.add('Id'); receivingNoteFieldApiSet.add('Name'); receivingNoteFieldApiSet.add('Asset__c'); receivingNoteFieldApiSet.add('Fixture_Model_No_F__c'); receivingNoteFieldApiSet.add('Consum_Apply__r.AnimalExerimentFlag__c'); receivingNoteFieldApiSet.add('Has_Picture__c'); receivingNoteFieldApiSet.add('Asset_Center_Confirm__c'); receivingNoteFieldApiSet.add('Consum_Apply__r.HP_received_sign_rich__c'); receivingNoteFieldApiSet.add('StockDown__c'); receivingNoteFieldApiSet.add('Picture1__c'); receivingNoteFieldApiSet.add('Picture2__c'); } //private List getPlickList(String objApi, String fieldApi) { // Schema.DescribeFieldResult fieldResult = Schema.getGlobalDescribe().get(objApi).getDescribe().fields.getMap().get(fieldApi).getDescribe(); // List pickListValuesList= new List(); // List ple = fieldResult.getPicklistValues(); // pickListValuesList.add(new SelectOption('', NONE)); // for( Schema.PicklistEntry pickListVal : ple){ // pickListValuesList.add(new SelectOption(pickListVal.getValue(), pickListVal.getLabel())); // } // return pickListValuesList; //} private List setFIeldSet(String objApi, String fieldSetApi, Set fieldApiSet) { List lvList = new List(); Map fsMap = Schema.getGlobalDescribe().get(objApi).getDescribe().fieldSets.getMap(); Schema.FieldSet fs = fsMap.get(fieldSetApi); List fsmList = fs.getFields(); for (FieldSetMember fsm : fsmList) { if (objApi == 'Asset') { fieldApiSet.add('Assert__r.' + fsm.getFieldPath()); } else { fieldApiSet.add(fsm.getFieldPath()); } if (fsm.getFieldPath() == 'Case_OR_animal_organ__c') { lvList.add(new LabelValue(case_OR_animal_organLabel, fsm.getFieldPath())); } else { lvList.add(new LabelValue(fsm.getLabel(), fsm.getFieldPath())); } } return lvList; } // 适用区域信息提示 //public void showApplyMsg() { // String applyMsg = ApexPages.currentPage().getParameters().get('applyMsg'); // String applyMsgType = ApexPages.currentPage().getParameters().get('applyMsgType'); // String applyErrorLabel = ApexPages.currentPage().getParameters().get('applyErrorLabel'); // if(applyMsgType == 'Fail') { // ApexPages.Message vfApplyMsg = new ApexPages.Message(ApexPages.severity.ERROR, applyMsg); // ApexPages.addMessage(vfApplyMsg); // System.debug('applyMsg===' + applyMsg); // System.debug('applyMsgType===' + applyMsgType); // System.debug('applyErrorLabel===' + applyErrorLabel); // if(applyErrorLabel != null) { // System.debug('applyErrorLabel===' + applyErrorLabel); // } // } else { // ApexPages.Message vfApplyMsg = new ApexPages.Message(ApexPages.severity.INFO, applyMsg); // ApexPages.addMessage(vfApplyMsg); // } //} //public List getCase_OR_animal_organOps() { // String tex = '病例'; // if (pageB.rn.AnimalExerimentFlag__c) { // tex = '动物脏器'; // } // List opList = new List(); // for (Integer i = 0; i < 20; i ++) { // String value = tex + String.valueOf((i + 1)).leftPad(2,'0'); // opList.add(new SelectOption(value, value)); // } // return opList; //} // 保存画面上的数据 public PageReference saveRecord() { try { List caesdListUpdate = new List(); Set assetIdSet = new Set(); // 点保存发生在上传图片之后,所以需要重新查一下当前的图片情况,防止入力规则报错 // 图片相关字段要比画面中对象要新,Received_Confirm__c和Received_NG_Content__c比画面中对象要旧 Map oldCaesdMap = new Map([ SELECT Id , Has_Picture__c , Received_Confirm__c , Received_NG_Content__c , Picture1__c , Picture2__c , Asset_Center_Confirm__c FROM Consum_Apply_Equipment_Set_Detail__c WHERE Consum_Apply__c =: parId AND (Cancel_Select__c = false OR NG_Select_Again__c = true) ]); String ngMessage = ''; String ngModifyMessage = ''; String ngRejectMessage = ''; Boolean sendNGMail = false; Boolean sendNGModifyMail = false; Boolean sendNGRejectMail = false; Boolean ngContentEmpty = false; Boolean ngPictureEmpty = false; for (RowBean rowB : pageB.rowBList) { Consum_Apply_Equipment_Set_Detail__c newCaesd = rowB.rnd; Consum_Apply_Equipment_Set_Detail__c oldCaesd = oldCaesdMap.get(newCaesd.Id); if (rowB.checked) { if(newCaesd.Received_Confirm__c == 'NG'){ // NG时检查 if(String.isBlank(newCaesd.Received_NG_Content__c)){ ngContentEmpty = true; } // 由于上传图片在另一个画面中更新,所以要取一下图片上传情况 if(!oldCaesdMap.get(newCaesd.Id).Has_Picture__c){ ngPictureEmpty = true; } else { newCaesd.Has_Picture__c = true; } ngMessage += newCaesd.Fixture_Model_No_F__c + '\t' + newCaesd.Received_NG_Content__c + '\n'; // 新增的NG明细 if(oldCaesd.Received_Confirm__c != 'NG'){ sendNGMail = true; } // 理由或图片变化的NG明细 else if (oldCaesd.Received_NG_Content__c != newCaesd.Received_NG_Content__c || oldCaesd.Picture1__c != newCaesd.Picture1__c || oldCaesd.Picture2__c != newCaesd.Picture2__c){ sendNGModifyMail = true; ngModifyMessage+= newCaesd.Fixture_Model_No_F__c + '\t' + newCaesd.Received_NG_Content__c + '\n'; newCaesd.Asset_Center_Confirm__c = null; } // 备品中心确认过的NG明细 if(oldCaesd.Asset_Center_Confirm__c != 'OK' && newCaesd.Asset_Center_Confirm__c == 'OK'){ newCaesd.Check_lost_Item_Final__c = '消耗'; assetIdSet.add(newCaesd.Asset__c); } // 备品中心驳回的NG明细 if(oldCaesd.Asset_Center_Confirm__c != 'NG' && newCaesd.Asset_Center_Confirm__c == 'NG'){ sendNGRejectMail = true; ngRejectMessage += newCaesd.Fixture_Model_No_F__c + '\t' + newCaesd.Received_NG_Content__c + '\n'; } } else if(newCaesd.Received_Confirm__c == 'OK'){ newCaesd.Received_NG_Content__c = null; newCaesd.Picture1__c = null; newCaesd.Picture2__c = null; } caesdListUpdate.add(newCaesd); } else{ // 没有被更新的NG明细 if(oldCaesd.Received_Confirm__c == 'NG') { ngMessage += newCaesd.Fixture_Model_No_F__c + '\t' + newCaesd.Received_NG_Content__c + '\n'; } } } if(ngContentEmpty){ throw new ControllerUtil.myException('没有填写【到货NG内容】!'); } if(ngPictureEmpty){ throw new ControllerUtil.myException('没有上传到货NG图片!'); } if (caesdListUpdate.size() > 0) { List assetList = [ SELECT Id FROM Asset WHERE Id IN:assetIdSet FOR UPDATE ]; FixtureUtil.withoutUpdate(caesdListUpdate, true); // 发送邮件用 Consum_Apply__c ca0 = new Consum_Apply__c(Id=parId); Consum_Apply__c ca1 = new Consum_Apply__c(Id=parId); if(sendNGMail){ ca0.Received_NG_Detail_ForEmail__c = null; ca1.Received_NG_Detail_ForEmail__c = ngMessage; } if(sendNGRejectMail){ ca0.Received_NG_Reject_Detail_ForEmail__c = null; ca1.Received_NG_Reject_Detail_ForEmail__c = ngRejectMessage; } if(sendNGModifyMail){ ca0.Received_NG_Content_Modified_ForEmail__c = null; ca1.Received_NG_Content_Modified_ForEmail__c = ngModifyMessage; ca1.Received_NG_Content_Modified_Time__c = System.Now(); } FixtureUtil.withoutUpdate(new List {ca0}); FixtureUtil.withoutUpdate(new List {ca1}); init(); ApexPages.Message message = new ApexPages.Message(ApexPages.severity.INFO, '保存成功'); ApexPages.addMessage(message); pageB.fixMode = false; return null; } } catch (Exception e) { ApexPages.addMessages(e); pageB.fixMode = true; return null; } return null; } public class PageBean { /* ------------ 复数编辑用 ------------ */ public Consum_Apply_Equipment_Set_Detail__c caesdInput {get{ if (caesdInput == null) { caesdInput = [SELECT Id FROM Consum_Apply_Equipment_Set_Detail__c LIMIT 1]; caesdInput = new Consum_Apply_Equipment_Set_Detail__c(Id = caesdInput.Id); } return caesdInput; } set;} /* ------------ 检索用 ------------ */ public Consum_Apply__c rn {get; set;} //public String fixture_Model_No {get; set;} /* ------------ 显示用 ------------ */ //public String spareLocationName {get; set;} //public String dateOfDelivery {get; set;} //public String orderCode {get; set;} //public String approvalNumber {get; set;} //public String receivingNoteNo {get; set;} public String asreceivedCondition {get; set;} /* ------------ 明细一覧 ------------ */ public List rowBList {get; set;} /* ------------ 记录用 ------------ */ public Boolean fixMode {get; set;} public Integer currentPageRecordCnt {get; set;} //public PageBean() { // rn = new Consum_Apply__c(); // rowBList = new List(); // fixMode = false; // currentPageRecordCnt = 0; //} public PageBean(Consum_Apply__c rnObj, List assetFieldApiList, List receivingNoteFieldApiList, Boolean isCenter) { rn = rnObj; asreceivedCondition = rnObj.RA_Status__c; // dateOfDelivery = rnObj.DateOfDelivery__c == null ? '' : rnObj.DateOfDelivery__c.format(); rowBList = new List(); fixMode = false; search(null, assetFieldApiList, receivingNoteFieldApiList, isCenter); } public void search(String wher, List assetFieldApiList, List receivingNoteFieldApiList, Boolean isCenter) { wher = String.isBlank(wher) ? '' : wher; System.debug(assetFieldApiList); String soql = 'SELECT ' + String.join(assetFieldApiList, ',') + (assetFieldApiList.size() > 0 ? ',' : '') + String.join(receivingNoteFieldApiList, ',') + ' FROM Consum_Apply_Equipment_Set_Detail__c ' + ' WHERE Consum_Apply_Equipment_Set__r.Consum_Apply__c = \'' + rn.Id + '\'' + wher + ' AND (Cancel_Select__c = false OR NG_Select_Again__c = true)' + ' ORDER BY Consum_Apply_Equipment_Set__r.Consum_Apply__c, Consum_Apply_Equipment_Set__c, Fixture_Model_No_F__c, NG_Select_Again__c, Degree_Of_Importance__c Limit 501'; rowBList = new List(); System.debug(soql); List rndList = Database.query(soql); if (rndList.size() > 500) { ApexPages.Message vfApplyMsg = new ApexPages.Message(ApexPages.severity.WARNING, '耗材明细超过500条,只显示前500条耗材明细'); ApexPages.addMessage(vfApplyMsg); rndList.remove(500); } Map maxDegree_Of_ImportanceMap = new Map(); for (Consum_Apply_Equipment_Set_Detail__c rnd : rndList) { if (maxDegree_Of_ImportanceMap.containsKey(rnd.Fixture_Model_No_F__c) == false) { maxDegree_Of_ImportanceMap.put(rnd.Fixture_Model_No_F__c, 0); } maxDegree_Of_ImportanceMap.put(rnd.Fixture_Model_No_F__c, maxDegree_Of_ImportanceMap.get(rnd.Fixture_Model_No_F__c) + 1); } Integer i = 0; for (Consum_Apply_Equipment_Set_Detail__c rnd : rndList) { RowBean rb = new RowBean(rnd, i, isCenter); if(rnd.RAESD_Status__c == Consum_ApplyUtil.CaStatusMap.get(Consum_ApplyUtil.CaesdStatus.Yi_Xiao_Hao.ordinal()) || String.isNotBlank(rnd.Consum_Apply__r.HP_received_sign_rich__c) ) { // 已消耗时,不可编辑 rb.canChange = false; } rb.maxDegree_Of_Importance = maxDegree_Of_ImportanceMap.get(rb.rnd.Fixture_Model_No_F__c); rowBList.add(rb); i ++; } currentPageRecordCnt = rndList.size(); } } public class RowBean { public Boolean checked {get; set;} public Boolean canChange {get; set;} public Boolean dataConfirmation {get; set;} public String canChangeField {get; set;} public Integer lineNo {get; set;} public Boolean haveAsset {get; set;} public Integer maxDegree_Of_Importance {get; set;} public Consum_Apply_Equipment_Set_Detail__c rnd {get; set;} public Boolean canCenterEdit {get;set;} // public Asset asse {get; set;} //public RowBean() { // checked = false; // rnd = new Consum_Apply_Equipment_Set_Detail__c(); // // asse = new Asset(); // canChange = true; // dataConfirmation = false; // canChangeField = ''; //} public RowBean(Consum_Apply_Equipment_Set_Detail__c rnd1, Integer lineNo1, Boolean isCenter) { rnd = rnd1; // asse = rnd1.RNDAssert__r; checked = false; canChange = false; canCenterEdit = false; lineNo = lineNo1 + 1; canChangeField = 'Received_Confirm__c, Received_NG_Content__c'; // 未出库的不可编辑 if(!rnd.StockDown__c){ canChange = false; canCenterEdit = false; } // 备品中心 else if(isCenter){ if(rnd.Received_Confirm__c == 'OK' || String.isBlank(rnd.Received_Confirm__c) || (rnd.Received_Confirm__c == 'NG' && rnd.Asset_Center_Confirm__c == 'OK')) { canChange = false; canCenterEdit = false; } else{ canChange = true; canCenterEdit = true; } } // 申请人 else{ canCenterEdit = false; if(rnd.Received_Confirm__c == 'OK'){ // 为了使OK变NG时可以立即编辑,OK时的只读用js控制 canChange = true; } else if(rnd.Received_Confirm__c == 'NG'){ if(rnd.Asset_Center_Confirm__c == 'OK'){ canChange = false; } else{ canChange = true; } } else if(rnd.Received_Confirm__c == '默认签收-OK') { if(rnd.Asset_Center_Confirm__c == 'NG'){ canChange = true; } else{ canChange = false; } } else { canChange = true; } } haveAsset = String.isNotBlank(rnd1.Asset__c); } } public class LabelValue { public String label {get; set;} public String value {get; set;} public LabelValue() { label = ''; value = ''; } public LabelValue(String l, String v) { label = l; value = v; } } }