package com.ytpm.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ytpm.advertise.enums.AdPlatformTypeEnum;
import com.ytpm.advertise.param.ChannelAppParam;
import com.ytpm.agent.model.YtChannel;
import com.ytpm.agent.param.ChannelListParam;
import com.ytpm.agent.param.ChannelParam;
import com.ytpm.agent.view.AgentChannelView;
import com.ytpm.dao.ChannelMapper;
import com.ytpm.general.RepMessage;
import com.ytpm.general.Result;
import com.ytpm.general.ResultTable;
import com.ytpm.service.ChannelService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ChannelServiceImpl implements ChannelService {

    @Resource
    private ChannelMapper channelMapper;

    /**
     * 获取渠道商列表
     */
    @Override
    public ResultTable<AgentChannelView> channelList(ChannelListParam param) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        return ResultTable.resultTableOk(new PageInfo<AgentChannelView>(channelMapper.channelList()));
    }

    /**
     * 新增广告渠道
     */
    @Override
    public Result<?> addOne(ChannelParam param) {
        YtChannel channel = new YtChannel();
        BeanUtils.copyProperties(param, channel);
        channel.setChannelId(IdWorker.get32UUID());
        channel.setEnabled(1);
        channel.setChannelName(AdPlatformTypeEnum.getDesc(param.getAdPlatformType()));
        channelMapper.insert(channel);
        return Result.resultOk(RepMessage.SAVE_SUCCESS);
    }

    /**
     * 修改广告渠道
     */
    @Override
    public Result<?> updateOne(ChannelParam param) {
        YtChannel channel = new YtChannel();
        BeanUtils.copyProperties(param, channel);
        channelMapper.update(channel);
        return Result.resultOk(RepMessage.SAVE_SUCCESS);
    }

    /**
     * 删除广告渠道
     */
    @Override
    public Result<?> deleteOne(String channelId) {
        YtChannel channel = new YtChannel();
        channel.setChannelId(channelId);
        channel.setEnabled(0);
        channelMapper.update(channel);
        return Result.resultOk(RepMessage.DELETE_SUCCESS);
    }

    @Override
    public ResultTable<AgentChannelView> relativeChannel(ChannelAppParam param) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        return ResultTable.resultTableOk(new PageInfo<AgentChannelView>(channelMapper.relativeChannel(param.getAppId())));
    }
}
