com.crec.demo.GeneralController.java Source code

Java tutorial

Introduction

Here is the source code for com.crec.demo.GeneralController.java

Source

package com.crec.demo;

import static com.mongodb.client.model.Filters.and;
import static com.mongodb.client.model.Filters.eq;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

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

import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.crec.util.CodeUtil;
import com.mongodb.gridfs.GridFSDBFile;
import com.platform.io.bean.Account;
import com.platform.io.bean.Company;
import com.platform.io.bean.Material;
import com.platform.io.bean.OrderOrContract;
import com.platform.io.bean.Person;
import com.platform.io.bean.WaybillInfo;
import com.platform.mongo.s1.MongoDirverS1;
import com.platform.mongo.s2.MongoDirver;
import com.platform.mongo.util.FileUtils;
import com.platform.mongo.util.JavaBeanToDBObject;
import com.platform.mongo.util.MD5Util;
import com.platform.mongo.util.TimeUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
public class GeneralController {

    @RequestMapping("/query")
    public String index_jsp(Model model) {
        model.addAttribute("key", "MVC of Spring!");
        return "/index";
    }

    @RequestMapping("/json")
    public void json(@RequestParam String name, HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        System.out.println("JSON HERE!!!");
        response.getWriter().print("{'name'," + name + "}");
    }

    @RequestMapping("/queryCert")
    public void QueryCertification(@RequestParam String str, @RequestParam int start, @RequestParam int limit,
            HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println(str);
        MongoDirverS1 md = new MongoDirverS1();
        String result = md.queryCertifications(str, start, limit);
        md.close();
        response.getWriter().print(result);
    }

    @RequestMapping("/queryPurchase_bidding")
    public void QueryPurchaseBidding(@RequestParam String str, @RequestParam String industry,
            @RequestParam int start, @RequestParam int limit, HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        System.out.println(industry);
        MongoDirverS1 md = new MongoDirverS1();
        String result = md.queryPurchaseBidding(str, industry, start, limit);
        md.close();
        System.out.println(result);
        response.getWriter().print(result);
    }

    @RequestMapping("/queryCertMenu_tz")
    public void QueryCertificationByLeaf(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        MongoDirverS1 md = new MongoDirverS1();
        String result = md.queryCertification_menu_tz();
        md.close();
        response.getWriter().print(result);
    }

    @RequestMapping("/queryLatestStandard")
    public void QueryLatestStandard(@RequestParam String str, @RequestParam int start, @RequestParam int limit,
            HttpServletRequest request, HttpServletResponse response) throws IOException {
        MongoDirverS1 md = new MongoDirverS1();
        String result = md.queryLatestStandards(str, start, limit);
        md.close();
        response.getWriter().print(result);
    }

    @RequestMapping("/queryStandard")
    public void QueryStandard(@RequestParam String str, @RequestParam String standard_group,
            @RequestParam String standard_status, @RequestParam String special_subject, @RequestParam int start,
            @RequestParam int limit, HttpServletRequest request, HttpServletResponse response) throws IOException {
        MongoDirverS1 md = new MongoDirverS1();
        String result = md.queryStandards(str, standard_group, standard_status, special_subject, start, limit);
        md.close();
        response.getWriter().print(result);
    }

    @RequestMapping("/queryPriceMenu")
    public void QueryPriceMenu(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String json = "{\"name\":\"\",\"childs\":[{\"name\":\"??\",\"childs\":[{\"name\":\"????\"},"
                + "{\"name\":\"\",\"childs\":[{\"name\":\"H\"},{\"name\":\"\"},{\"name\":\"\"},"
                + "{\"name\":\"\"},{\"name\":\"\"},{\"name\":\"\"},{\"name\":\"\"},"
                + "{\"name\":\"??\"},{\"name\":\"\"}]},{\"name\":\"??\",\"childs\":[{\"name\":\"?\"},"
                + "{\"name\":\"?\"},{\"name\":\"?\"},{\"name\":\"?\"},{\"name\":\"?\"}]},{\"name\":\"\"},"
                + "{\"name\":\"\",    \"childs\":[{\"name\":\"\"},{\"name\":\"\"},{\"name\":\"?\"}]},"
                + "{\"name\":\"???\"},{\"name\":\"??\"},{\"name\":\"?\"}]},"
                + "{\"name\":\"???\",\"childs\":[{\"name\":\"??\",\"childs\":[{\"name\":\"\",\"childs\":[{\"name\":\"?\"}]}]}]}]}";
        response.getWriter().print(json);
    }

    @RequestMapping("/queryPrice")
    public void QueryPrice(@RequestParam String name, @RequestParam String date, @RequestParam String specification,
            @RequestParam String city, @RequestParam int start, @RequestParam int limit, HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        MongoDirverS1 md = new MongoDirverS1();
        String result = md.queryPrice(name, date, specification, city, start, limit);
        md.close();
        response.getWriter().print(result);
    }

    @RequestMapping("/queryCompanyForPrice")
    public void queryCompanyForPrice(@RequestParam String name, @RequestParam String specification,
            HttpServletRequest request, HttpServletResponse response) throws IOException {
        MongoDirverS1 md = new MongoDirverS1();
        String result = md.queryCompanyForPrice(name, specification);
        md.close();
        response.getWriter().print(result);
    }

    @RequestMapping("/queryPriceHistory1")
    public void QueryPriceHistory(@RequestParam String id, HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        MongoDirverS1 md = new MongoDirverS1();
        // 
        Document price_info = md.queryPrice(id);
        // 
        Calendar now = TimeUtil.getToday();
        Calendar month_ago = TimeUtil.getDay(Calendar.MONTH, -1);
        Map<String, Integer> day_price = new TreeMap<String, Integer>();
        List<Document> data = md.queryPriceHistory(id);

        int init_price = data.remove(0).getInteger("price");

        for (Document d : data) {
            Date date = d.get("date", Date.class);
            String ymd = TimeUtil.parserStringYMD(date);
            day_price.put(ymd, d.getInteger("price"));
        }

        List<Document> labels = new ArrayList<Document>();
        List<Document> values = new ArrayList<Document>();
        while (month_ago.compareTo(now) <= 0) {
            Document label = new Document();
            Document value = new Document();
            String ymd = TimeUtil.parserStringYMD(month_ago.getTime());
            Integer p = day_price.get(ymd);
            if (p != null && p > 0) {
                init_price = p;
                value.put("value", p);
            } else {
                value.put("value", init_price);
            }
            label.put("label", ymd);
            labels.add(label);
            values.add(value);
            month_ago.add(Calendar.DAY_OF_MONTH, 1);
        }
        String caption = price_info.getString("name") + " " + price_info.getString("specification") + " "
                + price_info.getString("company");
        Document json = new Document();
        json.put("chart", new Document("caption", caption).append("numberprefix", "")
                .append("plotgradientcolor", "").append("bgcolor", "FFFFFF").append("showalternatehgridcolor", "0")
                .append("divlinecolor", "CCCCCC").append("showvalues", "0").append("showcanvasborder", "0")
                .append("canvasborderalpha", "0").append("canvasbordercolor", "CCCCCC")
                .append("canvasborderthickness", "1").append("yaxismaxvalue", "3000").append("captionpadding", "30")
                .append("linethickness", "3").append("yaxisvaluespadding", "15").append("legendshadow", "0")
                .append("legendborderalpha", "0")
                .append("palettecolors", "#f8bd19,#008ee4,#33bdda,#e44a00,#6baa01,#583e78")
                .append("showborder", "0"));
        Document category = new Document();
        category.put("category", labels);
        List<Document> categorys = new ArrayList<Document>();
        categorys.add(category);
        json.put("categories", categorys);

        List<Document> datasets = new ArrayList<Document>();
        Document dataset = new Document();
        dataset.put("seriesname", price_info.getString("city"));
        dataset.put("data", values);
        datasets.add(dataset);
        json.put("dataset", datasets);

        md.close();
        response.getWriter().print(json.toJson());
    }

    @RequestMapping("/queryPriceHistory2")
    public void QueryPriceHistory(@RequestParam String id, @RequestParam String name,
            @RequestParam String specification, @RequestParam String company, @RequestParam String city,
            HttpServletRequest request, HttpServletResponse response) throws IOException {
        MongoDirverS1 md = new MongoDirverS1();
        // 
        Document price_info = md.queryPrice(id);
        // 
        Calendar now = TimeUtil.getToday();
        Calendar month_ago = TimeUtil.getDay(Calendar.MONTH, -1);
        Map<String, Integer> day_price = new TreeMap<String, Integer>();
        Map<String, Integer> day_price1 = new TreeMap<String, Integer>();
        List<Document> data = md.queryPriceHistory(id);
        List<Document> data1 = md.queryPriceHistory(name, specification, company, city);

        int init_price = data.remove(0).getInteger("price");
        int init_price1 = data1.remove(0).getInteger("price");

        for (Document d : data) {
            Date date = d.getDate("date");
            String ymd = TimeUtil.parserStringYMD(date);
            day_price.put(ymd, d.getInteger("price"));
        }

        for (Document d : data1) {
            Date date = d.getDate("date");
            String ymd = TimeUtil.parserStringYMD(date);
            day_price1.put(ymd, d.getInteger("price"));
        }

        List<Document> labels = new ArrayList<Document>();
        List<Document> values = new ArrayList<Document>();

        List<Document> labels1 = new ArrayList<Document>();
        List<Document> values1 = new ArrayList<Document>();
        while (month_ago.compareTo(now) <= 0) {
            Document label = new Document();
            Document value = new Document();

            Document label1 = new Document();
            Document value1 = new Document();
            String ymd = TimeUtil.parserStringYMD(month_ago.getTime());
            Integer p = day_price.get(ymd);
            Integer p1 = day_price1.get(ymd);
            if (p != null && p > 0) {
                init_price = p;
                value.put("value", p);
            } else {
                value.put("value", init_price);
            }

            if (p1 != null && p1 > 0) {
                init_price1 = p1;
                value1.put("value", p1);
            } else {
                value1.put("value", init_price1);
            }

            label.put("label", ymd);
            label1.put("label", ymd);
            labels.add(label);
            labels1.add(label1);
            values.add(value);
            values1.add(value1);
            month_ago.add(Calendar.DAY_OF_MONTH, 1);
        }
        String caption = price_info.getString("name") + " " + price_info.getString("specification") + " "
                + price_info.getString("texture");
        Document json = new Document();
        json.put("chart", new Document("caption", caption).append("numberprefix", "")
                .append("plotgradientcolor", "").append("bgcolor", "FFFFFF").append("showalternatehgridcolor", "0")
                .append("divlinecolor", "CCCCCC").append("showvalues", "0").append("showcanvasborder", "0")
                .append("canvasborderalpha", "0").append("canvasbordercolor", "CCCCCC")
                .append("canvasborderthickness", "1").append("yaxismaxvalue", "3000").append("captionpadding", "30")
                .append("linethickness", "3").append("yaxisvaluespadding", "15").append("legendshadow", "0")
                .append("legendborderalpha", "0")
                .append("palettecolors", "#f8bd19,#008ee4,#33bdda,#e44a00,#6baa01,#583e78")
                .append("showborder", "0"));
        Document category = new Document();
        category.put("category", labels);
        List<Document> categorys = new ArrayList<Document>();
        categorys.add(category);
        json.put("categories", categorys);

        List<Document> datasets = new ArrayList<Document>();
        Document dataset = new Document();
        dataset.put("seriesname", price_info.getString("city"));
        dataset.put("data", values);
        datasets.add(dataset);

        Document dataset1 = new Document();
        dataset1.put("seriesname", city);
        dataset1.put("data", values1);
        datasets.add(dataset1);

        json.put("dataset", datasets);

        md.close();
        response.getWriter().print(json.toJson());
    }

    /**
     * ????
     * @author niyn
     * @param orderOrContracts  
     * @param session
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addOrderOrContract")
    @ResponseBody
    public String addOrderOrContract(@RequestBody OrderOrContract orderOrContracts, HttpSession session,
            HttpServletResponse response) throws Exception {
        System.out.println(orderOrContracts);
        Account a = (Account) session.getAttribute("account");
        List<Integer> list = a.getFiled();
        orderOrContracts.setFiled(list);
        MongoDirver md = new MongoDirver();
        md.addOrderOrContract(orderOrContracts, a.getName());
        return "success";
    }

    /**
     * ?????
     * @author niyn
     * @param contract_id ????
     * @param purchasing_company ??
     * @param company_name 
     * @param start
     * @param limit
     * @param request
     * @param session
     * @param response
     * @throws IOException
     */
    @RequestMapping("/queryOrderOrContract")
    public void queryOrderOrContract(@RequestParam String contract_id, @RequestParam String purchasing_company,
            @RequestParam String company_name, @RequestParam int start, @RequestParam int limit,
            HttpServletRequest request, HttpSession session, HttpServletResponse response) throws IOException {
        MongoDirver md = new MongoDirver();
        Account a = (Account) session.getAttribute("account");
        String result = md.queryOrderOrContract(contract_id, purchasing_company, company_name, a.getFiled(), start,
                limit);
        md.close();
        response.getWriter().print(result);
    }

    /**
     * ?id
     * @author niyn
     * @param str
     * @param start
     * @param limit
     */
    @RequestMapping("/queryPurchasingById")
    public void queryPurchasingById(@RequestParam String _id, HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        MongoDirver md = new MongoDirver();
        String result = md.queryPurchasingById(_id);
        System.out.println("?_idjson" + result);
        md.close();
        response.getWriter().print(result);
    }

    /**
     * ??
     * @author niyn
     * @param str
     * @param start
     * @param limit
     */
    @RequestMapping("/queryPurchasingByCode")
    public void queryPurchasingByCode(@RequestParam String material_code, HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        MongoDirver md = new MongoDirver();
        String result = md.queryPurchasingByCode(material_code);
        md.close();
        response.getWriter().print(result);
    }

    /**
     * ??
     * @author niyn
     * @param str
     * @param start
     * @param limit
     */
    @RequestMapping("/querySupplyDetailByCode")
    public void querySupplyDetailByCode(@RequestParam String materialCode, HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        MongoDirver md = new MongoDirver();
        String result = md.querySupplyDetailByCode(materialCode);
        md.close();
        response.getWriter().print(result);
    }

    /**
     * ????
     * @author niyn
     * @param contract_id  ????
     */
    @RequestMapping("/queryOrderOrContractDetail")
    public void queryOrderOrContractDetail(@RequestParam String _id, HttpServletRequest request,
            HttpSession session, HttpServletResponse response) throws IOException {
        MongoDirver md = new MongoDirver();
        Account a = (Account) session.getAttribute("account");
        String result = md.queryOrderOrContractDetail(_id, a.getFiled());
        md.close();
        response.getWriter().print(result);
    }

    /**
     * ?/??
     * @author niyn
     * @throws Exception 
     */
    @RequestMapping(value = "/updateOrderOrContract")
    @ResponseBody
    public void updateOrderOrContract(@RequestBody OrderOrContract orderOrContracts) throws Exception {
        System.out.println(orderOrContracts);
        MongoDirver md = new MongoDirver();
        md.updateOrderOrContract(orderOrContracts);
    }

    /**
     * ??
     * @author niyn
     * @param waybillInfo
     */
    @RequestMapping(value = "/addWaybillInfo")
    @ResponseBody
    public String addWaybillInfo(@RequestBody WaybillInfo waybillInfo, HttpSession session,
            HttpServletResponse response) throws Exception {
        System.out.println(waybillInfo);
        Account a = (Account) session.getAttribute("account");
        waybillInfo.setFiled(a.getFiled());
        MongoDirver md = new MongoDirver();
        md.addWaybillInfo(waybillInfo);
        return "success";
    }

    /**
     * ???
     * @author niyn
     * @param logistics_id                
     * @param logistics_company
     * @param car_license
     * @param contract_id
     * @param logistics_stats
     * @param good_num
     * @param start
     * @param limit
     */
    @RequestMapping("/queryWaybillInfo")
    public void queryWaybillInfo(@RequestParam String logistics_id, @RequestParam String logistics_company,
            @RequestParam String car_license, @RequestParam String contract_id,
            @RequestParam String logistics_stats, @RequestParam String good_num, @RequestParam int start,
            @RequestParam int limit, HttpServletRequest request, HttpSession session, HttpServletResponse response)
            throws IOException {
        MongoDirver md = new MongoDirver();
        Account a = (Account) session.getAttribute("account");
        String result = md.queryWaybillInfo(logistics_id, logistics_company, car_license, contract_id,
                logistics_stats, good_num, a.getFiled(), start, limit);
        System.out.println(result);
        md.close();
        response.getWriter().print(result);
    }

    @RequestMapping("/queryGoodsInfo")
    public void queryGoodsInfo(@RequestParam String _id, @RequestParam int start, @RequestParam int limit,
            HttpServletRequest request, HttpSession session, HttpServletResponse response) throws IOException {
        Account a = (Account) session.getAttribute("account");
        MongoDirver md = new MongoDirver();
        String result = md.queryGoodsInfo(_id, a.getFiled(), start, limit);
        System.out.println(result);
        md.close();
        response.getWriter().print(result);
    }

    @RequestMapping("/queryLogisticsInfo")
    public void queryLogisticsInfo(@RequestParam String _id, HttpServletRequest request, HttpSession session,
            HttpServletResponse response) throws IOException {
        MongoDirver md = new MongoDirver();
        Account a = (Account) session.getAttribute("account");
        String result = md.queryLogisticsInfo(_id, a.getFiled());
        System.out.println(result);
        md.close();
        response.getWriter().print(result);
    }

    /**
     * 
     * @author niyn
     * @param username  ??
     * @param password   ?
     */
    @RequestMapping("/goLogin")
    public ModelAndView login(String username, String password, HttpSession session) throws Exception {
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        MongoDirver md = new MongoDirver();
        Account account = md.login(username, MD5Util.MD5(password));
        System.out.println(account + "");
        if (account != null) {
            session.setAttribute("account", account);
            jsonMap.put("success", true);
            return new ModelAndView("queryCodeSearch");
        } else {
            return new ModelAndView("queryLogin");
        }
    }

    /**
     * 
     * @author niyn
     */
    @RequestMapping("/goLogout")
    public ModelAndView logout(HttpSession session) {
        session.invalidate();
        return new ModelAndView("queryLogin");
    }

    /**
     * ?
     * @author niyn
     * @param com_name   ???
     * @param org_code      ?
     * @param start
     * @param limit
     */
    @RequestMapping("/queryCompanyList")
    public void queryCompanyList(@RequestParam String com_name, @RequestParam String org_code,
            HttpServletRequest request, HttpSession session, HttpServletResponse response, int start, int limit)
            throws IOException {
        MongoDirver md = new MongoDirver();
        String result = md.queryCompany(com_name, org_code, start, limit);
        System.out.println(result);
        md.close();
        response.getWriter().print(result);
    }

    /**
     * ?
     * @author niyn         
     * @param company    company
     */
    @RequestMapping("/addCompany")
    @ResponseBody
    public void addCompany(Company company) throws Exception {
        MongoDirver md = new MongoDirver();
        md.addCompany(company);
        md.close();
    }

    /**
     * ?
     * @author niyn
     * @param name            ??
     * @param username     ??
     * @param company      ?
     * @param start
     * @param limit
     */

    @RequestMapping("/queryAccountList")
    public void queryAccountList(@RequestParam String name, @RequestParam String username,
            @RequestParam String company, HttpServletRequest request, HttpSession session,
            HttpServletResponse response, int start, int limit) throws IOException {
        MongoDirver md = new MongoDirver();
        String result = md.queryAccount(name, username, company, start, limit);
        System.out.println(result);
        md.close();
        response.getWriter().print(result);
    }

    /**
     * 
     * @author niyn
     * @param name         ??
     * @param password   ?
     * @param username  ?? 
     * @param company   ? 
     * @param tel             ?
     * @param email        
     */
    @RequestMapping("/addAccount")
    @ResponseBody
    public void addAccount(@RequestParam String name, @RequestParam String password, @RequestParam String username,
            @RequestParam String company, @RequestParam String tel, @RequestParam String email,
            HttpServletRequest request, HttpSession session, HttpServletResponse response) throws Exception {
        Account a = new Account();
        Person p = new Person();
        p.setCompany(company);
        p.setEmail(email);
        p.setUsername(username);
        p.setTel(tel);
        List<Integer> filed = new ArrayList<Integer>();
        a.setName(name);
        a.setPassword(MD5Util.MD5(password));
        a.setPerson(p);
        a.setFiled(filed);
        MongoDirver md = new MongoDirver();
        md.addAccount(a);
        md.close();
    }

    /**
     * ????
     * @author niyn
     * @param _id      idaccount_id
     */
    @RequestMapping("/queryAuthorityInfo")
    public void queryAuthorityInfo(@RequestParam String _id, HttpServletRequest request, HttpSession session,
            HttpServletResponse response) throws Exception {
        MongoDirver md = new MongoDirver();
        String result = md.queryAuthorityInfo(_id);
        System.out.println(result);
        md.close();
        response.getWriter().print(result);
    }

    /**
     * ????
     * @author niyn
     * @param _id      idaccount_id
     */
    @RequestMapping("/queryOperationInfo")
    public void queryOperationInfo(@RequestParam String _id, HttpServletRequest request, HttpSession session,
            HttpServletResponse response) throws Exception {
        MongoDirver md = new MongoDirver();
        String result = md.queryOperationInfo(_id);
        System.out.println(result);
        md.close();
        response.getWriter().print(result);
    }

    /**
     * ?????
     * @author niyn
     * @param fileds  ???operationoper_num
     * @param _id      idaccount_id
     */
    @RequestMapping("/assignOperation")
    public void assignOperation(@RequestParam String fileds, @RequestParam String _id, HttpServletRequest request,
            HttpSession session, HttpServletResponse response) throws Exception {
        String[] f = {};
        if ((fileds.length() > 0) && fileds != null) {
            f = fileds.split(",");
        }
        MongoDirver md = new MongoDirver();
        md.assignOperation(f, _id);
        md.close();
    }

    /**
     * ?????
     * @author niyn
     * @param fileds ?? filed(companycom_filed)
     * @param _id     idaccount_id
     */
    @RequestMapping("/assign")
    public void assign(@RequestParam String fileds, @RequestParam String _id, HttpServletRequest request,
            HttpSession session, HttpServletResponse response) throws Exception {
        String[] f = {};
        if ((fileds.length() > 0) && fileds != null) {
            f = fileds.split(",");
        }
        MongoDirver md = new MongoDirver();
        md.assign(f, _id);
        md.close();
    }

    /**
     * PDF??
     * @author niyn
     * @param file_name ??4855.pdf??4855
     */
    @RequestMapping("/download")
    public void download(@RequestParam String file_name, HttpServletResponse res) throws Exception {
        OutputStream os = res.getOutputStream();
        try {
            res.reset();
            res.setHeader("Content-Disposition",
                    "attachment;filename=" + new String((file_name + ".pdf").getBytes(), "iso-8859-1"));
            res.setContentType("application/octet-stream");
            MongoDirver md = new MongoDirver();
            GridFSDBFile gfFile = md.downloadPDF(file_name, null);
            if (gfFile != null) {
                gfFile.writeTo(os);
            } else {
                res.getWriter().println("");
            }
            os.flush();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

    /**
     * ???
     * @author niyn
     * @param code ??
     * @throws Exception 
     */
    @RequestMapping("/generateBarCode")
    public void generateBarCode(@RequestParam String code, HttpServletResponse response) throws Exception {
        byte[] barCodeByte = CodeUtil.barcode(code);
        response.getWriter().print(barCodeByte);
    }

    /**
     * ??
     * @author niyn
     * @param code ??
     * @throws Exception 
     */
    @RequestMapping("/generateQrCode")
    public void generateQrCode(@RequestParam String code, HttpServletResponse response) throws Exception {
        byte[] barCodeByte = CodeUtil.qrcode(code);
        System.out.println("??:" + barCodeByte);
        response.getWriter().print(barCodeByte);
    }

    /**
    * 
    * @author niyn
    */
    @RequestMapping("/addMaterial")
    @ResponseBody
    public String addMaterial(@RequestBody List<Material> material) throws Exception {
        System.out.println("material?" + material);
        JSONArray jsonarray = JSONArray.fromObject(material);
        List<Material> list = (List) JSONArray.toCollection(jsonarray, Material.class);
        MongoDirver md = new MongoDirver();
        md.addMaterial(list);
        return "success";
    }

    /**
     * 
     * @author niyn
     * @return
     * @throws IOException 
     */
    @RequestMapping("/queryMaterial")
    public void queryMaterial(@RequestParam String material_code, @RequestParam String material_name,
            @RequestParam int start, @RequestParam int limit, HttpServletResponse response) throws IOException {
        MongoDirver md = new MongoDirver();
        String result = md.queryMaterial(material_code, material_name, start, limit);
        response.getWriter().println(result);
    }

    /**
     * ?id
     * @author niyn
     * @param _id
     * @throws Exception
     */
    @RequestMapping("/queryMaterialById")
    public void queryMaterialById(@RequestParam String _id, HttpServletResponse response) throws Exception {
        MongoDirver md = new MongoDirver();
        String result = md.queryMaterialById(_id);
        response.getWriter().println(result);
    }

    /**
     * [?]
     * @author niyn
     * @param _id
     * @param material
     * @throws Exception
     */
    @RequestMapping("/updateMaterial")
    public void updateMaterial(@RequestParam String _id, @RequestParam String material_code,
            @RequestParam String material_name, @RequestParam String specification,
            @RequestParam String measurement, @RequestParam Integer isPrecious, HttpServletResponse response)
            throws Exception {
        MongoDirver md = new MongoDirver();
        Material m = new Material();
        m.setMaterial_code(material_code);
        m.setMaterial_name(material_name);
        m.setSpecification(specification);
        m.setMeasurement(measurement);
        m.setIsPrecious(isPrecious);
        md.updateMaterial(_id, m);
        response.getWriter().println("success");
    }

    /**
    * 
    * @author niyn
    * @param _id
    * @param material
    * @throws Exception
    */
    @RequestMapping("/deleteMaterial")
    public void deleteMaterial(@RequestParam String _id, HttpServletResponse response) throws Exception {
        MongoDirver md = new MongoDirver();
        md.deleteMaterial(_id);
        response.getWriter().println("success");
    }
}