package com.xhkjedu.slive.service.xz;

import com.xhkjedu.slive.mapper.liveplay.CourseMapper;
import com.xhkjedu.slive.mapper.xz.XzKjMapper;
import com.xhkjedu.slive.model.xz.LXzKj;
import com.xhkjedu.slive.vo.xz.XzDateKjVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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

/**
 * @Description 行政直播课节
 * @Author WN
 * Date 2022/3/30 15:26
 **/
@Service
@Slf4j
public class XzKjService {
    @Resource
    private XzKjMapper xzKjMapper;
    @Resource
    private CourseMapper courseMapper;

    /**
     * @Description  获取行政直播课节基本信息
     * @Param [xzid]
     * @Author wn
     * @Date 2022/3/31 14:07
     **/
    public List<XzDateKjVo> listXzKjsByXzid(Integer xzid) {
        return xzKjMapper.listXzKjsByXzid(xzid);
    }

    /**
     * @Description 修改行政直播课节信息
     * @Param [xzKjs]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/3/30 21:51
     **/
    @Transactional(rollbackFor = Exception.class)
    public ResultVo updateBatch(List<LXzKj> xzKjs) {
        try {
            for(LXzKj kj : xzKjs){
                boolean rtn = false;

                if (kj.getKjid() == null || kj.getKjid() == 0) {
                    rtn = true;
                }
                if (N_Utils.isEmpty(kj.getKjname()) || N_Utils.isEmpty(kj.getBegintime()) || N_Utils.isEmpty(kj.getEndtime())) {
                    rtn = true;
                }
                if(rtn){
                    return new ResultVo(1,"课节信息不完善");
                }
            }

            //批量修改课节信息
            xzKjMapper.updateBatchXzKj(xzKjs);
            //批量修改课节课程关联课节时间
            courseMapper.updateBatchCourseSectionTime(xzKjs);

            return new ResultVo(0,"修改课节信息成功");
        }catch (Exception e){
            log.error("修改课节信息出错："+e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResultVo(1,"修改课节信息失败");

        }

    }

}
