package com.sptg.pay.biz.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.pay.api.dto.PayChannelConfigDto;
import com.sptg.pay.api.entity.PayChannelConfig;
import com.sptg.pay.biz.mapper.PayChannelConfigMapper;
import com.sptg.pay.biz.service.PayChannelConfigServiceInterface;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;

/**
 * @author zwx
 * @create 2020/5/25 16:21
 */
@Service
public class PayChannelConfigServiceImpl extends ServiceImpl<PayChannelConfigMapper, PayChannelConfig> implements PayChannelConfigServiceInterface {

    @Override
    public ResultBean<Page<PayChannelConfig>> showPayChannelConfigPage(PayChannelConfigDto payChannelConfigDto) {
        // 构建查询条件
        Wrapper<PayChannelConfig> wrapper = new EntityWrapper<>();
        //过滤删除
        wrapper.notIn("i_status", OperationConstant.MINUS_ONE);
        //  排序字段 时间降序
        wrapper.orderBy("t_create_date",false);
        return new ResultBean<>(ResultEnum.SUCCESS, this.selectPage(payChannelConfigDto.getPage(), wrapper));
    }

    @Override
    public ResultBean<Boolean> addPayChannelConfig(PayChannelConfigDto payChannelConfigDto) {
        Assert.notNull(payChannelConfigDto.getId(),"支付渠道配置已存在，请勿重复添加");
        Date today = new Date();
        payChannelConfigDto.setCreateDate(today);
        payChannelConfigDto.setUpdateDate(today);
        if(this.insert(payChannelConfigDto)){
            return new ResultBean<>(ResultEnum.SUCCESS, true);
        }
        return new ResultBean<>(ResultEnum.ERROR, false);
    }

    @Override
    public ResultBean<Boolean> updatePayChannelConfig(PayChannelConfig payChannelConfig) {
        boolean result = this.updateById(payChannelConfig);
        if(result){
            return new ResultBean<>(ResultEnum.SUCCESS,result);
        }
        return new ResultBean<>(ResultEnum.ERROR,result);
    }

    @Override
    public PayChannelConfig findPayChannelConfigById(Long id) {
        Wrapper<PayChannelConfig> wrapper = new EntityWrapper<>();
        wrapper.eq("l_id",id);
        //过滤删除
        wrapper.notIn("i_status", OperationConstant.MINUS_ONE);
        return this.selectOne(wrapper);
    }

    @Override
    public PayChannelConfig findPayChannelConfigByCondition(PayChannelConfigDto payChannelConfigDto) {
        Assert.notNull(payChannelConfigDto, "查询对象不能空");
        Wrapper<PayChannelConfig> wrapper = new EntityWrapper<>();
        //过滤删除
        wrapper.notIn("i_status", OperationConstant.MINUS_ONE);
        return this.baseMapper.findPayChannelConfigByCondition(payChannelConfigDto, wrapper);
    }
}
