package com.bsdkj.cgpm.modules.service.bas;
import java.io.File;
import java.io.IOException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import com.bsdkj.cgpm.common.service.core.BaseService;
import com.bsdkj.cgpm.common.util.ExcelUtils;
import com.bsdkj.cgpm.modules.bean.cg.bas.BasBom;
import com.bsdkj.cgpm.modules.bean.cg.bas.BasBomDetail;
import com.bsdkj.cgpm.modules.service.main.GoodsService;
@IocBean(args = { "refer:dao" })
public class BasBomService extends BaseService {
private static final Log log = Logs.get();
@Inject
GoodsService goodsService;
public BasBomService(Dao dao) {
super(dao);
}
/**
* 导入工序信息
*
* @param file
* @return
*/
public BasBom readLines(File file){
Workbook workbook = ExcelUtils.getWorkbook(file);
BasBom basBom = new BasBom();
List lines = new ArrayList();
int r = 0;
if(null != workbook) {
try {
Sheet sheet = workbook.getSheetAt(0);
//获取总行数
int rowNum = sheet.getLastRowNum();
//获取总列数
int colNum = sheet.getRow(0).getPhysicalNumberOfCells();
for(int i = 1; i <= rowNum; i++) {
Row row = sheet.getRow(i);
if(i==1) {
basBom.setType("父件");
String goods_code = ExcelUtils.getCellString(row.getCell(2));
basBom.setGoodscode(goods_code);
String goods_name = ExcelUtils.getCellString(row.getCell(3));
basBom.setGoods_name(goods_name);
String specs = ExcelUtils.getCellString(row.getCell(4));
basBom.setSpecs(specs);
String unit = ExcelUtils.getCellString(row.getCell(5));
basBom.setUnit(unit);
String count = ExcelUtils.getCellString(row.getCell(6));
basBom.setCount(Float.parseFloat(count));
//判断速达数据库里面是否存在code的相同值
int a = goodsService.count(Cnd.where("code", "=", goods_code));
if (a > 0) {
basBom.setState1(1);
}
String name = basBom.getGoods_name();
String specs1 = basBom.getSpecs();
int b = goodsService.count(Cnd.where("name", "=", name).and("specs", "=", specs));
if (b > 0) {
basBom.setState2(1);
}
}else {
BasBomDetail basBomDetail = new BasBomDetail();
List<BasBomDetail> basBomDetails = new ArrayList<BasBomDetail>();
for(int k = 0; k < basBomDetails.size();k++ ){
for(int p = 0; p < basBomDetails.size();p++ ){
if(String.valueOf(basBomDetails.get(p).getGoodscode()).equals(String.valueOf(basBomDetails.get(k).getGoodscode()))){
// basBomDetail = new BasBomDetail();//id,newName,age
basBomDetail.setGoodscode(basBomDetails.get(p).getGoodscode());
basBomDetail.setGoods_name(basBomDetails.get(p).getGoods_name());
basBomDetail.setSpecs(basBomDetails.get(p).getSpecs());
basBomDetail.setUnit(basBomDetails.get(p).getUnit());
System.err.println(basBomDetails.get(p).getGoodscode());
System.err.println(basBomDetails.get(k).getGoodscode());
float a = basBomDetails.get(p).getCount();
float b = basBomDetails.get(k).getCount();
a+=b;
basBomDetail.setCount(a);
//basBomDetails.add(basBomDetail);
//basBomDetails.remove(p);
break;
}else {
String goods_code = ExcelUtils.getCellString(row.getCell(2));
basBomDetail.setGoodscode(goods_code);
String goods_name = ExcelUtils.getCellString(row.getCell(3));
basBomDetail.setGoods_name(goods_name);
String specs = ExcelUtils.getCellString(row.getCell(4));
basBomDetail.setSpecs(specs);
String unit = ExcelUtils.getCellString(row.getCell(5));
basBomDetail.setUnit(unit);
String count = ExcelUtils.getCellString(row.getCell(6));
basBomDetail.setCount(Float.parseFloat(count));
}
}
}
//把货品编码取出来
String code1 = basBomDetail.getGoodscode();
//判断速达数据库里面是否存在code的相同值
int a1 = goodsService.count(Cnd.where("code", "=", code1));
//根据id进行查询 查询一下basbom表和basbomdetail表的数据
if (a1 > 0) {
basBomDetail.setState1(1);
}
String name1 = basBomDetail.getGoods_name();
String specs1 = basBomDetail.getSpecs();
int b1 = goodsService.count(Cnd.where("name", "=", name1).or("specs", "=", specs1));
if (b1 > 0) {
basBomDetail.setState2(1);
}
lines.add(basBomDetail);
}
basBom.setBasBomDetail(lines);
}
basBom = dao().insertWith(basBom, null);
} catch (Exception e) {
// TODO: handle exception
System.err.println(e);
log.error(e.getMessage());
}
}
return basBom;
}
}