package com.citygis.web.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.citygis.common.annotation.DataScope;
import com.citygis.common.core.domain.AjaxResult;
import com.citygis.web.domain.TabAccessConfig;
import com.citygis.web.mapper.TabAccessConfigMapper;
import com.citygis.web.service.ITabAccessConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.citygis.common.utils.SecurityUtils.*;

/**
 * <p>
 * 接入配置 服务实现类
 * </p>
 *
 * @author citygis
 * @since 2025-01-02
 */
@Service
public class TabAccessConfigServiceImpl extends ServiceImpl<TabAccessConfigMapper, TabAccessConfig> implements ITabAccessConfigService {

    @Resource
    TabAccessConfigMapper tabAccessConfigMapper;

    @Override
    @DataScope(deptAlias = "tac", userAlias = "tac")
    public List<TabAccessConfig> getTabAccessConfigList(TabAccessConfig tabAccessConfig) {
        return tabAccessConfigMapper.getTabAccessConfigList(tabAccessConfig);
    }

    @Override
    public AjaxResult getTabAccessConfigById(String id) {
        TabAccessConfig byId = this.getById(id);
        if (byId != null) {
            return AjaxResult.success(byId);
        } else {
            return AjaxResult.success("查询失败");
        }
    }

    @Override
    @Transactional
    public AjaxResult insertTabAccessConfig(TabAccessConfig tabAccessConfig) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        long id = snowflake.nextId();
        tabAccessConfig.setId(id);
        tabAccessConfig.setCreateBy(getUsername());
        tabAccessConfig.setUserId(getUserId());
        tabAccessConfig.setDeptId(getDeptId());
        tabAccessConfig.setCreateTime(new Date());
        boolean b = this.save(tabAccessConfig);
        if (b) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.success("添加失败");
        }
    }

    @Override
    @Transactional
    public AjaxResult updateTabAccessConfigById(TabAccessConfig tabAccessConfig) {
        tabAccessConfig.setUpdateBy(getUsername());
        tabAccessConfig.setUpdateTime(new Date());
        boolean b = this.updateById(tabAccessConfig);
        if (b) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.success("修改失败");
        }
    }

    @Override
    @Transactional
    public AjaxResult falseDeleteTabAccessConfigById(Long id) {
        TabAccessConfig tabAccessConfig = new TabAccessConfig();
        tabAccessConfig.setId(id);
        tabAccessConfig.setIsDel(1);
        boolean b = this.updateById(tabAccessConfig);
        if (b) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.success("删除失败");
        }
    }

}
