package cn.superdesk.uniorder.controller.fund;

import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.exception.constants.ExceptionMsg;
import cn.superdesk.uniorder.common.exception.UnifiedOrderRuntimeException;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.fund.SysFundActivated;
import cn.superdesk.uniorder.dto.vo.fund.SysFundActivatedVoEx;
import cn.superdesk.uniorder.dubbo.consumer.fund.SysFundActivatedDubboConsumerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;


@Slf4j
@RestController
@RequestMapping("/fundActivated")
public class SysFundActivatedController extends BaseController {


	@Autowired
	private SysFundActivatedDubboConsumerService sysFundActivatedDubboConsumerService;

	@PostMapping("/create")
	@ResponseBody
	public R<?> create() throws UnifiedOrderRuntimeException {

		R<?> result = new R<>();

		SysFundActivated sysFundActivated = new SysFundActivated();
		sysFundActivated.setFundUserId(getLong("fundUserId"));
		sysFundActivated.setInvoiceNumber(getString("invoiceNumber")); // 修改标记 20211201
		sysFundActivated.setCheckCount(getBigDecimal("checkCount"));
		sysFundActivated.setFundCount(getBigDecimal("fundCount"));

		sysFundActivatedDubboConsumerService.create(getToken(), sysFundActivated);

		return result;
	}

	@PostMapping("/update")
	@ResponseBody
	public R<?> update() {

		R<?> result = new R<>();

		SysFundActivated sysFundActivated = new SysFundActivated();
		sysFundActivated.setId(getLong("id"));
		sysFundActivatedDubboConsumerService.update(getToken(), sysFundActivated);

		return result;
	}

	@PostMapping("/findAllViewPage")
	@ResponseBody
	public R<List<SysFundActivatedVoEx>> findAllViewPage() {

		R<List<SysFundActivatedVoEx>> result = new R<>();

		Map<String, Object> params = new HashMap<>();

		params.put("organizationName", getString("organizationName"));
		params.put("enterpriseName", getString("enterpriseName"));
		params.put("mobile", getString("mobile"));
		params.put("realname", getString("realname"));
		params.put("invoiceNumber", getString("invoiceNumber")); // 修改标记 20211201
		params.put("activatedStatus", getInteger("activatedStatus"));

		result = sysFundActivatedDubboConsumerService.findAllViewPage(params, getPageNum(), getPageSize());

		return result;
	}

	@PostMapping("/importExcel")
	@ResponseBody
	public R<?> importExcel(@RequestParam("excelFile") MultipartFile excelFile) throws Exception {
		R<?> result = new R<>();

		String fileName = excelFile.getOriginalFilename();
		if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
			throw new UnifiedOrderRuntimeException(ExceptionMsg.SYS_ERROR.getCode(), "上传文件格式不正确");
		}
		boolean isExcel2003 = true;
		if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
			isExcel2003 = false;
		}


		InputStream is = excelFile.getInputStream();
		Workbook    wb = null;
		if (isExcel2003) {
			wb = new HSSFWorkbook(is);
		} else {
			wb = new XSSFWorkbook(is);
		}
		Sheet sheet = wb.getSheetAt(0);
		if (sheet == null) {
			throw new UnifiedOrderRuntimeException(ExceptionMsg.SYS_ERROR.getCode(), "Excel文件无内容");
		}
		int rowCounts = sheet.getLastRowNum();

		Map<String, List<Map<String, Object>>> map = new LinkedHashMap<String, List<Map<String, Object>>>();
		//通过mobile分类
		for (int i = 1; i <= rowCounts; i++) {
			Map<String, Object> excelMap = new HashMap<>();
			sheet.getRow(i).getCell(2).setCellType(CellType.STRING);
			String mobile = sheet.getRow(i).getCell(2).getStringCellValue();

			sheet.getRow(i).getCell(3).setCellType(CellType.STRING);
			String invoiceNumber = sheet.getRow(i).getCell(3).getStringCellValue();

			sheet.getRow(i).getCell(4).setCellType(CellType.STRING);
			BigDecimal checkCount = new BigDecimal(sheet.getRow(i).getCell(4).getStringCellValue());

			sheet.getRow(i).getCell(5).setCellType(CellType.STRING);
			BigDecimal fundCount = new BigDecimal(sheet.getRow(i).getCell(5).getStringCellValue());
			excelMap.put("mobile", mobile);
			excelMap.put("invoiceNumber", invoiceNumber);
			excelMap.put("checkCount", checkCount);
			excelMap.put("fundCount", fundCount);

			List<Map<String, Object>> list = map.get(mobile);
			if (list == null) {
				List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();
				list2.add(excelMap);
				map.put(mobile, list2);
			} else {
				list.add(excelMap);
			}
		}

		sysFundActivatedDubboConsumerService.importExcel(getToken(), map);

		return result;
	}
}
