package com.ytpm.service.impl;

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.param.DitchListParam;
import com.ytpm.agent.view.AgentDitchView;
import com.ytpm.agent.view.AgentUserInfo;
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.lemonios.feign.LemonIosFeign;
import com.ytpm.service.AgentDitchService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class AgentDitchServiceImpl implements AgentDitchService {

    @Resource
    private AgentDitchMapper agentDitchMapper;

    @Resource
    private AppMapper appMapper;

    @Resource
    private LemonIosFeign lemonIosFeign;


    @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(Objects.nonNull(ytApp)){
            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);
        return Result.resultOk(RepMessage.DELETE_SUCCESS);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> addDefaultConfig(YtDitch param, AgentUserInfo userInfo) {
        param.setIsDelete(0);

        List<YtApp> appList = appMapper.queryAll(userInfo.getUserId());
        String appId = "";
        if(appList != null && appList.size() > 0){
            appId = appList.get(0).getSuperiorId();
        }
        param.setAppId(appId);
        agentDitchMapper.insertOne(param);
        lemonIosFeign.getLastOne(param);
        return Result.resultOk(RepMessage.SAVE_SUCCESS);
    }
}
