/**
 * 版权所有 ©2021 莱欧有限公司
 * 工程名：yearBooks
 * 文件名：JinChuKouService.java
 * 创建日期：2021年10月21日 下午3:07:42
 * 作者：mt.W
 */
package com.lion.service;

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

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.lion.dao.JinChuKouDao;
import com.lion.dao.OrganizationTreeDao;
import com.lion.entity.JinChuKou;
import com.lion.entity.LoginUserInfo;
import com.lion.util.CheckUtil;
import com.lion.util.ExcelUtil;
import com.lion.util.TimeUtil;
import com.lion.util.Util;

import jodd.util.StringUtil;

/**
 * 类描述
 * @author mt.W
 * @since 2021年10月21日
 */
@Service
public class JinChuKouService {

	@Autowired
	private JinChuKouDao dao;
	@Autowired
	private OrganizationTreeDao oDao;
	
	public Map<String, Object> getJinChuKouRecord(String rows,String page,String year,String orgid){
		int row=Integer.valueOf(rows);
		int start=(Integer.valueOf(page)-1)*row;
		List<Map<String,Object>> list=dao.getJinChuKouRecordList(start, row, year, orgid);
		int count=dao.getJinChuKouRecordCount(year, orgid);
		Map<String,Object> map=Util.responseMap("true", list, count);
		int totalpages=count/row;
		if(count%row!=0){
			totalpages++;
		}
		map.put("currPage", page);
		map.put("totalpages",totalpages);
		return map;
	}
	
	public Map<String,Object> saveJinChuKouInfo(JinChuKou j,String orgid,String userid){
		j.setOrgid(orgid);
		int check=dao.checkIfExistByYearAndOrg(j);
		if(check>0){
			return Util.responseMap("false", j.getYear()+"年度的数据已存在，无需再次录入");
		}
		int count = 0;
		if(StringUtil.isNotEmpty(j.getId())){
			j.setUpdateTime(TimeUtil.getNowTime());
			j.setUpdateUser(userid);
			count = dao.editJinChuKouInfo(j);
		}else{
			
			j.setCreateTime(TimeUtil.getNowTime());
			j.setCreateUser(userid);
			count = dao.addJinChuKouInfo(j);
		}
		return Util.responseMap(count);
	}
	public Map<String,Object> deleteJinChuKouInfo(String id){
		int count = dao.deleteJinChuKouInfo(id);
		return Util.responseMap(count);
	}
	public Map<String,Object> exprotJinChuKouRecord(HttpServletRequest req,String year,String orgid){
		List<Map<String, Object>> list = dao.getJinChuKouRecordList(year, orgid);
		String[] title={"部门","年度","动物产品进口额(万元)","动物产品出口额(万元)","植物产品进口额(万元)","植物产品出口额(万元)","动植物油脂及蜡进口额(万元)","动植物油脂及蜡出口额(万元)","食品烟草及制品进口额(万元)","食品烟草及制品出口额(万元)","其他进口额(万元)","其他出口额(万元)","进口总额(万元)","出口总额(万元)","进出口总额(万元)"};
		String[] cellValue={"orgname","year","dwcp_in","dwcp_out","zwcp_in","zwcp_out","yzjl_in","yzjl_out","yczp_int","yczp_out","qt_in","qt_out","inzj","outzj","inoutzj"};
		String path=ExcelUtil.exportExcel(title, cellValue, list, "农产品进出口贸易", req);
		return Util.responseMap("true","操作成功", path);
	}
	
	@Transactional(rollbackFor=Exception.class)
	public Map<String,Object> importData(MultipartFile file,LoginUserInfo user){
		InputStream in = null;
		try {
			in = file.getInputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Workbook work = ExcelUtil.getWorkbook(in, file.getOriginalFilename());// 根据文件后缀，自适应上传文件的版本
		if (null == work) {// 判定格式不支持
			return Util.responseMap("false", "创建工作簿失败，excel表格格式不支持","");
		}
		Sheet sheet = work.getSheetAt(0);// 获取第一个工作表
		Row row = null;
		if(sheet.getLastRowNum()<1){// 解析excel不足两行，说明是空表格
			return Util.responseMap("false", "无数据可导入");
		}
		int cellNum=sheet.getRow(0).getLastCellNum();// 获取工作表中第3行最后一个单元格编号
		if(cellNum!=15){// 不等于15代表跟预设模板格式不符
			return Util.responseMap("false", "excel表格式错误，请下载模板");
		}
		List<Map<String,Object>> orgdeptList = new ArrayList<Map<String,Object>>();
		Map<String,Object> orgMap = new HashMap<String, Object>();
		if(user.getOrgid().length()==2){//市级
			orgdeptList = oDao.getAllOrgdeptNode();
			for(Map<String,Object> o:orgdeptList){
				orgMap.put(o.get("text").toString(), o.get("id"));
			}
		}

		List<JinChuKou> list = new ArrayList<JinChuKou>();
		// 遍历当前sheet中的所有行
		for(int j = 1; j < sheet.getLastRowNum() + 1; j++){// 从第2行开始遍历
			JinChuKou i = new JinChuKou();
			i.setCreateTime(TimeUtil.getNowTime());
			i.setCreateUser(user.getUserId());
			row = sheet.getRow(j); // 获取第j行
			if(user.getOrgid().length()>2){
				i.setOrgid(user.getOrgid());
			}else{
				String orgname = row.getCell(0) == null ?"":row.getCell(0).toString();
				if(StringUtil.isEmpty(orgname)){
					return Util.responseMap("false", "第"+(j+1)+"行,区县必填");
				}
				if(orgMap.get(orgname.toString().replaceAll(" ", ""))==null){
					return Util.responseMap("false", "第"+(j+1)+"行,区县不存在");
				}
				i.setOrgid(orgMap.get(orgname.trim()).toString());
			}
			
			// 遍历所有的列
			String year = row.getCell(1) == null ?"":row.getCell(1).toString();
			if(StringUtil.isEmpty(year)){
				return Util.responseMap("false", "第"+(j+1)+"行,年度必填");
			}
			if(year.indexOf(".")>0){
				year=year.substring(0,year.indexOf("."));
			}
			if(year.length()!=4||!CheckUtil.checkStrIsNum(year)){
				return Util.responseMap("false", "第"+(j+1)+"行,年度请输入4位数字");
			}
			i.setYear(year);
			try{
				BigDecimal dwcp_in=row.getCell(2) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(2).getNumericCellValue());
				i.setDwcp_in(dwcp_in+"");
				BigDecimal dwcp_out=row.getCell(3) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(3).getNumericCellValue());
				i.setDwcp_out(dwcp_out+"");
				BigDecimal zwcp_in=row.getCell(4) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(4).getNumericCellValue());
				i.setZwcp_in(zwcp_in+"");
				BigDecimal zwcp_out=row.getCell(5) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(5).getNumericCellValue());
				i.setZwcp_out(zwcp_out+"");
				BigDecimal yzjl_in=row.getCell(6) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(6).getNumericCellValue());
				i.setYzjl_in(yzjl_in+"");
				BigDecimal yzjl_out=row.getCell(7) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(7).getNumericCellValue());
				i.setYzjl_out(yzjl_out+"");
				BigDecimal yczp_int=row.getCell(8) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(8).getNumericCellValue());
				i.setYczp_int(yczp_int+"");
				BigDecimal yczp_out=row.getCell(9) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(9).getNumericCellValue());
				i.setYczp_out(yczp_out+"");
				BigDecimal qt_in=row.getCell(10) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(10).getNumericCellValue());
				i.setQt_in(qt_in+"");
				BigDecimal qt_out=row.getCell(11) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(11).getNumericCellValue());
				i.setQt_out(qt_out+"");
				BigDecimal inzj=row.getCell(12) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(12).getNumericCellValue());
				i.setInzj(inzj+"");
				BigDecimal outzj=row.getCell(13) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(13).getNumericCellValue());
				i.setOutzj(outzj+"");
				BigDecimal inoutzj=row.getCell(14) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(14).getNumericCellValue());
				i.setInoutzj(inoutzj+"");
			}catch(Exception e){
				e.printStackTrace();
				return Util.responseMap("true", "第"+(j+1)+"行，请输入数字");
			}
			list.add(i);
		}
		int count=0;
		if(list.size()>0){
			for(JinChuKou i:list){
				count=dao.checkIfExistByYearAndOrg(i);
				if(count>0){
					return Util.responseMap("false", i.getYear()+"年度的数据已存在，无需再次录入");
				}
				dao.addJinChuKouInfo(i);
			}
		}
		return Util.responseMap(1);
			
	}		
}
