package com.dcsoft.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.dcsoft.common.security.utils.SecurityUtils;
import com.dcsoft.system.api.model.LoginUser;
import com.dcsoft.system.domain.SysEqDownRecord;
import com.dcsoft.system.domain.SysPeopleEqDown;
import com.dcsoft.system.mapper.SysEqDownRecordMapper;
import com.dcsoft.system.service.ISysEqDownRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import static com.dcsoft.common.core.utils.PageUtils.startPage;

@Service
public class SysEqDownRecordServiceImpl implements ISysEqDownRecordService {

    @Autowired
    private SysEqDownRecordMapper sysEqDownRecordMapper;

    @Override
    public List<SysEqDownRecord> pageList(SysEqDownRecord sysEqDownRecord) {

        startPage();
        return sysEqDownRecordMapper.selectList(sysEqDownRecord);
    }

    @Override
    public void saveBatch(List<SysEqDownRecord> downRecordList) {
        LoginUser user = SecurityUtils.getLoginUser();
        for (SysEqDownRecord sysEqDownRecord : downRecordList) {
            sysEqDownRecord.setCreateTime(new Date());
            //人脸采集h5登录没有token，获取不到登录信息
            sysEqDownRecord.setCreateBy(null == user ? sysEqDownRecord.getPeopleId().toString() : user.getUsername());
            if (null == sysEqDownRecord.getFaceDownResult()) {
                sysEqDownRecord.setFaceDownResult(1);
            }
        }
        sysEqDownRecordMapper.insertBatch(downRecordList);
    }

    @Override
    public List<SysPeopleEqDown> selectListByIds(List<Long> ids, List<Long> peopleIds) {
        return sysEqDownRecordMapper.selectListByIds(ids, peopleIds);
    }

    @Override
    public void deleteByPeopleIds(List<Long> peopleIds) {
        if (CollUtil.isEmpty(peopleIds)) {
            return;
        }
        sysEqDownRecordMapper.deleteByPeopleIds(peopleIds);
    }
}
