package com.sptg.device.biz.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.device.api.dto.TimeStrategyPushTaskDto;
import com.sptg.device.api.entity.*;
import com.sptg.device.biz.mapper.TimeStrategyPushTaskMapper;
import com.sptg.device.biz.service.*;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * title: TimeStrategyPushTaskServiceImpl
 * package: com.sptg.device.biz.security.impl
 * description:时间策略发布任务-服务层实现类
 *
 *                
 *                
 */
@Service
@AllArgsConstructor
public class TimeStrategyPushTaskServiceImpl extends ServiceImpl<TimeStrategyPushTaskMapper, TimeStrategyPushTask> implements TimeStrategyPushTaskServiceInterface {
    private final TimeStrategyServiceInterface timeStrategyServiceInterface;
    private final TimeStrategyConfServiceInterface timeStrategyConfServiceInterface;
    private final TimeStrategyTaskConfServiceInterface timeStrategyTaskConfServiceInterface;
    private final TimeStrategyTaskDetailsServiceInterface timeStrategyTaskDetailsServiceInterface;

    @Override
    public Page<TimeStrategyPushTask> showListTimeStrategyPushTaskList(TimeStrategyPushTaskDto dto){
        EntityWrapper<TimeStrategyPushTask> wrapper=new EntityWrapper<TimeStrategyPushTask>();
        Page page=dto.getPage();
        //名称
        if(StringUtils.isNotBlank(dto.getStrategyName())){
            wrapper.like("s_strategy_name",dto.getStrategyName());
        }
        //发布状态
        if(dto.getPushStatus()!=null){
            wrapper.eq("i_push_status",dto.getPushStatus());
        }
        //  查询当前账号机构或子机构创建的屏保内容
        wrapper.like("s_org_position",dto.getOrgPosition(), SqlLike.RIGHT);
        //排序
        if (dto.getSortField() != null && dto.getSortOrder() != null){
            wrapper.orderBy(dto.getSortField(),dto.getSortOrder().equals("asc"));
        }else {
        //  排序字段 时间降序
            wrapper.orderBy("t_create_date",false);
        }
        page=this.selectPage(page,wrapper);
        return page;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addTimeStrategyPushTask(TimeStrategyPushTaskDto dto){
        Long timeStrategyId=dto.getTimeStrategyId();
        //保持时间策略信息
        TimeStrategy timeStrategy = timeStrategyServiceInterface.selectById(timeStrategyId);
        if(null==timeStrategy){
            return false;
        }
        dto.setStrategyType(timeStrategy.getType());
        dto.setWorkingDay(timeStrategy.getWorkingDay());
        dto.setBeginDate(timeStrategy.getBeginDate());
        dto.setEndDate(timeStrategy.getEndDate());
        dto.setStrategyName(timeStrategy.getName());
        dto.setRemark(timeStrategy.getRemark());
        //  获取第二天的凌晨3点
        GregorianCalendar calendar = new GregorianCalendar();
//        calendar.add(GregorianCalendar.DATE, 1);
//        calendar.set(Calendar.HOUR_OF_DAY, 3);
//        calendar.set(Calendar.MINUTE, 0);
//        calendar.set(Calendar.SECOND, 0);
//        calendar.set(Calendar.MILLISECOND, 0);
        //  预计发布时间
        dto.setEstimatePushTime(calendar.getTime());
        //保持发布任务
        boolean result=this.insert(dto);
        if(result){
                //保持时间策略时间参数详细信息
               Wrapper<TimeStrategyConf> wrapper=new EntityWrapper<>();
                wrapper.eq("l_strategy_id",timeStrategyId);
                List<TimeStrategyConf> timeStrategyConfList = timeStrategyConfServiceInterface.selectList(wrapper);
                if(timeStrategyConfList!=null&&!timeStrategyConfList.isEmpty()){
                    List<TimeStrategyTaskConf> timeStrategyTaskConfList=new ArrayList<>(timeStrategyConfList.size());
                            timeStrategyConfList.forEach(timeStrategyConf->{
                                TimeStrategyTaskConf taskConf=new TimeStrategyTaskConf();
                                taskConf.setTask(dto.getId());
                                taskConf.setBeginTime(timeStrategyConf.getBeginTime());
                                taskConf.setEndTime(timeStrategyConf.getEndTime());
                                taskConf.setSoundVolume(timeStrategyConf.getSoundVolume());
                                taskConf.setScreenLuminance(timeStrategyConf.getScreenLuminance());
                                taskConf.setFillLightLuminance(timeStrategyConf.getFillLightLuminance());
                                timeStrategyTaskConfList.add(taskConf);
                    });
                    timeStrategyTaskConfServiceInterface.insertBatch(timeStrategyTaskConfList);
                }
            }
            //保存任务详细信息
            List<Long> fkIdList = dto.getFkIdList();
            if(fkIdList!=null){
                List<TimeStrategyTaskDetails>  taskDetails=new ArrayList<TimeStrategyTaskDetails>(fkIdList.size());
                fkIdList.forEach(fkId->{
                    TimeStrategyTaskDetails timeStrategyTaskDetails=new TimeStrategyTaskDetails();
                    timeStrategyTaskDetails.setTaskId(dto.getId());
                    timeStrategyTaskDetails.setFkId(fkId);
                    taskDetails.add(timeStrategyTaskDetails);
                });
                timeStrategyTaskDetailsServiceInterface.insertBatch(taskDetails);
            }
        return result;
    }

    @Override
    public Page<TimeStrategyPushTask> selectOvertimeTask(Page<TimeStrategyPushTask> page) {
        Wrapper<TimeStrategyPushTask> wrapper = new EntityWrapper<>();
        //  未发布
        wrapper.eq("i_push_status", OperationConstant.ONE);
        wrapper.le("t_estimate_push_time",new Date());
        return this.selectPage(page,wrapper);
    }
}
