package org.dromara.ygpassport.service.impl;

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.springframework.stereotype.Service;
import org.dromara.ygpassport.domain.bo.InternshipsBo;
import org.dromara.ygpassport.domain.vo.InternshipsVo;
import org.dromara.ygpassport.domain.Internships;
import org.dromara.ygpassport.mapper.InternshipsMapper;
import org.dromara.ygpassport.service.IInternshipsService;

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

/**
 * 实习情况Service业务层处理
 *
 * @author Lion Li
 * @date 2024-03-16
 */
@RequiredArgsConstructor
@Service
public class InternshipsServiceImpl implements IInternshipsService {

    private final InternshipsMapper baseMapper;

    /**
     * 查询实习情况
     */
    @Override
    public InternshipsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询实习情况列表
     */
    @Override
    public TableDataInfo<InternshipsVo> queryPageList(InternshipsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Internships> lqw = buildQueryWrapper(bo);
        Page<InternshipsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询实习情况列表
     */
    @Override
    public List<InternshipsVo> queryList(InternshipsBo bo) {
        LambdaQueryWrapper<Internships> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 根据学号查询实习情况列表
     */
    @Override
    public List<InternshipsVo> queryListByStuNo(String stuNo){
        return baseMapper.selectVoListByStuNo(stuNo);
    }

    private LambdaQueryWrapper<Internships> buildQueryWrapper(InternshipsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Internships> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStuNo()), Internships::getStuNo, bo.getStuNo());
        lqw.like(StringUtils.isNotBlank(bo.getCompanyName()), Internships::getCompanyName, bo.getCompanyName());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkingHours()), Internships::getWorkingHours, bo.getWorkingHours());
        lqw.eq(StringUtils.isNotBlank(bo.getJob()), Internships::getJob, bo.getJob());
        lqw.eq(StringUtils.isNotBlank(bo.getJobDescription()), Internships::getJobDescription, bo.getJobDescription());
        return lqw;
    }

    /**
     * 新增实习情况
     */
    @Override
    public Boolean insertByBo(InternshipsBo bo) {
        Internships add = MapstructUtils.convert(bo, Internships.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改实习情况
     */
    @Override
    public Boolean updateByBo(InternshipsBo bo) {
        Internships update = MapstructUtils.convert(bo, Internships.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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