package com.hai.service.impl;

import com.hai.dao.HighGasChannelConfigMapper;
import com.hai.entity.HighGasChannelConfig;
import com.hai.entity.HighGasChannelConfigExample;
import com.hai.enum_type.GasChannel;
import com.hai.enum_type.GasChannelPayPlatformType;
import com.hai.service.HighGasChannelConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("gasChannelConfigService")
public class HighGasChannelConfigServiceImpl implements HighGasChannelConfigService {

    @Resource
    private HighGasChannelConfigMapper gasChannelConfigMapper;


    @Override
    public void editConfig(HighGasChannelConfig gasChannelConfig) {
        if (gasChannelConfig.getId() == null) {
            gasChannelConfig.setStatus(1);
            gasChannelConfig.setCreateTime(new Date());
            gasChannelConfig.setUpdateTime(new Date());
            gasChannelConfigMapper.insert(gasChannelConfig);
        } else {
            gasChannelConfig.setUpdateTime(new Date());
            gasChannelConfigMapper.updateByPrimaryKey(gasChannelConfig);
        }
    }

    @Override
    public HighGasChannelConfig getConfig(GasChannel channelId, GasChannelPayPlatformType payPlatformType) {
        HighGasChannelConfigExample example = new HighGasChannelConfigExample();
        example.createCriteria()
                .andChannelIdEqualTo(channelId.getType())
                .andPayPlatformTypeEqualTo(payPlatformType.getType())
                .andStatusEqualTo(1);
        List<HighGasChannelConfig> list = gasChannelConfigMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}
