package com.yonge.cooleshow.biz.dal.service.impl;

import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.biz.dal.dao.TeacherFreeTimeDao;
import com.yonge.cooleshow.biz.dal.dto.PracticeTimesSetting;
import com.yonge.cooleshow.biz.dal.entity.TeacherFreeTime;
import com.yonge.cooleshow.biz.dal.entity.TeacherSubjectPrice;
import com.yonge.cooleshow.biz.dal.service.TeacherFreeTimeService;
import com.yonge.cooleshow.biz.dal.vo.TeacherFreeTimeVo;
import com.yonge.cooleshow.biz.dal.vo.WeekVo;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.payment.core.entity.SysConfigPayment;
import com.yonge.toolset.utils.date.DateUtil;
import jodd.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 老师可排课时间表(TeacherFreeTime)表服务实现类
 * @author zx
 * @since 2022-03-25 10:04:27
 */
@Service("teacherFreeTimeService")
public class TeacherFreeTimeServiceImpl extends ServiceImpl<TeacherFreeTimeDao, TeacherFreeTime> implements TeacherFreeTimeService {

    private final static Logger log = LoggerFactory.getLogger(TeacherFreeTimeServiceImpl.class);

    @Override
    public TeacherFreeTimeDao getDao() {
        return this.baseMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void upSet(TeacherFreeTime teacherFreeTime) {
        Long teacherId = teacherFreeTime.getTeacherId();
        TeacherFreeTime oldTeacherFreeTime = baseMapper.selectOne(Wrappers.<TeacherFreeTime>lambdaQuery().eq(TeacherFreeTime::getTeacherId, teacherId));

//        TeacherFreeTime oldTeacherFreeTime = baseMapper.findOne(teacherFreeTime.getTeacherId(),
//                teacherFreeTime.getCourseMinutes(),
//                teacherFreeTime.getFreeMinutes());

        //删除原有设置
        if(Objects.nonNull(oldTeacherFreeTime)){
            baseMapper.deleteById(oldTeacherFreeTime.getId());
            baseMapper.deleteSubjectPrice(oldTeacherFreeTime.getId());
        }
        //将其他配置改为非默认
//        List<TeacherFreeTime> checkGapLock = baseMapper.selectList(Wrappers.<TeacherFreeTime>lambdaQuery().eq(TeacherFreeTime::getTeacherId, teacherFreeTime.getTeacherId()));
//        if (!CollectionUtils.isEmpty(checkGapLock)){
//            baseMapper.updateDefaultFlag(false,teacherFreeTime.getTeacherId());
//        }
        baseMapper.insert(teacherFreeTime);

        List<TeacherSubjectPrice> priceList= JSONArray.parseArray(teacherFreeTime.getSubjectPrice(), TeacherSubjectPrice.class);
        for (TeacherSubjectPrice price : priceList) {
            price.setTeacherFreeTimeId(teacherFreeTime.getId());
            price.setTeacherId(teacherFreeTime.getTeacherId());
            price.setFreeMinutes(teacherFreeTime.getFreeMinutes());
            price.setCourseMinutes(teacherFreeTime.getCourseMinutes());
        }
        baseMapper.insertPrice(priceList);
    }

    @Override
    public TeacherFreeTimeVo getDetail(PracticeTimesSetting practiceTimesSetting) {
        TeacherFreeTimeVo getDetail=baseMapper.getDetail(practiceTimesSetting);
        if (getDetail==null){
            return getDetail;
        }
        List<TeacherSubjectPrice> priceList=baseMapper.getPrice(getDetail.getId());
        getDetail.setSubjectPrice(priceList);
        return getDetail;
    }

    public static String timeOffSet(String week,Integer freeMinutes){
        List<WeekVo> list = JSONObject.parseArray(week, WeekVo.class);
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        List<WeekVo> weekList = new ArrayList<>();
        for (WeekVo weekVo : list) {
            WeekVo vo = new WeekVo();
            vo.setStartTime(weekVo.getStartTime());
            vo.setEndTime(sdf.format(DateUtil.offsetMinute(weekVo.getEndTime(), -freeMinutes, "HH:mm:ss")));
            weekList.add(vo);
        }
        return JSON.toJSON(weekList).toString();
    }
}

