package com.ytpm.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ytpm.agent.model.YtApp;
import com.ytpm.agent.model.YtDitch;
import com.ytpm.agent.model.YtPlatformUserApp;
import com.ytpm.agent.param.DitchListParam;
import com.ytpm.agent.view.AgentDitchView;
import com.ytpm.dao.AgentAppMapper;
import com.ytpm.dao.AgentDitchMapper;
import com.ytpm.dao.AppMapper;
import com.ytpm.general.RepMessage;
import com.ytpm.general.Result;
import com.ytpm.general.ResultTable;
import com.ytpm.service.AgentDitchService;
import com.ytpm.utils.FeignClientInvoker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class AgentDitchServiceImpl implements AgentDitchService {

    @Resource
    private AgentDitchMapper agentDitchMapper;
    @Resource
    private AppMapper appMapper;
    @Resource
    private FeignClientInvoker feignInvoker;
    @Resource
    private AgentAppMapper agentAppMapper;

    @Override
    public ResultTable<AgentDitchView> ditchList(DitchListParam param) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        List<AgentDitchView> agentDitchViews = agentDitchMapper.ditchList(param);
        return ResultTable.resultTableOk(new PageInfo<>(agentDitchViews));
    }

    @Override
    public Result<?> addOne(YtDitch param) {
        param.setIsDelete(0);
        agentDitchMapper.insertOne(param);
        return Result.resultOk(RepMessage.SAVE_SUCCESS);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> updateOne(YtDitch param) {
        YtDitch ytDitch = agentDitchMapper.selectById(param.getDitchId());
        if(Objects.isNull(ytDitch)){
            return Result.resultErr(RepMessage.OBJECT_NOT_EXIST);
        }
        agentDitchMapper.update(param);
        //同时修改app中的渠道类型名称
        Long ditchId = param.getDitchId();
        YtApp ytApp = appMapper.selectByDitchId(ditchId);
        if (ytApp != null) {
            ytApp.setDitchName(param.getDitchName());
            appMapper.updateOne(ytApp);
        }
        return Result.resultOk(RepMessage.MODIFY_SUCCESS);
    }

    @Override
    public Result<?> deleteOne(Long ditchId) {
        YtDitch ytDitch = new YtDitch();
        ytDitch.setIsDelete(1);
        ytDitch.setDitchId(ditchId);
        agentDitchMapper.update(ytDitch);
        // 20250917 删除渠道后 同步删除渠道下应用（假删除）及应用风控配置（真删除）
        YtApp ytApp = appMapper.selectByDitchId(ditchId);
        if(ytApp != null){
            List<String> ids = Collections.singletonList(ytApp.getAppId());
            appMapper.deleteAppIds(ids);
            appMapper.deleteRisks(ids);
            // ios应用 feign调用更新子库 记录
            if (ytApp.getAppType() == 2 && StrUtil.isNotEmpty(ytApp.getSuperiorId())) {
                try {
                    YtPlatformUserApp superior = agentAppMapper.selectByPrimaryKey(ytApp.getSuperiorId());
                    if (superior != null) {
                        feignInvoker.invoke(superior.getServiceName(), "delDitchByAppId", ytApp.getAppId());
                    }
                }catch (Exception ignored){
                    log.error("同步 ditch删除失败, appId:{}", ytApp.getAppId());
                }
            }
        }
        return Result.resultOk(RepMessage.DELETE_SUCCESS);
    }
}
