package com.gic.haoban.manage.service.service.impl;

import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.UniqueIdUtils;
import com.gic.haoban.manage.api.service.OpenStaffApiService;
import com.gic.haoban.manage.service.dao.mapper.OpenStaffMapper;
import com.gic.haoban.manage.service.entity.TabOpenStaff;
import com.gic.haoban.manage.service.service.OpenStaffService;
import com.gic.haoban.manage.service.util.OpenUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service("openStaffService")
public class OpenStaffServiceImpl implements OpenStaffService {

    private static final Logger log = LogManager.getLogger(OpenStaffService.class);

    @Autowired
    private OpenStaffMapper openStaffMapper ;

    @Override
    public TabOpenStaff save(TabOpenStaff tab) {
        tab.setUpdateTime(new Date());
        if(null == tab.getOpenStaffId()) {
            tab.setOpenStaffId(UniqueIdUtils.uniqueLong());
            tab.setDeleteFlag(0);
            tab.setStatusFlag(2);
            tab.setCreateTime(new Date());
            this.openStaffMapper.insert(tab) ;
        }else {
            this.openStaffMapper.update(tab) ;
        }
        return tab ;
    }

    @Override
    public TabOpenStaff getByUUID(String uuid) {
        TabOpenStaff openStaff = this.openStaffMapper.getByUUID(uuid) ;
        if(null == openStaff) {
            log.info("通过uuid查不到={}",uuid);
            OpenUtils.logout(uuid);
        }
        return openStaff;
    }

    @Override
    public void logout(String uuid) {
        TabOpenStaff openStaff = this.getByUUID(uuid) ;
        if(null == openStaff) {
            return ;
        }
        OpenUtils.logout(uuid);
        openStaff.setStep(0);
        openStaff.setStatusFlag(2);
        openStaff.setValidFlag(0);
        openStaff.setValidQrcodeFlag(0);
        this.openStaffMapper.update(openStaff) ;
    }
}
