package cn.com.qmth.stmms.ms.admin.utils;

import cn.com.qmth.stmms.ms.core.domain.Organization;
import cn.com.qmth.stmms.ms.core.domain.Work;
import cn.com.qmth.stmms.ms.core.domain.enums.BuiltInAccountEnum;
import cn.com.qmth.stmms.ms.core.domain.user.MarkUser;
import cn.com.qmth.stmms.ms.core.domain.user.Role;
import cn.com.qmth.stmms.ms.core.repository.MarkUserRepo;
import cn.com.qmth.stmms.ms.core.repository.OrganizationRepo;
import cn.com.qmth.stmms.ms.core.repository.WorkRepo;
import cn.com.qmth.stmms.ms.core.vo.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 根据机构生成账号纪检
 */
@Component
public class BultInAccountUtil {

    @Autowired
    MarkUserRepo markUserRepo;

    @Autowired
    OrganizationRepo organizationRepo;

    @Autowired
    RandomUtils randomUtils;

    public void createAccout(Work work) {
        List<MarkUser> userList = new ArrayList<>();

        Organization organization = organizationRepo.findOne(work.getOrganizationId());
        if (organization == null) {
            throw new RuntimeException("机构不存在");
        }
        List<Map> mapList = BuiltInAccountEnum.listAccounts();
        if (mapList.size() > 0) {
            for (Map map : mapList) {
                //采集账号
                if (Objects.equals(BuiltInAccountEnum.SCAN.name(), map.get("type").toString())) {
                    for (int i = 1; i <= 10; i++) {
                        StringJoiner stringJoiner = new StringJoiner("-");
                        String tempName = map.get("code").toString().toLowerCase() + lpad(2, i);
                        stringJoiner.add(organization.getAbbreviation().toLowerCase()).add(work.getId().toString()).add(tempName);
                        String loginName = stringJoiner.toString();
                        MarkUser markUser = markUserRepo.findByLoginName(loginName);
                        if (markUser != null) {
                            continue;
                        }
                        //默认密码123456
                        String password = randomUtils.alphanumericPassword();
                        markUser = new MarkUser(loginName, password, work.getId(), Subject.SC, loginName, Role.findByName((String) map.get("name")), null);
                        markUser.setOrganizationId(organization.getId());
                        //默认为禁用
                        markUser.setEnabled(false);
                        userList.add(markUser);
                    }
                } else {
                    StringJoiner stringJoiner = new StringJoiner("-");
                    String tempName = map.get("code").toString().toLowerCase();
                    stringJoiner.add(organization.getAbbreviation().toLowerCase()).add(tempName);
                    String loginName = stringJoiner.toString();
                    MarkUser markUser = markUserRepo.findByLoginName(loginName.toLowerCase());
                    if (markUser != null) {
                        continue;
                    }
                    String password = randomUtils.alphanumericPassword();
                    markUser = new MarkUser(loginName, password, 0l, Subject.SC, loginName, Role.findByName((String) map.get("name")), null);
                    markUser.setOrganizationId(organization.getId());
                    userList.add(markUser);
                }
            }
            markUserRepo.save(userList);
        }
    }

    /**
     * 补齐不足长度
     *
     * @param length 长度
     * @param number 数字
     * @return
     */
    private static String lpad(int length, int number) {
        String f = "%0" + length + "d";
        return String.format(f, number);
    }
}
