package com.fuyuanshen.app.service.impl;

import com.fuyuanshen.app.domain.vo.AppFileVo;
import com.fuyuanshen.common.core.utils.MapstructUtils;
import com.fuyuanshen.common.core.utils.StringUtils;
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
import com.fuyuanshen.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.fuyuanshen.app.domain.bo.AppBusinessFileBo;
import com.fuyuanshen.app.domain.vo.AppBusinessFileVo;
import com.fuyuanshen.app.domain.AppBusinessFile;
import com.fuyuanshen.app.mapper.AppBusinessFileMapper;
import com.fuyuanshen.app.service.IAppBusinessFileService;

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

/**
 * app业务文件Service业务层处理
 *
 * @author Lion Li
 * @date 2025-07-03
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AppBusinessFileServiceImpl implements IAppBusinessFileService {

    private final AppBusinessFileMapper baseMapper;

    /**
     * 查询app业务文件
     *
     * @param id 主键
     * @return app业务文件
     */
    @Override
    public AppBusinessFileVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询app业务文件列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return app业务文件分页列表
     */
    @Override
    public TableDataInfo<AppBusinessFileVo> queryPageList(AppBusinessFileBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppBusinessFile> lqw = buildQueryWrapper(bo);
        Page<AppBusinessFileVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的app业务文件列表
     *
     * @param bo 查询条件
     * @return app业务文件列表
     */
    @Override
    public List<AppBusinessFileVo> queryList(AppBusinessFileBo bo) {
        LambdaQueryWrapper<AppBusinessFile> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppBusinessFile> buildQueryWrapper(AppBusinessFileBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppBusinessFile> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(AppBusinessFile::getId);
        lqw.eq(bo.getFileId()!=null, AppBusinessFile::getFileId, bo.getFileId());
        lqw.eq(bo.getBusinessId() != null, AppBusinessFile::getBusinessId, bo.getBusinessId());
        lqw.eq(bo.getFileType() != null, AppBusinessFile::getFileType, bo.getFileType());
        lqw.in(bo.getIds()!=null, AppBusinessFile::getId, bo.getIds());
        return lqw;
    }

    /**
     * 新增app业务文件
     *
     * @param bo app业务文件
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AppBusinessFileBo bo) {
        AppBusinessFile add = MapstructUtils.convert(bo, AppBusinessFile.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改app业务文件
     *
     * @param bo app业务文件
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AppBusinessFileBo bo) {
        AppBusinessFile update = MapstructUtils.convert(bo, AppBusinessFile.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public List<AppFileVo> queryAppFileList(AppBusinessFileBo bo) {
        return baseMapper.queryAppFileList(bo);
    }
}
