package com.sptg.estate.biz.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.estate.api.dto.AccessFacePushRecordsDto;
import com.sptg.estate.api.entity.AccessFacePushRecords;
import com.sptg.estate.biz.mapper.AccessFacePushRecordsMapper;
import com.sptg.estate.biz.service.AccessFacePushRecordsServiceInterface;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * title: AccessFacePushRecordsServiceImpl
 * package: com.sptg.estate.biz.security.impl
 * description: 出入记录 服务层实现类
 *
 *
 *
 */
@Service
@RequiredArgsConstructor
public class AccessFacePushRecordsServiceImpl extends ServiceImpl<AccessFacePushRecordsMapper, AccessFacePushRecords> implements AccessFacePushRecordsServiceInterface {

    @Override
    public ResultBean<Page<AccessFacePushRecords>> showAccessFacePushRecordsList(AccessFacePushRecordsDto accessFacePushRecordsDto) {
        Wrapper<AccessFacePushRecords> wrapper = new EntityWrapper<>();
        //根据机构
        //根据下发时间日期
        if(null != accessFacePushRecordsDto.getBeginDate()) {
            wrapper.ge("t_access_date",accessFacePushRecordsDto.getBeginDate());
        }
        if(null != accessFacePushRecordsDto.getEndDate()) {
            wrapper.le("t_access_date",accessFacePushRecordsDto.getEndDate());
        }
        //排序字段 时间排序
        wrapper.orderBy("t_access_date",false);
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectPage(accessFacePushRecordsDto.getPage(), wrapper));
    }

    @Override
    public AccessFacePushRecords findAccessFacePushRecordsById(Long id) {
        return this.selectById(id);
    }

    @Override
    public List<AccessFacePushRecords> findAccessFacePushRecordsByUserId(Long userId) {
        Wrapper<AccessFacePushRecords> wrapper = new EntityWrapper<>();
        wrapper.eq("l_user_id", userId);
        return this.selectList(wrapper);
    }

    @Override
    public ResultBean<Boolean> addAccessFacePushRecords(AccessFacePushRecords accessFacePushRecords) {
        boolean insert = this.insert(accessFacePushRecords);
        return new ResultBean<>(ResultEnum.SUCCESS, insert);
    }

    @Override
    public ResultBean<Boolean> updateAccessFacePushRecords(AccessFacePushRecordsDto accessFacePushRecordsDto) {
        return null;
    }

    @Override
    public ResultBean<Boolean> updateAccessFacePushRecords(List<AccessFacePushRecords> accessFacePushRecordsList) {
        Assert.notEmpty(accessFacePushRecordsList, "没有需要更新的记录");
        if(this.updateBatchById(accessFacePushRecordsList)){
            return new ResultBean<>(ResultEnum.SUCCESS, true);
        }
        return new ResultBean<>(ResultEnum.SUCCESS, false);
    }

    @Override
    public ResultBean<Boolean> deleteAccessFacePushRecords(AccessFacePushRecordsDto accessFacePushRecordsDto) {
        return null;
    }

    @Override
    public Long getAccessFacePushRecordsCount(AccessFacePushRecordsDto accessFacePushRecordsDto){
        EntityWrapper<AccessFacePushRecords> wrapper=new EntityWrapper<>();
        wrapper.setSqlSelect("COUNT(l_id)");

        //查询某月数据
        if(accessFacePushRecordsDto.getUserId()!=null){
            wrapper.eq("l_user_id", accessFacePushRecordsDto.getUserId());
        }
        return (Long) this.selectObj(wrapper);
    }

    @Override
    public ResultBean<AccessFacePushRecords> getListByUserId(Long userId, Integer accessUserType, Integer specialDevice) {
        Wrapper<AccessFacePushRecords> wrapper = new EntityWrapper<>();
        wrapper.orderBy("l_id", false);
        wrapper.eq("l_user_id", userId);
        wrapper.eq("i_access_user_type", accessUserType);
        if(specialDevice != null){
            if(specialDevice.equals(OperationConstant.ONE) || specialDevice.equals(OperationConstant.TWO)){
                wrapper.eq("i_send_device_type", specialDevice);
            }
        }
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectList(wrapper));
    }
}
