com.qkj.qkjmanage.action.OilManageAction.java Source code

Java tutorial

Introduction

Here is the source code for com.qkj.qkjmanage.action.OilManageAction.java

Source

package com.qkj.qkjmanage.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.iweb.sys.ActionAttr;
import org.iweb.sys.ContextHelper;

import org.iweb.sys.mail.SysMail;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.opensymphony.xwork2.ActionSupport;
import com.qkj.qkjmanage.dao.GetLicenseDAO;
import com.qkj.qkjmanage.dao.OilManageDAO;
import com.qkj.qkjmanage.dao.ProcessDAO;
import com.qkj.qkjmanage.domain.GetLicense;
import com.qkj.qkjmanage.domain.LastOilManage;
import com.qkj.qkjmanage.domain.OilManage;

public class OilManageAction extends ActionSupport implements ActionAttr {
    private static final long serialVersionUID = 1L;
    private static Log log = LogFactory.getLog(OilManageAction.class);
    private Map<String, Object> map = new HashMap<String, Object>();
    private OilManageDAO dao = new OilManageDAO();
    private GetLicenseDAO cardao = new GetLicenseDAO();
    private OilManage oilmanage;
    private List<OilManage> oilmanages;
    private LastOilManage lastoilmanage;
    private List<LastOilManage> lastoilmanages;
    private GetLicense licenseInfo;
    private List<GetLicense> licenseInfos;
    private String message;
    private String viewFlag;
    private int recCount;
    private int pageSize;
    private int currPage;
    private String path = "<a href='/manager/default'></a>&nbsp;&gt;&nbsp;?";

    public OilManage getOilmanage() {
        return oilmanage;
    }

    public void setOilmanage(OilManage oilmanage) {
        this.oilmanage = oilmanage;
    }

    public List<OilManage> getOilmanages() {
        return oilmanages;
    }

    public void setOilmanages(List<OilManage> oilmanages) {
        this.oilmanages = oilmanages;
    }

    public LastOilManage getLastoilmanage() {
        return lastoilmanage;
    }

    public void setLastoilmanage(LastOilManage lastoilmanage) {
        this.lastoilmanage = lastoilmanage;
    }

    public List<LastOilManage> getLastoilmanages() {
        return lastoilmanages;
    }

    public void setLastoilmanages(List<LastOilManage> lastoilmanages) {
        this.lastoilmanages = lastoilmanages;
    }

    public GetLicense getLicenseInfo() {
        return licenseInfo;
    }

    public void setLicenseInfo(GetLicense licenseInfo) {
        this.licenseInfo = licenseInfo;
    }

    public List<GetLicense> getLicenseInfos() {
        return licenseInfos;
    }

    public void setLicenseInfos(List<GetLicense> licenseInfos) {
        this.licenseInfos = licenseInfos;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getViewFlag() {
        return viewFlag;
    }

    public void setViewFlag(String viewFlag) {
        this.viewFlag = viewFlag;
    }

    public int getRecCount() {
        return recCount;
    }

    public void setRecCount(int recCount) {
        this.recCount = recCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getCurrPage() {
        return currPage;
    }

    public void setCurrPage(int currPage) {
        this.currPage = currPage;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String list() throws Exception {
        ContextHelper.isPermit("QKJ_QKJMANAGE_OILMANAGE_LIST");
        try {
            map.clear();
            if (oilmanage == null)
                oilmanage = new OilManage();
            ContextHelper.setSearchDeptPermit4Search("QKJ_QKJMANAGE_OILMANAGE_LIST", map, "apply_depts",
                    "apply_user");
            ContextHelper.SimpleSearchMap4Page("QKJ_QKJMANAGE_OILMANAGE_LIST", map, oilmanage, viewFlag);
            this.setPageSize(ContextHelper.getPageSize(map));
            this.setCurrPage(ContextHelper.getCurrPage(map));
            this.setOilmanages(dao.list(map));
            this.setRecCount(dao.getResultCount());
            path = "<a href='/manager/default'></a>&nbsp;&gt;&nbsp;";
        } catch (Exception e) {
            log.error(this.getClass().getName() + "!list ??:", e);
            throw new Exception(this.getClass().getName() + "!list ??:", e);
        }
        return SUCCESS;
    }

    public String load() throws Exception {
        try {
            if (null == viewFlag) {
                this.setOilmanage(null);
                setMessage("?!");
            } else if ("add".equals(viewFlag)) {
                this.setOilmanage(null);
                this.getCarInfo();
                path = "<a href='/manager/default'></a>&nbsp;&gt;&nbsp;<a href='/qkjmanage/_list?viewFlag=relist'>?</a>&nbsp;&gt;&nbsp;";
            } else if ("mdy".equals(viewFlag) || "view".equals(viewFlag)) {
                if (!(oilmanage == null || oilmanage.getUuid() == null)) {
                    this.setOilmanage((OilManage) dao.get(oilmanage.getUuid()));
                    if (oilmanage.getStatus() < 3) {
                        this.getCarInfo();
                    }
                } else {
                    this.setOilmanage(null);
                }
                path = "<a href='/manager/default'></a>&nbsp;&gt;&nbsp;<a href='/qkjmanage/_list?viewFlag=relist'>?</a>&nbsp;&gt;&nbsp;";
            } else {
                this.setOilmanage(null);
                setMessage("?!");
            }
        } catch (Exception e) {
            log.error(this.getClass().getName() + "!load ??:", e);
            throw new Exception(this.getClass().getName() + "!load ??:", e);
        }
        return SUCCESS;
    }

    public String add() throws Exception {
        ContextHelper.isPermit("QKJ_QKJMANAGE_OILMANAGE_ADD");
        try {
            oilmanage.setAdd_user(ContextHelper.getUserLoginUuid());
            oilmanage.setApply_user(ContextHelper.getUserLoginUuid());
            oilmanage.setApply_dept(ContextHelper.getUserLoginDept());
            oilmanage.setAdd_time(new Date());
            oilmanage.setStatus(0);

            //????
            this.getCarInfo();
            if (licenseInfos != null && licenseInfos.size() > 0) {
                for (int i = 0; i < licenseInfos.size(); i++) {
                    if (licenseInfos.get(i).getUuid().equals(oilmanage.getCar_id())) {
                        oilmanage.setLast_close_it_oiladd_time(licenseInfos.get(i).getLast_close_it_oiladd_time());
                    }
                }
            }
            this.setLastoilmanage(this.getLastResult(oilmanage.getCar_id()));
            oilmanage.setLast_currentMileage(lastoilmanage.getCurrentMileage());
            oilmanage.setLast_close_it_oil_mass(lastoilmanage.getClose_it_oil_mass());
            //liuqianru add
            oilmanage.setUuid((Integer) dao.add(oilmanage));
            addProcess("_ADD", "");
        } catch (Exception e) {
            log.error(this.getClass().getName() + "!add ?:", e);
            throw new Exception(this.getClass().getName() + "!add ?:", e);
        }
        return SUCCESS;
    }

    public String save() throws Exception {
        ContextHelper.isPermit("QKJ_QKJMANAGE_OILMANAGE__MDY");
        try {
            oilmanage.setLm_user(ContextHelper.getUserLoginUuid());
            oilmanage.setLm_time(new Date());
            dao.save(oilmanage);
            addProcess("_MDY", "");
        } catch (Exception e) {
            log.error(this.getClass().getName() + "!save ?:", e);
            throw new Exception(this.getClass().getName() + "!save ?:", e);
        }
        return SUCCESS;
    }

    public String del() throws Exception {
        ContextHelper.isPermit("QKJ_QKJMANAGE_OILMANAGE_DEL");
        try {
            mdyStatus(-2);
        } catch (Exception e) {
            log.error(this.getClass().getName() + "!del ?:", e);
            throw new Exception(this.getClass().getName() + "!del ?:", e);
        }
        return SUCCESS;
    }

    // ?
    // -1: 0: 1: 2: 3: 4: 5:

    /**
     * 
     * 
     * @return
     * @throws Exception
     * @date2017-03-23 ?10:10:19
     */
    public String mdyStatus_1() throws Exception {
        ContextHelper.isPermit("QKJ_QKJMANAGE_OILMANAGE_STATUS_1");
        try {
            mdyStatus(-1);
            //???
            mdySDStatus(0);
        } catch (Exception e) {
            log.error(this.getClass().getName() + "!mdyStatus0 ?:", e);
            throw new Exception(this.getClass().getName() + "!mdyStatus0 ?:", e);
        }
        return SUCCESS;
    }

    /**
     * /
     * 
     * @return
     * @throws Exception
     * @date2017-03-23 ?10:10:19
     */
    public String mdyStatus0() throws Exception {
        ContextHelper.isPermit("QKJ_QKJMANAGE_OILMANAGE_STATUS0");
        try {
            mdyStatus(1);
            mdySDStatus(10);
            //?
            String email = ContextHelper.getUserLoginInfo().getSenior_email();//
            sendemail(email);
        } catch (Exception e) {
            log.error(this.getClass().getName() + "!mdyStatus0 ?:", e);
            throw new Exception(this.getClass().getName() + "!mdyStatus0 ?:", e);
        }
        return SUCCESS;
    }

    /**
     * 
     * 
     * @return
     * @throws Exception
     * @date2017-03-23 ?10:21:02
     */
    public String mdySDStatus5() throws Exception {
        ContextHelper.isPermit("QKJ_QKJMANAGE_OILMANAGE_SDSTATUS");
        try {
            mdySDStatus(5);
            mdyStatus(0);
        } catch (Exception e) {
            log.error(this.getClass().getName() + "!mdySDStatus5 ?:", e);
            throw new Exception(this.getClass().getName() + "!mdySDStatus5 ?:", e);
        }
        return SUCCESS;
    }

    /**
     * 
     * 
     * @return
     * @throws Exception
     * @date2017-03-23 ?10:21:02
     */
    public String mdySDStatus20() throws Exception {
        ContextHelper.isPermit("QKJ_QKJMANAGE_OILMANAGE_SDSTATUS20");
        try {
            mdySDStatus(20);
            mdyStatus(2);
        } catch (Exception e) {
            log.error(this.getClass().getName() + "!mdySDStatus20 ?:", e);
            throw new Exception(this.getClass().getName() + "!mdySDStatus20 ?:", e);
        }
        return SUCCESS;
    }

    /*********************************************************************/
    /* ? */
    /*********************************************************************/
    public String closeLoad() throws Exception {
        ContextHelper.isPermit("QKJ_QKJMANAGE_OILMANAGE_CLOSE");
        try {
            if (!(oilmanage == null || oilmanage.getUuid() == null)) {
                this.setOilmanage((OilManage) dao.get(oilmanage.getUuid()));
            } else {
                this.setOilmanage(null);
            }
            path = "<a href='/manager/default'></a>&nbsp;&gt;&nbsp;<a href='/qkjmanage/_list?viewFlag=relist'>?</a>&nbsp;&gt;&nbsp;";
        } catch (Exception e) {
            log.error(this.getClass().getName() + "!load ??:", e);
            throw new Exception(this.getClass().getName() + "!load ??:", e);
        }
        return SUCCESS;
    }

    /**
     * ?(??)
     * 
     * @return
     * @throws Exception
     */
    public String mdyClose() throws Exception {
        ContextHelper.isPermit("QKJ_QKJMANAGE_OILMANAGE_CLOSE_MDY");
        try {
            oilmanage.setLm_user(ContextHelper.getUserLoginUuid());
            oilmanage.setLm_time(new Date());
            dao.mdyCloseMaintain(oilmanage);
            addProcess("_CLOSE_MDY", "?");
        } catch (Exception e) {
            log.error(this.getClass().getName() + "!mdyClose ?:", e);
            throw new Exception(this.getClass().getName() + "!mdyClose ?:", e);
        }
        return SUCCESS;
    }

    /**
     * 
     * 
     * @return
     * @throws Exception
     * @date2017-03-23 ?10:10:19
     */
    public String mdyStatus2() throws Exception {
        ContextHelper.isPermit("QKJ_QKJMANAGE_OILMANAGE_CLOSE_STATUS2");
        try {
            oilmanage.setStatus(3);
            oilmanage.setClose_start(new Date());
            dao.startMaintainCloseFlow(oilmanage);
            addProcess("_START_CLOSE", "");
        } catch (Exception e) {
            log.error(this.getClass().getName() + "!mdyStatus2 ?:", e);
            throw new Exception(this.getClass().getName() + "!mdyStatus2 ?:", e);
        }
        return SUCCESS;
    }

    /**
     * 
     * 
     * @return
     * @throws Exception
     * @date2017-03-23 ?10:10:19
     */
    public String mdyStatus3() throws Exception {
        ContextHelper.isPermit("QKJ_QKJMANAGE_OILMANAGE_CLOSE_STATUS3");
        try {
            mdyStatus(4);
            mdyCloseSDStatus(10);
            //?
            String email = ContextHelper.getUserLoginInfo().getSenior_email();//
            sendemail(email);
        } catch (Exception e) {
            log.error(this.getClass().getName() + "!mdyStatus3 ?:", e);
            throw new Exception(this.getClass().getName() + "!mdyStatus3 ?:", e);
        }
        return SUCCESS;
    }

    // ? 0:?? 5: 10: 20:

    /**
     * -
     * 
     * @return
     * @throws Exception
     * @date2017-03-23 ?10:29:40
     */
    public String mdyCloseSDStatus5() throws Exception {
        ContextHelper.isPermit("QKJ_QKJMANAGE_OILMANAGE_CLOSE_SDSTATUS5");
        try {
            mdyCloseSDStatus(5);
            mdyStatus(3);
        } catch (Exception e) {
            log.error(this.getClass().getName() + "!mdyCloseSDStatus5 ?:", e);
            throw new Exception(this.getClass().getName() + "!mdyCloseSDStatus5 ?:", e);
        }
        return SUCCESS;
    }

    /**
     * - 
     * 
     * @return
     * @throws Exception
     * @date2017-03-23 ?10:29:40
     */
    public String mdyCloseSDStatus20() throws Exception {
        ContextHelper.isPermit("QKJ_QKJMANAGE_OILMANAGE_CLOSE_SDSTATUS20");
        try {
            mdyCloseSDStatus(20);
            mdyStatus(5);
        } catch (Exception e) {
            log.error(this.getClass().getName() + "!mdyCloseSDStatus20 ?:", e);
            throw new Exception(this.getClass().getName() + "!mdyCloseSDStatus20 ?:", e);
        }
        return SUCCESS;
    }

    /**
     * ?
     * 
     * @param status
     * @return
     * @date2017-03-23 ?10:15:02
     */
    private int mdyStatus(int status) {
        oilmanage.setStatus(status);
        oilmanage.setLm_user(ContextHelper.getUserLoginUuid());
        oilmanage.setLm_time(new Date());
        if (status == 2 || status == 1) {
            oilmanage.setPass_time(new Date());
        }
        if (status == 5 || status == 4) {
            oilmanage.setClose_pass_time(new Date());
        }
        return dao.mdyMaintainStatus(oilmanage);
    }

    /**
     * ?
     * 
     * @param sd_status
     * @return
     * @date2017-03-23 ?10:20:39
     */
    public int mdySDStatus(int sd_status) {
        String noteflag = "";
        if (sd_status == 5) {
            noteflag = "";
        }
        if (sd_status == 10) {
            noteflag = "";
        }
        if (sd_status == 20) {
            noteflag = "";
        }
        oilmanage.setSd_status(sd_status);
        oilmanage.setSd_time(new Date());
        oilmanage.setSd_user(ContextHelper.getUserLoginUuid());
        oilmanage.setLm_user(ContextHelper.getUserLoginUuid());
        String note = "?-??-" + noteflag;
        addProcess("_MDY_SDSTATUS", note);
        return dao.mdyMaintainSDStatus(oilmanage);
    }

    /**
     * ????
     * 
     * @return
     * @throws Exception
     * @date2017-03-23 ?10:25:25
     */
    public int mdyCloseSDStatus(int close_sd_status) {
        String noteflag = "";
        if (close_sd_status == 5) {
            noteflag = "";
        }
        if (close_sd_status == 10) {
            noteflag = "";
        }
        if (close_sd_status == 20) {
            noteflag = "";
        }
        oilmanage.setClose_sd_status(close_sd_status);
        oilmanage.setClose_sd_time(new Date());
        oilmanage.setClose_sd_user(ContextHelper.getUserLoginUuid());
        oilmanage.setLm_user(ContextHelper.getUserLoginUuid());
        String note = "-??-" + noteflag;
        addProcess("_CLOSE_SDSTATUS", note);

        return dao.mdyCloseMaintainSDStatus(oilmanage);
    }

    private LastOilManage getLastResult(int carId) throws Exception {
        try {
            map.clear();
            lastoilmanage = new LastOilManage();
            map.put("car_id", carId);
            this.setLastoilmanages(dao.getLastList(map));
            if (lastoilmanages.size() > 0) {
                return lastoilmanages.get(0);
            }
        } catch (Exception e) {
            log.error(this.getClass().getName() + "!list ??:", e);
            throw new Exception(this.getClass().getName() + "!list ??:", e);
        }
        return lastoilmanage;
    }

    public void getCarInfo() throws Exception {
        try {
            map.clear();
            if (licenseInfo == null)
                licenseInfo = new GetLicense();
            this.setLicenseInfos(cardao.list(map));
            this.setRecCount(cardao.getResultCount());
        } catch (Exception e) {
            log.error(this.getClass().getName() + "!getCarInfo ??:", e);
            throw new Exception(this.getClass().getName() + "!getCarInfo ??:", e);
        }
    }

    public void getOilPrice() throws Exception {
        List<String> prices = new ArrayList<>();
        //?
        try {
            Document doc = null;
            doc = Jsoup.connect("http://ny.gold600.com/qinghai.html").get();
            Elements element1 = doc.getElementsByClass("JO_330q63");
            Elements element2 = doc.getElementsByClass("JO_331q63");
            Elements element3 = doc.getElementsByClass("JO_332q63");

            prices.add(element1.text());
            prices.add(element2.text());
            prices.add(element3.text());

            HttpServletRequest request = ServletActionContext.getRequest();
            JSONArray jsonArray = JSONArray.fromObject(prices);
            HttpServletResponse response = ServletActionContext.getResponse();
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().print(jsonArray);
        } catch (Exception e) {
            log.error(this.getClass().getName() + "!getOilPrice ??:", e);
            throw new Exception(this.getClass().getName() + "!getOilPrice ??:", e);
        }
    }

    private void sendemail(String email) {
        if (email != null && email != "") {
            StringBuffer url = new StringBuffer();
            HttpServletRequest request = ServletActionContext.getRequest();
            String url2 = request.getRequestURL().toString();
            String uri = request.getRequestURI();
            //url.append(url2.substring(0, url2.indexOf(uri)));
            url.append("http://cl.qkj.com.cn");
            StringBuffer content = new StringBuffer();
            content.append("<div><div style='margin-left:4%;'>");
            content.append("<p style='color:red;'>");
            content.append("</p>");
            content.append(
                    "<p style='text-indent: 2em;'>?????</p>");
            content.append("<p style='text-indent: 2em;display: block;word-break: break-all;'>");
            content.append("?<a style='text-decoration: none;' href='" + url.toString() + "'>"
                    + url.toString() + "</a></p>");
            content.append("</div>");
            content.append("<ul style='color: rgb(169, 169, 189);font-size: 18px;'>");
            /*content.append("<li>???12?</li>");
            content.append("<li>?</li>");*/
            content.append(
                    "<li>?????</li>");
            content.append("<li>??</li>");
            content.append("</ul>");
            content.append("</div>");
            SysMail.sendHtml(email, "?", content.toString());

        }
    }

    private void addProcess(String p_sign, String p_note) {
        ProcessDAO pdao = new ProcessDAO();
        if (oilmanage != null) {
            pdao.addProcess(3, oilmanage.getUuid(), p_sign, p_note, oilmanage.getStatus(), oilmanage.getSd_status(),
                    oilmanage.getClose_sd_status(), ContextHelper.getUserLoginUuid());
        }
    }

}