package cn.itcast.elec.service.impl;
import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.elec.dao.IElecSystemDDLDao;
import cn.itcast.elec.dao.IElecUserDao;
import cn.itcast.elec.domain.ElecUser;
import cn.itcast.elec.service.IElecUserService;
import cn.itcast.elec.util.GenerateSqlFromExcel;
import cn.itcast.elec.util.MD5keyBean;
import cn.itcast.elec.util.PageInfo;
import cn.itcast.elec.util.StringHelper;
import cn.itcast.elec.web.form.ElecUserForm;
//严格控制业务层事务:类级别的事务定义为只读模式(只有增删改方法需要事务写的权限,可以在这些方法上赋予事务写的权限
@Transactional(readOnly=true)
@Service(IElecUserService.SERVICE_NAME)
public class ElecUserServiceImpl implements IElecUserService{
@Resource(name=IElecUserDao.SERVICE_NAME)
private IElecUserDao elecUserDao;
// 引入数据字典
@Resource(name=IElecSystemDDLDao.SERVICE_NAME)
private IElecSystemDDLDao elecSystemDDLDao;
/**
* @description 查询所有用户列表信息
* 判断用户姓名是否为空,如果不为空,按照用户姓名组织查询条件
* @create date 2017-12-29
* @param ElecUserForm elecUserForm VO对象存放用户姓名
* @return List<ElecUserForm>用户信息结果集对象
*/
@Override
public List<ElecUserForm> findElecUserList(ElecUserForm elecUserForm,HttpServletRequest request) {
// 组织查询条件
String hqlWhere = "";
List<String> paramsList = new ArrayList<String>();
if(elecUserForm!=null && elecUserForm.getUserName()!=null && !elecUserForm.getUserName().equals("")){
hqlWhere += " and o.userName like ?";
paramsList.add("%" + elecUserForm.getUserName() + "%");
}
// 集合转换成数组
Object[] params = paramsList.toArray();
// 组织排序
LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
// 按入职时间降序排序
orderby.put("o.onDutyDate", "desc");
// begin:2018-02-27修改,添加分页功能
PageInfo pageInfo = new PageInfo(request);
// 执行底层查询条,返回PO对象
// List<ElecUser> list = elecUserDao.findCollectionByConditionNoPage(hqlWhere, params, orderby);
List<ElecUser> list = elecUserDao.findCollectionByConditionWithPage(hqlWhere, params, orderby ,pageInfo);
request.setAttribute("page", pageInfo.getPageBean());
// end
// 将PO对象转换为VO对象
List<ElecUserForm> formList = this.elecUserPOListToVOList(list);
return formList;
}
// 将PO对象转换为VO对象
private List<ElecUserForm> elecUserPOListToVOList(List<ElecUser> list) {
List<ElecUserForm> formList = new ArrayList<ElecUserForm>();
ElecUserForm elecUserForm = null;
for(int i=0;list!=null && i<list.size();i++){
ElecUser elecUser =list.get(i);
elecUserForm = new ElecUserForm();
// 将Integer类型转换为String类型
elecUserForm.setUserID(elecUser.getUserID()); //主键id
elecUserForm.setLogonName(elecUser.getLogonName()); // 登录名
elecUserForm.setUserName(elecUser.getUserName()); // 用户姓名
// 将code转换为数据字典中的value值显示到页面
// 通过性别code和对应数据类型查询性别value值
elecUserForm.setSexID(elecUser.getSexID()!=null && !elecUser.getSexID().equals("")?elecSystemDDLDao.findDDLName(elecUser.getSexID(),"性别"):""); // 性别
elecUserForm.setContactTel(elecUser.getContactTel()); // 联系电话
// 将code转换为数据字典中的value值显示到页面
// 通过是否在职code和对应数据类型查询是否在职value值
elecUserForm.setIsDuty(elecUser.getIsDuty()!=null && !elecUser.getIsDuty().equals("")?elecSystemDDLDao.findDDLName(elecUser.getIsDuty(),"是否在职"):""); // 是否在职
formList.add(elecUserForm);
}
return formList;
}
/**
* @description 保存用户添加页面信息,需将此方法的事务定义为可写的
* @create date 2017-12-31
* @param ElecUserForm elecUserForm VO对象用于存放用户信息
* @return 无
*/
@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
@Override
public void saveElecUser(ElecUserForm elecUserForm) {
// 将VO对象转换成PO对象
ElecUser elecUser = this.elecUserVOToPO(elecUserForm);
// 从页面获取userID的值,如果userID不为空则执行更新操作
if(elecUserForm.getUserID()!=null && !elecUserForm.getUserID().equals("")){
elecUserDao.update(elecUser);
}else{// 如果userID为空则执行保存操作
elecUserDao.save(elecUser);
}
}
/*
* 将页面用户信息从VO对象转换成PO对象
*/
private ElecUser elecUserVOToPO(ElecUserForm elecUserForm) {
ElecUser elecUser = new ElecUser();
if(elecUserForm!=null){
if(elecUserForm.getUserID()!=null && !elecUserForm.getUserID().equals("")){
// 出现的严重问题:将elecUser.setUserID写成了elecUser.setJctID,导致update不起作用,但save方法起作用
elecUser.setUserID(elecUserForm.getUserID());
if(elecUserForm.getOffDutyDate()!=null && !elecUserForm.getOffDutyDate().equals("")){
elecUser.setOffDutyDate(StringHelper.stringCoverDate(elecUserForm.getOffDutyDate()));
}
}
elecUser.setJctID(elecUserForm.getJctID());
elecUser.setUserName(elecUserForm.getUserName());
elecUser.setLogonName(elecUserForm.getLogonName());
/**************** begin******************/
// 2018-01-29修改对密码进行MD5加密
String password = elecUserForm.getLogonPwd();
String md5Password = "";
// 如果没有设置密码,则默认密码为6个0
if(password==null || "".equals(password)){
password = "000000";
}
String md5flag = elecUserForm.getMd5flag();
// 修改用户时,若前后两次密码一致,则不需要进行加密
if(md5flag!=null && md5flag.equals("1")){
md5Password=password;
}
// 无论是新增用户还是修改密码,都需要加密
else{
MD5keyBean md5 = new MD5keyBean();
md5Password = md5.getkeyBeanofStr(password);
}
elecUser.setLogonPwd(md5Password);
/***********************end**************************/
elecUser.setSexID(elecUserForm.getSexID());
// 从字符串类型转换为日期类型
if(elecUserForm.getBirthday()!=null && !elecUserForm.getBirthday().equals("")){
elecUser.setBirthday(StringHelper.stringCoverDate(elecUserForm.getBirthday()));
}
elecUser.setAddress(elecUserForm.getAddress());
elecUser.setContactTel(elecUserForm.getContactTel());
elecUser.setEmail(elecUserForm.getEmail());
elecUser.setMobile(elecUserForm.getMobile());
elecUser.setIsDuty(elecUserForm.getIsDuty());
// 从字符串类型转换为日期类型
if(elecUserForm.getOnDutyDate()!=null && !elecUserForm.getOnDutyDate().equals("")){
elecUser.setOnDutyDate(StringHelper.stringCoverDate(elecUserForm.getOnDutyDate()));
}
elecUser.setRemark(elecUserForm.getRemark());
}
return elecUser;
}
/**
* @description 使用用户ID进行查询,获取用户的详细信息
* @create date 2017-12-31
* @param ElecUserForm elecUserForm VO对象用于存放用户ID
* @return ElecUserForm VO对象存放用户的详细信息
*/
@Override
public ElecUserForm findElecUser(ElecUserForm elecUserForm) {
String userID = elecUserForm.getUserID();
ElecUser elecUser = elecUserDao.findObjectById(userID);
// PO对象转换成VO对象,方式一:这种方法需要在ElecUserAction使用值栈的方式传递elecUserForm对象
// elecUserForm = this.elecUserPOToVO(elecUser);
// PO对象转换成VO对象,方式二:使用参数的方式传递elecUserForm对象,不需要在ElecUserAction中使
lly202406
- 粉丝: 2816
- 资源: 5456
最新资源
- 微信小程序毕业设计-基于SSM的电影交流小程序【代码+论文+PPT】.zip
- 微信小程序毕业设计-基于SSM的食堂线上预约点餐小程序【代码+论文+PPT】.zip
- 锐捷交换机的堆叠,一个大问题
- 微信小程序毕业设计-基于SSM的校园失物招领小程序【代码+论文+PPT】.zip
- MATLAB《结合萨克拉门托模型和遗传算法为乐安河流域建立一个水文过程预测模型》+项目源码+文档说明
- 基于人工神经网络/随机森林/LSTM的径流预测项目
- 微信小程序毕业设计-基于SSM的驾校预约小程序【代码+论文+PPT】.zip
- Aspose.Words 18.7 版本 Word转成PDF无水印
- 微信小程序毕业设计-基于Python的摄影竞赛小程序【代码+论文+PPT】.zip
- PCS7 Drive ES APL V9.1
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈