package org.dromara.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.app.domain.bo.AppScheduleDetailBo;
import org.dromara.app.domain.vo.AppScheduleDetailVo;
import org.dromara.app.domain.AppScheduleDetail;
import org.dromara.app.mapper.AppScheduleDetailMapper;
import org.dromara.app.service.IAppScheduleDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 排程详情Service业务层处理
 *
 * @author Lion Li
 * @date 2026-04-25
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AppScheduleDetailServiceImpl implements IAppScheduleDetailService {

    private final AppScheduleDetailMapper baseMapper;

    /**
     * 查询排程详情
     *
     * @param id 主键
     * @return 排程详情
     */
    @Override
    public AppScheduleDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询排程详情列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 排程详情分页列表
     */
    @Override
    public TableDataInfo<AppScheduleDetailVo> queryPageList(AppScheduleDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppScheduleDetail> lqw = buildQueryWrapper(bo);
        Page<AppScheduleDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的排程详情列表
     *
     * @param bo 查询条件
     * @return 排程详情列表
     */
    @Override
    public List<AppScheduleDetailVo> queryList(AppScheduleDetailBo bo) {
        LambdaQueryWrapper<AppScheduleDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppScheduleDetail> buildQueryWrapper(AppScheduleDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppScheduleDetail> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(AppScheduleDetail::getId);
        lqw.eq(bo.getScheduleId() != null, AppScheduleDetail::getScheduleId, bo.getScheduleId());
        lqw.eq(StringUtils.isNotBlank(bo.getWeekday()), AppScheduleDetail::getWeekday, bo.getWeekday());
        lqw.eq(bo.getTimeData() != null, AppScheduleDetail::getTimeData, bo.getTimeData());
        lqw.eq(ObjectUtil.isNotNull(bo.getDurationMin()), AppScheduleDetail::getDurationMin, bo.getDurationMin());
        lqw.eq(StringUtils.isNotBlank(bo.getZones()), AppScheduleDetail::getZones, bo.getZones());
        lqw.eq(StringUtils.isNotBlank(bo.getTriggerType()), AppScheduleDetail::getTriggerType, bo.getTriggerType());
        return lqw;
    }

    /**
     * 新增排程详情
     *
     * @param bo 排程详情
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AppScheduleDetailBo bo) {
        AppScheduleDetail add = MapstructUtils.convert(bo, AppScheduleDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改排程详情
     *
     * @param bo 排程详情
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AppScheduleDetailBo bo) {
        AppScheduleDetail update = MapstructUtils.convert(bo, AppScheduleDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppScheduleDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除排程详情信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public List<AppScheduleDetailVo> queryByScheduleIdByStatus(Long id) {

        return baseMapper.selectVoList(new QueryWrapper<AppScheduleDetail>().eq("schedule_id",id));
    }
}
