package com.sun.order.service;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.sun.cususer.vo.CusUserVo;
import com.sun.cususeradd.vo.CusUserAddVo;
import com.sun.order.dao.OrderDao;
import com.sun.order.vo.OrderVo;
import com.sun.pay.dao.PayDao;
import com.sun.system.time.Time;
import com.sun.util.view.RequestPage;
@Service
@Transactional
public class OrderServiceImpl implements OrderService {
public static Log _log = LogFactory.getLog(OrderServiceImpl.class);
@Resource
private OrderDao orderDao;
@Resource
private PayDao payDao;
@Override
public void insert(CusUserVo cusUserVo,CusUserAddVo cusUserAddVo,OrderVo orderVo) {
_log.info("新增订单");
// TODO Auto-generated method stub
String mobileNumber = cusUserVo.getMobileNumber();
orderVo.setOrderNum(Time.getTimeString()+mobileNumber.substring(7));
orderVo.setCusId(cusUserVo.getCusId());
orderVo.setName(cusUserVo.getName());
orderVo.setMobileNum(mobileNumber);
orderVo.setIdentityCard(cusUserVo.getIdentityCard());
orderVo.setGender(cusUserVo.getGender());
orderVo.setSalary(cusUserAddVo.getSalary());
orderVo.setApplyDate(new Date());
orderVo.setSesameSeed(cusUserVo.getSesameSeed());
orderVo.setFirstFlag("S");;
orderVo.setFinalFlag("S");
orderVo.setGrantFlag("S");
orderVo.setPayCount(cusUserVo.getPayCount());
orderVo.setPayMoney(new BigDecimal(0));
orderVo.setCount(0);
insertOne(orderVo);
}
@Override
public OrderVo selectById(Long orderId) {
// TODO Auto-generated method stub
return orderDao.selectById(orderId);
}
@Override
public List<OrderVo> selectAll() {
// TODO Auto-generated method stub
return orderDao.selectAll();
}
@Override
public void insertOne(OrderVo orderVo) {
// TODO Auto-generated method stub
orderDao.insertOne(orderVo);
}
@Override
public void updateOne(OrderVo orderVo) {
// TODO Auto-generated method stub
orderDao.updateOne(orderVo);
}
@Override
public void deleteById(Long orderId) {
// TODO Auto-generated method stub
orderDao.deleteById(orderId);
}
@SuppressWarnings("unchecked")
@Override
public List<OrderVo> selectByWhere(RequestPage requestPage) {
// TODO Auto-generated method stub
String hql = "from OrderVo";
String queryHql = "";
Map<Object, Object> requestMap = requestPage.getRequestMap();
if(requestMap != null && requestMap.size() > 0){
Iterator iter = requestMap.entrySet().iterator();
boolean first = true;
Entry<Object, Object> entry = null;
Object key = null;
Object val = null;
while(iter.hasNext()){
entry = (Entry<Object, Object>) iter.next();
key = entry.getKey();
val = entry.getValue();
if(val != null && !val.equals("")){
if(first){
queryHql += " where "+key+" like '%"+val+"%'";
first = false;
}else{
queryHql += " and "+key+" like '%"+val+"%'";
}
}
}
}
queryHql += " order by orderId desc";
hql += " "+queryHql;
return orderDao.find(hql,requestPage.getPageStart(), requestPage.getRowCount());
}
@SuppressWarnings("unchecked")
public List<OrderVo> selectByCusId(RequestPage requestPage) {
// TODO Auto-generated method stub
String hql = "from OrderVo where cusId =";
String queryHql = "";
Map<Object, Object> requestMap = requestPage.getRequestMap();
if(requestMap != null && requestMap.size() > 0){
hql += requestMap.get("cusId").toString();
}
queryHql += " order by orderId desc";
hql += " "+queryHql;
return orderDao.find(hql,requestPage.getPageStart(), requestPage.getRowCount());
}
@SuppressWarnings("unchecked")
@Override
public int getRowCount(RequestPage requestPage) {
// TODO Auto-generated method stub
String hql = "select count(orderId) from OrderVo";
String queryHql = "";
Map<Object, Object> requestMap = requestPage.getRequestMap();
if(requestMap != null && requestMap.size() > 0){
Iterator iter = requestMap.entrySet().iterator();
boolean first = true;
Entry<Object, Object> entry = null;
Object key = null;
Object val = null;
while(iter.hasNext()){
entry = (Entry<Object, Object>) iter.next();
key = entry.getKey();
val = entry.getValue();
if(val != null && !val.equals("")){
if(first){
queryHql += " where "+key+" like '%"+val+"%'";
first = false;
}else{
queryHql += " and "+key+" like '%"+val+"%'";
}
}
}
}
hql += " "+queryHql;
return orderDao.getRowCount(hql);
}
@Override
public int getRowCountByCusid(RequestPage requestPage) {
// TODO Auto-generated method stub
String hql = "select count(orderId) from OrderVo where cusId =";
Map<Object, Object> requestMap = requestPage.getRequestMap();
if(requestMap != null && requestMap.size() > 0){
hql += requestMap.get("cusId").toString();
}
return orderDao.getRowCount(hql);
}
public int getOrderCount(Long cusUserId) {
String hql = "select count(orderId) from OrderVo where cusId = "+cusUserId;
return orderDao.getRowCount(hql);
}
/**
* 获取待审批申请
*/
@SuppressWarnings("unchecked")
@Override
public List<OrderVo> getApproveFirst(Long orderId) {
_log.info("获取待审批申请="+orderId);
// TODO Auto-generated method stub
String hql = "from OrderVo where cusId = "+orderId+" and firstFlag = 'S' order by orderId desc";
return orderDao.find(hql,-1,-1);
}
/**
* 获取审批中申请
*/
@SuppressWarnings("unchecked")
@Override
public List<OrderVo> getApproveFinal(Long orderId) {
_log.info("获取审批中申请="+orderId);
// TODO Auto-generated method stub
String hql = "from OrderVo where cusId = "+orderId+" and firstFlag = 'Y' and finalFlag = 'S' order by orderId desc";
return orderDao.find(hql,-1,-1);
}
/**
* 获取待放款
*/
@SuppressWarnings("unchecked")
@Override
public List<OrderVo> getGrantFirst(Long orderId) {
_log.info("获取待放款="+orderId);
// TODO Auto-generated method stub
String hql = "from OrderVo where cusId = "+orderId+" and firstFlag = 'Y' and finalFlag = 'Y' and grantFlag = 'S' order by orderId desc";
return orderDao.find(hql,-1,-1);
}
/**
* 获取已放款
*/
@SuppressWarnings("unchecked")
@Override
public List<OrderVo> getGrantFinal(Long orderId) {
_log.info("获取已放款="+orderId);
// TODO Auto-generated method stub
String hql = "from OrderVo where cusId = "+orderId+" and firstFlag = 'Y' and finalFlag = 'Y' and grantFlag = 'Y' order by orderId desc";
return orderDao.find(hql,-1,-1);
}
/**
* 获取审批退回
*/
@SuppressWarnings("unchecked")
@Override
public List<OrderVo> getReturnApply(Long orderId) {
_log.info("获取审批退回="+orderId);
// TODO Auto-generated method stub
String hql = "from OrderVo where cusId = "+orderId+" and (firstFlag = 'N' or finalFlag = 'N') order by orderId desc";
return orderDao.find(hql,-1,-1);
}
/**
* 获取逾期
*/
@SuppressWarnings("unchecked")
@Override
public List<OrderVo> getNotOverFlag(Long orderId) {
_log.info("获取逾期="+orderId);
// TODO Auto-generated method stub
String hql = "from OrderVo where cusId = "+orderId+" and firstFlag = 'Y' and finalFlag = 'Y' and grantFlag = 'Y' and overFlag = 'N' order by orderId desc";
return orderDao.find(hql,-1,-1);
}
/**
* 获取结清
*/
@SuppressWarnings("unchecked")
@Override
public List<OrderVo> getOverFlag(Long orderId) {
_log.info("获取结清="+orderId);
// TODO Auto-generated method stub
St