package com.sifang.modules.account.service;

import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.account.dao.TotalAssetDetailsDao;
import com.sifang.modules.account.handler.CustomerEntryGeExcelImportHandler;
import com.sifang.modules.customer.dao.CustomerEntryDao;
import com.sifang.modules.customer.entity.CustomerEntry;
import com.sifang.modules.order.utils.excelExt.ExcelReader;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.util.LayuiPage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TotalAssetDetailsService extends BaseService {

    @Autowired
    private TotalAssetDetailsDao  detailsDao;
    @Autowired
    private CustomerEntryDao customerDao;

    public void entryListAccount(LayuiPage<Record> page, Map<String, String> map, Operator operator) {
        page.setData(detailsDao.entryListAccount(page, map, operator));
        page.setCount(detailsDao.countEntryListAccount(map, operator));
    }

    @SuppressWarnings("unchecked")
    public Map<String, Object> checkImportBarcode(Map<String, String> params, InputStream in) {
        Map<String, Object> resultMap = Maps.newHashMap();
        resultMap.put("operType", "check");
        CustomerEntryGeExcelImportHandler handler = new CustomerEntryGeExcelImportHandler();
        handler.setStartSheet(1);
        handler.setStartRow(1);
        try {
            new ExcelReader().check(in, params.get("extName"), handler);
            Map<String, Object> map = (Map<String, Object>) handler.getHandlerResult();
            String errorMsg = (String) map.get("TemplateError");
            if (!handler.isCheckFlag()) {
                handler.appendErrorDetail("<p>未填充数据</p>");
            }
            String errorDetail = handler.getErrorDetail();
            if (StringUtils.isNotBlank(errorMsg)) {
                resultMap.put("errorDetail", errorMsg);
                resultMap.put("errorMessage", "y");
            } else if (StringUtils.isNotBlank(errorDetail)) {
                resultMap.put("errorDetail", errorDetail);
                resultMap.put("errorMessage", "y");
            } else if ("overLimit".equalsIgnoreCase((String) map.get("overLimit"))) {
                resultMap.put("overLimit", "y");
            } else {
                resultMap.put("pass", "y");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultMap;
    }

    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> importBarcode(Map<String, String> params, InputStream in) {
        Map<String, Object> retMap = Maps.newHashMap();
        retMap.put("operType", "import");
        CustomerEntryGeExcelImportHandler handler = new CustomerEntryGeExcelImportHandler();
        handler.setStartSheet(1);
        handler.setStartRow(2);

        try {
            new ExcelReader().readExcel(in, params.get("fileName"), handler);

            retMap.put("pass", "y");
            List<CustomerEntry> exeSqlParams = handler.getRetList();
            if (exeSqlParams != null && exeSqlParams.size() > 0) {
                importProjectContract(exeSqlParams);
            }
            retMap.put("successCount", exeSqlParams.size());//成功条数
        } catch (Exception e) {
            e.printStackTrace();
            retMap.put("pass", "no");
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return retMap;
    }

    public Result importProjectContract(List<CustomerEntry> paramList) {
        List<CustomerEntry> entryList = new ArrayList<>();
        for (CustomerEntry item : paramList) {
            CustomerEntry entry = customerDao.get(item.getId());
            entry.setEtd(item.getEtd());
            entry.setEto(item.getEto());
            entry.setEtdc(item.getEtdc());
            entry.setEtoc(item.getEtoc());
            entryList.add(entry);
        }
        customerDao.save(entryList);
        return Result.ok();
    }

}
