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.AccessCardPushRecordsDto;
import com.sptg.estate.api.entity.AccessCardPushRecords;
import com.sptg.estate.biz.mapper.AccessCardPushRecordsMapper;
import com.sptg.estate.biz.service.AccessCardPushRecordsServiceInterface;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

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

    @Override
    public ResultBean<Page<AccessCardPushRecords>> showAccessCardPushRecordsList(AccessCardPushRecordsDto accessPushRecordsDto) {
        Wrapper<AccessCardPushRecords> wrapper = new EntityWrapper<>();
        //根据机构
        //根据下发时间日期
        if(null != accessPushRecordsDto.getBeginDate()) {
            wrapper.ge("t_access_date",accessPushRecordsDto.getBeginDate());
        }
        if(null != accessPushRecordsDto.getEndDate()) {
            wrapper.le("t_access_date",accessPushRecordsDto.getEndDate());
        }
        //根据开门卡号
        if(StringUtils.isNotBlank(accessPushRecordsDto.getCardNo())){
            wrapper.like("s_card_no",accessPushRecordsDto.getCardNo());
        }
        //排序字段 时间排序
        wrapper.orderBy("t_access_date",false);
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectPage(accessPushRecordsDto.getPage(), wrapper));
    }

    @Override
    public AccessCardPushRecords findAccessCardPushRecordsById(Long id) {
        return this.selectById(id);
    }

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

    @Override
    public ResultBean<Boolean> addAccessCardPushRecords(AccessCardPushRecords accessPushRecords) {
        boolean insert = this.insert(accessPushRecords);
        return new ResultBean<>(ResultEnum.SUCCESS, insert);
    }

    @Override
    public ResultBean<Boolean> updateAccessCardPushRecords(AccessCardPushRecordsDto accessPushRecordsDto) {
        return null;
    }

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

    @Override
    public ResultBean<Boolean> deleteAccessCardPushRecords(AccessCardPushRecordsDto accessPushRecordsDto) {
        return null;
    }

    @Override
    public Long getAccessCardPushRecordsCount(AccessCardPushRecordsDto accessPushRecordsDto){
        EntityWrapper<AccessCardPushRecords> wrapper=new EntityWrapper<AccessCardPushRecords>();
        wrapper.setSqlSelect("COUNT(l_id)");

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

    @Override
    public ResultBean<AccessCardPushRecords> getListByUserId(Long userId, Integer accessUserType, Integer specialDevice) {
        Wrapper<AccessCardPushRecords> 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));
    }
}
