package com.ibeetl.admin.flow.service;

import com.ibeetl.admin.core.service.BaseService;
import com.ibeetl.admin.flow.dao.DirectedChannelUrlDao;
import com.ibeetl.admin.flow.dao.DirectedFestivalDao;
import com.ibeetl.admin.flow.entity.DirectedChannelUrlEntity;
import org.apache.commons.lang.StringUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DirectedChannelUrlService extends BaseService<DirectedChannelUrlEntity> {

    @Autowired
    private DirectedChannelUrlDao channelUrlDao;

    private DirectedFestivalDao directedFestivalDao;

    public void searchChannelUrl(PageQuery<DirectedChannelUrlEntity> query) {
        PageQuery<DirectedChannelUrlEntity> ret = channelUrlDao.searchChannelUrl(query);
        queryListAfter(ret.getList());
    }

    public List<DirectedChannelUrlEntity> findByFesAndChannel(String fescod, String orderchannel) {
        return channelUrlDao.findByFesAndChannel(fescod, orderchannel);
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveChannelFestivalAndUrl(DirectedChannelUrlEntity entity) {
        // 往 tb_ordercenter_fes_chan 插入数据，插入多条
        // 关联渠道与活动组
        channelUrlDao.saveChannelFestival(entity);

        // 保存活动结束以后的基础活动，活动 与 渠道关联
        if (!StringUtils.isEmpty(entity.getParitycode())) {
            channelUrlDao.saveParityChannelFestival(entity);
        }

        channelUrlDao.saveChannelUrl(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveChannelUrl(DirectedChannelUrlEntity entity) {
        channelUrlDao.saveChannelUrl(entity);
    }

    public DirectedChannelUrlEntity findChannelUrlById(String id) {
        return channelUrlDao.findChannelUrlById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateChannelUrl(DirectedChannelUrlEntity entity) {
        channelUrlDao.updateChannelFestival(entity);

        if (!StringUtils.isEmpty(entity.getParitycode())) {
            channelUrlDao.updateParityChannelFestival(entity);
        }

        channelUrlDao.updateChannelUrl(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteChannelUrl(DirectedChannelUrlEntity entity) {
        if (StringUtils.isNotEmpty(entity.getParitycode())) {
            channelUrlDao.deleteParityChannelFestival(entity);
        }

        channelUrlDao.deleteChannelUrlById(entity);
    }

}
