package org.dromara.game.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.game.domain.PlatChannel;
import org.dromara.game.domain.bo.PlatChannelBo;
import org.dromara.game.domain.vo.PlatChannelVo;
import org.dromara.game.mapper.PlatChannelMapper;
import org.dromara.game.service.IPlatChannelService;
import org.springframework.stereotype.Service;

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

/**
 * 渠道Service业务层处理
 *
 * @author Hixon
 * @date 2024-08-07
 */
@RequiredArgsConstructor
@Service
public class PlatChannelServiceImpl implements IPlatChannelService {

    private final PlatChannelMapper baseMapper;

    /**
     * 查询渠道
     *
     * @param id 主键
     * @return 渠道
     */
    @Override
    public PlatChannelVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

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

    /**
     * 查询符合条件的渠道列表
     *
     * @param bo 查询条件
     * @return 渠道列表
     */
    @Override
    public List<PlatChannelVo> queryList(PlatChannelBo bo) {
        LambdaQueryWrapper<PlatChannel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PlatChannel> buildQueryWrapper(PlatChannelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PlatChannel> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProductId() != null, PlatChannel::getProductId, bo.getProductId());
        lqw.eq(bo.getProxyId() != null, PlatChannel::getProxyId, bo.getProxyId());
        lqw.eq(StringUtils.isNotBlank(bo.getAccountId()), PlatChannel::getAccountId, bo.getAccountId());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), PlatChannel::getProductName, bo.getProductName());
        lqw.eq(StringUtils.isNotBlank(bo.getPlatType()), PlatChannel::getPlatType, bo.getPlatType());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), PlatChannel::getUserName, bo.getUserName());
        lqw.like(StringUtils.isNotBlank(bo.getName()), PlatChannel::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCompany()), PlatChannel::getCompany, bo.getCompany());
        lqw.eq(StringUtils.isNotBlank(bo.getOperatingMode()), PlatChannel::getOperatingMode, bo.getOperatingMode());
        lqw.eq(StringUtils.isNotBlank(bo.getDeliveryMode()), PlatChannel::getDeliveryMode, bo.getDeliveryMode());
        lqw.eq(StringUtils.isNotBlank(bo.getActiveRules()), PlatChannel::getActiveRules, bo.getActiveRules());
        lqw.eq(StringUtils.isNotBlank(bo.getActionRules()), PlatChannel::getActionRules, bo.getActionRules());
        lqw.eq(StringUtils.isNotBlank(bo.getIsReport()), PlatChannel::getIsReport, bo.getIsReport());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), PlatChannel::getStatus, bo.getStatus());
        return lqw;
    }

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

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

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

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