package cn.iocoder.yudao.module.system.service.face;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.system.controller.app.face.dto.QueryDto;
import cn.iocoder.yudao.module.system.dal.dataobject.face.UserFace;
import cn.iocoder.yudao.module.system.dal.mysql.face.FaceMapper;
import cn.iocoder.yudao.module.system.service.deviceinfo.DeviceInfoService;
import cn.iocoder.yudao.module.system.service.facedeviceinfo.FaceDeviceInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class FaceService extends ServiceImpl<FaceMapper, UserFace> implements IService<UserFace> {

    @Autowired
    private FaceMapper mapper;
    @Resource
    private HttpServletRequest httpServletRequest;
    @Resource
    private FaceDeviceInfoService faceDeviceInfoService;
    //注册人脸
    public void faceAdd(UserFace user) {
        mapper.add(user);
    }
    //查找人脸
    public List<UserFace> getAllUserFace() {
        return mapper.getAllUser();
    }

    public UserFace getUserById(Long userId) {
        return mapper.getUserById(userId);
    }

    public Page<UserFace> getList(QueryDto dto) {

        String authorization = httpServletRequest.getHeader("Authorization");
        Long carteenId = faceDeviceInfoService.getInfo(authorization).getCarteenId();
        Page<UserFace> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<UserFace> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(dto.getPhone()), UserFace::getPhone, dto.getPhone());
        wrapper.and(w -> w.eq(UserFace::getCarteenId,carteenId)
                .or().isNull(UserFace::getCarteenId));
        wrapper.orderByDesc(UserFace::getCreateTime);
        return mapper.selectPage(page, wrapper);
    }

    public Map<String,String> getMemberUserById(Long id) {
        return mapper.getMemberUserById(id);
    }

}
