package com.hzs.activity.register.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.activity.base.param.ActivityParam;
import com.hzs.activity.base.service.IActivityService;
import com.hzs.activity.register.mapper.AcRegisterGiftMapper;
import com.hzs.activity.register.param.RegisterGiftParam;
import com.hzs.activity.register.service.IAcRegisterGiftService;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.activity.base.AcBaseConfig;
import com.hzs.common.domain.activity.register.AcRegisterGift;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 注册赠送 服务实现类
 */
@Service
public class AcRegisterGiftServiceImpl extends ServiceImpl<AcRegisterGiftMapper, AcRegisterGift> implements IAcRegisterGiftService {

    @Autowired
    private IAcRegisterGiftService registerGiftService;
    @Autowired
    private IActivityService activityService;

    @Override
    public void createRegisterGift(ActivityParam activityParam) {
        Long pkId = activityService.saveActivity(activityParam);
        List<AcRegisterGift> list = new ArrayList<>();
        for (RegisterGiftParam registerGiftParam : activityParam.getRegisterGiftParamList()) {
            AcRegisterGift registerGift = BeanUtil.copyProperties(registerGiftParam, AcRegisterGift.class);
            registerGift.setCreationTime(DateUtils.currentDateTime());
            registerGift.setPkBaseId(pkId);
            registerGift.setPkCreator(activityParam.getPkCreator());
            list.add(registerGift);
        }
        registerGiftService.saveBatch(list);
    }

    @Override
    public void updateRegisterGift(ActivityParam activityParam) {
        AcBaseConfig baseConfig = activityService.updateBaseConfig(activityParam);
        List<AcRegisterGift> list = new ArrayList<>();
        registerGiftService.remove(new QueryWrapper<AcRegisterGift>().eq("PK_BASE_ID", baseConfig.getPkId()));
        for (RegisterGiftParam registerGiftParam : activityParam.getRegisterGiftParamList()) {
            AcRegisterGift registerGift = BeanUtil.copyProperties(registerGiftParam, AcRegisterGift.class);
            registerGift.setModifiedTime(DateUtils.currentDateTime());
            registerGift.setPkBaseId(baseConfig.getPkId());
            registerGift.setPkModified(activityParam.getPkCreator());
            registerGift.setCreationTime(DateUtils.currentDateTime());
            registerGift.setPkCreator(activityParam.getPkCreator());
            list.add(registerGift);
        }
        registerGiftService.saveBatch(list);
    }


}
