package com.ningdatech.carapi.user;

import java.io.*;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.carapi.AppTests;
import com.ningdatech.carapi.common.constant.UserRoleTypeEnum;
import com.ningdatech.carapi.sys.entity.Company;
import com.ningdatech.carapi.sys.entity.UserRole;
import com.ningdatech.carapi.sys.service.CompanyService;
import com.ningdatech.carapi.sys.service.UserRoleService;
import com.ningdatech.carapi.user.entity.NdUserAuth;
import com.ningdatech.carapi.user.entity.NdUserInfo;
import com.ningdatech.carapi.user.security.auth.constant.AuthTypeEnum;
import com.ningdatech.carapi.user.service.INdUserAuthService;
import com.ningdatech.carapi.user.service.INdUserInfoService;

/**
 * @Classname InsureOutdangerTest
 * @Description
 * @Date 2022/12/22 10:47
 * @Created by PoffyZhang
 */
public class UserInfoTest extends AppTests {

    @Autowired
    private  CompanyService companyService;
    @Autowired
    private INdUserInfoService userInfoService;
    @Autowired
    private INdUserAuthService userAuthService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private UserRoleService userRoleService;

    @Test
    public void test() throws FileNotFoundException {
        File f = new File("C:\\Users\\CMM\\Desktop\\年全省预拌混凝土、砂浆企业基本情况调查表2\\账号.xlsx");
        Workbook wb = readExcel(new FileInputStream(f),f.getName());
        Row row = null;
        if(wb != null){
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);
            //获取最大列数
            for (int i = 1; i<rownum; i++) {
                row = sheet.getRow(i);

                // 用户名
                String userName = Objects.nonNull(row.getCell(1)) ? row.getCell(1).toString() : StringUtils.EMPTY;
                // 登录账号
                String account = Objects.nonNull(row.getCell(2)) ? row.getCell(2).toString() : StringUtils.EMPTY;
                // 登录密码
                String password = Objects.nonNull(row.getCell(3)) ? row.getCell(3).toString() : StringUtils.EMPTY;

                System.out.println(userName + "," + account + "," + password);

                Company company = companyService.getOne(Wrappers.lambdaQuery(Company.class).eq(Company::getName, userName).orderByDesc(Company::getCreateOn).last("limit 1"));
                // 保存用户信息
                NdUserInfo userInfo = new NdUserInfo();
                userInfo.setUsername(userName);
                userInfo.setCompanyId(company.getId());
                userInfo.setCreateOn(LocalDateTime.now());
                userInfo.setUpdateOn(LocalDateTime.now());
                userInfoService.save(userInfo);
                // 保存登录账号、密码
                NdUserAuth userAuth = new NdUserAuth();
                userAuth.setUserId(userInfo.getId());
                // 设置账号
                userAuth.setIdentifier(account);
                // 设置密码
                userAuth.setCredential(passwordEncoder.encode(password));
                userAuth.setAuthType(AuthTypeEnum.ACCOUNT_PASSWORD.getKey());
                userAuthService.save(userAuth);

                UserRole userRole = new UserRole();
                userRole.setUserId(userInfo.getId());
                userRole.setRoleId(UserRoleTypeEnum.COMPANY_INFO_OFFICER_ROLE_ID.getId());
                userRoleService.save(userRole);

                System.out.println("准备插入 " + i  + "条:" + JSON.toJSONString(userAuth));
            }
        }
    }

    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
    }

    //这个下面是单独函数
    public static Workbook readExcel(InputStream is, String extString){
        Workbook wb = null;
        extString = extString.substring(extString.lastIndexOf("."));
        try {
            if(".xls".equals(extString)){
                return wb = new HSSFWorkbook(is);
            }else if(".xlsx".equals(extString)){
                return wb = new XSSFWorkbook(is);
            }else {
                return wb = null;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }
}
