package com.sunxung.factoring.start.impl;

import java.util.ArrayList;
import java.util.List;

import com.sunxung.factoring.component.util.ConstantConversionUtil;
import com.sunxung.factoring.component.util.SpringUtil;
import com.sunxung.factoring.entity.sys.User;
import com.sunxung.factoring.entity.sys.UserOrgRel;
import com.sunxung.factoring.service.sys.OrgService;
import com.sunxung.factoring.service.sys.UserService;

/**
 * 用户初始化
 */
public class UserInit {

    public void run() {
        List<UserOrgRel> userOrgRelList = new ArrayList<>();
        UserOrgRel ug = new UserOrgRel(getOrgService().getByCode("cayk").getId(), false);
        userOrgRelList.add(ug);
        /** 超级管理员 **/
        User admin = new User();
        admin.setLoginName(ConstantConversionUtil.ADMINISTORIES);
        admin.setName(ConstantConversionUtil.ADMINISTORIES_DISPLAYNAME);
        admin.setPassword("admin123");
        admin.setUserOrgRelList(userOrgRelList);
        admin.setMobile("18888888888");
        admin.setEmail("admin@163.com");
        admin.setEnabled(true);
        admin.setRelationDingDing(false);
        admin.setAdmin(true);
        getUserService().add(admin);
        /** 业务超管 **/
        User businessAdmin = new User();
        businessAdmin.setLoginName(ConstantConversionUtil.BUSINESS_ADMINISTORIES);
        businessAdmin.setName(ConstantConversionUtil.BUSINESS_ADMINISTORIES_DISPLAYNAME);
        businessAdmin.setPassword("admin123");
        businessAdmin.setUserOrgRelList(userOrgRelList);
        businessAdmin.setMobile("18888888888");
        businessAdmin.setEmail("businessAdmin@163.com");
        businessAdmin.setEnabled(true);
        businessAdmin.setRelationDingDing(false);
        businessAdmin.setAdmin(false);// false:因为业务超管只具有客户营销到账后以及消息模块内所有菜单权限而已
        getUserService().add(businessAdmin);
    }

    private static OrgService orgService;

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

    private static UserService userService;

    private static UserService getUserService() {
        if (userService == null) {
            userService = SpringUtil.getBeanByType(UserService.class);
        }
        return userService;
    }
}
