package org.dromara.system.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.system.domain.SysPicture;
import org.dromara.system.domain.bo.SysPictureBo;
import org.dromara.system.domain.vo.SysPictureVo;
import org.dromara.system.mapper.SysPictureMapper;
import org.dromara.system.service.ISysPictureService;
import org.springframework.stereotype.Service;

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

/**
 * 画册Service业务层处理
 *
 * @author Maosw
 * @date 2025-02-26
 */
@RequiredArgsConstructor
@Service
public class SysPictureServiceImpl implements ISysPictureService {

    private final SysPictureMapper baseMapper;

    /**
     * 查询画册
     *
     * @param id 主键
     * @return 画册
     */
    @Override
    public SysPictureVo queryById(Long id){
        // 使用 LambdaUpdateWrapper 直接更新浏览量
        baseMapper.update(null, Wrappers.<SysPicture>lambdaUpdate()
            .setIncrBy(SysPicture::getNum, 1)
            .eq(SysPicture::getId, id));
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询画册列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 画册分页列表
     */
    @Override
    public TableDataInfo<SysPictureVo> queryPageList(SysPictureBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysPicture> lqw = buildQueryWrapper(bo);
        Page<SysPictureVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 分页查询画册列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     */
    @Override
    public TableDataInfo<SysPictureVo> selectPageList(SysPictureBo bo, PageQuery pageQuery) {
        bo.setStatus(1L);
        LambdaQueryWrapper<SysPicture> lqw = buildQueryWrapper(bo);
        Page<SysPictureVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的画册列表
     *
     * @param bo 查询条件
     * @return 画册列表
     */
    @Override
    public List<SysPictureVo> queryList(SysPictureBo bo) {
        LambdaQueryWrapper<SysPicture> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysPicture> buildQueryWrapper(SysPictureBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysPicture> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getType() != null, SysPicture::getType, bo.getType());
        lqw.eq(bo.getUserId() != null, SysPicture::getUserId, bo.getUserId());
        lqw.eq(bo.getWork() != null, SysPicture::getWork, bo.getWork());
        lqw.eq(bo.getTenant() != null, SysPicture::getTenant, bo.getTenant());
        lqw.like(StringUtils.isNotBlank(bo.getTitle()), SysPicture::getTitle, bo.getTitle());
        lqw.like(StringUtils.isNotBlank(bo.getWorkName()), SysPicture::getWorkName, bo.getWorkName());
        lqw.eq(bo.getStatus() != null, SysPicture::getStatus, bo.getStatus());
        lqw.eq(bo.getSort() != null, SysPicture::getSort, bo.getSort());
        lqw.in(bo.getFlags() != null, SysPicture::getFlag, Arrays.asList(bo.getFlags().split(",")));
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            SysPicture::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.orderByDesc(SysPicture::getCreateTime, SysPicture::getSort);
        return lqw;
    }

    /**
     * 新增画册
     *
     * @param bo 画册
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SysPictureBo bo) {
        SysPicture add = MapstructUtils.convert(bo, SysPicture.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改画册
     *
     * @param bo 画册
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SysPictureBo bo) {
        SysPicture update = MapstructUtils.convert(bo, SysPicture.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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