package com.ylcm.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ylcm.sys.domain.AdTheatre;
import com.ylcm.sys.domain.AdTheatreTv;
import com.ylcm.sys.mapper.AdTheatreTvMapper;
import com.ylcm.sys.service.AdTheatreService;
import com.ylcm.sys.service.AdTheatreTvService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylcm.sys.service.AdTheatreTvStatService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 关联电视剧数据 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-01
 */
@Service
public class AdTheatreTvServiceImpl extends ServiceImpl<AdTheatreTvMapper, AdTheatreTv> implements AdTheatreTvService {

    @Resource
    private AdTheatreTvMapper adTheatreTvMapper;

    @Resource
    private AdTheatreTvStatService adTheatreTvStatService;

    @Resource
    private AdTheatreService adTheatreService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AdTheatreTv getByTheatreId(Integer adTheatreId) {
        LambdaQueryWrapper<AdTheatreTv> queryWrapper = new LambdaQueryWrapper<AdTheatreTv>().eq(AdTheatreTv::getAdTheatreId, adTheatreId)
        		                                                                            .last("LIMIT 1");
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String add(AdTheatreTv record) {
        AdTheatreTv old = getByTheatreId(record.getAdTheatreId());
        if (old == null){
            // insert
            baseMapper.insert(record);
        } else {
            // update
            record.setId(old.getId());
            baseMapper.updateById(record);
        }

        AdTheatre adTheatre = adTheatreService.getById(record.getAdTheatreId());
        adTheatreTvStatService.saveStatByTheatreTv(adTheatre);
        return "ok";
    }
}
