Skip to content

Instantly share code, notes, and snippets.

@kejun91
Created April 1, 2014 01:52
Show Gist options
  • Save kejun91/9906253 to your computer and use it in GitHub Desktop.
Save kejun91/9906253 to your computer and use it in GitHub Desktop.
pagination
/*
*功能:用于Temporary STR页面审批控制类
*负责人:
*时间:2014-3-9
*/
public with sharing class TemporarySTRApprovalPageController {
//存放服务器基本路径
public String baseurl{get;set;}
//获得查询记录的年
public String StrDateYear{set;get;}
//获得查询记录的月
public String StrDateMonth{set;get;}
//用于装载年份
public List<String> list_strYears;
//用于装载月份
public List<String> list_strMonths;
//整数Temporary STR分页的一个类
public Paging PTemSTRPage{get;set;}
//存放查询出对应的整数Temporary STR
//public list<Temporary_obj_STR__c> listPTemSTR{get{if(PTemSTRPage != null && PTemSTRPage.list_obj != null){return (list<Temporary_obj_STR__c>)PTemSTRPage.list_obj;}
//else{return new list<Temporary_obj_STR__c>();}}set;}
//存放查询出对应的整数Temporary STR内部类
public list<TemSTRWrapper> listTemSTRWrapperP{get;set;}
//整数Temporary STR分页的一个类
public Paging NTemSTRPage{get;set;}
//存放查询出对应的负数Temporary STR
//public list<Temporary_obj_STR__c> listNTemSTR{get{if(NTemSTRPage != null && NTemSTRPage.list_obj != null){return (list<Temporary_obj_STR__c>)NTemSTRPage.list_obj;}
//else{return new list<Temporary_obj_STR__c>();}}set;}
//存放查询出对应的负数Temporary STR内部类
public list<TemSTRWrapper> listTemSTRWrapperN{get;set;}
//存放正数消息提示是否显示
public Boolean Pmessage{get;set;}
//存放负数消息提示是否显示
public Boolean Nmessage{get;set;}
//save按钮是否显示
public Boolean viewSave{get;set;}
//用于装载查询记录的虚拟零售商的名称
public String retailerName{get;set;}
//用于装载虚拟零售商
public list<String> listRetailers{get;set;}
//获取年份列表
public List<SelectOption> getYears()
{
List<SelectOption> options = new List<SelectOption>();
for(String str:list_strYears){
options.add(new SelectOption(str,str));
}
return options;
}
//获取月份列表
public List<SelectOption> getMonths()
{
List<SelectOption> options = new List<SelectOption>();
for(String str:list_strMonths){
options.add(new SelectOption(str,str));
}
return options;
}
//获取虚拟零售商的名称
public list<SelectOption> getRetailers()
{
list<SelectOption> options = new list<SelectOption>();
for(String strRName : listRetailers)
{
options.add(new SelectOption(strRName,strRName));
}
return options;
}
//存放Temporary STR
public Temporary_obj_STR__c temstr{get;set;}
//存放查询条件中选择的零售商
public obj_Retailer__c temRetailer{get;set;}
//存放查询条件中选择的经销商
public Account temAcc{get;set;}
//存放应该选择的虚拟零售商名称
public String strVRetailerName{get;set;}
public TemporarySTRApprovalPageController(){
baseurl = URL.getSalesforceBaseUrl().toExternalForm();
temstr = new Temporary_obj_STR__c();
temRetailer = new obj_Retailer__c();
temAcc = new Account();
viewSave = false;
list_strYears = new List<String>();
list_strMonths = new List<String>();
//装载年份
list_strYears.add('2012');
list_strYears.add('2013');
list_strYears.add('2014');
list_strYears.add('2015');
list_strYears.add('2016');
list_strYears.add('2017');
list_strYears.add('2018');
list_strYears.add('2019');
list_strYears.add('2020');
//装载月份
for(Integer i=1;i<13;i++)
{
list_strMonths.add(String.valueOf(i));
}
listRetailers = new list<String>();
//装载各个大区的虚拟零售商的名称
for(obj_Retailer__c r : [select Id,Name
from obj_Retailer__c
where Name = '临时STR审批专用-South' or Name = '临时STR审批专用-West'
or Name = '临时STR审批专用-North' or Name = '临时STR审批专用-Central'
or Name = '临时STR审批专用-East'])
{
listRetailers.add(r.Name);
}
StrDateYear = String.valueof(date.today().addMonths(-1).year());//初始化页面上的年
StrDateMonth = String.valueof(date.today().addMonths(-1).month());//初始化页面上的月
retailerName = '';
strVRetailerName = '';
//获取当前用户的角色
UserRole currentUserRole = [Select Id,Name From UserRole Where Id = :UserInfo.getUserRoleId()];
String currentUserRoleName = currentUserRole.Name;
obj_Retailer__c visualRetailer = new obj_Retailer__c();
//初始化页面上需要加载的虚拟零售商的名称
if(currentUserRoleName == 'East China')
{
visualRetailer = [select Id,Name from obj_Retailer__c where Name = '临时STR审批专用-East'];
retailerName = visualRetailer.Name;
strVRetailerName = '临时STR审批专用-East';
}
else if(currentUserRoleName == 'West China')
{
visualRetailer = [select Id,Name from obj_Retailer__c where Name = '临时STR审批专用-West'];
retailerName = visualRetailer.Name;
strVRetailerName = '临时STR审批专用-West';
}
else if(currentUserRoleName == ' South China')
{
visualRetailer = [select Id,Name from obj_Retailer__c where Name = '临时STR审批专用-South'];
retailerName = visualRetailer.Name;
strVRetailerName = '临时STR审批专用-South';
}
else if(currentUserRoleName == 'North China')
{
visualRetailer = [select Id,Name from obj_Retailer__c where Name = '临时STR审批专用-North'];
retailerName = visualRetailer.Name;
strVRetailerName = '临时STR审批专用-North';
}
else if(currentUserRoleName == 'Central China')
{
visualRetailer = [select Id,Name from obj_Retailer__c where Name = '临时STR审批专用-Central'];
retailerName = visualRetailer.Name;
strVRetailerName = '临时STR审批专用-Central';
}
searchMethod();//初始化页面数据
}
public String strPTemSTR;
public String strNTemSTR;
public Date startDate;
public Date endDate;
//查询方法
public void searchMethod()
{
String year = StrDateYear;
String month = StrDateMonth;
String day = '1';
String hour = '0';
String minute = '0';
String second = '0';
String strStartDate = year + '-' + month+ '-' + day + ' ' + hour + ':' + minute + ':' + second;
startDate = date.valueOf(strStartDate);
endDate = date.valueOf(strStartDate).addMonths(1);
if(temstr.Tem_fld_str_Retailer__c != null)
{
//获取查询条件中的选择的零售商
temRetailer = [select Id,Name,fld_retailer_Salesman__c,fld_retailer_Salesman__r.Approval_RSM__c
from obj_Retailer__c
where Id =: temstr.Tem_fld_str_Retailer__c];
temstr.Tem_fld_str_Retailer__c = temRetailer.Id;
}
if(temstr.Tem_fld_str_Distributor__c != null)
{
temAcc = [select Id from Account where Id =: temstr.Tem_fld_str_Distributor__c];
temstr.Tem_Wholesaler1__c = temAcc.Id;
}
//listPTemSTR = new list<Temporary_obj_STR__c>();
//listNTemSTR = new list<Temporary_obj_STR__c>();
strPTemSTR = 'select Id,Name,Tem_fld_str_Distributor__r.Name,Tem_fld_str_Product__r.Name,Tem_fld_str_STR_Date__c,'
+'Tem_fld_str_Volume__c,Tem_fld_STR_Gross_Sales__c,Tem_HistoryPriceBTPB__c,Tem_fld_str_Retailer__c,'
+'Tem_fld_str_Retailer__r.Name,Tem_IfSelect__c,Tem_fld_str_Distributor__c,Tem_Parent_retailer__c,Tem_Parent_retailer__r.Name '
+'from Temporary_obj_STR__c ';
if(temstr.Tem_fld_str_Retailer__c == null && temstr.Tem_Wholesaler1__c == null)
{
strPTemSTR += 'where Tem_IfSelect__c = false and Tem_fld_str_Volume__c > 0';
}
else if(temstr.Tem_fld_str_Retailer__c != null && temstr.Tem_Wholesaler1__c == null)
{
strPTemSTR += 'where Tem_IfSelect__c = false and Tem_fld_str_Volume__c > 0 and Tem_fld_str_Retailer__c = \''+temstr.Tem_fld_str_Retailer__c+'\'';
}
else if(temstr.Tem_fld_str_Retailer__c == null && temstr.Tem_Wholesaler1__c != null)
{
strPTemSTR += 'where Tem_IfSelect__c = false and Tem_fld_str_Volume__c > 0 and Tem_fld_str_Distributor__c = \''+temstr.Tem_Wholesaler1__c +'\'';
}
else if(temstr.Tem_fld_str_Retailer__c != null && temstr.Tem_Wholesaler1__c != null)
{
strPTemSTR += 'where Tem_IfSelect__c = false and Tem_fld_str_Volume__c > 0 and Tem_fld_str_Retailer__c = \''+temstr.Tem_fld_str_Retailer__c+'\' and Tem_fld_str_Distributor__c = \''+temstr.Tem_Wholesaler1__c+'\'';
}
//当前选择年月零售商获取数量(Tem_fld_str_Volume__c)大于0的Temporary STR
//listPTemSTR = Database.query(strPTemSTR);
PTemSTRPage = new Paging(strPTemSTR,5,startDate,endDate);
setListTemSTRWrapperP();//加载整数Temporary STR首页
if(listTemSTRWrapperP != null && listTemSTRWrapperP.size()>0)
{
Pmessage = false;
viewSave = true;
}
else
{
Pmessage = true;
}
strNTemSTR = 'select Id,Name,Tem_fld_str_Distributor__r.Name,Tem_fld_str_Product__r.Name,Tem_fld_str_STR_Date__c,'
+'Tem_fld_str_Volume__c,Tem_fld_STR_Gross_Sales__c,Tem_HistoryPriceBTPB__c,Tem_fld_str_Retailer__c,'
+'Tem_fld_str_Retailer__r.Name,Tem_IfSelect__c,Tem_fld_str_Distributor__c,Tem_Parent_retailer__c,Tem_Parent_retailer__r.Name '
+'from Temporary_obj_STR__c ';
if(temstr.Tem_fld_str_Retailer__c == null && temstr.Tem_Wholesaler1__c == null)
{
strNTemSTR += 'where Tem_IfSelect__c = false and Tem_fld_str_Volume__c < 0';
}
else if(temstr.Tem_fld_str_Retailer__c != null && temstr.Tem_Wholesaler1__c == null)
{
strNTemSTR += 'where Tem_IfSelect__c = false and Tem_fld_str_Volume__c < 0 and Tem_fld_str_Retailer__c = \''+temstr.Tem_fld_str_Retailer__c+'\'';
}
else if(temstr.Tem_fld_str_Retailer__c == null && temstr.Tem_Wholesaler1__c != null)
{
strNTemSTR += 'where Tem_IfSelect__c = false and Tem_fld_str_Volume__c < 0 and Tem_fld_str_Distributor__c = \''+temstr.Tem_Wholesaler1__c +'\'';
}
else if(temstr.Tem_fld_str_Retailer__c != null && temstr.Tem_Wholesaler1__c != null)
{
strNTemSTR += 'where Tem_IfSelect__c = false and Tem_fld_str_Volume__c < 0 and Tem_fld_str_Retailer__c = \''+temstr.Tem_fld_str_Retailer__c+'\' and Tem_fld_str_Distributor__c = \''+temstr.Tem_Wholesaler1__c+'\'';
}
//当前选择年月零售商获取数量(Tem_fld_str_Volume__c)小于0的Temporary STR
//listNTemSTR = Database.query(strNTemSTR);
NTemSTRPage = new Paging(strNTemSTR,5,startDate,endDate);
setListTemSTRWrapperN();//加载负数Temporary STR首页
if(listTemSTRWrapperN != null && listTemSTRWrapperN.size()>0)
{
Nmessage = false;
viewSave = true;
}
else
{
Nmessage = true;
}
}
//保存方法
public PageReference saveMethod()
{
string year = StrDateYear;
string month = StrDateMonth;
string day = '1';
string hour = '0';
string minute = '0';
string second = '0';
string strStartDate = year + '-' + month+ '-' + day + ' ' + hour + ':' + minute + ':' + second;
Date startDate = date.valueOf(strStartDate);
Date endDate = date.valueOf(strStartDate).addMonths(1);
list<Temporary_STR_Apply__c> listPTSTRApply = new list<Temporary_STR_Apply__c>();//存放要插入的Temporary STR的数量是整数的Temporary STR Apply
list<Temporary_STR_Apply__c> listNTSTRApply = new list<Temporary_STR_Apply__c>();//存放要插入的Temporary STR的数量是负数的Temporary STR Apply
list<Temporary_obj_STR__c> listPUpdTSTR = new list<Temporary_obj_STR__c>();//存放要更新的正数Temporary STR
list<Temporary_obj_STR__c> listNUpdTSTR = new list<Temporary_obj_STR__c>();//存放要更新的负数Temporary STR
checkSelectdP();
if(allTemSTRPValue)
{
doAllPTemSTRItem();
}
if(setTemSTRIdP != null && setTemSTRIdP.size()>0)
{
Temporary_STR_Apply__c tPstrApply = new Temporary_STR_Apply__c();
tPstrApply.Date__c = Date.newInstance(Integer.valueof(StrDateYear),Integer.valueof(StrDateMonth)+1, 1).addDays(-1);
tPstrApply.Status__c = 'Draft';
if(retailerName != null)
{
list<obj_Retailer__c> listVR = new list<obj_Retailer__c>();
listVR = [select Id,Name,fld_retailer_Salesman__r.Approval_RSM__c from obj_Retailer__c where Name =: retailerName];
if(listVR != null && listVR.size()>0)
{
obj_Retailer__c virtualRetailer = listVR[0];
tPstrApply.Retailer__c = virtualRetailer.Id;
tPstrApply.Approval_RSM__c = virtualRetailer.fld_retailer_Salesman__r.Approval_RSM__c;
}
}
tPstrApply.IfPositive__c = true;
listPTSTRApply.add(tPstrApply);
listPUpdTSTR = [select Id,Temporary_STR_Apply__c,Tem_IfSelect__c from Temporary_obj_STR__c where Id IN :setTemSTRIdP];
}
checkSelectdN();
if(allTemSTRNValue)
{
doAllNTemSTRItem();
}
if(setTemSTRIdN != null && setTemSTRIdN.size()>0)
{
Temporary_STR_Apply__c tNstrApply = new Temporary_STR_Apply__c();
tNstrApply.Date__c = Date.newInstance(Integer.valueof(StrDateYear),Integer.valueof(StrDateMonth)+1, 1).addDays(-1);
tNstrApply.Status__c = 'Draft';
if(retailerName != null)
{
list<obj_Retailer__c> listVR = new list<obj_Retailer__c>();
listVR = [select Id,Name,fld_retailer_Salesman__r.Approval_RSM__c from obj_Retailer__c where Name =: retailerName];
if(listVR != null && listVR.size()>0)
{
obj_Retailer__c virtualRetailer = listVR[0];
tNstrApply.Retailer__c = virtualRetailer.Id;
tNstrApply.Approval_RSM__c = virtualRetailer.fld_retailer_Salesman__r.Approval_RSM__c;
}
}
tNstrApply.IfPositive__c = false;
listNTSTRApply.add(tNstrApply);
listNUpdTSTR = [select Id,Temporary_STR_Apply__c,Tem_IfSelect__c from Temporary_obj_STR__c where Id IN :setTemSTRIdN];
}
if(listPTSTRApply != null && listPTSTRApply.size()>0)
{
insert listPTSTRApply;
for(Temporary_obj_STR__c tstr : listPUpdTSTR)
{
tstr.Temporary_STR_Apply__c = listPTSTRApply[0].Id;
tstr.Tem_IfSelect__c = true;
}
update listPUpdTSTR;
return new PageReference(baseurl+'/'+listPTSTRApply[0].Id);
}
if(listNTSTRApply != null && listNTSTRApply.size()>0)
{
insert listNTSTRApply;
for(Temporary_obj_STR__c tstr : listNUpdTSTR)
{
tstr.Temporary_STR_Apply__c = listNTSTRApply[0].Id;
tstr.Tem_IfSelect__c = true;
}
update listNUpdTSTR;
return new PageReference(baseurl+'/'+listNTSTRApply[0].Id);
}
return null;
}
//取消方法
public void cancelMethod()
{
searchMethod();
}
public set<Id> setTemSTRIdP = new set<Id>();//存放页面上勾选上的整数Temporary STR集合
//为每一页的整数Temporary STR内部类赋值
public void setListTemSTRWrapperP()
{
listTemSTRWrapperP = new list<TemSTRWrapper>();
for(Temporary_obj_STR__c temstr : (list<Temporary_obj_STR__c>)PTemSTRPage.controller.getRecords())
{
TemSTRWrapper temstrWrapper = new TemSTRWrapper();
temstrWrapper.temSTR = temstr;
if(!setTemSTRIdP.contains(temstr.Id))
{
temstrWrapper.IsChecked = false;
}
else
{
temstrWrapper.IsChecked = true;
}
listTemSTRWrapperP.add(temstrWrapper);
}
}
//再点击翻页按钮时把当前页被勾选整数Temporary STR的放到集合中或取消勾选的移除集合
public void checkSelectdP()
{
for(TemSTRWrapper tsw : listTemSTRWrapperP)
{
if(tsw.IsChecked == true)
{
setTemSTRIdP.add(tsw.temSTR.Id);
}
else
{
if(setTemSTRIdP.contains(tsw.temSTR.Id))
setTemSTRIdP.remove(tsw.temSTR.Id);
}
}
}
public set<Id> setTemSTRIdN = new set<Id>();//存放页面上勾选上的负数Temporary STR集合
//为每一页的负数Temporary STR内部类赋值
public void setListTemSTRWrapperN()
{
listTemSTRWrapperN = new list<TemSTRWrapper>();
for(Temporary_obj_STR__c temstr : (list<Temporary_obj_STR__c>)NTemSTRPage.controller.getRecords())
{
TemSTRWrapper temstrWrapper = new TemSTRWrapper();
temstrWrapper.temSTR = temstr;
if(!setTemSTRIdN.contains(temstr.Id))
{
temstrWrapper.IsChecked = false;
}
else
{
temstrWrapper.IsChecked = true;
}
listTemSTRWrapperN.add(temstrWrapper);
}
}
//再点击翻页按钮时把当前页被勾选负数Temporary STR的放到集合中或取消勾选的移除集合
public void checkSelectdN()
{
for(TemSTRWrapper tsw : listTemSTRWrapperN)
{
if(tsw.IsChecked == true)
{
setTemSTRIdN.add(tsw.temSTR.Id);
}
else
{
if(setTemSTRIdN.contains(tsw.temSTR.Id))
setTemSTRIdN.remove(tsw.temSTR.Id);
}
}
}
//内部类
public class TemSTRWrapper
{
public Temporary_obj_STR__c temSTR{get;set;}
public Boolean IsChecked{get;set;}
}
/*整数Temporary STR分页----------start-------------*/
public void FirstPageP()
{
if(allTemSTRPValue)
{
doAllPTemSTRItem();
}
checkSelectdP();
PTemSTRPage.FirstPage();
setListTemSTRWrapperP();
}
public void NextPageP()
{
if(allTemSTRPValue)
{
doAllPTemSTRItem();
}
checkSelectdP();
PTemSTRPage.NextPage();
setListTemSTRWrapperP();
}
public void PreviousPageP()
{
if(allTemSTRPValue)
{
doAllPTemSTRItem();
}
checkSelectdP();
PTemSTRPage.PreviousPage();
setListTemSTRWrapperP();
}
public void LastPageP()
{
if(allTemSTRPValue)
{
doAllPTemSTRItem();
}
checkSelectdP();
PTemSTRPage.LastPage();
setListTemSTRWrapperP();
}
/*整数Temporary STR分页----------end-------------*/
/*整数Temporary STR分页----------start-------------*/
public void FirstPageN()
{
checkSelectdN();
NTemSTRPage.FirstPage();
setListTemSTRWrapperN();
}
public void NextPageN()
{
checkSelectdN();
NTemSTRPage.NextPage();
setListTemSTRWrapperN();
}
public void PreviousPageN()
{
checkSelectdN();
NTemSTRPage.PreviousPage();
setListTemSTRWrapperN();
}
public void LastPageN()
{
checkSelectdN();
NTemSTRPage.LastPage();
setListTemSTRWrapperN();
}
/*整数Temporary STR分页----------end-------------*/
public Boolean allTemSTRPValue{get;set;}//存放整数Temporary STR控制全选的复选框的值
//整数Temporary STR全选
public void doAllPTemSTRItem()
{
strPTemSTR += ' and Tem_fld_str_STR_Date__c >=: startDate and Tem_fld_str_STR_Date__c <: endDate';
if(allTemSTRPValue)
{
for(Temporary_obj_STR__c temstr : Database.query(strPTemSTR))
{
setTemSTRIdP.add(temstr.Id);
}
}
}
public Boolean allTemSTRNValue{get;set;}//存放负数Temporary STR控制全选的复选框的值
//负数Temporary STR全选
public void doAllNTemSTRItem()
{
strNTemSTR += ' and Tem_fld_str_STR_Date__c >=: startDate and Tem_fld_str_STR_Date__c <: endDate';
if(allTemSTRNValue)
{
for(Temporary_obj_STR__c temstr : Database.query(strNTemSTR))
{
setTemSTRIdN.add(temstr.Id);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment