package com.imdroid.beidou.service;


import com.imdroid.beidou.common.Role;
import com.imdroid.beidou.entity.Tenant;
import com.imdroid.beidou.entity.TenantMapper;
import com.imdroid.beidou.entity.User;
import com.imdroid.beidou.entity.UserMapper;
import com.imdroid.beidou.utils.BCryptPasswordEncoderUtil;
import com.imdroid.secapi.dto.WarningCfg;
import com.imdroid.secapi.dto.WarningCfgMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * springboot启动完成后，立即进行业务初始化
 */

@Slf4j
@Component
public class ServiceEnvInit implements ApplicationRunner {
    @Value("${default_user_name}")
    private String default_user_name;
    @Value("${default_pwd}")
    private String default_pwd;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TenantMapper tenantMapper;
    @Autowired
    private WarningCfgMapper warningCfgMapper;

    @Autowired
    private BCryptPasswordEncoderUtil bCryptPasswordEncoderUtil;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        if (tenantMapper.selectById(0) == null) {
            createDefaultTenant();
        }
        //如果用户表为空，自动创建admin，初始密码admin123
        if (userMapper.selectCount(null) == 0) {
            createDefualtUser();
        }

        if(warningCfgMapper.selectCount(null) == 0){
            createDefaultWarningCfg();
        }
    }

    private void createDefualtUser() {
        log.info("create default user");
        User user = new User();
        user.setId(0L);
        user.setName(default_user_name);
        user.setPwd(bCryptPasswordEncoderUtil.encode(default_pwd));
        user.setRole(Role.SUPER_ADMIN);
        user.setTenantid(Tenant.SAAS_PROVIDER_ID);
        user.setTenantname(Tenant.SAAS_PROVIDER_NAME);
        userMapper.insert(user);
    }

    private void createDefaultTenant() {
        log.info("create default tenant");
        Tenant tenant = new Tenant();
        tenant.setId(Tenant.SAAS_PROVIDER_ID);
        tenant.setName(Tenant.SAAS_PROVIDER_NAME);
        tenantMapper.insert(tenant);
    }

    private void createDefaultWarningCfg() {
        WarningCfg warningCfg = new WarningCfg();
        warningCfg.setDevicetype(WarningCfg.TYPE_GNSS);
        warningCfg.setLevel(WarningCfg.LEVEL_2);
        warningCfg.setType(WarningCfg.TYPE_LOW_VOLTAGE);
        warningCfg.setTypename(WarningCfg.TYPE_NAME_LOW_VOLTAGE);
        warningCfg.setValue(9500);
        warningCfgMapper.insert(warningCfg);

        warningCfg = new WarningCfg();
        warningCfg.setDevicetype(WarningCfg.TYPE_GNSS);
        warningCfg.setLevel(WarningCfg.LEVEL_2);
        warningCfg.setType(WarningCfg.TYPE_DEVICE_OFF_LINE);
        warningCfg.setTypename(WarningCfg.TYPE_NAME_DEVICE_OFF_LINE);
        warningCfg.setValue(0);
        warningCfgMapper.insert(warningCfg);
    }
}