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.mapper.ArchivesGoNewMapper;
import org.springframework.stereotype.Service;
import org.dromara.ygpassport.domain.bo.ArchivesGoBo;
import org.dromara.ygpassport.domain.vo.ArchivesGoVo;
import org.dromara.ygpassport.domain.ArchivesGo;
import org.dromara.ygpassport.mapper.ArchivesGoMapper;
import org.dromara.ygpassport.service.IArchivesGoService;

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

/**
 * 档案去向Service业务层处理
 *
 * @author Lion Li
 * @date 2024-01-11
 */
@RequiredArgsConstructor
@Service
public class ArchivesGoServiceImpl extends MPJBaseServiceImpl<ArchivesGoNewMapper, ArchivesGo> implements IArchivesGoService {

    private final ArchivesGoMapper baseMapper;

    private final ArchivesGoNewMapper newMapper;

    /**
     * 查询档案去向
     */
    @Override
    public ArchivesGoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询档案去向列表
     */
    @Override
    public TableDataInfo<ArchivesGoVo> queryPageList(ArchivesGoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ArchivesGo> lqw = buildQueryWrapper(bo);
        Page<ArchivesGoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询档案去向列表
     */
    @Override
    public List<ArchivesGoVo> queryList(ArchivesGoBo bo) {
        LambdaQueryWrapper<ArchivesGo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ArchivesGo> buildQueryWrapper(ArchivesGoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ArchivesGo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStuNo()), ArchivesGo::getStuNo, bo.getStuNo());
        lqw.eq(StringUtils.isNotBlank(bo.getExperienceFilePath()), ArchivesGo::getExperienceFilePath, bo.getExperienceFilePath());
        return lqw;
    }

    /**
     * 新增档案去向
     */
    @Override
    public Boolean insertByBo(ArchivesGoBo bo) {
        ArchivesGo add = MapstructUtils.convert(bo, ArchivesGo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改档案去向
     */
    @Override
    public Boolean updateByBo(ArchivesGoBo bo) {
        ArchivesGo update = MapstructUtils.convert(bo, ArchivesGo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public List<ArchivesGoVo> queryListByStuNo(String stuNo) {
        return baseMapper.selectVoListByStuNo(stuNo);
    }

    @Override
    public ArchivesGoVo queryByStuNo(String stuNo) {
        return baseMapper.selectVoByStuNo(stuNo);
    }


}
