package cn.iocoder.yudao.module.member.service.impl;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.entity.ActiveMain;
import cn.iocoder.yudao.module.member.entity.ActiveRule;
import cn.iocoder.yudao.module.member.entity.MemberCoupon;
import cn.iocoder.yudao.module.member.mapper.ActiveMainMapper;
import cn.iocoder.yudao.module.member.service.ActiveMainService;
import cn.iocoder.yudao.module.member.service.ActiveRuleCouponService;
import cn.iocoder.yudao.module.member.service.ActiveRuleService;
import cn.iocoder.yudao.module.member.vo.ActiveMainPageReqVO;
import cn.iocoder.yudao.module.member.vo.ActiveMainRespVO;
import cn.iocoder.yudao.module.member.vo.ActiveMainSaveReqVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.common.BaseConstants.ACTIVE_RULE_COUPON_ACTIVE;

/**
 * 营销活动 Service 实现类
 *
 * @author pqz
 */
@Service
@Validated
public class ActiveMainServiceImpl extends ServiceImpl<ActiveMainMapper, ActiveMain> implements ActiveMainService {

    @Resource
    private ActiveMainMapper activeMainMapper;
    @Resource
    private ActiveRuleService ruleService;
    @Resource
    private ActiveRuleCouponService activeRuleCouponService;



    /**
     * 保存营销活动
     *
     * @param saveReqVO ActiveMainSaveReqVO
     * @author PQZ
     * @date 14:14 2024/9/19
     **/
    @Override
    public void saveActiveMain(ActiveMainSaveReqVO saveReqVO) {
        //保存活动
        ActiveMain activeMain = BeanUtils.toBean(saveReqVO, ActiveMain.class);
        saveOrUpdate(activeMain);
        //保存规则
        if (!saveReqVO.getRuleList().isEmpty()){
            ruleService.saveActiveRule(saveReqVO.getRuleList(),activeMain.getId());
        }
        //保存卡券关联关系
        activeRuleCouponService.saveActiveRuleCoupon(saveReqVO.getCouponList(),activeMain.getId(),ACTIVE_RULE_COUPON_ACTIVE);
    }

    /**
     * 删除活动
     *
     * @param id 活动id
     * @author PQZ
     * @date 18:27 2024/9/19
     **/
    @Override
    public void deleteActiveMain(String id) {
        // 删除
        activeMainMapper.deleteById(id);
    }


    /**
     * 根据营销活动获取详情
     *
     * @param id 营销活动id
     * @return cn.iocoder.yudao.module.member.vo.ActiveMainRespVO
     * @author PQZ
     * @date 18:23 2024/9/19
     **/
    @Override
    public ActiveMainRespVO getActiveMain(String id,boolean isChild) {
        ActiveMain activeMain = activeMainMapper.selectById(id);
        ActiveMainRespVO result = BeanUtils.toBean(activeMain, ActiveMainRespVO.class);
        //查询关联规则
        List<ActiveRule> ruleList = ruleService.listByActiveId(id);
        //查询关联卡券
        List<MemberCoupon> couponList = activeRuleCouponService.queryByMainId(id,isChild);
        result.setRuleList(ruleList);
        result.setCouponList(couponList);
        return result;
    }

    /**
     * 分页查询营销活动
     *
     * @param pageReqVO ActiveMainPageReqVO实体
     * @param page      分页参数
     * @return com.baomidou.mybatisplus.core.metadata.IPage<cn.iocoder.yudao.module.member.vo.ActiveMainRespVO>
     * @author PQZ
     * @date 14:17 2024/9/19
     **/
    @Override
    public IPage<ActiveMainRespVO> queryListPage(ActiveMainPageReqVO pageReqVO, Page<ActiveMainRespVO> page) {
        return activeMainMapper.selectListPage(pageReqVO, page);
    }

    /**
     * 不分页查询可参与的活动规则
     *
     * @param pageReqVO ActiveMainPageReqVO实体
     * @return java.util.List<cn.iocoder.yudao.module.member.vo.ActiveMainRespVO>
     * @author PQZ
     * @date 16:11 2024/9/23
     **/
    @Override
    public List<ActiveMainRespVO> listActive(ActiveMainPageReqVO pageReqVO) {
         return activeMainMapper.listActiveMain(pageReqVO);
    }


}