package com.ruoyi.clock.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.clock.domain.bo.AgentBo;
import com.ruoyi.clock.domain.vo.AgentVo;
import com.ruoyi.clock.exception.ActivityAgentExceptionEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.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 org.springframework.stereotype.Service;
import com.ruoyi.clock.domain.bo.ActivityAgentBo;
import com.ruoyi.clock.domain.vo.ActivityAgentVo;
import com.ruoyi.clock.domain.ActivityAgent;
import com.ruoyi.clock.mapper.ActivityAgentMapper;
import com.ruoyi.clock.service.IActivityAgentService;

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

/**
 * 活动与代理商关联Service业务层处理
 *
 * @author pengc
 * @date 2026-03-04
 */
@RequiredArgsConstructor
@Service
public class ActivityAgentServiceImpl implements IActivityAgentService {

    private final ActivityAgentMapper baseMapper;

    /**
     * 查询活动与代理商关联分页
     *
     * @param bo 活动与代理商关联
     * @return 活动与代理商关联
     */
    @Override
    public TableDataInfo<ActivityAgentVo> queryPageList(ActivityAgentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ActivityAgent> lqw = buildQueryWrapper(bo);
        Page<ActivityAgentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询活动与代理商关联列表
     *
     * @param bo 活动与代理商关联
     * @return 活动与代理商关联
     */
    @Override
    public List<ActivityAgentVo> queryList(ActivityAgentBo bo) {
        //LambdaQueryWrapper<ActivityAgent> lqw = buildQueryWrapper(bo);
        return baseMapper.queryList(bo);
    }

    private LambdaQueryWrapper<ActivityAgent> buildQueryWrapper(ActivityAgentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ActivityAgent> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getActivityId() != null, ActivityAgent::getActivityId, bo.getActivityId());
        lqw.eq(bo.getAgentId() != null, ActivityAgent::getAgentId, bo.getAgentId());
        return lqw;
    }

    /**
     * 查询活动与代理商关联
     *
     * @param id 活动与代理商关联主键
     * @return 活动与代理商关联
     */
    @Override
    public ActivityAgentVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 详情活动与代理商关联
     *
     * @param id 活动与代理商关联主键
     * @return 活动与代理商关联
     */
    @Override
    public ActivityAgent loadById(Long id, Boolean tw){
        ActivityAgent info = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(info) && tw){
            throw new ServiceException(ActivityAgentExceptionEnum.ActivityAgent_IS_NOT_EXISTS);
        }
        return info;
    }

    /**
     * 新增活动与代理商关联
     *
     * @param bo 活动与代理商关联
     * @return 结果
     */
    @Override
    public Boolean insertByBo(ActivityAgentBo bo) {
        ActivityAgent add = BeanUtil.toBean(bo, ActivityAgent.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改活动与代理商关联
     *
     * @param bo 活动与代理商关联
     * @return 结果
     */
    @Override
    public Boolean updateByBo(ActivityAgentBo bo) {
        ActivityAgent activityAgent = baseMapper.selectById(bo.getId());
        ActivityAgent update = BeanCopyUtils.copy(bo, activityAgent);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ActivityAgent entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除活动与代理商关联
     *
     * @param ids 需要删除的活动与代理商关联主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<AgentVo> queryAgentList(Long activityId) {
        return baseMapper.selectAgentList(activityId);
    }

    @Override
    public Boolean insertByBoList(List<AgentBo> boList, Long activityId) {

        QueryWrapper<ActivityAgent> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ActivityAgent::getActivityId, activityId);
        this.baseMapper.delete(queryWrapper);

        List<ActivityAgent> list = new ArrayList<>();
        for (AgentBo bo : boList) {
            ActivityAgent activityAgent = new ActivityAgent();
            activityAgent.setAgentId(bo.getAgentId());
            activityAgent.setActivityId(activityId);
            list.add(activityAgent);
        }
        return baseMapper.insertBatch(list);
    }
}
