📄 assetaction.java
字号:
/*
* @(#)AssetAction.java Apr 4, 2009
* Copyright 2009 qrsx organization, Inc. All rights reserved
*/
package com.qrsx.asset.action;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.qrsx.asset.dao.AssetDAO;
import com.qrsx.asset.dao.AssetTypeDAO;
import com.qrsx.asset.form.AssetForm;
import com.qrsx.asset.model.Asset;
import com.qrsx.asset.model.Page;
/**
*
* Author : 马利永<br>
* Company : qrsx software<br>
* Version : 1.0<br>
* Date : Apr 4, 2009<br>
*/
public class AssetAction extends BaseAction{
/**
* 保存资产类型:更新,创建
* @param mapping
* @param form
* @param request
* @param response
* @return
*/
public ActionForward save(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)throws Exception{
//验证数据有效性(手动)
ActionErrors errors =form.validate(mapping, request);
if(!errors.isEmpty()){
saveErrors(request,errors);
// return mapping.findForward("edit");
return edit(mapping,form,request,response);
}
AssetForm assetForm=(AssetForm)form;
Asset asset=new Asset();
//从form中获取值,组装assetType实体对象
BeanUtils.copyProperties(asset, form);
AssetDAO dao=new AssetDAO();
if(assetForm.getId()==null||assetForm.getId().trim().length()==0){
dao.create(asset);
saveMessage(request,"assetForm.added",asset.getName());
}else{
dao.update(asset);
saveMessage(request,"assetForm.updated",asset.getName());
}
return mapping.findForward("success");
}
/**
* 编辑资产类型:进入新建。更新页面
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward edit(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)throws Exception{
String id = request.getParameter("id");
if(id!=null&&id.trim().length()>0){ //现判断是否为空,否则id.trim()报错
AssetDAO dao=new AssetDAO();
Asset asset=dao.findById(Integer.valueOf(id));
//将assetType的值赋给form
if(asset!=null){
BeanUtils.copyProperties(form, asset);
}
}
AssetForm assetForm=(AssetForm) form;
//存储为form的一个属性
List addTypes= new ArrayList();
addTypes.add("自建");
addTypes.add("购入");
addTypes.add("融资租赁");
addTypes.add("捐赠");
assetForm.setAddTypes(addTypes);
//存储为单独的集合bean
List depts =new ArrayList();
depts.add("技术部");
depts.add("业务部");
depts.add("就业部");
depts.add("教务部");
request.setAttribute("depts", depts);
//存放单独的AssetType bean集合
AssetTypeDAO dao=new AssetTypeDAO();
List assetTypes= dao.findAll();
request.setAttribute("assetTypes", assetTypes);
return mapping.findForward("edit");
//存放单独的employee Bean集合
}
/**
* 删除资产类型
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward delete(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)throws Exception{
String id=request.getParameter("id");
if(id!=null&&id.trim().length()>0){
AssetDAO dao=new AssetDAO();
dao.delete(Integer.valueOf(id));
//设置提示信息
saveMessage(request,"assetForm.deleted");
}
return mapping.findForward("success");
}
/**
* 检索所有的资产类型
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward findAll(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)throws Exception{
AssetDAO dao=new AssetDAO();
List<Asset> list=dao.findAll();
request.setAttribute("assets", list);
return mapping.findForward("list");
}
/**
* 动态检索所有的资产类型
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward 对象
* @throws Exception
*/
public ActionForward list(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)throws Exception{
//获取当前页数,空则置1
String page = request.getParameter("currentpage");
Integer currentpage=1;
if(page!=null){
currentpage=Integer.valueOf(page);
}
//实例化dao 与asset实例
AssetDAO dao=new AssetDAO();
Asset asset=new Asset();
//从form获取数据
AssetForm assetform = (AssetForm)form;
BeanUtils.copyProperties(asset, assetform);
//获取显式传递过来的name值,若不空则转码,并设为assetType的值
String name=request.getParameter("na");
if(name!=null){
name=new String (name.getBytes("ISO-8859-1"),"gb2312" );
asset.setName(name);
}
Object [] object=dao.list(asset,currentpage);
//保存list与page到request范围中
request.setAttribute("assets", object[0]);
request.setAttribute("page", object[1]);
//动态查询,保存用户输入的内容,方便下一页显示
request.setAttribute("assetForm",asset);
//存放单独的bean集合(初始化类型查询)
AssetTypeDAO dao1=new AssetTypeDAO();
List assetTypes= dao1.findAll();
request.setAttribute("assetTypes", assetTypes);
return mapping.findForward("list");
}
/**
* 动态检索资产
*/
public ActionForward dislist(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception{
//获取当前页数,空则置1
String page = request.getParameter("currentpage");
Integer currentpage=1;
if(page!=null){
currentpage=Integer.valueOf(page);
}
//实例化dao 与asset实例
AssetDAO dao=new AssetDAO();
Asset asset=new Asset();
//从form获取数据
AssetForm assetform = (AssetForm)form;
BeanUtils.copyProperties(asset, assetform);
//获取显式传递过来的name值,若不空则转码,并设为assetType的值
String name=request.getParameter("na");
if(name!=null){
name=new String (name.getBytes("ISO-8859-1"),"gb2312" );
asset.setName(name);
}
Object [] object=dao.list(asset,currentpage);
//保存list与page到request范围中
request.setAttribute("assets", object[0]);
request.setAttribute("page", object[1]);
//动态查询,保存用户输入的内容,方便下一页显示
request.setAttribute("assetForm",asset);
//存放单独的bean集合(初始化类型查询)
AssetTypeDAO dao1=new AssetTypeDAO();
List assetTypes= dao1.findAll();
request.setAttribute("assetTypes", assetTypes);
return mapping.findForward("dislist");
}
/**
* 计算折旧价格,更新保存
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward disprice(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)throws Exception{
//实例化dao 与asset实例
AssetDAO dao=new AssetDAO();
Asset asset=new Asset();
Integer currentpage=1;
Object [] object=dao.list(asset,currentpage);
//获取list与page对象
ArrayList<Asset> list=(ArrayList<Asset>) object[0];
Page page= (Page) object[1];
//计算本次list中的对象的折旧价格,并更新
countprice(list);
//获取记录总个数
Integer totalpage=page.getTotalpage();
//循环获取list,加上上面的一次,共totalpage次
for(int i=0;i<totalpage-1;i++){
++currentpage;
Object [] object1=dao.list(asset,currentpage);
//获取list对象
ArrayList<Asset> list1=(ArrayList<Asset>) object1[0];
//计算本次list中的对象的折旧价格,并更新
countprice(list1);
}
return mapping.findForward("success2");
}
public void countprice(ArrayList<Asset> list) throws Exception{
//获取当前日期
Date date = new Date();
String format="yyyy-MM-dd";
SimpleDateFormat form=new SimpleDateFormat(format);
String currentTime=form.format(date);
AssetDAO dao = new AssetDAO();
for(int i=0;i<list.size();i++){
Asset asset1=list.get(i);
if(asset1.getDisprice()<=0){
asset1.setState("4");
dao.updateState(asset1);
}else{
//计算折旧率
Double dis = (asset1.getLife()*12)/asset1.getPrice();
//计算使用几个月
String[] s1 = asset1.getBuyDate().split("-");
String[] s2 = currentTime.split("-");
// 截止到目前使用时间
Integer useMonth = (Integer.valueOf(s2[0]) - Integer.valueOf(s1[0]))* 12 + Integer.valueOf(s2[1]) - Integer.valueOf(s1[1]);
// 折旧价格
Double price = asset1.getPrice();
Double disprice = price - price*dis*useMonth;
asset1.setDisprice(disprice);
if(disprice>0){
dao.updateDisprice(asset1);
}else {
asset1.setState("4");
dao.updateDisprice(asset1);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -