package com.zsElectric.boot.business.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsElectric.boot.business.mapper.UserExchangeIntegralRuleMapper;
import com.zsElectric.boot.business.service.UserExchangeIntegralRuleService;
import com.zsElectric.boot.business.model.entity.UserExchangeIntegralRule;
import com.zsElectric.boot.business.model.form.UserExchangeIntegralRuleForm;
import com.zsElectric.boot.business.model.query.UserExchangeIntegralRuleQuery;
import com.zsElectric.boot.business.model.vo.UserExchangeIntegralRuleVO;
import com.zsElectric.boot.business.converter.UserExchangeIntegralRuleConverter;

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

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import org.springframework.transaction.annotation.Transactional;

/**
 * 积分兑换规则服务实现类
 *
 * @author zsElectric
 * @since 2025-12-12 10:07
 */
@Service
@RequiredArgsConstructor
public class UserExchangeIntegralRuleServiceImpl extends ServiceImpl<UserExchangeIntegralRuleMapper, UserExchangeIntegralRule> implements UserExchangeIntegralRuleService {

    private final UserExchangeIntegralRuleConverter userExchangeIntegralRuleConverter;

    /**
    * 获取积分兑换规则分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<UserExchangeIntegralRuleVO>} 积分兑换规则分页列表
    */
    @Override
    public IPage<UserExchangeIntegralRuleVO> getUserExchangeIntegralRulePage(UserExchangeIntegralRuleQuery queryParams) {
        Page<UserExchangeIntegralRuleVO> pageVO = this.baseMapper.getUserExchangeIntegralRulePage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取积分兑换规则表单数据
     *
     * @param id 积分兑换规则ID
     * @return 积分兑换规则表单数据
     */
    @Override
    public UserExchangeIntegralRuleForm getUserExchangeIntegralRuleFormData(Long id) {
        UserExchangeIntegralRule entity = this.getById(id);
        return userExchangeIntegralRuleConverter.toForm(entity);
    }
    
    /**
     * 新增积分兑换规则
     *
     * @param formData 积分兑换规则表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveUserExchangeIntegralRule(UserExchangeIntegralRuleForm formData) {
        UserExchangeIntegralRule entity = userExchangeIntegralRuleConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新积分兑换规则
     *
     * @param id   积分兑换规则ID
     * @param formData 积分兑换规则表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateUserExchangeIntegralRule(Long id,UserExchangeIntegralRuleForm formData) {
        UserExchangeIntegralRule entity = userExchangeIntegralRuleConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除积分兑换规则
     *
     * @param ids 积分兑换规则ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteUserExchangeIntegralRules(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的积分兑换规则数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean editStatus(Long id) {
        UserExchangeIntegralRule rule = this.baseMapper.selectById(id);
        if (ObjUtil.isNull(rule)) {
            throw new RuntimeException("积分兑换规则不存在");
        }
            int status;
            if (rule.getStatus() == 0) {
                status = 1;
            } else {
                status = 0;
            }
            // 判断参数值，表只允许存在1个状态为1的数据
            if (status == 1) {
                // 将当前状态为1的记录状态设为0
                this.update(Wrappers.lambdaUpdate(UserExchangeIntegralRule.class)
                        .eq(UserExchangeIntegralRule::getStatus, 1)
                        .set(UserExchangeIntegralRule::getStatus, 0));
            }
            // 更新目标记录的状态
            rule.setStatus(status);
            return this.updateById(rule);
    }

}
