package org.dromara.work.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.work.domain.TpRemittance;
import org.dromara.work.domain.bo.TpRemittanceBo;
import org.dromara.work.domain.vo.TpRemittanceVo;
import org.dromara.work.mapper.TpRemittanceMapper;
import org.dromara.work.service.ITpRemittanceService;
import org.springframework.stereotype.Service;

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

/**
 * 汇款Service业务层处理
 *
 * @author Maosw
 * @date 2024-08-12
 */
@RequiredArgsConstructor
@Service
public class TpRemittanceServiceImpl implements ITpRemittanceService {

    private final TpRemittanceMapper baseMapper;

    /**
     * 查询汇款
     *
     * @param id 主键
     * @return 汇款
     */
    @Override
    public TpRemittanceVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

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

    /**
     * 查询符合条件的汇款列表
     *
     * @param bo 查询条件
     * @return 汇款列表
     */
    @Override
    public List<TpRemittanceVo> queryList(TpRemittanceBo bo) {
        LambdaQueryWrapper<TpRemittance> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TpRemittance> buildQueryWrapper(TpRemittanceBo bo) {
        LambdaQueryWrapper<TpRemittance> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAlias()), TpRemittance::getAlias, bo.getAlias());
        lqw.eq(bo.getQid() != null, TpRemittance::getQid, bo.getQid());
        lqw.eq(bo.getType() != null, TpRemittance::getType, bo.getType());
        lqw.like(StringUtils.isNotBlank(bo.getName()), TpRemittance::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getQrcode()), TpRemittance::getQrcode, bo.getQrcode());
        lqw.eq(bo.getOpentime() != null, TpRemittance::getOpentime, bo.getOpentime());
        lqw.eq(StringUtils.isNotBlank(bo.getLink()), TpRemittance::getLink, bo.getLink());
        lqw.eq(bo.getState() != null, TpRemittance::getState, bo.getState());
        lqw.eq(bo.getAddUser() != null, TpRemittance::getAddUser, bo.getAddUser());
        lqw.eq(bo.getIsDel() != null, TpRemittance::getIsDel, bo.getIsDel());
        lqw.eq(bo.getAddTime() != null, TpRemittance::getAddTime, bo.getAddTime());
        return lqw;
    }

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

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

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

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