package com.seeyon.apps.qrCodeForm.util;

import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.privilege.bo.PrivMenuBO;
import com.seeyon.ctp.privilege.enums.PrivMenuTypeEnums;
import com.seeyon.ctp.privilege.manager.PrivilegeMenuManager;
import com.seeyon.ctp.privilege.po.PrivMenu;
import com.seeyon.ctp.util.DBAgent;
import com.seeyon.ctp.util.DateUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class InitQrCodeFormUitl {
    private static Log log = LogFactory.getLog(InitQrCodeFormUitl.class);
    private static PrivilegeMenuManager privilegeMenuManager = (PrivilegeMenuManager) AppContext.getBean("privilegeMenuManager");

    public static void initMenu() {
        try {
            List<PrivMenu> listmenu = new ArrayList<PrivMenu>();

            StringBuffer queryString = new StringBuffer(" FROM ");
            queryString.append(PrivMenu.class.getSimpleName()).append(" where ")
                    .append(" path = '711' ");

            listmenu = DBAgent.find(queryString.toString());
            if (listmenu == null || listmenu.size() < 1) {
                creatFileImportMenu();//创建菜单资源
            }
            log.info("模板二维码菜单完成");

        } catch (Exception e) {
            log.error("模板二维码菜单失败", e);
        }

    }

    /**
     * 配置菜单
     *
     * @return
     * @throws BusinessException
     */
    private static void creatFileImportMenu() {
        try {
            List<PrivMenu> menus = new ArrayList<PrivMenu>();
            PrivMenu configmenu = new PrivMenu();
            configmenu.setNewId();
            configmenu.setName("模板二维码");
            configmenu.setIcon("conferencearrangemen.png");
            configmenu.setSortid(1);
            configmenu.setPath("711");
            configmenu.setExt1("0");
            configmenu.setExt2("1");
            configmenu.setExt3("");
            configmenu.setExt4(1);
            configmenu.setExt7(configmenu.getName());
            configmenu.setExt8("");
            configmenu.setExt9("");
            configmenu.setExt10("");
            configmenu.setExt11("");
            configmenu.setExt12(0);
            configmenu.setExt13(0);
            configmenu.setExt14(0);
            configmenu.setExt15(0);
            configmenu.setExt16(0);
            Date nowDate = new Date();
            configmenu.setCreatedate(nowDate);
            configmenu.setUpdatedate(nowDate);
            configmenu.setType(PrivMenuTypeEnums.systemPresetMenu.getKey());
            configmenu.setParentId(0L);
            menus.add(configmenu);

            PrivMenu syncConfig = new PrivMenu();//二维码生成
            syncConfig.setNewId();
            syncConfig.setName("二维码生成");
            syncConfig.setIcon("b_menu_thridpartyspace.png");
            syncConfig.setTarget("mainfrm");
            syncConfig.setSortid(1);
            syncConfig.setPath("711001");
            syncConfig.setExt1("0");
            syncConfig.setExt2("2");
            syncConfig.setExt3("");
            syncConfig.setExt4(1);
            syncConfig.setExt7(syncConfig.getName());
            syncConfig.setExt8("0");
            syncConfig.setExt9("0");
            syncConfig.setExt10("0");
            syncConfig.setExt11("1");
            syncConfig.setExt19(2L);
            syncConfig.setEnterResource(1);
            syncConfig.setResourceNavurl("/qrCodeForm.do?method=index");
            syncConfig.setResourceCode("qrCodeForm");
            syncConfig.setResourceModuleid("none");
            syncConfig.setCreatedate(nowDate);
            syncConfig.setUpdatedate(nowDate);
            syncConfig.setUpdateuserid(1L);
            syncConfig.setShow(true);
            syncConfig.setControl(true);
            syncConfig.setType(PrivMenuTypeEnums.systemPresetMenu.getKey());
            syncConfig.setParentId(configmenu.getId());
            menus.add(syncConfig);

            DBAgent.saveAll(menus);
        } catch (Exception e) {
            log.error("组织机构同步菜单异常", e);
        }
    }

}
