package com.hzs.activity.tree.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.IAcBaseConfigService;
import com.hzs.activity.base.service.IAcGoalMemberConfigService;
import com.hzs.activity.base.service.IActivityService;
import com.hzs.activity.tree.mapper.AcTreeRuleConfigMapper;
import com.hzs.activity.tree.service.IAcTreeRuleConfigService;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.activity.base.AcBaseConfig;
import com.hzs.common.domain.activity.base.AcGoalMemberConfig;
import com.hzs.common.domain.activity.tree.AcTreeRuleConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 植树公益配置规格表 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2023-06-09
 */
@Service
public class AcTreeRuleConfigServiceImpl extends ServiceImpl<AcTreeRuleConfigMapper, AcTreeRuleConfig> implements IAcTreeRuleConfigService {
    @Autowired
    private IAcGoalMemberConfigService goalMemberConfigService;
    @Autowired
    private IActivityService activityService;
    @Autowired
    private IAcBaseConfigService baseConfigService;
    /**
     * 植树配置添加
     * @param activityParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveTreeActivity(ActivityParam activityParam) {
        // 基础配置
        AcBaseConfig baseConfig = activityService.saveBaseConfig(activityParam);
        // 目标群体配置
        if (activityParam.getGoalMemberConfigList() != null){
            activityService.saveGoalMemberConfig(activityParam, baseConfig);
        }
        saveTreeRuleConfig(activityParam.getTreeRuleConfigList(),baseConfig);

    }
    /**
     * 植树配置修改
     * @param activityParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTreeActivity(ActivityParam activityParam) {
        AcBaseConfig baseConfig = baseConfigService.getById(activityParam.getPkId());
        BeanUtil.copyProperties(activityParam,baseConfig);
        baseConfigService.updateById(baseConfig);
        QueryWrapper<AcGoalMemberConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_BASE_ID",activityParam.getPkId());
        goalMemberConfigService.remove(queryWrapper);
        QueryWrapper<AcTreeRuleConfig> tr = new QueryWrapper<>();
        tr.eq("PK_BASE_ID",activityParam.getPkId());
        remove(tr);
        // 目标群体配置
        if (activityParam.getGoalMemberConfigList() != null){
            activityService.saveGoalMemberConfig(activityParam, baseConfig);
        }
        saveTreeRuleConfig(activityParam.getTreeRuleConfigList(),baseConfig);
    }
    /**
     * 删除植树配置
     * @param pkId
     */
    @Override
    public void removeTreeActivity(Long pkId) {
        baseConfigService.removeById(pkId);
        QueryWrapper<AcGoalMemberConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_BASE_ID",pkId);
        goalMemberConfigService.remove(queryWrapper);
        QueryWrapper<AcTreeRuleConfig> tr = new QueryWrapper<>();
        tr.eq("PK_BASE_ID",pkId);
        remove(tr);
    }

    /**
     * 新增植树配置
     * @param treeRuleConfigList
     */
    private void saveTreeRuleConfig(List<AcTreeRuleConfig> treeRuleConfigList, AcBaseConfig baseConfig){
        List<AcTreeRuleConfig> configList = treeRuleConfigList.stream().map(tr -> {
            tr.setCreationTime(DateUtils.currentDateTime());
            tr.setPkCountry(baseConfig.getPkCountry());
            tr.setPkBaseId(baseConfig.getPkId());
            tr.setPkCreator(baseConfig.getPkCreator());
            return tr;
        }).collect(Collectors.toList());
        saveBatch(configList);
    }
}
