package com.citygis.web.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.*;
import com.citygis.web.mapper.TabAccessFeedbackMapper;
import com.citygis.web.service.*;
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-07
 */
@Service
public class TabAccessFeedbackServiceImpl extends ServiceImpl<TabAccessFeedbackMapper, TabAccessFeedback> implements ITabAccessFeedbackService {

    @Resource
    TabAccessFeedbackMapper tabAccessFeedbackMapper;

    @Resource
    ITabAccessReadyService tabAccessReadyService;

    @Resource
    ITabAccessAndReadyService tabAccessAndReadyService;

    @Resource
    ITabFileInfoService tabFileInfoService;

    @Resource
    ITabEtlService tabEtlService;

    @Resource
    ITabKafkaService tabKafkaService;

    @Resource
    ITabKbService tabKbService;

    @Resource
    ITabSftpService tabSftpService;

    @Resource
    ITabUrlService tabUrlService;

    @Resource
    @Override
    @DataScope(userAlias = "taf", deptAlias = "taf")
    public List<TabAccessFeedback> getTabAccessFeedbackList(TabAccessFeedback tabAccessFeedback) {

        if (tabAccessFeedback.getStates() != null) {
            Integer state = tabAccessFeedback.getState();
            List<Integer> states = new ArrayList<>();
            //正式运行
            if (state == 1) {
                states.add(5);
                states.add(6);
            } else if (state == 2) {//申请反馈
                states.add(1);
                states.add(2);
                states.add(3);
                states.add(4);
            }
            tabAccessFeedback.setStates(states);
        }

        return tabAccessFeedbackMapper.getTabAccessFeedbackList(tabAccessFeedback);
    }

    @Override
    public AjaxResult getTabAccessFeedbackById(Long id) {
        TabAccessFeedback byId = this.getById(id);

        if (byId == null) {
            return AjaxResult.success("查询失败");
        }

        return AjaxResult.success(byId);
    }


    @Override
    @Transactional
    public AjaxResult insertTabAccessFeedback(List<TabAccessFeedback> tabAccessFeedbacks) {
        tabAccessFeedbacks.forEach(tabAccessFeedback -> {
            Snowflake snowflake = IdUtil.getSnowflake(1, 1);
            long id = snowflake.nextId();
            tabAccessFeedback.setId(id);
            tabAccessFeedback.setCreateBy(getUsername());
            tabAccessFeedback.setCreateTime(new Date());
            tabAccessFeedback.setUserId(getUserId());
            tabAccessFeedback.setDeptId(getDeptId());
        });

        boolean b = this.saveBatch(tabAccessFeedbacks);
        if (b) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.success("添加失败");
        }
    }

    @Override
    @Transactional
    public AjaxResult updateTabAccessFeedbackById(List<TabAccessFeedback> tabAccessFeedbacks) {

        tabAccessFeedbacks.forEach(tabAccessFeedback -> {
            tabAccessFeedback.setUpdateBy(getUsername());
            tabAccessFeedback.setUpdateTime(new Date());
        });
        boolean b = this.saveOrUpdateBatch(tabAccessFeedbacks);
        if (b) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.success("修改失败");
        }
    }

    @Override
    @Transactional
    public AjaxResult falseDeleteTabAccessFeedbackById(Long id) {
        TabAccessFeedback tabAccessFeedback = new TabAccessFeedback();
        tabAccessFeedback.setId(id);
        boolean b = this.removeById(tabAccessFeedback);
        if (b) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.success("删除失败");
        }
    }

    @Override
    public AjaxResult getTabAccessFeedbackListByAccessOnlyId(Long accessOnlyId) {
        LambdaQueryWrapper<TabAccessFeedback> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TabAccessFeedback::getAccessOnlyId, accessOnlyId);
        return AjaxResult.success(this.list(queryWrapper));
    }

    @Override
    @Transactional
    public AjaxResult deleteTabAccessFeedbackById(List<Long> accessOnlyIds) {

        accessOnlyIds.forEach(id -> {

            tabAccessReadyService.removeById(id);

            LambdaQueryWrapper<TabAccessFeedback> qw = new LambdaQueryWrapper<>();
            qw.eq(TabAccessFeedback::getId, id);
            this.remove(qw);

            LambdaQueryWrapper<TabAccessAndReady> qw1 = new LambdaQueryWrapper<>();
            qw1.eq(TabAccessAndReady::getAccessOnlyId, id);
            tabAccessAndReadyService.remove(qw1);

            LambdaQueryWrapper<TabFileInfo> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(TabFileInfo::getOutsideId, id);
            tabFileInfoService.remove(qw2);

            LambdaQueryWrapper<TabEtl> qw3 = new LambdaQueryWrapper<>();
            qw3.eq(TabEtl::getAccessFeedbackId, id);
            tabEtlService.remove(qw3);

            LambdaQueryWrapper<TabKafka> qw4 = new LambdaQueryWrapper<>();
            qw4.eq(TabKafka::getAccessFeedbackId, id);
            tabKafkaService.remove(qw4);

            LambdaQueryWrapper<TabKb> qw5 = new LambdaQueryWrapper<>();
            qw5.eq(TabKb::getAccessFeedbackId, id);
            tabKbService.remove(qw5);

            LambdaQueryWrapper<TabSftp> qw6 = new LambdaQueryWrapper<>();
            qw6.eq(TabSftp::getAccessFeedbackId, id);
            tabSftpService.remove(qw6);

            LambdaQueryWrapper<TabUrl> qw7 = new LambdaQueryWrapper<>();
            qw7.eq(TabUrl::getAccessFeedbackId, id);
            tabUrlService.remove(qw7);

        });

        return AjaxResult.success("删除成功");
    }
}