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

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
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.TimeStrategyDto;
import com.sptg.device.api.entity.TimeStrategy;
import com.sptg.device.api.entity.TimeStrategyConf;
import com.sptg.device.biz.mapper.TimeStrategyMapper;
import com.sptg.device.biz.service.TimeStrategyConfServiceInterface;
import com.sptg.device.biz.service.TimeStrategyServiceInterface;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * title: TimeStrategyServiceImpl
 * package: com.sptg.device.biz.security.impl
 * description: 时间策略-业务层实现类
 *
 *
 *
 */
@Service
@RequiredArgsConstructor
public class TimeStrategyServiceImpl extends ServiceImpl<TimeStrategyMapper, TimeStrategy> implements TimeStrategyServiceInterface {
    private final TimeStrategyConfServiceInterface timeStrategyConfServiceInterface;


    @Override
    public Page<TimeStrategy> showTimeStrategyList(TimeStrategyDto dto){
        EntityWrapper<TimeStrategy> wrapper=new EntityWrapper<TimeStrategy>();
        Page page=dto.getPage();
        //名称
        if(StringUtils.isNotBlank(dto.getName())){
            wrapper.like("s_name",dto.getName());
        }
        //正常状态
        wrapper.eq("i_status", OperationConstant.ZERO);
        //  查询当前账号机构或子机构创建的屏保内容
        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 addTimeStrategy(TimeStrategyDto dto){
        boolean result=this.insert(dto);
        //添加时间策略详细
        if(result){
            //删除旧时间策略参数
            EntityWrapper<TimeStrategyConf> wrapper=new EntityWrapper<TimeStrategyConf>();
            wrapper.eq("l_strategy_id",dto.getId());
            result=timeStrategyConfServiceInterface.delete(wrapper);
            if(result){
                List<TimeStrategyConf> timeStrategyConfList = dto.getTimeStrategyConfList();
                //设置关联id
                if(timeStrategyConfList!=null){
                    timeStrategyConfList.forEach(timeStrategyDetails->{
                        timeStrategyDetails.setStrategyId(dto.getId());
                    });
                }
                timeStrategyConfServiceInterface.insertBatch(timeStrategyConfList);
            }
        }
        return result;
    }
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean editTimeStrategy(TimeStrategyDto dto){
        boolean result=this.updateById(dto);
        //添加时间策略详细
        if(result){
            //删除旧时间策略参数
            EntityWrapper<TimeStrategyConf> wrapper=new EntityWrapper<TimeStrategyConf>();
            wrapper.eq("l_strategy_id",dto.getId());
            result=timeStrategyConfServiceInterface.delete(wrapper);
            if(result){
                List<TimeStrategyConf> timeStrategyConfList = dto.getTimeStrategyConfList();
                //设置关联id
                if(timeStrategyConfList!=null){
                    timeStrategyConfList.forEach(timeStrategyDetails->{
                        timeStrategyDetails.setStrategyId(dto.getId());
                    });
                }
                timeStrategyConfServiceInterface.insertBatch(timeStrategyConfList);
            }
        }
        return result;
    }
    @Override
    public TimeStrategy getTimeStrategyById(long id){
        TimeStrategy timeStrategy=this.selectById(id);
        if(timeStrategy!=null){
            //获取时间策略参数信息
            EntityWrapper<TimeStrategyConf> wrapper=new EntityWrapper<TimeStrategyConf>();
            wrapper.eq("l_strategy_id",id);
            List<TimeStrategyConf> timeStrategyConfList=timeStrategyConfServiceInterface.selectList(wrapper);
            timeStrategy.setTimeStrategyConfList(timeStrategyConfList);
        }
        return timeStrategy;
    }
}
