package org.dromara.ygpassport.service.impl;

import com.github.yulichang.base.MPJBaseServiceImpl;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.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 org.dromara.ygpassport.domain.CombatProject;
import org.dromara.ygpassport.mapper.CombatProjectNewMapper;
import org.dromara.ygpassport.mapper.StudentAlbumNewMapper;
import org.springframework.stereotype.Service;
import org.dromara.ygpassport.domain.bo.StudentAlbumBo;
import org.dromara.ygpassport.domain.vo.StudentAlbumVo;
import org.dromara.ygpassport.domain.StudentAlbum;
import org.dromara.ygpassport.mapper.StudentAlbumMapper;
import org.dromara.ygpassport.service.IStudentAlbumService;

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

/**
 * 学生相册Service业务层处理
 *
 * @author Lion Li
 * @date 2024-09-12
 */
@RequiredArgsConstructor
@Service
public class StudentAlbumServiceImpl extends MPJBaseServiceImpl<StudentAlbumNewMapper, StudentAlbum> implements IStudentAlbumService {

    private final StudentAlbumMapper baseMapper;

    private final StudentAlbumNewMapper newMapper;

    /**
     * 查询学生相册
     */
    @Override
    public StudentAlbumVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询学生相册列表
     */
    @Override
    public TableDataInfo<StudentAlbumVo> queryPageList(StudentAlbumBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StudentAlbum> lqw = buildQueryWrapper(bo);
        Page<StudentAlbumVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询学生相册列表
     */
    @Override
    public List<StudentAlbumVo> queryList(StudentAlbumBo bo) {
        LambdaQueryWrapper<StudentAlbum> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StudentAlbum> buildQueryWrapper(StudentAlbumBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StudentAlbum> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStuNo() != null, StudentAlbum::getStuNo, bo.getStuNo());
        lqw.eq(bo.getPhotoId() != null, StudentAlbum::getPhotoId, bo.getPhotoId());
        lqw.eq(bo.getType() != null, StudentAlbum::getType, bo.getType());
        lqw.eq(bo.getStatus() != null, StudentAlbum::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增学生相册
     */
    @Override
    public Boolean insertByBo(StudentAlbumBo bo) {
        StudentAlbum add = MapstructUtils.convert(bo, StudentAlbum.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改学生相册
     */
    @Override
    public Boolean updateByBo(StudentAlbumBo bo) {
        StudentAlbum update = MapstructUtils.convert(bo, StudentAlbum.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除学生相册
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
