package org.dromara.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.domain.suggestionFj.SuggestionFj;
import org.dromara.domain.suggestionFj.bo.SuggestionFjBo;
import org.dromara.domain.suggestionFj.vo.SuggestionFjVo;
import org.dromara.mapper.SuggestionFjMapper;
import org.dromara.service.ISuggestionFjService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 投诉建议_附件Service业务层处理
 *
 * @author boman
 * @date 2025-04-11
 */
@RequiredArgsConstructor
@Service
public class SuggestionFjServiceImpl implements ISuggestionFjService {

    private final SuggestionFjMapper baseMapper;

    /**
     * 查询投诉建议_附件
     *
     * @param suggestionFjId 主键
     * @return 投诉建议_附件
     */
    @Override
    public SuggestionFjVo queryById(Long suggestionFjId){
        return baseMapper.selectVoById(suggestionFjId);
    }

    /**
     * 分页查询投诉建议_附件列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 投诉建议_附件分页列表
     */
    @Override
    public TableDataInfo<SuggestionFjVo> queryPageList(SuggestionFjBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SuggestionFj> lqw = buildQueryWrapper(bo);
        Page<SuggestionFjVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的投诉建议_附件列表
     *
     * @param bo 查询条件
     * @return 投诉建议_附件列表
     */
    @Override
    public List<SuggestionFjVo> queryList(SuggestionFjBo bo) {
        LambdaQueryWrapper<SuggestionFj> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SuggestionFj> buildQueryWrapper(SuggestionFjBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SuggestionFj> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(SuggestionFj::getSuggestionFjId);
        lqw.eq(bo.getSuggestionId() != null, SuggestionFj::getSuggestionId, bo.getSuggestionId());
        lqw.like(StringUtils.isNotBlank(bo.getOldName()), SuggestionFj::getOldName, bo.getOldName());
        lqw.like(StringUtils.isNotBlank(bo.getName()), SuggestionFj::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), SuggestionFj::getUrl, bo.getUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getBigType()), SuggestionFj::getBigType, bo.getBigType());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), SuggestionFj::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增投诉建议_附件
     *
     * @param bo 投诉建议_附件
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SuggestionFjBo bo) {
        SuggestionFj add = MapstructUtils.convert(bo, SuggestionFj.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSuggestionFjId(add.getSuggestionFjId());
        }
        return flag;
    }

    /**
     * 修改投诉建议_附件
     *
     * @param bo 投诉建议_附件
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SuggestionFjBo bo) {
        SuggestionFj update = MapstructUtils.convert(bo, SuggestionFj.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SuggestionFj entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除投诉建议_附件信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
