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.ActivityAgent;
import com.ruoyi.clock.domain.Product;
import com.ruoyi.clock.domain.bo.AgentBo;
import com.ruoyi.clock.domain.bo.ProductBo;
import com.ruoyi.clock.domain.vo.ProductVo;
import com.ruoyi.clock.exception.ActivityProductExceptionEnum;
import com.ruoyi.clock.mapper.ProductMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
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.ActivityProductBo;
import com.ruoyi.clock.domain.vo.ActivityProductVo;
import com.ruoyi.clock.domain.ActivityProduct;
import com.ruoyi.clock.mapper.ActivityProductMapper;
import com.ruoyi.clock.service.IActivityProductService;

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 ActivityProductServiceImpl implements IActivityProductService {

    private final ActivityProductMapper baseMapper;

    private final ProductMapper productMapper;

    /**
     * 查询活动与产品关联分页
     *
     * @param bo 活动与产品关联
     * @return 活动与产品关联
     */
    @Override
    public TableDataInfo<ActivityProductVo> queryPageList(ActivityProductBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ActivityProduct> lqw = buildQueryWrapper(bo);
        Page<ActivityProductVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询活动与产品关联列表
     *
     * @param bo 活动与产品关联
     * @return 活动与产品关联
     */
    @Override
    public List<ActivityProductVo> queryList(ActivityProductBo bo) {
        LambdaQueryWrapper<ActivityProduct> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ActivityProduct> buildQueryWrapper(ActivityProductBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ActivityProduct> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getActivityId() != null, ActivityProduct::getActivityId, bo.getActivityId());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), ActivityProduct::getProductName, bo.getProductName());
        return lqw;
    }

    /**
     * 查询活动与产品关联
     *
     * @param productId 活动与产品关联主键
     * @return 活动与产品关联
     */
    @Override
    public ActivityProductVo queryById(Long productId){
        return baseMapper.selectVoById(productId);
    }

    /**
     * 详情活动与产品关联
     *
     * @param productId 活动与产品关联主键
     * @return 活动与产品关联
     */
    @Override
    public ActivityProduct loadById(Long productId, Boolean tw){
        ActivityProduct info = this.baseMapper.selectById(productId);
        if(ObjectUtil.isEmpty(info) && tw){
            throw new ServiceException(ActivityProductExceptionEnum.ActivityProduct_IS_NOT_EXISTS);
        }
        return info;
    }

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

    /**
     * 修改活动与产品关联
     *
     * @param bo 活动与产品关联
     * @return 结果
     */
    @Override
    public Boolean updateByBo(ActivityProductBo bo) {
        ActivityProduct activityProduct = baseMapper.selectById(bo.getId());
        ActivityProduct update = BeanCopyUtils.copy(bo, activityProduct);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ActivityProduct 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<ProductVo> selectProductList(Long activityId) {
        return baseMapper.selectProductList(activityId);
    }

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

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

        List<ActivityProduct> list = new ArrayList<>();
        for (ActivityProductBo bo : boList) {
            ActivityProduct activityProduct = new ActivityProduct();
            activityProduct.setActivityId(activityId);
            activityProduct.setProductName(bo.getProductName());
            list.add(activityProduct);
        }
        return baseMapper.insertBatch(list);
    }
}
