package com.liquidnet.client.admin.zhengzai.smile.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liquidnet.client.admin.zhengzai.smile.service.ISmileUserService;
import com.liquidnet.client.admin.zhengzai.smile.vo.SmileUserVo;
import com.liquidnet.client.admin.zhengzai.smile.vo.SmileUserVoOrg;
import com.liquidnet.service.smile.entity.SmileUser;
import com.liquidnet.service.smile.mapper.SmileUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
@SuppressWarnings("all")
public class SmileUserServiceImpl extends ServiceImpl<SmileUserMapper, SmileUser> implements ISmileUserService {

    @Autowired
    SmileUserMapper smileUserMapper;

    @Override
    public List<SmileUserVo> userList() {
        //查询未删除，未通过审核的用户
        LambdaQueryWrapper<SmileUser> queryWrapper = Wrappers.lambdaQuery(SmileUser.class);
        queryWrapper.eq(SmileUser::getDelTag,1);
        queryWrapper.eq(false,SmileUser::getType,0);
        List<SmileUser> smileUsers = smileUserMapper.selectList(queryWrapper);
        return smileUsers.stream().map(smileUser -> {
            SmileUserVo smileUserVo = new SmileUserVo();
            BeanUtils.copyProperties(smileUser, smileUserVo);
            return smileUserVo;
        }).collect(Collectors.toList());
    }

    @Override
    public SmileUser getUserById(Long uid) {
        log.info("查询用户id:{}",uid);
        //查询未删除，未通过审核的用户,id=uid
        LambdaQueryWrapper<SmileUser> queryWrapper = Wrappers.lambdaQuery(SmileUser.class);
        queryWrapper.eq(SmileUser::getDelTag,1);
        queryWrapper.eq(false,SmileUser::getType,0);
        queryWrapper.eq(SmileUser::getId,uid);
        SmileUser smileUser = smileUserMapper.selectById(queryWrapper);
        log.info("smileUser:{}",smileUser);
        return smileUser;
    }

    @Override
    public int create(SmileUser smileUser) {
        smileUser.setCreatedDate(LocalDateTime.now());
        smileUser.setDelTag(1);
        log.info("smileUser:{}",smileUser);
        int id = smileUserMapper.insert(smileUser);
        log.info("return id:{}.",id);
        return id;
    }

    @Override
    public List<SmileUserVoOrg> getUserListByOrgId(Long orgId,Long agentId) {
        //查询根据组织ID查询人员信息
        LambdaQueryWrapper<SmileUser> queryWrapper = Wrappers.lambdaQuery(SmileUser.class);
        queryWrapper.eq(SmileUser::getOrgId,orgId);
        List<SmileUserVoOrg> smileUserVos = smileUserMapper.selectList(queryWrapper).stream().map(smileUser -> {
            SmileUserVoOrg smileUserVo = new SmileUserVoOrg();
            BeanUtils.copyProperties(smileUser, smileUserVo);

            return smileUserVo;
        }).collect(Collectors.toList());
        return smileUserVos;
    }

    @Override
    public SmileUserVo getUserByPhone(String phone) {
        log.info("getUserByPhone phone:{}",phone);
        LambdaQueryWrapper<SmileUser> queryWrapper = Wrappers.lambdaQuery(SmileUser.class);
        queryWrapper.eq(SmileUser::getPhone,phone);
        SmileUser smileUser = smileUserMapper.selectOne(queryWrapper);
        SmileUserVo userVo = new SmileUserVo();
        BeanUtils.copyProperties(smileUser, userVo);
        return userVo;
    }

}