package com.sunxung.factoring.start.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.core.io.ClassPathResource;

import com.sunxung.factoring.component.util.SpringUtil;
import com.sunxung.factoring.component.util.StringUtil;
import com.sunxung.factoring.entity.sys.Org;
import com.sunxung.factoring.service.sys.OrgService;

/**
 * 组织结构字典项初始化
 */
public class OrgInit {

    private static Logger log = LogManager.getLogger(OrgInit.class);

    public void run() {
        List<Org> orgList = null;
        try {
            orgList = findOrgFromExcel();
        } catch (Exception e) {
            log.error(e);
        }
        if (orgList == null || orgList.isEmpty()) {
            return;
        }
        for (Org org : orgList) {
            if (org.getParent() != null && !StringUtil.isEmpty(org.getParent().getDeptNo())) {
                org.setParent(getOrgService().getByCode(org.getParent().getDeptNo()));
            } else {
                org.setParent(new Org());
                org.setDepartmentId(1L);
            }
            getOrgService().add(org);
        }
    }

    private List<Org> findOrgFromExcel() throws Exception {
        Workbook workbook = null;
        Set<String> codeSet = new HashSet<String>();
        List<Org> orgList = new ArrayList<Org>();
        try {
            workbook = WorkbookFactory.create(new ClassPathResource("org.xlsx").getInputStream());
            Sheet sheet = workbook.getSheetAt(0);
            for (int i = 1, len1 = sheet.getLastRowNum(); i <= len1; i++) {
                Row row = sheet.getRow(i);
                if (row == null) {
                    continue;
                }
                for (int j = 0, len2 = row.getLastCellNum(); j < len2; j += 3) {
                    if (codeSet.contains(row.getCell(j).toString())) {
                        continue;
                    }
                    codeSet.add(row.getCell(j).toString());
                    Org org = new Org();
                    org.setName(row.getCell(j).toString());
                    // org.setOrgType(getDictionary(row.getCell(j + 1).toString()));
                    org.setDeptNo(row.getCell(j + 1).toString());
                    if (j != 0) {
                        Org parentOrg = new Org();
                        parentOrg.setDeptNo(row.getCell(j - 1).toString());
                        org.setParent(parentOrg);
                    }
                    orgList.add(org);
                }
            }
        } finally {
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (IOException e) {
                }
            }
        }
        return orgList;
    }

    private static OrgService orgService;

    private static OrgService getOrgService() {
        if (orgService == null) {
            orgService = SpringUtil.getBeanByType(OrgService.class);
        }
        return orgService;
    }
}
