package com.huiju.estateagents.property.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.center.taUser.entity.TaUser;
import com.huiju.estateagents.property.common.UserElement;
import com.huiju.estateagents.property.dao.TaVisitorLicenseSettingMapper;
import com.huiju.estateagents.property.model.TaVisitorLicenseSetting;
import com.huiju.estateagents.property.service.ITaVisitorLicenseSettingService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 录入访客车牌功能开关表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-05-27
 */
@Service
public class TaVisitorLicenseSettingServiceImpl extends ServiceImpl<TaVisitorLicenseSettingMapper, TaVisitorLicenseSetting> implements ITaVisitorLicenseSettingService {

    @Override
    public ResponseBean saveOrDelete(TaUser userElement, Boolean visitorLicenseBool) {
        ResponseBean responseBean = new ResponseBean();

        QueryWrapper<TaVisitorLicenseSetting> visitorLicenseSettingQueryWrapper = new QueryWrapper<>();
        visitorLicenseSettingQueryWrapper.eq("community_id", userElement.getOrgId());
        TaVisitorLicenseSetting taVisitorLicenseSetting = this.getOne(visitorLicenseSettingQueryWrapper);

        // true 表示开启访客邀请车辆
        if (visitorLicenseBool) {
            if (null != taVisitorLicenseSetting) {
                responseBean.addSuccess("操作成功!");
                return responseBean;
            }
            TaVisitorLicenseSetting visitorLicenseSetting = new TaVisitorLicenseSetting();
            visitorLicenseSetting.setCommunityId(userElement.getOrgId());
            visitorLicenseSetting.setCreateDate(LocalDateTime.now());
            visitorLicenseSetting.setCreateUser(userElement.getUserId());

            this.save(visitorLicenseSetting);

            responseBean.addSuccess("操作成功！");
            return responseBean;
        }


        if (null != taVisitorLicenseSetting) {
            this.removeById(taVisitorLicenseSetting.getId());
        }
        responseBean.addSuccess("操作成功！");
        return responseBean;
    }

    @Override
    public ResponseBean getVisitorLicense(TaUser userElement) {
        ResponseBean responseBean = new ResponseBean();

        QueryWrapper<TaVisitorLicenseSetting> visitorLicenseSettingQueryWrapper = new QueryWrapper<>();
        visitorLicenseSettingQueryWrapper.eq("community_id", userElement.getOrgId());
        TaVisitorLicenseSetting visitorLicenseSetting = this.getOne(visitorLicenseSettingQueryWrapper);
        // true 表示启用了访客邀请的车辆， false 表示未启用访客邀请的车辆
        responseBean.addSuccess(null != visitorLicenseSetting ? true : false);
        return responseBean;
    }
}
