package com.founder.ec.cms.service.impl;

import com.founder.ec.cms.service.ProductListImportService;
import com.founder.ec.common.page.JspPage;
import com.founder.ec.common.page.Page;
import com.founder.ec.common.service.impl.BaseClassServiceImpl;

import com.founder.ec.goods_no.Goods;
import com.founder.ec.goods_no.ProductBrand;
import com.founder.ec.goods_no.ProductExpand;
import com.founder.ec.goods_no.ProductList;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangchuanfu on 17/9/5.
 */
@Service
public class ProductListImportServiceImpl extends
        BaseClassServiceImpl implements ProductListImportService {

    /**
     * 批量导入
     *
     * @param
     * @param is
     * @return
     * @throws Exception
     */

    @Override
    public String saveData(Goods goods, InputStream is) throws Exception {
        ProductList  productList=new ProductList();
        //String fieldValues = "";
        String nogoods = "";

        goods.setAddTime(goods.getAddTime());
        goods.setAddUserId(goods.getAddUserId());
        // .xlsx的Excel文件

        if (goods.getFileName().indexOf(".xlsx") > -1) {

            XSSFWorkbook xssfWorkbook = null;
            try {
                xssfWorkbook = new XSSFWorkbook(is);
            } catch (Exception e) {
                e.printStackTrace();
            }
            XSSFRow row;
            XSSFCell cell;
            // 获取第一个sheet
            XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
            int rowNum = xssfSheet.getLastRowNum();
            for (int i = 1; i <= rowNum; i++) {
                String fieldValues = "";
                row = xssfSheet.getRow(i);
                if(row!=null){
                    // 商品编码 1
                    cell = row.getCell(0);//
                    if (null != cell) {
                        fieldValues = "";
                        if (cell.getCellType() == 0) {
                            double cellValue = cell.getNumericCellValue();
                            fieldValues = new DecimalFormat("#").format(cellValue);
                        } else if (cell.getCellType() == 1) {
                            fieldValues = cell.getStringCellValue();
                        }
                    }else{
                        return "empty";
                    }

                    productList.setProductCode((cell == null ? null : fieldValues));
                    nogoods += fieldValues + ",";
                }else{
                    return "empty";
                }

            }
            String[] goodsnos = nogoods.split(",");
            for (int j = 0; j < goodsnos.length; j++) {
                if ("null".equals(goodsnos[j]) || goodsnos[j] == "") {
                    return "empty";
                }else{

                    ProductList goodsAbraod = getGoodsByGoodsNo(goodsnos[j]);
                    if (goodsAbraod == null) {
                        return "empty";
                    }
                }

            }

            /**
             * 数据在导入之前,先判断该商品编码是都为空,如果有一条为空,则整个excel不进行导入,
             * 再根据商品编码查询数据,如果有一条为空,则整个excel依然不进行数据的导入,
             * 此时,数据才是完全符合需求的,进行数据的导入

             */

            for (int i = 1; i <= rowNum; i++) {
                String fieldValues = "";
                row = xssfSheet.getRow(i);

                // 商品编码 1
                cell = row.getCell(0);//
                if (null != cell) {
                    fieldValues = "";
                    if (cell.getCellType() == 0) {
                        double cellValue = cell.getNumericCellValue();
                        fieldValues = new DecimalFormat("#").format(cellValue);
                    } else if (cell.getCellType() == 1) {
                        fieldValues = cell.getStringCellValue();
                    }
                }
                productList.setProductCode((cell == null ? null : fieldValues));

                //通过商品编码查询productId,通过productId进行更新
                /**
                 * --product 进行更新的时用PRODUCT_CODE,先根据product表中PRODUCT_CODE查询goods表中对应的product_id
                 --goods   进行更新的时用PRODUCT_ID;

                 --product : 商品编码:product_code,商品名称:product_name,最小订购量:product_least_order
                 --限购量:product_limit_amount

                 --goods  :包装规格:stand_package, 重量:weight,市场价:market_price,售价:ec_price,
                 --库存:available_stock
                 */

                //商品名称

                cell = row.getCell(1);//
                if (null != cell) {
                    fieldValues = "";
                    if (cell.getCellType() == 0) {
                        double cellValue = cell.getNumericCellValue();
                        fieldValues = new DecimalFormat("#").format(cellValue);
                    } else if (cell.getCellType() == 1) {
                        fieldValues = cell.getStringCellValue();
                    }
                }
                productList.setProductName((cell == null ? null : fieldValues));

                //最小订购量小于1不更新,
                cell = row.getCell(2);
                if (null != cell) {
                    fieldValues = "";
                    if (cell.getCellType() == 0) {
                        double cellValue = cell.getNumericCellValue();
                        fieldValues = new DecimalFormat("#").format(cellValue);
                    } else if (cell.getCellType() == 1) {
                        fieldValues = cell.getStringCellValue();
                    }
                }

                if(cell==null||cell.toString()=="" ){
                    productList.setProductLeastOrder(null);
                }

                else if(Integer.parseInt(fieldValues)<1){
                    productList.setProductLeastOrder(null);
                }

                else{
                    productList.setProductLeastOrder(new BigDecimal( fieldValues));
                }

                //每单订购量小于1不更新

                cell = row.getCell(3);//
                if (null != cell) {
                    fieldValues = "";
                    if (cell.getCellType() == 0) {
                        double cellValue = cell.getNumericCellValue();
                        fieldValues = new DecimalFormat("#").format(cellValue);
                    } else if (cell.getCellType() == 1) {
                        fieldValues = cell.getStringCellValue();
                    }
                }
                if(cell==null || cell.toString()==""){
                    productList.setOrderLimitAmount(null);
                }
                else if(Integer.parseInt(fieldValues)<1 ){
                    productList.setOrderLimitAmount(null);
                }else{
                    productList.setOrderLimitAmount( new BigDecimal(fieldValues));
                }

                //包装规格

                cell = row.getCell(4);//
                if (null != cell) {
                    fieldValues = "";
                    if (cell.getCellType() == 0) {
                        double cellValue = cell.getNumericCellValue();
                        fieldValues = new DecimalFormat("#").format(cellValue);
                    } else if (cell.getCellType() == 1) {
                        fieldValues = cell.getStringCellValue();
                    }
                }
                goods.setPackageStandard((cell == null ? null : fieldValues));
                //重量(kg)小于0时不更新
                cell = row.getCell(5);//
                if (null != cell) {
                    fieldValues = "";
                    if (cell.getCellType() == 0) {
                        double cellValue = cell.getNumericCellValue();
                        //fieldValues = new DecimalFormat("#").format(cellValue);
                        BigDecimal bg = new BigDecimal(cellValue);
                        fieldValues=bg.setScale(2, BigDecimal.ROUND_HALF_UP).toString().trim();
                    } else if (cell.getCellType() == 1) {
                        fieldValues = cell.getStringCellValue();

                    }
                }
                if(cell==null ||cell.toString()==""){
                    goods.setWeight(null);
                }else{
                    //重量保留两位小数
                    BigDecimal wei= new BigDecimal(fieldValues);

                    goods.setWeight((cell == null ? null :wei));
                    int d=wei.compareTo(BigDecimal.ZERO);
                    if(d==-1){
                        goods.setWeight(null);
                    }
                }

                //库存小于0不更新
                cell = row.getCell(6);//
                if (null != cell) {
                    fieldValues = "";
                    if (cell.getCellType() == 0) {
                        double cellValue = cell.getNumericCellValue();
                        fieldValues = new DecimalFormat("#").format(cellValue);
                    } else if (cell.getCellType() == 1) {
                        fieldValues = cell.getStringCellValue();
                    }
                }
                if(cell==null||cell.toString()==""){
                    goods.setAvailableStock(null);
                }
                else if(Integer.parseInt(fieldValues)<0){
                    goods.setAvailableStock(null);
                }else{
                    goods.setAvailableStock((new BigDecimal(fieldValues)));
                }

                //售价小于10不更新
                cell = row.getCell(7);//
                /**
                 *

                if (null != cell) {
                    fieldValues = "";
                    if (cell.getCellType() == 0) {
                        double cellValue = cell.getNumericCellValue();
                        fieldValues = new DecimalFormat("#").format(cellValue);
                    } else if (cell.getCellType() == 1) {
                        fieldValues = cell.getStringCellValue();
                    }
                }
                if(cell==null|| cell.toString()==""){
                    goods.setEcPrice(null);
                }
                else if(Integer.parseInt(fieldValues)<10){
                    goods.setEcPrice(null);
                }else {
                    goods.setEcPrice(( new BigDecimal(fieldValues)));
                }
                 */

                if (null != cell) {
                    fieldValues = "";
                    if (cell.getCellType() == 0) {
                        double cellValue = cell.getNumericCellValue();
                        //fieldValues = new DecimalFormat("#").format(cellValue);
                        BigDecimal bg = new BigDecimal(cellValue);
                        fieldValues=bg.setScale(2, BigDecimal.ROUND_HALF_UP).toString().trim();
                    } else if (cell.getCellType() == 1) {
                        fieldValues = cell.getStringCellValue();

                    }
                }

                if(cell==null ||cell.toString()=="" ){
                    goods.setEcPrice(null);
                }else{
                    //售价保留两位小数
                    BigDecimal wei= new BigDecimal(fieldValues);
                    if(wei.intValue()<10){
                        goods.setEcPrice(null);
                    }
                    goods.setEcPrice((cell == null ? null :wei));
                    int d=wei.compareTo(BigDecimal.ZERO);
                    if(d==-1){
                        goods.setEcPrice(null);
                    }
                }

                //市场价小于0不更新

                cell = row.getCell(8);//
                /**
                 *
                     if (null != cell) {
                     fieldValues = "";
                     if (cell.getCellType() == 0) {
                     double cellValue = cell.getNumericCellValue();
                     fieldValues = new DecimalFormat("#").format(cellValue);
                     } else if (cell.getCellType() == 1) {
                     fieldValues = cell.getStringCellValue();
                     }
                     }
                     if(cell==null|| cell.toString()==""){
                     goods.setMarketPrice(null);
                     }
                     else if(Integer.parseInt(fieldValues)<0){
                     goods.setMarketPrice(null);
                     }
                     else{
                     goods.setMarketPrice(( new BigDecimal(fieldValues)));
                 }
                 */
                if (null != cell) {
                    fieldValues = "";
                    if (cell.getCellType() == 0) {
                        double cellValue = cell.getNumericCellValue();
                        //fieldValues = new DecimalFormat("#").format(cellValue);
                        BigDecimal bg = new BigDecimal(cellValue);
                        fieldValues=bg.setScale(2, BigDecimal.ROUND_HALF_UP).toString().trim();
                    } else if (cell.getCellType() == 1) {
                        fieldValues = cell.getStringCellValue();

                    }
                }
                if(cell==null ||cell.toString()==""){
                    goods.setMarketPrice(null);
                }else{
                    //市场价保留两位小数
                    BigDecimal wei= new BigDecimal(fieldValues);

                    goods.setMarketPrice((cell == null ? null :wei));
                    int d=wei.compareTo(BigDecimal.ZERO);
                    if(d==-1){
                        goods.setMarketPrice(null);
                    }
                }

                if(productList!=null ||productList.getProductCode()!=null){

                    /**
                     * 更新PRODUCT表,根据PRODUCT_CODE
                     */

                    commonDao.update("ProductList.updateImportProductList",productList);

                }
                /**
                 *更新GOODS表,根据productId
                 */

                ProductList p=  getGoodsByGoodsNo(productList.getProductCode())  ;
                if(p!=null){
                    goods.setProductID(p.getProductID());
                }

                /**
                 * 分别查询prefixName,suffixName,brandName,productCommonName的值
                 */

                /**
                 * 查询品牌名称
                 */
                String brandName="";
                String prefixName="";
                String suffixName="";
                String productNames="";
                ProductBrand brand=getProductBrandByProductId(p.getProductBrandID());

                if(brand!=null){
                     brandName=brand.getProductBrandName()==null?"":brand.getProductBrandName();
                }
                ProductExpand productExpand=getProductExpandByProId(p.getProductID());

                if(productExpand!=null){
                    prefixName=(productExpand.getStandby2()==null)?"":productExpand.getStandby2();
                    suffixName=(productExpand.getStandby3()==null)?"":productExpand.getStandby3();
                }
                String productCommonName=(p.getProductCommonName()==null)?"":p.getProductCommonName();
                 productNames=productList.getProductName()==null?"":productList.getProductName();
                if(productNames==null ||productNames==""){
                    goods.setGoodsName(null);
                }else{
                    goods.setGoodsName(prefixName+" "+brandName+" "+productNames+" "+productCommonName+" "+suffixName);
                }

                if(p.getProductID()!=null ){
                    commonDao.update("ProductList.updateImportGoods", goods);
                }

            }
        }
        return "ok";
    }

    private ProductExpand getProductExpandByProId(Long productID) {
        return (ProductExpand) commonDao.queryForObject(
                "ProductList.getProductExpandByProductId", productID);
    }

    /**
     * 根据excel所给的商品编码查询是否符合该商品
     * @param goodsNo
     * @return
     */
    public ProductList getGoodsByGoodsNo(String goodsNo) {
        return (ProductList) commonDao.queryForObject(
                "ProductList.getGoodsByGoodsNoImport", goodsNo);

    }
    private ProductBrand getProductBrandByProductId(Long productBrandID) {
        return (ProductBrand) commonDao.queryForObject(
                "ProductList.getProductBrandByProductId", productBrandID);
    }

    @Override
    public Map<String, Object> getProductListPage(ProductList productList, Integer pageNo, Integer pageSize) {

        productList.setStartRow((pageNo - 1) * pageSize + 1);
        productList.setEndRow(productList.getStartRow() + pageSize
                - 1);

        List<ProductList> lmt = getProductListPage(productList);
        Integer count = getProductListCount(productList);
        // 实际起始位置
        int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
        Page page = new JspPage(skipResults, count, pageSize, lmt);

        Map<String, Object> resMap = new HashMap<String, Object>();
        resMap.put("total", page.getTotalCount());
        resMap.put("rows", page.getResult());
        return resMap;
    }

    private Integer getProductListCount(
            ProductList productList) {
        return (Integer) commonDao.queryForObject(
                "ProductList.getProductListCount",
                productList);

    }

    private List<ProductList> getProductListPage(
            ProductList pharmacistComment) {
        List<ProductList> ttmList = commonDao
                .queryForList("ProductList.getProductListPage",
                        pharmacistComment);
        return ttmList;
    }

}
package com.founder.ec.cms.service.impl;

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

import jxl.Cell;
import jxl.Sheet;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.founder.ec.cms.model.MedicineDose;
import com.founder.ec.cms.service.MedicineDoseService;
import com.founder.ec.common.dao.CommonDao;
import com.founder.ec.common.service.impl.BaseClassServiceImpl;
import com.founder.ec.common.utils.StringUtil;
import com.founder.ec.product.model.Goods;

/**
 * 类 <code>MedicineDoseServiceImpl</code>{商品用药信息 实体service 实现}
 *
 * @author dongqing
 * @version 2014-7-24下午3:14:49
 */
@Service
public class MedicineDoseServiceImpl extends BaseClassServiceImpl<MedicineDose> implements MedicineDoseService {

//    @Autowired
//    private GoodsService goodsService;

    @Autowired
    private CommonDao commonDao;

    @Override
    public Map<String, Object> importMedicineDose(Sheet sheet) {
        HashMap<String, Object> rslt = new HashMap<String, Object>();
        //保存重复添加的商品编码
        List<String> goodsNoList = new ArrayList<String>();
        //保存在Goods表不存在的商品编码
        List<String> goodsNotExistsList = new ArrayList<String>();
        try {
            int countColumn = sheet.getColumns();
            int countRow = sheet.getRows();
            if (countColumn < 10) {
                rslt.put("code", -2);// Excel表格应该等于10列
            } else if (countRow <= 1) {
                rslt.put("code", -2);// Excel表格应该大于1行
            } else {
                StringBuffer msg = new StringBuffer(); // 记录错误消息
                MedicineDose medicineDose = new MedicineDose();
                Cell cell = null;
                out: for (int i = 1; i < countRow; i++) {
                    for (int j = 0; j < countColumn; j++) {
                        cell = sheet.getCell(j, i);
                        String content = null == cell.getContents() ? "" : cell.getContents().trim();
                        switch (j) {
                        case 0: // 这里的商品Id修改为商品编码
                            try {
                                //medicineDose.setGoodsId(Long.parseLong(content));
                                medicineDose.setGoodsNo(content);
                            } catch (Exception e) {
                                msg.append("第" + (i + 1) + "行,第" + (j + 1) + "列,商品编码有误。").append("<br>");
                                break out;
                            }
                            break;
                        case 1:// 每次最低用量
                            try {
                                medicineDose.setLowestDoseTime(Double.parseDouble(content));
                            } catch (Exception e) {
                                msg.append("第" + (i + 1) + "行,第" + (j + 1) + "列,每次最低用量有误。").append("<br>");
                                break out;
                            }
                            break;
                        case 2:// 每天最低用药次数
                            try {
                                medicineDose.setLowestDoseDay(Long.parseLong(content));
                            } catch (Exception e) {
                                msg.append("第" + (i + 1) + "行,第" + (j + 1) + "列,每天最低用药次数有误。").append("<br>");
                                break out;
                            }
                            break;
                        case 3:// 每次最高用量
                            try {
                                medicineDose.setHighestDoseTime(Double.parseDouble(content));
                            } catch (Exception e) {
                                msg.append("第" + (i + 1) + "行,第" + (j + 1) + "列,每次最高用量有误。").append("<br>");
                                break out;
                            }
                            break;
                        case 4:// 每天最高用药次数
                            try {
                                medicineDose.setHighestDoseDay(Long.parseLong(content));
                            } catch (Exception e) {
                                msg.append("第" + (i + 1) + "行,第" + (j + 1) + "列,每天最高用药次数有误。").append("<br>");
                                break out;
                            }
                            break;
                        case 5:// 每盒总量
                            try {
                                medicineDose.setTotalDose(Double.parseDouble(content));
                            } catch (Exception e) {
                                msg.append("第" + (i + 1) + "行,第" + (j + 1) + "列,每盒总量有误。").append("<br>");
                                break out;
                            }
                            break;
                        case 6:// 是否为二次营销用药
                            try {
                                if (content.length() == 1) {
                                    medicineDose.setIsTwice(content);
                                } else {
                                    msg.append("第" + (i + 1) + "行,第" + (j + 1) + "列,是否为二次营销用药有误。").append("<br>");
                                    break out;
                                }
                            } catch (Exception e) {
                                msg.append("第" + (i + 1) + "行,第" + (j + 1) + "列,是否为二次营销用药有误。").append("<br>");
                                break out;
                            }
                            break;
                        case 7:// 药品等级
                            try {
                                medicineDose.setMedicineGrade(content);
                            } catch (Exception e) {
                                msg.append("第" + (i + 1) + "行,第" + (j + 1) + "列,药品等级有误。").append("<br>");
                                break out;
                            }
                            break;
                        case 8:// 药品对应疾病
                            try {
                                medicineDose.setMedicineToDisease(content);
                            } catch (Exception e) {
                                msg.append("第" + (i + 1) + "行,第" + (j + 1) + "列,药品对应疾病有误。").append("<br>");
                                break out;
                            }
                            break;
                        case 9:// 药品最小单位
                            try {
                                medicineDose.setUnit(content);
                            } catch (Exception e) {
                                msg.append("第" + (i + 1) + "行,第" + (j + 1) + "列,药品最小单位有误。").append("<br>");
                                break out;
                            }
                            break;
                        default:
                            break;
                        }
                    }

                    // 当没有错误消息的时候保存
                    if (StringUtils.isEmpty(msg.toString())) {

                        //根据商品编码查询到此商品Id
                        Goods goods = new Goods();
                        goods.setGoodsNo(medicineDose.getGoodsNo());
                        goods=(Goods) commonDao.queryForObject("Goods.getProductIdByGoodsNo", goods);

                        if(goods!=null){
                            //根据商品Id查询medicine_Dose表是否有这个商品记录
                            MedicineDose md=(MedicineDose) commonDao.queryForObject("MedicineDose.getMedicineDoseByGoodsId", goods.getGoodsId());
                            if(md!=null){ //若有则将原来的记录标记为Y
                                goodsNoList.add(md.getGoodsNo());
                                md.setIsDelete("Y") ;
                                super.update(md);
                            }
                            medicineDose.setGoodsId(goods.getGoodsId());// 设置商品Id
                            super.save(medicineDose);
                        }else{
                            //将在goods表不存在的商品编码加入到集合中
                            goodsNotExistsList.add(medicineDose.getGoodsNo());
                        }
                    }
                }
                if (StringUtil.isEmpty(msg.toString())) {
                    rslt.put("code", 0);

                    //处理重复导入
                    if(goodsNoList!=null && goodsNoList.size()>0){
                        msg.append("商品编码为");
                        for(String str:goodsNoList){
                            msg.append(str).append(",");
                        }
                        msg.deleteCharAt(msg.length()-1); //去掉末尾的,
                        msg.append("被重复添加<br />");
                    }
                    //处理goods表查不到导入的商品
                    if(goodsNotExistsList!=null && goodsNotExistsList.size()>0){
                        msg.append("商品编码为");
                        for(String str:goodsNotExistsList){
                            msg.append(str).append(",");
                        }
                        msg.deleteCharAt(msg.length()-1); //去掉末尾的,
                        msg.append("在Goods表不存在,请重新核对信息再导入.<br />");
                    }
                    rslt.put("msg", msg.toString());
                } else {
                    rslt.put("code", -1);
                    rslt.put("msg", msg.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rslt;
    }
}
package com.founder.ec.goods.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Cell;
import jxl.Sheet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.founder.ec.common.dao.CommonDao;
import com.founder.ec.common.utils.StringUtil;
import com.founder.ec.goods.model.EcGoodsCode;
import com.founder.ec.goods.model.EcGoodsPrice;
import com.founder.ec.goods.model.Goods;
import com.founder.ec.goods.service.ImportExcelService;

@Service
public class ImportExcelServiceImpl implements ImportExcelService {

    private List<EcGoodsPrice> ecGoodsPriceList;

    private Map<String, Goods> goodsMap;

    @Autowired
    private CommonDao commonDao;

    @Override
    public HashMap<String, Object> importOrderItemTc(Sheet sheet,
            EcGoodsPrice item) {
        // TODO Auto-generated method stub
        HashMap<String, Object> rslt = new HashMap<String, Object>();
        ecGoodsPriceList = new ArrayList<EcGoodsPrice>();
        goodsMap = new HashMap<String, Goods>();
        EcGoodsCode code=null;

        int countColumn = sheet.getColumns();
        int countRow = sheet.getRows();
        if (countColumn < 6) {
            rslt.put("code", -2);// Excel表格应该大于4列
        } else if (countRow <= 1) {
            rslt.put("code", -2);// Excel表格应该大于1行
        } else {
            StringBuffer msg = new StringBuffer();
            boolean isErr = false;
            EcGoodsPrice goodsPrice = null;

            Cell cell = null;
            for (int i = 1; i < countRow; i++) {
                for (int j = 0; j < countColumn; j++) {
                    cell = sheet.getCell(j, i);
                    String content = null == cell.getContents() ? "" : cell
                            .getContents().trim();
                    switch (j) {
                    case 0:// 商品编码
                        try {
                            Goods g = null;
                            goodsPrice = new EcGoodsPrice();
                            if (null != goodsMap.get(content)) {
                                g = goodsMap.get(content);
                            } else {
                                g = new Goods();
                                g.setGoodsNo(content);
                                g = (Goods) commonDao.queryForObject(
                                        "Goods.getByGoodsNo", content);
                            }
                            if (null != g) {
                                goodsMap.put(content, g);
                                goodsPrice.setGoodsNo(g.getGoodsNo());
                                isErr = false;
                            } else {
                                msg.append("第" + (i + 1) + "行,第" + (j + 1) + "列,商品编码" + content + "不存在<br>");
                                break;
                            }
                        } catch (Exception e) {
                            // isErr = true;
                            msg.append(
                                    "第" + (i + 1) + "行,第" + (j + 1)
                                            + "列,未能找到该编码的商品。").append("<br>");
                            break;
                        }
                        break;
                    case 1:// 市场分类
                        try {
                            if (goodsPrice != null) {
                                if (content!=null&&!"".equals(content)) {
                                    code=new EcGoodsCode();
                                    code.setIsDelete("N");
                                    code.setType("market");
                                    code.setName(content);
                                    code=(EcGoodsCode) commonDao.queryForObject("EcGoodsCode.getSimilarEcGoodsCode",code);
                                    if (code!=null) {
                                        goodsPrice.setAutoType(code.getId());
                                        isErr=false;
                                    }else {
                                        isErr = true;
                                        msg.append(
                                                "第" + (i + 1) + "行,第" + (j + 1) + "列,市场分类"+content+"不存在。")
                                                .append("<br>");
                                        break;
                                    }

                                }
                                isErr = false;
                            }

                            isErr = false;
                        } catch (Exception e) {
                            isErr = true;
                            msg.append(
                                    "第" + (i + 1) + "行,第" + (j + 1) + "列,市场分类有误。")
                                    .append("<br>");
                            break;
                        }
                        break;
                    case 2:// 主要推广方式
                        try {
                            if (goodsPrice != null) {
                                if (content!=null&&!"".equals(content)) {
                                    code=new EcGoodsCode();
                                    code.setIsDelete("N");
                                    code.setType("way");
                                    code.setName(content);
                                    code=(EcGoodsCode) commonDao.queryForObject("EcGoodsCode.getSimilarEcGoodsCode",code);
                                    if (code!=null) {
                                        goodsPrice.setMainSpreada(code.getId());
                                        isErr=false;
                                    }else {
                                        isErr = true;
                                        msg.append(
                                                "第" + (i + 1) + "行,第" + (j + 1) + "列,主要推广方式"+content+"不存在。")
                                                .append("<br>");
                                        break;
                                    }

                                }
                            }

                            isErr = false;
                        } catch (Exception e) {
                            isErr = true;
                            msg.append(
                                    "第" + (i + 1) + "行,第" + (j + 1) + "列,主要推广方式有误。")
                                    .append("<br>");
                            break;
                        }
                        break;
                    case 3:// 竞争对手
                        try {
                            if (goodsPrice != null) {
                                if (content!=null&&!"".equals(content)) {
                                    code=new EcGoodsCode();
                                    code.setIsDelete("N");
                                    code.setType("comp");
                                    code.setName(content);
                                    code=(EcGoodsCode) commonDao.queryForObject("EcGoodsCode.getSimilarEcGoodsCode",code);
                                    if (code!=null) {
                                        goodsPrice.setTargetWeb(code.getId());
                                        isErr=false;
                                    }else {
                                        isErr = true;
                                        msg.append(
                                                "第" + (i + 1) + "行,第" + (j + 1) + "列,竞争对手"+content+"不存在。")
                                                .append("<br>");
                                        break;
                                    }

                                }
                            }
                        } catch (Exception e) {
                            isErr = true;
                            msg.append(
                                    "第" + (i + 1) + "行,第" + (j + 1) + "列,竞争对手有误。")
                                    .append("<br>");
                            break;
                        }
                        break;
                    case 4:// 厂家指导价
                        try {
                            if (goodsPrice != null) {
                                goodsPrice.setGuidePrice(new BigDecimal(content));
                                isErr = false;
                            }
                        } catch (Exception e) {
                            isErr = true;
                            msg.append(
                                    "第" + (i + 1) + "行,第" + (j + 1) + "列,厂家指导价有误。")
                                    .append("<br>");
                            break;
                        }
                        break;
                    case 5:// 价格
                        try {
                            if (goodsPrice != null) {
                                goodsPrice.setTargetWebPrice(new BigDecimal(content));
                                isErr = false;
                            }
                        } catch (Exception e) {
                            isErr = true;
                            msg.append(
                                    "第" + (i + 1) + "行,第" + (j + 1) + "列,价格有误。")
                                    .append("<br>");
                            break;
                        }
                        break;
                    default:
                        break;
                    }
                    if (isErr) {
                        goodsPrice = null;
                        break;
                    }
                }
                if (goodsPrice!=null&&goodsPrice.getGoodsNo()!=null) {

                    goodsPrice.setAddUserId(item.getAddUserId());

                    ecGoodsPriceList.add(goodsPrice);
                }
            }
            if (StringUtil.isEmpty(msg.toString())) {
                rslt.put("code", 0);
            } else {
                rslt.put("code", -1);
                rslt.put("msg", msg.toString());
            }
        }
        try {
            if (item.getOperate()!=null&&!"".equals(item.getOperate())) {
                if ("save".equals(item.getOperate())) {
                    //批量插入至ORDER_ITEM_TC
                    commonDao.batchInsert("EcGoodsPrice.save", ecGoodsPriceList);
                }else {
                    //批量插入至ORDER_ITEM_TC
                    commonDao.batchInsert("EcGoodsPrice.update", ecGoodsPriceList);
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return rslt;
    }

}
$(function() {
    $(document).keydown(function() {// 禁止使用F5刷新
        var keynum;
        var e = arguments[0];
        if (window.event) {// IE
            keynum = e.keyCode;
        } else if (e.which) {// Netscape/Firefox/Opera
            keynum = e.which;
        }
        if (keynum == 116) {
            e.cancelBubble = true;
            return false;
        }
    });

    $(document).bind("contextmenu", function(e) {// 屏蔽右键
        return false;
    });
//    var now = new Date();
//    $('#saleTime').datebox(
//            'setValue',
//            now.getFullYear() + "-" + (now.getMonth() + 1) + "-"
//                    + now.getDate());

    initSWFUpload({
        upload_url : appPath + "/page/importExcel/orderItemTc",
        post_params : {
            "userId" : $("#userId").val(),
            "saleTime" : $("#saleTime").datebox("getValue")
        },
        button_placeholder_id : "orderImportBtn",
        upload_start_handler : uploadStartFn,
        upload_success_handler : importOk,
        file_types : "*.xls",
        debug : false,
        custom_settings : {
            progressTarget : "fsUploadProgress",
            cancelButtonId : "btnCancel"
        }
    });
});

function uploadStartFn(file) {
    $("#swfText").val(file.name);

     var saleTime = $("#saleTime").datebox("getValue");
     if(saleTime==null||saleTime==''){
         $.messager.alert("系统提示","请选择销售日期!");
         return false;
     }
    var flag = false;
    doAjax({
        url : appPath + "/jsp/common/session_user_id.jsp",
        cache : false,
        async : false,
        success : function(userIdDom) {
            $("#rslt").html("");
            $("#uploadUserIdDiv").html(userIdDom);
            swfUploadObj.removePostParam("uploadUserId");
            swfUploadObj
                    .addPostParam("uploadUserId", $("#sessionUserId").val());
            swfUploadObj.addPostParam("addUserId", $("#sessionUserId").val());
            swfUploadObj.addPostParam("userId", $("#sessionUserId").val());
            swfUploadObj.addPostParam("saleTime", saleTime);
            flag = true;
        }
    });
    return flag;
}

function importOk(file, serverData) {
    var serverDataObj = eval("(" + serverData + ")");
    if (typeof (serverDataObj.code) != "undefined") {
        var progress = new FileProgress(file,
                swfUploadObj.customSettings.progressTarget);
        progress.setComplete();
        progress.setStatus("完成.");
        progress.toggleCancel(false);
        switch (serverDataObj.code) {
        case 0:
            $("#rslt").html("");
            $("#rslt").html("导入成功");
            break;
        case -1:
            if (typeof (serverDataObj.msg) != "undefined") {
                if (serverDataObj.msg.indexOf('不存在') != -1) {
                    $("#rslt").html("导入成功!但以下数据有误:<br/>" + serverDataObj.msg);
                } else {
                    $("#rslt").html("以下数据有误:<br/>" + serverDataObj.msg);
                }
            }
            break;
        case -2:
            $("#rslt").html("提供的Excel格式或者数据有误");
            break;
        case -3:
            $("#rslt").html("内部错误,请联系管理员");
            break;
        default:
            break;
        }

    }
}

Excel 导入遍历的更多相关文章

  1. C# Excel导入、导出【源码下载】

    本篇主要介绍C#的Excel导入.导出. 目录 1. 介绍:描述第三方类库NPOI以及Excel结构 2. Excel导入:介绍C#如何调用NPOI进行Excel导入,包含:流程图.NOPI以及C#代 ...

  2. 利用反射实现通用的excel导入导出

    如果一个项目中存在多种信息的导入导出,为了简化代码,就需要用反射实现通用的excel导入导出 实例代码如下: 1.创建一个 Book类,并编写set和get方法 package com.bean; p ...

  3. Excel 导入到Datatable 中,再使用常规方法写入数据库

    首先呢?要看你的电脑的office版本,我的是office 2013 .为了使用oledb程序,需要安装一个引擎.名字为AccessDatabaseEngine.exe.这里不过多介绍了哦.它的数据库 ...

  4. Excel导入导出(篇二)

    <body> <h3>一.Excel导入</h3> <h5>.模板下载:<a href="UpFiles/TemplateFiles/学 ...

  5. C# Excel导入、导出

    本篇主要介绍C#的Excel导入.导出. 目录 1. 介绍:描述第三方类库NPOI以及Excel结构 2. Excel导入:介绍C#如何调用NPOI进行Excel导入,包含:流程图.NOPI以及C#代 ...

  6. Octopus——excel导入导出工具

    Octopus Octopus是一个简易的Excel导入导出工具.目前主要就两个功能: 导入:将excel中一行数据转换为指定的java对象,并通过指定的正则表达式检查合法性. 导出:按照给定的xml ...

  7. JeeSite中Excel导入导出

    在各种管理系统中,数据的导入导出是经常用到的功能,通常导入导出以Excel.CSV格式居多.如果是学习的过程中,最好是自己实现数据导入与导出的功能,然而在项目中,还是调用现成的功能比较好.近期一直使用 ...

  8. Java 使用 Jxl 实现 Excel 导入导出

    开发过程中经常需要用到数据的导入导出功能,之前用的是POI,这次使用JXL,JXL相对于POI来说要轻量简洁许多,在数据量不大的情况下还是非常实用的.这里做一下使用JXL的学习记录.首先需要导入相应的 ...

  9. Spring Boot学习笔记----POI(Excel导入导出)

    业务:动态生成模板导出Excel,用户修改完再导入Excel. Spring boot + bootstrap + poi 1.添加Dependence <dependency> < ...

随机推荐

  1. 百度echarts极速入门

    1.http://echarts.baidu.com/ 进入官网 2.打开官方示例 3.随便打开一个列子并打开f12浏览器调试模式,找到例子的iframe地址 4.打开地址 5.下载例子,so eas ...

  2. MAC配置Xcode的Cocos2d-x环境

    Version:0.9 StartHTML:-1 EndHTML:-1 StartFragment:00000099 EndFragment:00003988 1.Mac配置环境变量,即编辑命令: o ...

  3. c数组

    一维数组 有三种赋值方式 1.原始的赋值 2. 初始化赋值 3.动态赋值 数组的花式玩法 void main() { ] = {, , , , , , , , , }; test(a); putcha ...

  4. 7款开源ERP系统比较

    [网络转载] 现在有许多企业将ERP项目,在企 业中没有实施好,都归咎于软件产品不好.其实,这只是你们的借口.若想要将ERP软件真正与企业融合一体,首先得考虑企业的自身情况,再去选择适合的 ERP软件 ...

  5. 使用Maven创建Web应用程序项目

    用到的技术/工具: Maven 3.3.3 Eclipse 4.3 JDK 8 Spring 4.1.1.RELEASED Tomcat 7 Logback 1.0.13 1. 从Maven模板创建W ...

  6. 从SQL查询分析器中读取EXCEL中的内容

    很早以前就用sql查询分析器来操作过EXCEL文件了. 由于对于excel公式并不是很了解,所以很多时候处理excel中的内容,常常是用sql语句来处理的.[什么样的人有什么样的办法吧 :)] 今又要 ...

  7. (诊断)处理错误fatal error: Python.h: No such file or directory

    安装与Python版本对应的 python-dev 即可,比如: $ -dev

  8. php写文件操作

    function writeLog($file, $msg, $mode='a+') { $fp = fopen($file, $mode); if(flock($fp, LOCK_EX)) { fw ...

  9. Windows7双系统卸载Ubuntu

    正确的删除ubuntu方法如下: 第1步,修复MBR 1.进入win7,下载个软件MbrFix.exe,放在C:\windows\system32文件夹中 2.点击开始>所有程序>附件&g ...

  10. CentOS 经常使用系统命令

    # uname -a # 查看内核/操作系统/CPU信息# head -n 1 /etc/issue   # 查看操作系统版本号# cat /proc/cpuinfo      # 查看CPU信息# ...