package org.dromara.main.service.impl;

import cn.hutool.core.util.ObjectUtil;
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.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.main.domain.MainCompanyApply;
import org.dromara.main.domain.bo.MainCompanyApplyBo;
import org.dromara.main.domain.vo.MainCompanyApplyVo;
import org.dromara.main.mapper.MainCompanyApplyMapper;
import org.dromara.main.service.IMainCompanyApplyService;
import org.springframework.stereotype.Service;

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

@RequiredArgsConstructor
@Service
public class MainCompanyApplyServiceImpl implements IMainCompanyApplyService {

    private final MainCompanyApplyMapper baseMapper;

    @Override
    public TableDataInfo<MainCompanyApplyVo> queryPageList(MainCompanyApplyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MainCompanyApply> lqw = buildQueryWrapper(bo);
        Page<MainCompanyApplyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<MainCompanyApplyVo> queryList(MainCompanyApplyBo bo) {
        LambdaQueryWrapper<MainCompanyApply> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public MainCompanyApplyVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public Boolean insertByBo(MainCompanyApplyBo bo) {
        MainCompanyApply add = MapstructUtils.convert(bo, MainCompanyApply.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(MainCompanyApplyBo bo) {
        MainCompanyApply update = MapstructUtils.convert(bo, MainCompanyApply.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    private LambdaQueryWrapper<MainCompanyApply> buildQueryWrapper(MainCompanyApplyBo bo) {
        LambdaQueryWrapper<MainCompanyApply> lqw = Wrappers.lambdaQuery();
        lqw.like(ObjectUtil.isNotNull(bo.getApplyNo()), MainCompanyApply::getApplyNo, bo.getApplyNo());
        lqw.like(ObjectUtil.isNotNull(bo.getCompanyName()), MainCompanyApply::getCompanyName, bo.getCompanyName());
        lqw.eq(ObjectUtil.isNotNull(bo.getCreditCode()), MainCompanyApply::getCreditCode, bo.getCreditCode());
        lqw.eq(ObjectUtil.isNotNull(bo.getApplyStatus()), MainCompanyApply::getApplyStatus, bo.getApplyStatus());
        lqw.eq(ObjectUtil.isNotNull(bo.getMobile()), MainCompanyApply::getMobile, bo.getMobile());
        lqw.orderByDesc(MainCompanyApply::getCreateTime);
        return lqw;
    }

    private void validEntityBeforeSave(MainCompanyApply entity) {
    }
}
