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

import com.zsElectric.boot.business.service.UserFeedbackService;
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.UserFeedbackMapper;
import com.zsElectric.boot.business.model.entity.UserFeedback;
import com.zsElectric.boot.business.model.form.UserFeedbackForm;
import com.zsElectric.boot.business.model.query.UserFeedbackQuery;
import com.zsElectric.boot.business.model.vo.UserFeedbackVO;
import com.zsElectric.boot.business.converter.UserFeedbackConverter;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 用户反馈服务实现类
 *
 * @author wzq
 * @since 2025-11-26 15:37
 */
@Service
@RequiredArgsConstructor
public class UserFeedbackServiceImpl extends ServiceImpl<UserFeedbackMapper, UserFeedback> implements UserFeedbackService {

    private final UserFeedbackConverter userFeedbackConverter;

    /**
    * 获取用户反馈分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<UserFeedbackVO>} 用户反馈分页列表
    */
    @Override
    public IPage<UserFeedbackVO> getUserFeedbackPage(UserFeedbackQuery queryParams) {
        Page<UserFeedbackVO> pageVO = this.baseMapper.getUserFeedbackPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取用户反馈表单数据
     *
     * @param id 用户反馈ID
     * @return 用户反馈表单数据
     */
    @Override
    public UserFeedbackForm getUserFeedbackFormData(Long id) {
        UserFeedback entity = this.getById(id);
        return userFeedbackConverter.toForm(entity);
    }
    
    /**
     * 新增用户反馈
     *
     * @param formData 用户反馈表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveUserFeedback(UserFeedbackForm formData) {
        UserFeedback entity = userFeedbackConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新用户反馈
     *
     * @param id   用户反馈ID
     * @param formData 用户反馈表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateUserFeedback(Long id,UserFeedbackForm formData) {
        UserFeedback entity = userFeedbackConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除用户反馈
     *
     * @param ids 用户反馈ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteUserFeedbacks(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的用户反馈数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
