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.estate.api.dto.GuestRecordDto;
import com.sptg.estate.api.entity.GuestRecord;
import com.sptg.estate.biz.mapper.GuestRecordMapper;
import com.sptg.estate.biz.service.GuestRecordServiceInterface;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * title: GuestRecordServiceImpl
 * package: com.sptg.estate.biz.security.impl
 * description: 访客登记信息记录 服务层实现类
 *
 *
 *
 */
@Service
public class GuestRecordServiceImpl extends ServiceImpl<GuestRecordMapper, GuestRecord> implements GuestRecordServiceInterface {

    @Override
    public Page<GuestRecord> showGuestRecordList(GuestRecordDto guestRecordDto) {
        Wrapper<GuestRecord> wrapper = new EntityWrapper<>();
        if(guestRecordDto.getId() != null){
            wrapper.eq("l_id",guestRecordDto.getId());
        }

        if(guestRecordDto.getUserIds() != null){
            wrapper.in("l_user_id",guestRecordDto.getUserIds());
        }

        if(guestRecordDto.getAuditStatus() != null){
            wrapper.eq("i_audit_status",guestRecordDto.getAuditStatus());
        }

        if(guestRecordDto.getGuestName()!= null){
            wrapper.like("s_guest_name",guestRecordDto.getGuestName());
        }
        if(guestRecordDto.getGuestTel()!= null){
            wrapper.like("s_guest_tel",guestRecordDto.getGuestTel());
        }
        wrapper.orderBy("t_create_date",false);
        return this.selectPage(guestRecordDto.getPage(),wrapper);
    }

    @Override
    public ResultBean<GuestRecord> addGuestRecord(GuestRecord guestRecord) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            guestRecord.setCreateDate(df.parse(df.format(new Date())));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        boolean insert = this.insert(guestRecord);
        if(insert){
            return new ResultBean<>(ResultEnum.SUCCESS,guestRecord);
        }else {
            return new ResultBean<>(ResultEnum.ERROR);
        }

    }

    @Override
    public GuestRecord findGuestRecordById(Long id) {
        return this.selectById(id);
    }

    @Override
    public ResultBean<Boolean> updateGuestRecord(GuestRecord guestRecord) {
        return new ResultBean<>(ResultEnum.SUCCESS,this.updateById(guestRecord));
    }

    @Override
    public ResultBean<Boolean> deleteGuestRecord(GuestRecordDto guestRecordDto) {
        return new ResultBean<>(ResultEnum.SUCCESS,this.deleteById(guestRecordDto.getId()));
    }



    @Override
    public ResultBean<GuestRecord> getListByUserId(List<Long> userIds) {
        //获取当前时间的前一个月的当天
//        Calendar c = Calendar.getInstance();
//        c.add(Calendar.MONTH, -1);
//        Date date = c.getTime();
        Wrapper<GuestRecord> wrapper = new EntityWrapper<>();
        wrapper.orderBy("l_id", false);
//        wrapper.eq("i_enable_type",OperationConstant.ZERO);
        //只查询最近一个月内的密码
//        wrapper.ge("t_create_date",date);
        //用户id
        wrapper.in("l_user_id", userIds);
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectList(wrapper));
    }

    @Override
    public List<GuestRecord> findByCondition(GuestRecord guestRecord) {
        Wrapper<GuestRecord> wrapper = new EntityWrapper<>();
        if(guestRecord.getGuestAuthId() != null){
            wrapper.eq("l_guest_auth_id",guestRecord.getGuestAuthId());
        }

        if(guestRecord.getOpenId() != null){
            wrapper.eq("s_open_id",guestRecord.getOpenId());
        }
        if(guestRecord.getAuditStatus() != null){
            wrapper.eq("i_audit_status",guestRecord.getAuditStatus());
        }
        return this.selectList(wrapper);
    }


}
