package com.ruoyi.clock.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.clock.domain.Employee;
import com.ruoyi.clock.domain.bo.*;
import com.ruoyi.clock.exception.ActivityExceptionEnum;
import com.ruoyi.clock.service.IActivityAgentService;
import com.ruoyi.clock.service.IActivityProductService;
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.vo.ActivityVo;
import com.ruoyi.clock.domain.Activity;
import com.ruoyi.clock.mapper.ActivityMapper;
import com.ruoyi.clock.service.IActivityService;

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

/**
 * 活动信息Service业务层处理
 *
 * @author pengc
 * @date 2026-03-04
 */
@RequiredArgsConstructor
@Service
public class ActivityServiceImpl implements IActivityService {

    private final ActivityMapper baseMapper;

    private final IActivityAgentService activityAgentService;

    private final IActivityProductService activityProductService;

    /**
     * 查询活动信息分页
     *
     * @param bo 活动信息
     * @return 活动信息
     */
    @Override
    public TableDataInfo<ActivityVo> queryPageList(ActivityBo bo, PageQuery pageQuery) {
        //LambdaQueryWrapper<Activity> lqw = buildQueryWrapper(bo);
        Page<ActivityVo> result = baseMapper.selectActivityList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询活动信息列表
     *
     * @param bo 活动信息
     * @return 活动信息
     */
    @Override
    public List<ActivityVo> queryList(ActivityBo bo) {
        return baseMapper.selectActivityList(bo);
    }

    private LambdaQueryWrapper<Activity> buildQueryWrapper(ActivityBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Activity> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getActivityName()), Activity::getActivityName, bo.getActivityName());
        lqw.eq(bo.getType() != null, Activity::getType, bo.getType());
        lqw.eq(bo.getStartDate() != null, Activity::getStartDate, bo.getStartDate());
        lqw.eq(bo.getEndDate() != null, Activity::getEndDate, bo.getEndDate());
        //lqw.eq(StringUtils.isNotBlank(bo.getImage()), Activity::getImage, bo.getImage());
        lqw.eq(bo.getUnit() != null, Activity::getUnit, bo.getUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getActivityRules()), Activity::getActivityRules, bo.getActivityRules());
        lqw.eq(StringUtils.isNotBlank(bo.getActivityRewards()), Activity::getActivityRewards, bo.getActivityRewards());
        lqw.eq(bo.getStatus() != null, Activity::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 查询活动信息
     *
     * @param activityId 活动信息主键
     * @return 活动信息
     */
    @Override
    public ActivityVo queryById(Long activityId){
        return baseMapper.selectActivityById(activityId);
    }

    /**
     * 详情活动信息
     *
     * @param activityId 活动信息主键
     * @return 活动信息
     */
    @Override
    public Activity loadById(Long activityId, Boolean tw){
        Activity info = this.baseMapper.selectById(activityId);
        if(ObjectUtil.isEmpty(info) && tw){
            throw new ServiceException(ActivityExceptionEnum.ACTIVITY_IS_NOT_EXISTS);
        }
        return info;
    }

    /**
     * 新增活动信息
     *
     * @param bo 活动信息
     * @return 结果
     */
    @Override
    public Boolean insertByBo(ActivityBo bo) {
        validEntityBeforeSave(bo);
        Activity add = BeanUtil.toBean(bo, Activity.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setActivityId(add.getActivityId());
            //新增代理商信息
            List<AgentBo> agentList = bo.getAgentList();
            activityAgentService.insertByBoList(agentList, add.getActivityId());
            //新增产品信息
            List<ActivityProductBo> productList = bo.getProductList();
            activityProductService.insertByBoList(productList, add.getActivityId());
        }
        return flag;
    }

    /**
     * 修改活动信息
     *
     * @param bo 活动信息
     * @return 结果
     */
    @Override
    public Boolean updateByBo(ActivityBo bo) {
        validEntityBeforeSave(bo);
        Activity activity = baseMapper.selectById(bo.getActivityId());
        Activity update = BeanCopyUtils.copy(bo, activity);

        //新增代理商信息
        List<AgentBo> agentList = bo.getAgentList();
        activityAgentService.insertByBoList(agentList, bo.getActivityId());
        //新增产品信息
        List<ActivityProductBo> productList = bo.getProductList();
        activityProductService.insertByBoList(productList, bo.getActivityId());
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param bo 实体类数据
     */
    private void validEntityBeforeSave(ActivityBo bo){
        //TODO 做一些数据校验,如唯一约束
        List<AgentBo> agentList = bo.getAgentList();
        if(ObjectUtil.isEmpty(agentList)){
            throw new ServiceException(ActivityExceptionEnum.ACTIVITY_AGENT_IS_EMPTY);
        }
        List<ActivityProductBo> productList = bo.getProductList();
        if(ObjectUtil.isEmpty(productList)){
            throw new ServiceException(ActivityExceptionEnum.ACTIVITY_PRODUCT_IS_EMPTY);
        }
    }

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


    @Override
    public Boolean updateStatus(ActivityStatusBo bo) {
        Activity activity = baseMapper.selectById(bo.getActivityId());
        activity.setStatus(bo.getStatus());
        return baseMapper.updateById(activity) > 0;
    }
}
