com.dz.module.vehicle.VehicleApprovalService.java Source code

Java tutorial

Introduction

Here is the source code for com.dz.module.vehicle.VehicleApprovalService.java

Source

package com.dz.module.vehicle;

import com.dz.common.factory.HibernateSessionFactory;
import com.dz.common.global.DateUtil;
import com.dz.common.global.ToDo;
import com.dz.common.global.WaitDeal;
import com.dz.common.global.WaitToDo;
import com.dz.common.other.ObjectAccess;
import com.dz.module.charge.ChargeDao;
import com.dz.module.charge.ChargePlan;
import com.dz.module.contract.Contract;
import com.dz.module.contract.ContractDao;
import com.dz.module.driver.Driver;
import com.dz.module.driver.DriverService;
import com.dz.module.driver.Driverincar;
import com.dz.module.user.RelationUr;
import com.dz.module.user.Role;
import com.dz.module.user.User;
import com.dz.module.user.message.Message;
import com.dz.module.user.message.MessageToUser;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@WaitDeal(name = "vehicleApprovalService")
public class VehicleApprovalService implements WaitToDo {
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private VehicleApprovalDao vehicleApprovalDao;

    /**
     * ?
     * @param vehicleApproval
     * @param contract 
     * @return
     */
    public boolean addVehicleApproval(VehicleApproval vehicleApproval, Contract contract) {
        HttpServletRequest request = ServletActionContext.getRequest();
        if (vehicleApproval == null) {
            request.setAttribute("msgStr", "???");
            return false;
        }

        long count = ObjectAccess.execute("select count(*) from VehicleApproval " + "where checkType='"
                + vehicleApproval.getCheckType() + "'" + " and state!=8 and state>0 "
                + "and contractId in (select id from Contract where idNum='" + contract.getIdNum() + "') ");
        if (count > 0) {
            request.setAttribute("msgStr",
                    "??????");
            return false;
        }

        String hql = "select count(*) from VehicleApproval " + "where checkType='" + vehicleApproval.getCheckType()
                + "'" + " and state!=8 and state>0 "
                + (StringUtils.isEmpty(contract.getCarframeNum()) ? " and 1=0 "
                        : "and contractId in (select id from Contract where carframeNum='"
                                + contract.getCarframeNum().trim() + "') ");

        //System.out.println(hql);

        long count2 = ObjectAccess.execute(hql);

        if (count2 > 0) {
            request.setAttribute("msgStr",
                    "??????");
            return false;
        }

        switch (vehicleApproval.getCheckType()) {
        case 0:
            contract.setState((short) 2);
            contract.setAscription(vehicleApproval.getAscription());
            if (contract.getContractFrom() != null && contract.getContractFrom() != 0) {
                //    
                Vehicle v = ObjectAccess.getObject(Vehicle.class, contract.getCarframeNum());
                v.setDriverId(contract.getIdNum());
                ObjectAccess.saveOrUpdate(v);

                if (v.getOperateCardTime() != null) {
                    vehicleApproval.setOperateCardDate(v.getOperateCardTime());
                }

                if (v.getLicenseRegistTime() != null) {
                    vehicleApproval.setOperateApplyDate(v.getLicenseRegistTime());
                }

                Contract c = ObjectAccess.getObject(Contract.class, contract.getContractFrom());
                contract.setContractId(c.getContractId());
                contract.setContractType(c.getContractType());
                contract.setContractEndDate(c.getContractEndDate());

            } else if (StringUtils.isNotBlank(contract.getCarNumOld())) {
                Contract c = ObjectAccess.execute(
                        "from Contract c where c.state=1 and c.carframeNum in (select v.carframeNum from Vehicle v where v.licenseNum='"
                                + contract.getCarNumOld().trim() + "') order by contractBeginDate desc");

                if (c == null || StringUtils.isBlank(c.getCarframeNum())) {
                    request.setAttribute("msgStr", "??");
                    return false;
                }

                Vehicle v = ObjectAccess.getObject(Vehicle.class, c.getCarframeNum());
                if (v == null) {
                    request.setAttribute("msgStr", "???");
                    return false;
                }
                v.setReused(true);
                ObjectAccess.saveOrUpdate(v);
            }

            if (!contractDao.contractWrite(contract)) {
                return false;
            }
            vehicleApproval.setContractId(contract.getId());
            break;
        case 1:
            /*if(!contractDao.contractAbandon(contract)){
               return false;
            }*/

            Contract c = contractDao.selectById(contract.getId());
            Vehicle vehicle = (Vehicle) ObjectAccess.getObject("com.dz.module.vehicle.Vehicle", c.getCarframeNum());
            VehicleMode vehicleMode = (VehicleMode) ObjectAccess.getObject("com.dz.module.vehicle.VehicleMode",
                    vehicle.getCarMode());
            if (vehicleMode != null)
                vehicleApproval.setFueltype(vehicleMode.getFuel());

            c.setAbandonRequest(contract.getAbandonRequest());
            c.setAbandonReason(contract.getAbandonReason());
            ObjectAccess.saveOrUpdate(c);

            vehicleApproval.setContractId(contract.getId());
            break;
        default:
            return false;
        }

        vehicleApproval.setState(1);
        vehicleApproval.setIsFinished(false);

        if (BooleanUtils.isTrue(contract.getGeneByImport())) {
            vehicleApproval.setBranchManagerName(1);
        } else {

            vehicleApproval.setBranchManagerName(((User) request.getSession().getAttribute("user")).getUid());
        }
        vehicleApproval.setBranchManagerApprovalDate(new Date());

        return vehicleApprovalDao.addVehicleApproval(vehicleApproval);
    }

    /**
     * ?
     * @param _vehicleApproval
     * @return
     */
    public boolean updateVehicleApproval(VehicleApproval _vehicleApproval) {
        HttpServletRequest request = null;
        int uName = 1;

        VehicleApproval vehicleApproval = vehicleApprovalDao.queryVehicleApprovalById(_vehicleApproval.getId());
        int state = vehicleApproval.getState();

        Contract contract = (Contract) ObjectAccess.getObject("com.dz.module.contract.Contract",
                vehicleApproval.getContractId());

        //      if(BooleanUtils.isTrue(contract.getGeneByImport())){
        //         request = new HttpServletRequest(){
        //            @Override
        //            public AsyncContext getAsyncContext() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public Object getAttribute(String name) {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public Enumeration<String> getAttributeNames() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String getCharacterEncoding() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public int getContentLength() {
        //               // TODO Auto-generated method stub
        //               return 0;
        //            }
        //
        //            @Override
        //            public long getContentLengthLong() {
        //               // TODO Auto-generated method stub
        //               return 0;
        //            }
        //
        //            @Override
        //            public String getContentType() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public DispatcherType getDispatcherType() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public ServletInputStream getInputStream() throws IOException {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String getLocalAddr() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String getLocalName() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public int getLocalPort() {
        //               // TODO Auto-generated method stub
        //               return 0;
        //            }
        //
        //            @Override
        //            public Locale getLocale() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public Enumeration<Locale> getLocales() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String getParameter(String name) {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public Map<String, String[]> getParameterMap() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public Enumeration<String> getParameterNames() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String[] getParameterValues(String name) {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String getProtocol() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public BufferedReader getReader() throws IOException {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String getRealPath(String path) {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String getRemoteAddr() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String getRemoteHost() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public int getRemotePort() {
        //               // TODO Auto-generated method stub
        //               return 0;
        //            }
        //
        //            @Override
        //            public RequestDispatcher getRequestDispatcher(String path) {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String getScheme() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String getServerName() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public int getServerPort() {
        //               // TODO Auto-generated method stub
        //               return 0;
        //            }
        //
        //            @Override
        //            public ServletContext getServletContext() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public boolean isAsyncStarted() {
        //               // TODO Auto-generated method stub
        //               return false;
        //            }
        //
        //            @Override
        //            public boolean isAsyncSupported() {
        //               // TODO Auto-generated method stub
        //               return false;
        //            }
        //
        //            @Override
        //            public boolean isSecure() {
        //               // TODO Auto-generated method stub
        //               return false;
        //            }
        //
        //            @Override
        //            public void removeAttribute(String name) {
        //               // TODO Auto-generated method stub
        //
        //            }
        //
        //            @Override
        //            public void setAttribute(String name, Object o) {
        //               // TODO Auto-generated method stub
        //
        //            }
        //
        //            @Override
        //            public void setCharacterEncoding(String env)
        //                  throws UnsupportedEncodingException {
        //               // TODO Auto-generated method stub
        //
        //            }
        //
        //            @Override
        //            public AsyncContext startAsync() throws IllegalStateException {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public AsyncContext startAsync(ServletRequest servletRequest,
        //                  ServletResponse servletResponse)
        //                  throws IllegalStateException {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public boolean authenticate(HttpServletResponse response)
        //                  throws IOException, ServletException {
        //               // TODO Auto-generated method stub
        //               return false;
        //            }
        //
        //            @Override
        //            public String changeSessionId() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String getAuthType() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String getContextPath() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public Cookie[] getCookies() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public long getDateHeader(String name) {
        //               // TODO Auto-generated method stub
        //               return 0;
        //            }
        //
        //            @Override
        //            public String getHeader(String name) {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public Enumeration<String> getHeaderNames() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public Enumeration<String> getHeaders(String name) {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public int getIntHeader(String name) {
        //               // TODO Auto-generated method stub
        //               return 0;
        //            }
        //
        //            @Override
        //            public String getMethod() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public Part getPart(String name) throws IOException,
        //                  ServletException {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public Collection<Part> getParts() throws IOException,
        //                  ServletException {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String getPathInfo() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String getPathTranslated() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String getQueryString() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String getRemoteUser() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String getRequestURI() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public StringBuffer getRequestURL() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String getRequestedSessionId() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public String getServletPath() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public HttpSession getSession() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public HttpSession getSession(boolean create) {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public Principal getUserPrincipal() {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //            @Override
        //            public boolean isRequestedSessionIdFromCookie() {
        //               // TODO Auto-generated method stub
        //               return false;
        //            }
        //
        //            @Override
        //            public boolean isRequestedSessionIdFromURL() {
        //               // TODO Auto-generated method stub
        //               return false;
        //            }
        //
        //            @Override
        //            public boolean isRequestedSessionIdFromUrl() {
        //               // TODO Auto-generated method stub
        //               return false;
        //            }
        //
        //            @Override
        //            public boolean isRequestedSessionIdValid() {
        //               // TODO Auto-generated method stub
        //               return false;
        //            }
        //
        //            @Override
        //            public boolean isUserInRole(String role) {
        //               // TODO Auto-generated method stub
        //               return false;
        //            }
        //
        //            @Override
        //            public void login(String username, String password)
        //                  throws ServletException {
        //               // TODO Auto-generated method stub
        //
        //            }
        //
        //            @Override
        //            public void logout() throws ServletException {
        //               // TODO Auto-generated method stub
        //
        //            }
        //
        //            @Override
        //            public <T extends HttpUpgradeHandler> T upgrade(
        //                  Class<T> handlerClass) throws IOException,
        //                  ServletException {
        //               // TODO Auto-generated method stub
        //               return null;
        //            }
        //
        //         };
        //      }else{
        //         request =  ServletActionContext.getRequest();
        //         uName = ((User)request.getSession().getAttribute("user")).getUid();
        //      }

        if (vehicleApproval.getCheckType() == 0) {
            if (state == 6)//?
            {
                vehicleApproval.setApprovalOfficeDate(new Date());
                vehicleApproval.setState(8);
                vehicleApproval.setOfficeName(uName);
                vehicleApproval.setOfficeRemark(_vehicleApproval.getOfficeRemark());
                vehicleApproval.setIsapprovalOffice(_vehicleApproval.getIsapprovalOffice());

                //            contractDao.changeState(_vehicleApproval.getContractId(),3);

                Session session = null;
                Transaction tx = null;
                try {
                    session = HibernateSessionFactory.getSession();
                    tx = session.beginTransaction();
                    Contract cx = (Contract) session.get(Contract.class, _vehicleApproval.getContractId());
                    cx.setState((short) 3);
                    session.update(cx);

                    Message msg = new Message();
                    User u = (User) session.get(User.class, uName);
                    msg.setFromUser(uName);
                    msg.setTime(new Date());

                    msg.setCarframeNum(contract.getCarframeNum());
                    msg.setIdNum(contract.getIdNum());
                    msg.setType("?");

                    Driver d = (Driver) session.get(Driver.class, contract.getIdNum());

                    msg.setMsg(String.format("%tF %s?\n"
                            + "%s(%s)  %s(%s) ????",
                            msg.getTime(), u.getUname(), contract.getCarNum(), contract.getCarframeNum(),
                            d.getName(), contract.getIdNum()));

                    session.save(msg);

                    Query q_u = session.createQuery(
                            "from RelationUr where " + "rid in (select rid from Role where rname = :rname )");

                    q_u.setString("rname", "");
                    List<RelationUr> users = q_u.list();
                    for (RelationUr relationUr : users) {
                        MessageToUser mu = new MessageToUser();
                        mu.setUid(relationUr.getUid());
                        mu.setMid(msg.getId());
                        mu.setAlreadyRead(false);
                        session.saveOrUpdate(mu);
                    }
                    session.update(vehicleApproval);

                    tx.commit();
                } catch (HibernateException e) {
                    e.printStackTrace();
                    if (tx != null) {
                        tx.rollback();
                    }
                    return false;
                } finally {
                    HibernateSessionFactory.closeSession();
                }

                return true;
            } else if (state == 3)//
            {
                vehicleApproval.setApprovalFinanceDate(new Date());
                vehicleApproval.setState(4);
                vehicleApproval.setFinanceName(uName);
                vehicleApproval.setFinanceRemark(_vehicleApproval.getFinanceRemark());
                vehicleApproval.setIsapprovalFinance(_vehicleApproval.getIsapprovalFinance());
                return vehicleApprovalDao.executeUpdate(vehicleApproval);
            } else if (state == 4)//
            {
                if (!BooleanUtils.isTrue(contract.getGeneByImport())) {
                    String rentStr = (String) request.getParameter("contract.rent");
                    if (StringUtils.isNotBlank(rentStr)) {
                        double rent = NumberUtils.toDouble(rentStr);
                        contract.setRent(rent);
                        ObjectAccess.saveOrUpdate(contract);
                    }
                }

                vehicleApproval.setFinanceManagerApprovalDate(new Date());
                vehicleApproval.setState(5);
                vehicleApproval.setFinanceManagerName(uName);
                vehicleApproval.setFinanceManagerRemark(_vehicleApproval.getFinanceManagerRemark());
                vehicleApproval.setIsapprovalFinanceManager(_vehicleApproval.getIsapprovalFinanceManager());
                return vehicleApprovalDao.executeUpdate(vehicleApproval);
            }
            //         else if(state==4)//???-??
            //         {
            //            vehicleApproval.setBranchManagerApprovalDate(new java.util.Date());
            //            vehicleApproval.setState(5);
            //            vehicleApproval.setBranchManagerName(uName);
            //            vehicleApproval.setBranchManagerRemark(_vehicleApproval.getBranchManagerRemark());
            //            vehicleApproval.setIsapprovalBranchManager(_vehicleApproval.getIsapprovalBranchManager());
            //            return vehicleApprovalDao.executeUpdate(vehicleApproval);      
            //         }
            else if (state == 1)//???-??
            {
                String carframeNum = contract.getCarframeNum();
                if (StringUtils.isEmpty(carframeNum)) {
                    request.setAttribute("msgStr",
                            "?--?");
                    return false;
                }

                Vehicle ve = ObjectAccess.getObject(Vehicle.class, carframeNum);
                if (ve != null && ve.getLicensePurseNum().matches("^\\w{6}")) {
                    Vehicle ov = ObjectAccess.getObject(Vehicle.class, ve.getLicensePurseNum());
                    if (ov != null && ov.getState() < 2) {
                        request.setAttribute("msgStr", "???");
                        return false;
                    }
                }

                List<Insurance> ilist = ObjectAccess.query(Insurance.class,
                        " carframeNum='" + contract.getCarframeNum() + "'");
                boolean hasJQX = false, hasSX = false;
                for (Insurance insurance : ilist) {
                    if (StringUtils.contains(insurance.getInsuranceClass(), "")) {
                        if (new Date().before(insurance.getEndDate())) {
                            hasJQX = true;
                        }
                    } else if (StringUtils.contains(insurance.getInsuranceClass(), "?")) {
                        if (new Date().before(insurance.getEndDate())) {
                            hasSX = true;
                        }
                    }
                }

                if (!hasJQX) {
                    request.setAttribute("msgStr", "");
                    return false;
                }

                if (!hasSX) {
                    request.setAttribute("msgStr", "");
                    return false;
                }

                vehicleApproval.setAssurerApprovalDate(new Date());
                vehicleApproval.setState(2);
                vehicleApproval.setAssurerName(uName);
                vehicleApproval.setAssurerRemark(_vehicleApproval.getAssurerRemark());
                vehicleApproval.setIsapprovalAssurer(_vehicleApproval.getIsapprovalAssurer());

                vehicleApproval.setDamageInsurance(_vehicleApproval.getDamageInsurance());
                vehicleApproval.setOnetimeAfterpay(_vehicleApproval.getOnetimeAfterpay());
                vehicleApproval.setPayBeginDate(_vehicleApproval.getPayBeginDate());
                vehicleApproval.setPayEndDate(_vehicleApproval.getPayEndDate());

                return vehicleApprovalDao.executeUpdate(vehicleApproval);
            } else if (state == 2)//???-??
            {
                //            String carframeNum = contract.getCarframeNum();
                //            if(StringUtils.isEmpty(carframeNum)){
                //               request.setAttribute("msgStr", "?");
                //               return false;
                //            }
                //            
                //            List<Insurance> ilist = ObjectAccess.query(Insurance.class, " carframeNum='"+contract.getCarframeNum()+"'");
                //            boolean hasJQX=false,hasSX = false;
                //            for(Insurance insurance:ilist){
                //               if(StringUtils.equalsIgnoreCase(insurance.getInsuranceClass(),"")){
                //                  if(new Date().before(insurance.getEndDate())){
                //                     hasJQX = true;
                //                  }
                //               }else if (StringUtils.equalsIgnoreCase(insurance.getInsuranceClass(), "??")) {
                //                  if(new Date().before(insurance.getEndDate())){
                //                     hasSX = true;
                //                  }
                //               }
                //            }
                //            
                //            if(!hasJQX){
                //               request.setAttribute("msgStr", "");
                //               return false;
                //            }
                //            
                //            if(!hasSX){
                //               request.setAttribute("msgStr", "");
                //               return false;
                //            }
                //            
                //            if(vehicleApproval.getOperateCardDate()==null){
                //               request.setAttribute("msgStr", "???");
                //               return false;
                //            }

                vehicleApproval.setManagerApprovalDate(new Date());
                vehicleApproval.setState(3);
                vehicleApproval.setManagerName(uName);
                vehicleApproval.setManagerRemark(_vehicleApproval.getManagerRemark());
                vehicleApproval.setIsapprovalManager(_vehicleApproval.getIsapprovalManager());
                return vehicleApprovalDao.executeUpdate(vehicleApproval);
            } else if (state == 5)//??
            {
                String carframeNum = contract.getCarframeNum();
                if (StringUtils.isEmpty(carframeNum)) {
                    request.setAttribute("msgStr", "?");
                    return false;
                }

                List<Insurance> ilist = ObjectAccess.query(Insurance.class,
                        " carframeNum='" + contract.getCarframeNum() + "'");
                boolean hasJQX = false, hasSX = false;
                for (Insurance insurance : ilist) {
                    if (StringUtils.contains(insurance.getInsuranceClass(), "")) {
                        if (new Date().before(insurance.getEndDate())) {
                            hasJQX = true;
                        }
                    } else if (StringUtils.contains(insurance.getInsuranceClass(), "?")) {
                        if (new Date().before(insurance.getEndDate())) {
                            hasSX = true;
                        }
                    }
                }

                if (!hasJQX) {
                    request.setAttribute("msgStr", "");
                    return false;
                }

                if (!hasSX) {
                    request.setAttribute("msgStr", "");
                    return false;
                }

                //            if(vehicleApproval.getOperateCardDate()==null){
                //               request.setAttribute("msgStr", "???");
                //               return false;
                //            }

                vehicleApproval.setApprovalDirectorDate(new Date());
                vehicleApproval.setState(6);
                vehicleApproval.setDirectorName(uName);
                vehicleApproval.setDirectorRemark(_vehicleApproval.getDirectorRemark());
                vehicleApproval.setIsapprovalDirector(_vehicleApproval.getIsapprovalDirector());
                vehicleApproval.setDiscountDays(_vehicleApproval.getDiscountDays());

                contract.setDiscountDays(vehicleApproval.getDiscountDays());
                ObjectAccess.saveOrUpdate(contract);

                //            contractDao.changeState(_vehicleApproval.getContractId(),3);

                return vehicleApprovalDao.executeUpdate(vehicleApproval);
            } else
                return false;
        } else if (vehicleApproval.getCheckType() == 1) {

            if (state == 1) {
                //contract.setAbandonedTime(contract.getAbandonedTime() == null?new Date():contract.getAbandonedTime());
                ObjectAccess.saveOrUpdate(contract);
                vehicleApproval.setAssurerRemark(_vehicleApproval.getAssurerRemark());
                vehicleApproval.setAssurerName(uName);
                vehicleApproval.setAssurerApprovalDate(new Date());
                vehicleApproval.setIsapprovalAssurer(_vehicleApproval.getIsapprovalAssurer());
                vehicleApproval.setState(3);//
            } else if (state == 2) {
                vehicleApproval.setCashierRemark(_vehicleApproval.getCashierRemark());
                vehicleApproval.setCashierName(uName);
                vehicleApproval.setCashierApprovalDate(new Date());
                vehicleApproval.setState(3);
            } else if (state == 3) {
                String carframeNum = contract.getCarframeNum();

                long count = ObjectAccess.execute(String
                        .format("select count(*) from Accident where carId='%s' and status!=3 ", carframeNum));
                if (count > 0) {
                    request.setAttribute("msgStr", "?");
                    return false;
                }

                vehicleApproval.setManagerRemark(_vehicleApproval.getManagerRemark());
                vehicleApproval.setManagerName(uName);
                vehicleApproval.setManagerApprovalDate(new Date());
                vehicleApproval.setState(4);
                vehicleApproval.setIsapprovalManager(_vehicleApproval.getIsapprovalManager());
            } else if (state == 4) {
                vehicleApproval.setOfficeRemark(_vehicleApproval.getOfficeRemark());
                vehicleApproval.setOfficeName(uName);
                vehicleApproval.setApprovalOfficeDate(new Date());
                vehicleApproval.setState(5);
                vehicleApproval.setIsapprovalOffice(_vehicleApproval.getIsapprovalOffice());
            } else if (state == 5) {
                vehicleApproval.setFinanceRemark(_vehicleApproval.getFinanceRemark());
                vehicleApproval.setFinanceName(uName);
                vehicleApproval.setApprovalFinanceDate(new Date());
                vehicleApproval.setState(6);
                vehicleApproval.setIsapprovalFinance(_vehicleApproval.getIsapprovalFinance());
            } else if (state == 6) {
                String rentStr = (String) request.getParameter("contract.rent");
                if (StringUtils.isNotBlank(rentStr)) {
                    double rent = NumberUtils.toDouble(rentStr);
                    contract.setRent(rent);
                    ObjectAccess.saveOrUpdate(contract);
                }

                vehicleApproval.setFinanceManagerRemark(_vehicleApproval.getFinanceManagerRemark());
                vehicleApproval.setFinanceManagerName(uName);
                vehicleApproval.setFinanceManagerApprovalDate(new Date());
                vehicleApproval.setState(7);
                vehicleApproval.setIsapprovalFinanceManager(_vehicleApproval.getIsapprovalFinanceManager());
            } else if (state == 7) {
                vehicleApproval.setDirectorRemark(_vehicleApproval.getDirectorRemark());
                vehicleApproval.setDirectorName(uName);
                vehicleApproval.setApprovalDirectorDate(new Date());
                vehicleApproval.setState(8);
                vehicleApproval.setIsapprovalDirector(_vehicleApproval.getIsapprovalDirector());

                Vehicle vehicle = ObjectAccess.getObject(Vehicle.class, contract.getCarframeNum());

                String strT = (String) request.getParameter("contract.abandonedChargeTime");
                if (StringUtils.isNotBlank(strT)) {
                    SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
                    try {
                        Date abandonedChargeTime = df.parse(strT);
                        contract.setAbandonedChargeTime(abandonedChargeTime);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }

                Session session = HibernateSessionFactory.getSession();
                Transaction tx = null;

                try {
                    tx = session.beginTransaction();

                    vehicle.setDriverId(null);

                    if (vehicleApproval.getHandleMatter()) {
                        vehicle.setState(3);
                    } else {
                        vehicle.setState(2);
                        if (contract.getAbandonedTime() == null) {
                            contract.setAbandonedTime(contract.getContractEndDate());
                        }

                        //???? -305-25
                        Calendar sp = Calendar.getInstance();
                        sp.setTime(contract.getAbandonedTime());

                        Calendar endTime = Calendar.getInstance();
                        endTime.setTime(contract.getContractEndDate());

                        sp.add(Calendar.MONTH, 1);

                        int amount = 0;

                        while (sp.before(endTime)) {
                            sp.add(Calendar.MONTH, 1);
                            if (sp.after(endTime)) {
                                if (endTime.get(Calendar.DATE) >= 20) {
                                    amount += 306 + 25;
                                }
                                break;
                            } else {
                                amount += 306 + 25;
                            }
                        }

                        sp.setTime(contract.getAbandonedTime());
                        if (sp.get(Calendar.DATE) > 26) {
                            sp.add(Calendar.MONTH, 1);
                        }
                        sp.set(Calendar.DATE, 1);

                        ChargePlan tplan = new ChargePlan();
                        tplan.setContractId(contract.getId());
                        tplan.setFee(BigDecimal.valueOf(amount));
                        tplan.setFeeType("plan_sub_contract");
                        tplan.setInTime(new Date());
                        tplan.setComment("???306,25");
                        tplan.setTime(sp.getTime());
                        tplan.setIsClear(false);
                        session.save(tplan);

                        chargeDao.planTransfer(contract.getId(), DateUtil.getNextMonth(contract.getAbandonedTime()),
                                contract.getId(), contract.getAbandonedTime());
                        List<String> dl = Arrays.<String>asList(vehicle.getFirstDriver(), vehicle.getSecondDriver(),
                                vehicle.getThirdDriver(), vehicle.getTempDriver());

                        for (int i = 0; i < dl.size(); i++) {
                            String idNum = dl.get(i);
                            if (!StringUtils.isEmpty(idNum)) {
                                Driver d = (Driver) session.get(Driver.class, idNum);
                                if (d != null && d.getDriverClass() != null) {
                                    if (d.getDriverClass().equals("")) {
                                        vehicle.setFirstDriver(null);
                                    } else if (d.getDriverClass().equals("")) {
                                        vehicle.setSecondDriver(null);
                                    } else if (d.getDriverClass().equals("")) {
                                        vehicle.setThirdDriver(null);
                                    } else if (d.getDriverClass().equals("")) {
                                        vehicle.setTempDriver(null);
                                    }

                                    Driverincar record = new Driverincar(d.getCarframeNum(), d.getIdNum(), "",
                                            new Date());
                                    record.setFinished(true);
                                    session.saveOrUpdate(record);
                                    //driverService.addDriverInCarRecord(record);

                                    d.setIsInCar(false);

                                    d.setRestTime(null);
                                    d.setCarframeNum(null);
                                    d.setDriverClass(null);
                                    d.setBusinessApplyTime(null);
                                    d.setBusinessApplyRegistrant(null);
                                    d.setBusinessApplyRegistTime(null);

                                    d.setBusinessReciveTime(null);
                                    d.setBusinessReciveRegistrant(null);
                                    d.setBusinessReciveRegistTime(null);

                                    d.setBusinessApplyCancelTime(null);
                                    d.setBusinessApplyCancelRegistrant(null);
                                    d.setBusinessApplyCancelRegistTime(null);

                                    d.setDept(null);
                                    d.setStatus(4);

                                    session.saveOrUpdate(d);
                                }
                            }
                        }

                        //               contract.setAbandonedFinalTime(new Date());
                    }

                    //            switch(contract.getAbandonReason()){
                    //            case "??":
                    //               vehicle.setState(2);
                    //               break;
                    //            case "":
                    //            case "":
                    //               vehicle.setState(3);
                    //               break;
                    //            }

                    contract.setState((short) 1);

                    session.saveOrUpdate(vehicle);
                    session.saveOrUpdate(contract);

                    session.saveOrUpdate(vehicleApproval);

                    //contractDao.changeState(_vehicleApproval.getContractId(),1);
                    Message msg = new Message();
                    User u = (User) session.get(User.class, uName);
                    msg.setFromUser(uName);
                    msg.setTime(new Date());

                    msg.setCarframeNum(contract.getCarframeNum());
                    msg.setIdNum(contract.getIdNum());
                    msg.setType("?");

                    Driver d = (Driver) session.get(Driver.class, contract.getIdNum());

                    msg.setMsg(String.format("%tF %s?\n"
                            + "%s(%s)  %s(%s) ??",
                            msg.getTime(), u.getUname(), contract.getCarNum(), contract.getCarframeNum(),
                            d.getName(), contract.getIdNum()));

                    session.save(msg);

                    Query q_u = session.createQuery(
                            "from RelationUr where " + "rid in (select rid from Role where rname = :rname )"
                                    + "and uid in (select uid from User where position like :position)");

                    q_u.setString("rname", "??");
                    q_u.setString("position", "%" + contract.getBranchFirm().trim().charAt(0) + "%");
                    List<RelationUr> users = q_u.list();
                    for (RelationUr relationUr : users) {
                        MessageToUser mu = new MessageToUser();
                        mu.setUid(relationUr.getUid());
                        mu.setMid(msg.getId());
                        mu.setAlreadyRead(false);
                        session.saveOrUpdate(mu);
                    }

                    tx.commit();
                    return true;
                } catch (HibernateException ex) {
                    ex.printStackTrace();
                    if (tx != null)
                        tx.rollback();

                } finally {
                    session.flush();
                    HibernateSessionFactory.closeSession();
                }
                return false;
            }

            return vehicleApprovalDao.executeUpdate(vehicleApproval);
        } else {
            return false;
        }
    }

    @Autowired
    private ChargeDao chargeDao;
    @Autowired
    private DriverService driverService;

    public void setDriverService(DriverService driverService) {
        this.driverService = driverService;
    }

    public VehicleApproval queryVehicleApprovalById(Integer approvalId) {
        return vehicleApprovalDao.queryVehicleApprovalById(approvalId);
    }

    public VehicleApproval queryVehicleApprovalByContractId(Integer contractId) {
        return vehicleApprovalDao.queryVehicleApprovalByContractId(contractId);
    }

    public void setContractDao(ContractDao contractDao) {
        this.contractDao = contractDao;
    }

    public void setVehicleApprovalDao(VehicleApprovalDao vehicleApprovalDao) {
        this.vehicleApprovalDao = vehicleApprovalDao;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, List<ToDo>> waitToDo(Role role) {
        Map<String, List<ToDo>> map = new TreeMap<String, List<ToDo>>();
        List<ToDo> toDolist = new ArrayList<ToDo>();
        List<VehicleApproval> approvalList;

        switch (role.getRname()) {
        case "??":
            break;
        case "????":
            approvalList = vehicleApprovalDao.queryVehicleApprovalByState((short) 0, 2);
            toDolist.addAll(
                    (List<ToDo>) CollectionUtils.collect(approvalList, new VehicleApprovalWaitDealTransformer()));
            break;
        case "??":
            approvalList = vehicleApprovalDao.queryVehicleApprovalByState((short) 0, 5);
            toDolist.addAll(
                    (List<ToDo>) CollectionUtils.collect(approvalList, new VehicleApprovalWaitDealTransformer()));
            break;
        case "?":
            approvalList = vehicleApprovalDao.queryVehicleApprovalByState((short) 0, 1);
            toDolist.addAll(
                    (List<ToDo>) CollectionUtils.collect(approvalList, new VehicleApprovalWaitDealTransformer()));
            break;
        case "":

            break;
        case "":
            approvalList = vehicleApprovalDao.queryVehicleApprovalByState((short) 0, 3);
            toDolist.addAll(
                    (List<ToDo>) CollectionUtils.collect(approvalList, new VehicleApprovalWaitDealTransformer()));
            break;
        case "??":
            approvalList = vehicleApprovalDao.queryVehicleApprovalByState((short) 0, 4);
            toDolist.addAll(
                    (List<ToDo>) CollectionUtils.collect(approvalList, new VehicleApprovalWaitDealTransformer()));
            break;
        case "":
            approvalList = vehicleApprovalDao.queryVehicleApprovalByState((short) 0, 6);
            toDolist.addAll(
                    (List<ToDo>) CollectionUtils.collect(approvalList, new VehicleApprovalWaitDealTransformer()));
            break;
        case "?":

            break;
        default:
            break;
        }
        map.put("", toDolist);

        toDolist = new ArrayList<ToDo>();
        switch (role.getRname()) {
        case "??":
            break;
        case "????":
            approvalList = vehicleApprovalDao.queryVehicleApprovalByState((short) 1, 3);
            toDolist.addAll((List<ToDo>) CollectionUtils.collect(approvalList,
                    new VehicleApprovalWaitDealTransformer("")));
            break;
        case "??":
            approvalList = vehicleApprovalDao.queryVehicleApprovalByState((short) 1, 7);
            toDolist.addAll((List<ToDo>) CollectionUtils.collect(approvalList,
                    new VehicleApprovalWaitDealTransformer("")));
            break;
        case "?":
            approvalList = vehicleApprovalDao.queryVehicleApprovalByState((short) 1, 1);
            toDolist.addAll((List<ToDo>) CollectionUtils.collect(approvalList,
                    new VehicleApprovalWaitDealTransformer("")));
            break;
        case "":
            //approvalList = vehicleApprovalDao.queryVehicleApprovalByState((short) 1,2);
            //toDolist.addAll((List<ToDo>) CollectionUtils.collect(approvalList, new VehicleApprovalWaitDealTransformer("")));
            break;
        case "":
            approvalList = vehicleApprovalDao.queryVehicleApprovalByState((short) 1, 5);
            toDolist.addAll((List<ToDo>) CollectionUtils.collect(approvalList,
                    new VehicleApprovalWaitDealTransformer("")));
            break;
        case "??":
            approvalList = vehicleApprovalDao.queryVehicleApprovalByState((short) 1, 6);
            toDolist.addAll((List<ToDo>) CollectionUtils.collect(approvalList,
                    new VehicleApprovalWaitDealTransformer("")));
            break;
        case "":
            approvalList = vehicleApprovalDao.queryVehicleApprovalByState((short) 1, 4);
            toDolist.addAll((List<ToDo>) CollectionUtils.collect(approvalList,
                    new VehicleApprovalWaitDealTransformer("")));
            break;
        case "?":
            break;
        default:
            break;
        }
        map.put("", toDolist);

        return map;
    }

    private static class VehicleApprovalWaitDealTransformer implements Transformer {
        private String base;

        public VehicleApprovalWaitDealTransformer() {
            this("");
        }

        public VehicleApprovalWaitDealTransformer(String base) {
            this.base = base;
        }

        @Override
        public Object transform(Object arg0) {
            VehicleApproval comp = (VehicleApproval) arg0;
            String msg = base + geneMsg(comp);

            return new ToDo(msg, "",
                    "/DZOMS/vehicle/vehicleApprovalPreUpdate.action?vehicleApproval.id=" + comp.getId());
        }

        protected String geneMsg(VehicleApproval comp) {
            String msg = "";
            try {
                int cid = comp.getContractId();
                Contract c = (Contract) ObjectAccess.getObject("com.dz.module.contract.Contract", cid);
                List<String> sl = new ArrayList<String>();

                if (StringUtils.isNotEmpty(c.getCarframeNum()))
                    sl.add("?:" + c.getCarframeNum());
                if (StringUtils.isNotEmpty(c.getCarNum()))
                    sl.add("?:" + c.getCarNum());
                if (StringUtils.isNotEmpty(c.getIdNum())) {
                    Driver d = (Driver) ObjectAccess.getObject("com.dz.module.driver.Driver", c.getIdNum());
                    sl.add(":" + d.getName());
                }

                msg = sl.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return msg;
        }
    }
}