package com.jd.lightapplication.service;

import cn.hutool.core.date.DateUtil;
import com.jd.lightapplication.model.MeetingPersonnel;
import com.jd.lightapplication.mapper.MeetingPersonnelMapper;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jd.lightapplication.model.SysUser;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
@AllArgsConstructor
public class MeetingPersonnelService extends ServiceImpl<MeetingPersonnelMapper, MeetingPersonnel> {

    private SysUserService sysUserService;

    /**
     * 参会人员添加账号
     * @param meetingPersonnelList
     */
    public void addAccount(List<MeetingPersonnel> meetingPersonnelList) {
        //先把参会人员映射成用户bean集合
        List<SysUser> sysUserList = meetingPersonnelList.stream().map(t -> {
            SysUser sysUser = new SysUser();
            sysUser.setPhone(t.getPhone())
                    .setUsernameShow(t.getName())
                    .setStatu(1)
                    .setCreated(DateUtil.now());
            return sysUser;
        }).collect(Collectors.toList());
        //再循环添加用户账号信息
        sysUserList.forEach(t -> {
            try {
                sysUserService.save(t);
            } catch (Exception e) {
                log.warn("用户添加失败，检查到重复的手机号：" + t.getPhone());
                log.warn(e.getMessage());
            }
        });
    }

}