/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.rechargeplanconfig.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.user.enums.BizUserStatusEnum;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.biz.modular.rechargeplanconfig.entity.BizRechargePlanConfig;
import vip.xiaonuo.biz.modular.rechargeplanconfig.mapper.BizRechargePlanConfigMapper;
import vip.xiaonuo.biz.modular.rechargeplanconfig.param.BizRechargePlanConfigAddParam;
import vip.xiaonuo.biz.modular.rechargeplanconfig.param.BizRechargePlanConfigEditParam;
import vip.xiaonuo.biz.modular.rechargeplanconfig.param.BizRechargePlanConfigIdParam;
import vip.xiaonuo.biz.modular.rechargeplanconfig.param.BizRechargePlanConfigPageParam;
import vip.xiaonuo.biz.modular.rechargeplanconfig.service.BizRechargePlanConfigService;

import java.util.List;

/**
 * 充值方案配置Service接口实现类
 *
 * @author wulei
 * @date 2025/02/04 16:32
 **/
@Service
public class BizRechargePlanConfigServiceImpl extends ServiceImpl<BizRechargePlanConfigMapper, BizRechargePlanConfig> implements BizRechargePlanConfigService {

    @Override
    public Page<BizRechargePlanConfig> page(BizRechargePlanConfigPageParam bizRechargePlanConfigPageParam) {
        QueryWrapper<BizRechargePlanConfig> queryWrapper = new QueryWrapper<BizRechargePlanConfig>().checkSqlInjection();
        if (ObjectUtil.isNotEmpty(bizRechargePlanConfigPageParam.getStatus())) {
            queryWrapper.eq("brpc.status", bizRechargePlanConfigPageParam.getStatus());
        }
        if (ObjectUtil.isNotEmpty(bizRechargePlanConfigPageParam.getOrgId())){
            queryWrapper.eq("brpc.org_id",bizRechargePlanConfigPageParam.getOrgId());
        }
        queryWrapper.eq("brpc.delete_flag","NOT_DELETE");
        queryWrapper.orderByDesc("brpc.create_time");
        return this.getBaseMapper().getPageList(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(BizRechargePlanConfigAddParam bizRechargePlanConfigAddParam) {
        BizRechargePlanConfig bizRechargePlanConfig = BeanUtil.toBean(bizRechargePlanConfigAddParam, BizRechargePlanConfig.class);
        if(ObjectUtil.isEmpty(bizRechargePlanConfigAddParam.getHelpCount())){
            bizRechargePlanConfig.setHelpCount(1);
        }
        this.save(bizRechargePlanConfig);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(BizRechargePlanConfigEditParam bizRechargePlanConfigEditParam) {
        BizRechargePlanConfig bizRechargePlanConfig = this.queryEntity(bizRechargePlanConfigEditParam.getId());
        BeanUtil.copyProperties(bizRechargePlanConfigEditParam, bizRechargePlanConfig);
        if(StringUtils.equals(bizRechargePlanConfigEditParam.getIsAccount(),"1")){
            bizRechargePlanConfig.setHelpCount(1);
        }
        this.updateById(bizRechargePlanConfig);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<BizRechargePlanConfigIdParam> bizRechargePlanConfigIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(bizRechargePlanConfigIdParamList, BizRechargePlanConfigIdParam::getId));
    }

    @Override
    public BizRechargePlanConfig detail(BizRechargePlanConfigIdParam bizRechargePlanConfigIdParam) {
        return this.queryEntity(bizRechargePlanConfigIdParam.getId());
    }

    @Override
    public BizRechargePlanConfig queryEntity(String id) {
        BizRechargePlanConfig bizRechargePlanConfig = this.getById(id);
        if (ObjectUtil.isEmpty(bizRechargePlanConfig)) {
            throw new CommonException("充值配置信息不存在");
        }
        return bizRechargePlanConfig;
    }

    @Override
    public void enableStatus(BizRechargePlanConfigIdParam bizRechargePlanConfigIdParam) {
        BizRechargePlanConfig bizRechargePlanConfig = queryEntity(bizRechargePlanConfigIdParam.getId());
        bizRechargePlanConfig.setStatus(BizUserStatusEnum.ENABLE.getValue());
        this.updateById(bizRechargePlanConfig);
    }

    @Override
    public void disableStatus(BizRechargePlanConfigIdParam bizRechargePlanConfigIdParam) {
        BizRechargePlanConfig bizRechargePlanConfig = queryEntity(bizRechargePlanConfigIdParam.getId());
        bizRechargePlanConfig.setStatus(BizUserStatusEnum.DISABLED.getValue());
        this.updateById(bizRechargePlanConfig);
    }
}
