package org.dromara.payment.common.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.payment.common.domain.bo.BusOperLogBo;
import org.dromara.payment.common.domain.vo.BusOperLogVo;
import org.dromara.payment.common.domain.BusOperLog;
import org.dromara.payment.common.mapper.BusOperLogMapper;
import org.dromara.payment.common.service.IBusOperLogService;

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

/**
 * 业务操作日志Service业务层处理
 *
 * @author LionLi
 * @date 2024-04-10
 */
@RequiredArgsConstructor
@Service
public class BusOperLogServiceImpl implements IBusOperLogService {

    private final BusOperLogMapper baseMapper;

    /**
     * 查询业务操作日志
     */
    @Override
    public BusOperLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询业务操作日志列表
     */
    @Override
    public TableDataInfo<BusOperLogVo> queryPageList(BusOperLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BusOperLog> lqw = buildQueryWrapper(bo);
        Page<BusOperLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询业务操作日志列表
     */
    @Override
    public List<BusOperLogVo> queryList(BusOperLogBo bo) {
        LambdaQueryWrapper<BusOperLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BusOperLog> buildQueryWrapper(BusOperLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BusOperLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getLogDesc()), BusOperLog::getLogDesc, bo.getLogDesc());
        lqw.eq(bo.getCreateRoleId() != null, BusOperLog::getCreateRoleId, bo.getCreateRoleId());
        lqw.like(StringUtils.isNotBlank(bo.getCreateRoleName()), BusOperLog::getCreateRoleName, bo.getCreateRoleName());
        lqw.eq(bo.getBusId() != null, BusOperLog::getBusId, bo.getBusId());
        lqw.eq(bo.getBusType() != null, BusOperLog::getBusType, bo.getBusType());
        return lqw;
    }

    /**
     * 新增业务操作日志
     */
    @Override
    public Boolean insertByBo(BusOperLogBo bo) {
        BusOperLog add = MapstructUtils.convert(bo, BusOperLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改业务操作日志
     */
    @Override
    public Boolean updateByBo(BusOperLogBo bo) {
        BusOperLog update = MapstructUtils.convert(bo, BusOperLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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