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.TabAccess;
import com.citygis.web.mapper.TabAccessMapper;
import com.citygis.web.service.ITabAccessService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

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

/**
 * <p>
 * 接入方管理 服务实现类
 * </p>
 *
 * @author citygis
 * @since 2025-01-03
 */
@Service
public class TabAccessServiceImpl extends ServiceImpl<TabAccessMapper, TabAccess> implements ITabAccessService {

    @Resource
    TabAccessMapper tabAccessMapper;

    @Override
    @DataScope(deptAlias = "ta", userAlias = "ta")
    public List<TabAccess> getTabAccessList(TabAccess tabAccess) {
        return tabAccessMapper.getTabAccessList(tabAccess);
    }

    @Override
    public AjaxResult getTabAccessById(Long id) {

        TabAccess byId = this.getById(id);
        if (byId != null) {
            return AjaxResult.success(byId);
        } else {
            return AjaxResult.success("查询失败");
        }
    }

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

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

    @Override
    @Transactional
    public AjaxResult falseDeleteTabAccessById(TabAccess tabAccess) {

        List<Long> ids = tabAccess.getIds();
        List<TabAccess> tabAccessList = new ArrayList<>();
        ids.forEach(id -> {
            TabAccess tabAccess1 = new TabAccess();
            tabAccess1.setId(id);
            tabAccess1.setIsDel(1);
            tabAccessList.add(tabAccess1);

        });
        boolean b = updateBatchById(tabAccessList);
        if (b) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.success("删除失败");
        }
    }

    @Override
    @Transactional
    public AjaxResult importData(List<TabAccess> tabAccessList) {
        tabAccessList.forEach(tabAccess -> {
            TabAccess byId = this.getById(tabAccess.getId());
            if (byId == null) {
                Snowflake snowflake = IdUtil.createSnowflake(1, 1);
                long id = snowflake.nextId();
                if (tabAccess.getId() != null) {
                    tabAccess.setId(tabAccess.getId());
                } else {
                    tabAccess.setId(id);
                }
                tabAccess.setCreateBy(getUsername());
                tabAccess.setCreateTime(new Date());
                tabAccess.setUserId(getUserId());
                tabAccess.setDeptId(getDeptId());
                this.save(tabAccess);
            } else {
                tabAccess.setUpdateBy(getUsername());
                tabAccess.setUpdateTime(new Date());
                this.updateById(tabAccess);
            }
        });
        return AjaxResult.success("导入成功");
    }
}
