2020-12-20

#快递E栈后台快递管理程序编写思路
##根据数据库创建快递类(Express)

package com.hjxs.bean;

import java.sql.Timestamp;
import java.util.Objects;

public class Express {
    private int id;
    private String number;
    private String userName;
    private String userPhone;
    private String company;
    private String code;
    private Timestamp inTime;
    private Timestamp outTime;
    private int status;
    private String sysPhone;

    public Express() {
    }

    public Express(String number, String userName, String userPhone, String company, String sysPhone) {
        this.number = number;
        this.userName = userName;
        this.userPhone = userPhone;
        this.company = company;
        this.sysPhone = sysPhone;
    }

    public Express(String number, String userName, String userPhone, String company, String sysPhone, String code) {
        this.number = number;
        this.userName = userName;
        this.userPhone = userPhone;
        this.company = company;
        this.sysPhone = sysPhone;
        this.code = code;
    }

    public Express(int id, String number, String userName, String userPhone, String company, String code, Timestamp inTime, Timestamp outTime, int status, String sysPhone) {
        this.id = id;
        this.number = number;
        this.userName = userName;
        this.userPhone = userPhone;
        this.company = company;
        this.code = code;
        this.inTime = inTime;
        this.outTime = outTime;
        this.status = status;
        this.sysPhone = sysPhone;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPhone() {
        return userPhone;
    }

    public void setUserPhone(String userPhone) {
        this.userPhone = userPhone;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Timestamp getInTime() {
        return inTime;
    }

    public void setInTime(Timestamp inTime) {
        this.inTime = inTime;
    }

    public Timestamp getOutTime() {
        return outTime;
    }

    public void setOutTime(Timestamp outTime) {
        this.outTime = outTime;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getSysPhone() {
        return sysPhone;
    }

    public void setSysPhone(String sysPhone) {
        this.sysPhone = sysPhone;
    }

    @Override
    public String toString() {
        return "Express{" +
                "id=" + id +
                ", number='" + number + '\'' +
                ", userName='" + userName + '\'' +
                ", userPhone='" + userPhone + '\'' +
                ", company='" + company + '\'' +
                ", code='" + code + '\'' +
                ", inTime=" + inTime +
                ", outTime=" + outTime +
                ", status=" + status +
                ", sysPhone='" + sysPhone + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Express express = (Express) o;
        return id == express.id &&
                status == express.status &&
                Objects.equals(number, express.number) &&
                Objects.equals(userName, express.userName) &&
                Objects.equals(userPhone, express.userPhone) &&
                Objects.equals(company, express.company) &&
                Objects.equals(code, express.code) &&
                Objects.equals(inTime, express.inTime) &&
                Objects.equals(outTime, express.outTime) &&
                Objects.equals(sysPhone, express.sysPhone);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, number, userName, userPhone, company, code, inTime, outTime, status, sysPhone);
    }
}

##创建实现快递增删改查方法的接口

package com.hjxs.dao;

import com.hjxs.bean.Express;
import com.hjxs.exception.DuplicationCodeException;

import java.util.List;
import java.util.Map;

public interface BaseExpressDao {
    /**
     * 用于查询数据库中的全部快递(总数+新增),代取件快递(总数+新增)
     * @return [{size:总数,day:新增},{size:总数,day:新增}]
     */
    List<Map<String,Integer>> console();

    /**
     * 用于查询所有快递
     * @param limit 是否分页的标记,true表示分页,false表示查询所有快递
     * @param offset SQL语句的起始索引
     * @param pageNumber 一页查询的数量
     * @return
     */
    List<Express> findAll(boolean limit,int offset,int pageNumber);

    /**
     * 根据快递单号查询快递信息
     * @param number 单号
     * @return 查询的快递信息,单号不存在时返回null
     */
    Express findByNumber(String number);

    /**
     * 根据快递取件码查询快递信息
     * @param code 取件码
     * @return 查询的快递信息,单号不存在时返回null
     */
    Express findByCode(String code);

    /**
     * 根据快递手机号查询快递信息
     * @param userPhone 手机号码
     * @return 查询的快递信息,单号不存在时返回null
     */
    List<Express> findByUserPhoneAndStatus(String userPhone,int status);

    List<Express> findByUserPhone(String userPhone);
    /**
     * 根据快递录入人手机号查询快递信息
     * @param sysPhone 录入人手机号码
     * @return 查询的快递信息,单号不存在时返回null
     */
    List<Express> findBySysPhone(String sysPhone);

    /**
     * 快递录入
     * @param e 录入的快递对象
     * @return 录入结果
     */
    boolean insert(Express e) throws DuplicationCodeException;

    /**
     * 快递修改
     * @param id 要修改的快递ID
     * @param newExpress 新的快递对象(number,company,username,userPhone)
     * @return 修改结果
     */
    boolean update(int id,Express newExpress);

    /**
     * 更改快递的状态为1,表示取件完成
     * @param code 要修改的快递取件码
     * @return 修改的结果
     */
    boolean updateStatus(String code);

    /**
     * 根据ID,删除单个快递信息
     * @param id 要删除的快递ID
     * @return 删除结果
     */
    boolean delete(int id);
}

##编写具体实现这些方法的类(ExpressDaoMysql)

package com.hjxs.dao.imp;

import com.hjxs.bean.Express;
import com.hjxs.dao.BaseExpressDao;
import com.hjxs.exception.DuplicationCodeException;
import com.hjxs.util.DruidUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExpressDaoMysql implements BaseExpressDao {

    private  static final String SQL_CONSOLE = "SELECT COUNT(ID) data1_size,COUNT(TO_DAYS(INTIME)=TO_DAYS(NOW()) OR NULL) data1_day,COUNT(STATUS=0 OR NULL) data2_size,COUNT(TO_DAYS(INTIME)=TO_DAYS(NOW()) AND STATUS=0 OR NULL) data2_day FROM EXPRESS";
    //用于查询数据库中的所有信息
    private  static final String SQL_FIND_ALL = "SELECT * FROM EXPRESS";
    //用于分页查询数据库中的所有信息
    private  static final String SQL_FIND_LIMIT = "SELECT * FROM EXPRESS LIMIT ?,?";
    //通过取件码查询快递信息
    private  static final String SQL_FIND_BY_CODE = "SELECT * FROM EXPRESS WHERE CODE=?";
    //通过快递单号查询快递信息
    private  static final String SQL_FIND_BY_NUMBER = "SELECT * FROM EXPRESS WHERE NUMBER=?";
    //通过收件人手机号查询快递信息
    private  static final String SQL_FIND_BY_SYSPHONE = "SELECT * FROM EXPRESS WHERE SYSPHONE=?";
    //通过录入人手机号查询快递信息
    private  static final String SQL_FIND_BY_USERPHONE = "SELECT * FROM EXPRESS WHERE USERPHONE =?";
    //快递录入
    private  static final String SQL_INSERT = "INSERT INTO EXPRESS(NUMBER,USERNAME,USERPHONE,COMPANY,CODE,INTIME,STATUS,SYSPHONE) VALUES(?,?,?,?,?,NOW(),0,?)";
    //快递修改
    private  static final String SQL_UPDATE = "UPDATE EXPRESS SET NUMBER=?,USERNAME=?,COMPANY=?,STATUS=? WHERE ID=?";
    //取件状态码改变
    private  static final String SQL_UPDATE_STATUS = "UPDATE EXPRESS SET STATUS=1,OUTTIME=NOW(),CODE=NULL WHERE CODE=?";
    //快递的删除
    private  static final String SQL_DELETE = "DELETE FROM EXPRESS WHERE ID=?";
    private  static final String SQL_FIND_BY_USERPHONE_AND_STATUS = "SELECT * FROM EXPRESS WHERE USERPHONE =? AND STATUS=?";





    /**
     * 用于查询数据库中的全部快递(总数+新增),代取件快递(总数+新增)
     *
     * @return [{size:总数,day:新增},{size:总数,day:新增}]
     */
    @Override
    public List<Map<String, Integer>> console() {
        //1.获取数据库连接
        ArrayList<Map<String,Integer>> data = new ArrayList<>();
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        //2.预编译SQL语句
        try {
            state = conn.prepareStatement(SQL_CONSOLE);
            result = state.executeQuery();
            //3.填充参数(可选)
            //4.执行SQL语句
            //5.获取执行结果
            if(result.next()){
                int data1_size = result.getInt("data1_size");
                int data1_day = result.getInt("data1_day");
                int data2_size = result.getInt("data2_size");
                int data2_day = result.getInt("data2_day");
                Map data1 = new HashMap();
                data1.put("data1_size",data1_size);
                data1.put("data1_day",data1_day);
                Map data2 = new HashMap();
                data2.put("data2_size",data2_size);
                data2.put("data2_day",data2_day);
                data.add(data1);
                data.add(data2);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //6.资源释放
            DruidUtil.close(conn,state,result);
        }
        return data;
    }

    /**
     * 用于查询所有快递
     *
     * @param limit      是否分页的标记,true表示分页,false表示查询所有快递
     * @param offset     SQL语句的起始索引
     * @param pageNumber 一页查询的数量
     * @return
     */
    @Override
    public List<Express> findAll(boolean limit, int offset, int pageNumber) {
        ArrayList<Express>  data = new ArrayList<>();
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        //2.预编译SQL语句
        try {
            if(limit) {
                state = conn.prepareStatement(SQL_FIND_LIMIT);
                state.setInt(1,offset);
                state.setInt(2,pageNumber);
            }else {
                state = conn.prepareStatement(SQL_FIND_ALL);
            }
            result = state.executeQuery();
            while(result.next()){
                int id = result.getInt("id");
                String number = result.getString("number");
                String userName = result.getString("userName");
                String userPhone = result.getString("userPhone");
                String company = result.getString("company");
                String code = result.getString("code");
                Timestamp inTime = result.getTimestamp("inTime");
                Timestamp outTime = result.getTimestamp("outTime");
                int status = result.getInt("status");
                String sysPhone = result.getString("sysPhone");
                Express e = new Express(id,number,userName,userPhone,company,code,inTime,outTime,status,sysPhone);
                data.add(e);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(conn,state,result);
        }
        return data;
    }

    /**
     * 根据快递单号查询快递信息
     *
     * @param number 单号
     * @return 查询的快递信息,单号不存在时返回null
     */
    @Override
    public Express findByNumber(String number) {
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        //2.预编译SQL语句
        try {
            state = conn.prepareStatement(SQL_FIND_BY_NUMBER);
            state.setString(1,number);
            result = state.executeQuery();
            if(result.next()){
                int id = result.getInt("id");
                String userName = result.getString("userName");
                String userPhone = result.getString("userPhone");
                String company = result.getString("company");
                String code = result.getString("code");
                Timestamp inTime = result.getTimestamp("inTime");
                Timestamp outTime = result.getTimestamp("outTime");
                int status = result.getInt("status");
                String sysPhone = result.getString("sysPhone");
                Express e = new Express(id,number,userName,userPhone,company,code,inTime,outTime,status,sysPhone);
                return  e;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(conn,state,result);
        }
        return null;
    }

    /**
     * 根据快递取件码查询快递信息
     *
     * @param code 取件码
     * @return 查询的快递信息,单号不存在时返回null
     */
    @Override
    public Express findByCode(String code) {
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        //2.预编译SQL语句
        try {
            state = conn.prepareStatement(SQL_FIND_BY_CODE);
            state.setString(1,code);
            result = state.executeQuery();
            if(result.next()){
                int id = result.getInt("id");
                String number = result.getString("number");
                String userName = result.getString("userName");
                String userPhone = result.getString("userPhone");
                String company = result.getString("company");
                Timestamp inTime = result.getTimestamp("inTime");
                Timestamp outTime = result.getTimestamp("outTime");
                int status = result.getInt("status");
                String sysPhone = result.getString("sysPhone");
                Express e = new Express(id,number,userName,userPhone,company,code,inTime,outTime,status,sysPhone);
                return  e;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(conn,state,result);
        }
        return null;
    }

    /**
     * 根据快递手机号查询快递信息
     *
     * @param userPhone 手机号码
     * @param status
     * @return 查询的快递信息,单号不存在时返回null
     */
    @Override
    public List<Express> findByUserPhoneAndStatus(String userPhone, int status) {
        ArrayList<Express> data = new ArrayList<>();
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        //2.预编译SQL语句
        try {
            state = conn.prepareStatement(SQL_FIND_BY_USERPHONE_AND_STATUS);
            state.setString(1,userPhone);
            state.setInt(2,status);
            result = state.executeQuery();
            while(result.next()){
                int id = result.getInt("id");
                String number = result.getString("number");
                String userName = result.getString("userName");
                String company = result.getString("company");
                String code = result.getString("code");
                Timestamp inTime = result.getTimestamp("inTime");
                Timestamp outTime = result.getTimestamp("outTime");
                String sysPhone = result.getString("sysPhone");
                Express e = new Express(id,number,userName,userPhone,company,code,inTime,outTime,status,sysPhone);
                data.add(e);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(conn,state,result);
        }
        return data;
    }

    /**
     * 根据快递手机号查询快递信息
     *
     * @param userPhone 手机号码
     * @return 查询的快递信息,单号不存在时返回null
     */
    @Override
    public List<Express> findByUserPhone(String userPhone) {
        ArrayList<Express> data = new ArrayList<>();
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        //2.预编译SQL语句
        try {
            state = conn.prepareStatement(SQL_FIND_BY_USERPHONE);
            state.setString(1,userPhone);
            result = state.executeQuery();
            while(result.next()){
                int id = result.getInt("id");
                String number = result.getString("number");
                String userName = result.getString("userName");
                String company = result.getString("company");
                String code = result.getString("code");
                Timestamp inTime = result.getTimestamp("inTime");
                Timestamp outTime = result.getTimestamp("outTime");
                int status = result.getInt("status");
                String sysPhone = result.getString("sysPhone");
                Express e = new Express(id,number,userName,userPhone,company,code,inTime,outTime,status,sysPhone);
                data.add(e);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(conn,state,result);
        }
        return data;
    }

    /**
     * 根据快递录入人手机号查询快递信息
     *
     * @param sysPhone 录入人手机号码
     * @return 查询的快递信息,单号不存在时返回null
     */
    @Override
    public List<Express> findBySysPhone(String sysPhone) {
        ArrayList<Express> data = new ArrayList<>();
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        //2.预编译SQL语句
        try {
            state = conn.prepareStatement(SQL_FIND_BY_SYSPHONE);
            state.setString(1,sysPhone);
            result = state.executeQuery();
            while(result.next()){
                int id = result.getInt("id");
                String number = result.getString("number");
                String userName = result.getString("userName");
                String userPhone = result.getString("userPhone");
                String company = result.getString("company");
                String code = result.getString("code");
                Timestamp inTime = result.getTimestamp("inTime");
                Timestamp outTime = result.getTimestamp("outTime");
                int status = result.getInt("status");
                Express e = new Express(id,number,userName,userPhone,company,code,inTime,outTime,status,sysPhone);
                data.add(e);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(conn,state,result);
        }
        return data;
    }

    /**
     * 快递录入
     *
     * @param e 录入的快递对象
     * @return 录入结果
     */
    @Override
    public boolean insert(Express e) throws DuplicationCodeException{
        //1.连接数据库
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        //2.预编译SQL语句
        try {
            state = conn.prepareStatement(SQL_INSERT);
            state.setString(1,e.getNumber());
            state.setString(2,e.getUserName());
            state.setString(3,e.getUserPhone());
            state.setString(4,e.getCompany());
            state.setString(5,e.getCode());
            state.setString(6,e.getSysPhone());
            return state.executeUpdate()>0?true:false;
        } catch (SQLException e1) {
            if(e1.getMessage().endsWith("for key 'code'")){
                DuplicationCodeException e2 = new DuplicationCodeException(e1.getMessage());
                throw e2;
            }else{
                e1.printStackTrace();
            }
        }finally {
            DruidUtil.close(conn,state, null);
        }
        //3.填充参数
        //4.执行语句,获取结果
        //5.释放资源
        return false;
    }

    /**
     * 快递修改
     *
     * @param id         要修改的快递ID
     * @param newExpress 新的快递对象(number,company,username,userPhone)
     * @return 修改结果
     */
    @Override
    public boolean update(int id, Express newExpress) {
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        try {
            state = conn.prepareStatement(SQL_UPDATE);
            state.setString(1,newExpress.getNumber());
            state.setString(2,newExpress.getUserName());
            state.setString(3,newExpress.getCompany());
            state.setInt(4,newExpress.getStatus());
            state.setInt(5,id);
            return state.executeUpdate()>0?true:false;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(conn,state,null);
        }
        return false;
    }

    /**
     * 更改快递的状态为1,表示取件完成
     *
     * @param code 要修改的快递单号
     * @return 修改的结果
     */
    @Override
    public boolean updateStatus(String code) {
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        try {
            state = conn.prepareStatement(SQL_UPDATE_STATUS);
            state.setString(1,code);
            return state.executeUpdate()>0?true:false;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(conn,state,null);
        }
        return false;
    }

    /**
     * 根据ID,删除单个快递信息
     *
     * @param id 要删除的快递ID
     * @return 删除结果
     */
    @Override
    public boolean delete(int id) {
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        try {
            state = conn.prepareStatement(SQL_DELETE);
            state.setInt(1,id);
            return state.executeUpdate()>0?true:false;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(conn,state,null);
        }
        return false;
    }
}

##编写service业务层(该层描述的是一种结构,调用dao,但不影响其它层代码)

package com.hjxs.service;

import com.hjxs.bean.Express;
import com.hjxs.dao.BaseCourierDao;
import com.hjxs.dao.BaseExpressDao;
import com.hjxs.dao.imp.CourierDaoMysql;
import com.hjxs.dao.imp.ExpressDaoMysql;
import com.hjxs.exception.DuplicationCodeException;
import com.hjxs.util.RandomUtil;
import com.hjxs.util.SMSUtil;

import java.util.List;
import java.util.Map;

public class ExpressService{
    private static BaseExpressDao dao = new ExpressDaoMysql();
    /**
     * 用于查询数据库中的全部快递(总数+新增),代取件快递(总数+新增)
     *
     * @return [{size:总数,day:新增},{size:总数,day:新增}]
     */

    public static List<Map<String, Integer>> console() {
        return dao.console();
    }

    /**
     * 用于查询所有快递
     *
     * @param limit      是否分页的标记,true表示分页,false表示查询所有快递
     * @param offset     SQL语句的起始索引
     * @param pageNumber 一页查询的数量
     * @return
     */

    public static List<Express> findAll(boolean limit, int offset, int pageNumber) {
        return dao.findAll(limit,offset,pageNumber);
    }

    /**
     * 根据快递单号查询快递信息
     *
     * @param number 单号
     * @return 查询的快递信息,单号不存在时返回null
     */
    public static Express findByNumber(String number) {
        return dao.findByNumber(number);
    }

    /**
     * 根据快递取件码查询快递信息
     *
     * @param code 取件码
     * @return 查询的快递信息,单号不存在时返回null
     */
    public static Express findByCode(String code) {
        return dao.findByCode(code);
    }

    /**
     * 根据快递手机号查询快递信息
     *
     * @param userPhone 手机号码
     * @return 查询的快递信息,单号不存在时返回null
     */
    public static List<Express> findByUserPhone(String userPhone) {
        return dao.findByUserPhone(userPhone);
    }

    public static List<Express> findByUserPhoneAndStatus(String userPhone,int status) {

        return dao.findByUserPhoneAndStatus(userPhone,status);
    }

    /**
     * 根据快递录入人手机号查询快递信息
     *
     * @param sysPhone 录入人手机号码
     * @return 查询的快递信息,单号不存在时返回null
     */
    public static List<Express> findBySysPhone(String sysPhone) {
        return dao.findBySysPhone(sysPhone);
    }



    /**
     * 快递录入
     *
     * @param e 录入的快递对象
     * @return 录入结果
     */
    public static boolean insert(Express e){
        //1.生成取件码
        e.setCode(RandomUtil.getCode()+"");
        BaseCourierDao dao2 = new CourierDaoMysql();
        String sysPhone = e.getSysPhone();
        try {
            boolean flag = dao.insert(e);
            boolean flag2 = dao2.updateCount(sysPhone);
            if(flag && flag2){
                //录入成功
                SMSUtil.sendSms(e.getUserPhone(),e.getCode());
            }
            return flag;
        } catch (DuplicationCodeException duplicationCodeException) {
            return insert(e);
        }
    }

    /**
     * 快递修改
     *
     * @param id         要修改的快递ID
     * @param newExpress 新的快递对象(number,company,username,userPhone)
     * @return 修改结果
     */
    public static boolean update(int id, Express newExpress) {
        if(newExpress.getUserPhone()!=null){
            dao.delete(id);
            return insert(newExpress);
        }else{
            boolean update = dao.update(id, newExpress);
            Express e = dao.findByNumber(newExpress.getNumber());
            if(newExpress.getStatus()==1){
                boolean b = updateStatus(e.getCode());
            }
            return update;
        }

    }

    /**
     * 更改快递的状态为1,表示取件完成
     *
     * @param code 要修改的快递取件码
     * @return 修改的结果
     */
    public static boolean updateStatus(String code) {
        return dao.updateStatus(code);
    }

    /**
     * 根据ID,删除单个快递信息
     *
     * @param id 要删除的快递ID
     * @return 删除结果
     */
    public static boolean delete(int id) {
        return dao.delete(id);
    }
}

##编写controller层获取数据,调用service层处理业务逻辑

package com.hjxs.controller;

import com.hjxs.bean.*;
import com.hjxs.mvc.ResponseBody;
import com.hjxs.service.CourierService;
import com.hjxs.service.ExpressService;
import com.hjxs.service.UserService;
import com.hjxs.util.DateFormatUtil;
import com.hjxs.util.JSONUtil;
import com.hjxs.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExpressController {
    @ResponseBody("/express/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        List<Map<String,Integer>> data = ExpressService.console();
        List<Map<String,Integer>> data2 = CourierService.couriers();
        List<Map<String,Integer>> data3 = UserService.users();
        Message msg = new Message();
        if(data.size()==0 && data2.size()==0 && data3.size()==0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(data);
        msg.setData2(data2);
        msg.setData3(data3);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/express/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        //获取查询数据起始偏移值
        int offset = Integer.parseInt(request.getParameter("offset"));
        //获取当前页要查询的数据量
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //进行分页查询
        List<Express> list = ExpressService.findAll(true, offset, pageNumber);
        List<BootStrapTableExpress> list2 = new ArrayList<>();
        for(Express e:list){
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus()==0?"待取件":"已取件";
            String code = e.getCode()==null?"已取件":e.getCode();
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUserName(),e.getUserPhone(),e.getCompany(),code,inTime,outTime,status,e.getSysPhone());
            list2.add(e2);
        }
        List<Map<String, Integer>> console = ExpressService.console();
        Integer total = console.get(0).get("data1_size");
        //将集合封装为bootstrap-table识别的格式
        ResultData<BootStrapTableExpress> data = new ResultData<>();
        data.setRows(list2);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/express/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        isProperty flag1 = new isProperty();
        Message msg = new Message();
        if(!flag1.isPhone(userPhone)){
            msg.setStatus(-1);
            msg.setResult("手机号输入错误");
        }else {
            Express e = new Express(number, username, userPhone, company, UserUtil.getUserPhone(request.getSession()));
            boolean flag = ExpressService.insert(e);
            if (flag) {
                //录入成功
                msg.setStatus(0);
                msg.setResult("快递录入成功");
            } else {
                //录入失败
                msg.setStatus(-1);
                msg.setResult("快递录入失败");
            }
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/express/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response){
        String number = request.getParameter("number");
        Express e = ExpressService.findByNumber(number);
        Message msg = new Message();
        if(e==null){
            msg.setStatus(-1);
            msg.setResult("单号不存在");
        }else{
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(e);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/express/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String userName = request.getParameter("userName");
        String userPhone = request.getParameter("userPhone");
        int status = Integer.parseInt(request.getParameter("status"));
        isProperty flag1 = new isProperty();
        Message msg = new Message();
        if(!flag1.isPhone(userPhone)){
            msg.setStatus(-1);
            msg.setResult("手机号输入错误");
        }else {
            Express newExpress = new Express();
            newExpress.setNumber(number);
            newExpress.setCompany(company);
            newExpress.setUserName(userName);
            newExpress.setUserPhone(userPhone);
            newExpress.setStatus(status);
            boolean flag = ExpressService.update(id, newExpress);
            if (flag) {
                msg.setStatus(0);
                msg.setResult("修改成功");
            } else {
                msg.setStatus(-1);
                msg.setResult("修改失败");
            }
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/express/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        boolean flag = ExpressService.delete(id);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
}


##在配置文件application.properties配置文件中加入controller类

express=com.hjxs.controller.ExpressController
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值