/**
 * 版权所有 ©2021 莱欧有限公司
 * 工程名：yearBooks
 * 文件名：YyzbHsyzblService.java
 * 创建日期：2021年10月26日 下午3:57:14
 * 作者：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.OrganizationTreeDao;
import com.lion.dao.YyzbHsyzblDao;
import com.lion.entity.LoginUserInfo;
import com.lion.entity.YyzbYzbl;
import com.lion.util.CheckUtil;
import com.lion.util.ExcelUtil;
import com.lion.util.TimeUtil;
import com.lion.util.Util;

import jodd.util.StringUtil;

/**
 * 类描述  渔业统计指标-海水养殖与捕捞 service
 * @author mt.W
 * @since 2021年10月26日
 */
@Service
public class YyzbHsyzblService {

	@Autowired
	private YyzbHsyzblDao dao;
	@Autowired
	private OrganizationTreeDao oDao;
	
	public Map<String, Object> getRecordList(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.getRecordList(start, row, year, orgid);
		int count=dao.getRecordCount(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> saveRecord(YyzbYzbl n,String userid,String orgid){
		n.setOrgid(orgid);
		int check=dao.checkIfExistByYearAndOrg(n);
		if(check>0){
			return Util.responseMap("false", n.getYear()+"年度的数据已存在，无需再次录入");
		}
		int count = 0;
		if(StringUtil.isNotEmpty(n.getId())){
			n.setUpdateTime(TimeUtil.getNowTime());
			n.setUpdateUser(userid);
			count = dao.editRecord(n);
		}else{
			
			n.setCreateTime(TimeUtil.getNowTime());
			n.setCreateUser(userid);
			count = dao.addRecord(n);
		}
		return Util.responseMap(count);
	}
	public Map<String,Object> deleteRecord(String id){
		int count = dao.deleteRecord(id);
		return Util.responseMap(count);
	}
	public Map<String,Object> exprotRecord(HttpServletRequest req,String year,String orgid){
		List<Map<String, Object>> list = dao.getRecordList(year, orgid);
		String[] title={"部门","年度","鱼类海水养殖(吨)","鱼类海水捕捞(吨)","鱼类养殖面积(公顷)","甲壳类海水养殖(吨)","甲壳类海水捕捞(吨)","甲壳类养殖面积(公顷)","贝类海水养殖(吨)","贝类海水捕捞(吨)","贝类养殖面积(公顷)"
				,"海水养殖总量(吨)","海水捕捞总量(吨)","养殖面积总量(公顷)"};
		String[] cellValue={"orgname","year","ylyz","ylbl","ylyzmj","jkyz","jkbl","jkyzmj","blyz","blbl","blyzmj","zyz","zbl","zyzmj"};
		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!=14){// 不等于12代表跟预设模板格式不符
			return Util.responseMap("false", "excel表格式错误，请下载模板");
		}
		List<YyzbYzbl> list = new ArrayList<YyzbYzbl>();
		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"));
			}
		}
		// 遍历当前sheet中的所有行
		for(int j = 1; j < sheet.getLastRowNum() + 1; j++){// 从第2行开始遍历
			YyzbYzbl i = new YyzbYzbl();
			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 ylyz=row.getCell(2) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(2).getNumericCellValue());
				i.setYlyz(ylyz+"");
				BigDecimal ylbl=row.getCell(3) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(3).getNumericCellValue());
				i.setYlbl(ylbl+"");
				BigDecimal ylyzmj=row.getCell(4) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(4).getNumericCellValue());
				i.setYlyzmj(ylyzmj+"");
				BigDecimal jkyz=row.getCell(5) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(5).getNumericCellValue());
				i.setJkyz(jkyz+"");
				BigDecimal jkbl=row.getCell(6) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(6).getNumericCellValue());
				i.setJkbl(jkbl+"");
				BigDecimal jkyzmj=row.getCell(7) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(7).getNumericCellValue());
				i.setJkyzmj(jkyzmj+"");
				BigDecimal blyz=row.getCell(8) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(8).getNumericCellValue());
				i.setBlyz(blyz+"");
				BigDecimal blbl=row.getCell(9) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(9).getNumericCellValue());
				i.setBlbl(blbl+"");
				BigDecimal blyzmj=row.getCell(10) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(10).getNumericCellValue());
				i.setBlyzmj(blyzmj+"");
				BigDecimal zyz=row.getCell(11) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(11).getNumericCellValue());
				i.setZyz(zyz+"");
				BigDecimal zbl=row.getCell(12) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(12).getNumericCellValue());
				i.setZbl(zbl+"");
				BigDecimal zyzmj=row.getCell(13) == null ? new BigDecimal(0.0) : BigDecimal.valueOf(row.getCell(13).getNumericCellValue());
				i.setZyzmj(zyzmj+"");
			}catch(Exception e){
				e.printStackTrace();
				return Util.responseMap("true", "第"+(j+1)+"行，请输入数字");
			}
			list.add(i);
		}
		int count=0;
		if(list.size()>0){
			for(YyzbYzbl i:list){
				count=dao.checkIfExistByYearAndOrg(i);
				if(count>0){
					return Util.responseMap("false", i.getYear()+"年度的数据已存在，无需再次录入");
				}
				dao.addRecord(i);
			}
		}
		return Util.responseMap(1);
			
	}		
}
