package com.zsc.edu.gateway;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsc.edu.gateway.modules.system.entity.Authority;
import com.zsc.edu.gateway.modules.system.entity.Dept;
import com.zsc.edu.gateway.modules.system.entity.Role;
import com.zsc.edu.gateway.modules.system.entity.User;
import com.zsc.edu.gateway.modules.system.repo.DeptRepository;
import com.zsc.edu.gateway.modules.system.repo.UserRepository;
import com.zsc.edu.gateway.modules.system.service.AuthorityService;
import com.zsc.edu.gateway.modules.system.service.DeptService;
import com.zsc.edu.gateway.modules.system.service.RoleService;
import com.zsc.edu.gateway.modules.system.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

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

/**
 * 系统初始化程序
 *
 * @author harry_yao
 */
@AllArgsConstructor
@Component
@Profile("!test")
public class FirstTimeInitializer implements CommandLineRunner {

    private final AuthorityService authorityService;
    private final UserService userService;
    private final RoleService roleService;
    private final DeptService deptService;
    private final DeptRepository deptRepo;
    private final PasswordEncoder passwordEncoder;

    //TODO 初始化赋权给admin
    @Override
    public void run(String... args) {
//        if (authorityService.count() == 0L) {
//            Authority userPerm = new Authority(null, "用户模块", "用户管理", "SYSTEM:USER", true, null);
//            Authority rolePerm = new Authority(null, "角色模块", "角色管理", "SYSTEM:ROLE", true, null);
//            Authority deptPerm = new Authority(null, "部门模块", "部门管理", "SYSTEM:DEPT", true, null);
//            Authority AuthorityPerm = new Authority(null, "权限模块", "权限管理", "SYSTEM:AUTHORITY", true, null);
//            authorityService.saveBatch(List.of(userPerm, rolePerm, deptPerm, AuthorityPerm));
//            List<Authority> authorities = new ArrayList<>();
//            authorities.add(new Authority(userPerm.getId(), "用户管理", "用户列表", "SYSTEM:USER:QUERY", true, null));
//            authorities.add(new Authority(userPerm.getId(), "用户管理", "用户新增", "SYSTEM:USER:CREATE",true, null));
//            authorities.add(new Authority(userPerm.getId(), "用户管理", "用户修改", "SYSTEM:USER:UPDATE",true, null));
//            authorities.add(new Authority(userPerm.getId(), "用户管理", "用户删除", "SYSTEM:USER:DELETE",true, null));
//            authorities.add(new Authority(rolePerm.getId(), "角色管理", "角色列表", "SYSTEM:ROLE:QUERY", true, null));
//            authorities.add(new Authority(rolePerm.getId(), "角色管理", "角色新增", "SYSTEM:ROLE:CREATE",true, null));
//            authorities.add(new Authority(rolePerm.getId(), "角色管理", "角色修改", "SYSTEM:ROLE:UPDATE",true, null));
//            authorities.add(new Authority(rolePerm.getId(), "角色管理", "角色删除", "SYSTEM:ROLE:DELETE",true, null));
//            authorities.add(new Authority(deptPerm.getId(), "部门管理", "部门列表", "SYSTEM:DEPT:QUERY", true, null));
//            authorities.add(new Authority(deptPerm.getId(), "部门管理", "部门新增", "SYSTEM:DEPT:CREATE",true, null));
//            authorities.add(new Authority(deptPerm.getId(), "部门管理", "部门修改", "SYSTEM:DEPT:UPDATE",true, null));
//            authorities.add(new Authority(deptPerm.getId(), "部门管理", "部门删除", "SYSTEM:DEPT:DELETE",true, null));
//            authorities.add(new Authority(AuthorityPerm.getId(), "权限管理", "权限列表", "SYSTEM:AUTHORITY:QUERY", true, null));
//            authorities.add(new Authority(AuthorityPerm.getId(), "权限管理", "权限新增", "SYSTEM:AUTHORITY:CREATE",true, null));
//            authorities.add(new Authority(AuthorityPerm.getId(), "权限管理", "权限修改", "SYSTEM:AUTHORITY:UPDATE",true, null));
//            authorities.add(new Authority(AuthorityPerm.getId(), "权限管理", "权限删除", "SYSTEM:AUTHORITY:DELETE",true, null));
//            authorityService.saveBatch(authorities);
//        }
        if (roleService.count() == 0L) {
            Role admin = new Role();
            admin.setName("管理员");
            admin.setEnabled(true);
            roleService.save(admin);
        }
        if (deptService.count() == 0L) {
            Dept dept = new Dept();
            dept.setName("总公司");
            deptService.save(dept);
        }
        if (userService.count() == 0L) {
            Dept dept = deptService.getOne(new QueryWrapper<>());
            Role role = roleService.getOne(new QueryWrapper<>());
            User user = new User();
            user.setUsername("admin");
            user.setPassword(passwordEncoder.encode("admin"));
            user.setPhone("15913375741");
            user.setEmail("admin@zsc.edu.cn");
            user.setName("admin");
            user.setRoleId(role.getId());
            user.setDeptId(dept.getId());
            userService.save(user);
        }

    }
}
