package com.hfkj.mer.impl;

import com.hfkj.dao.BsMerPayConfigMapper;
import com.hfkj.entity.BsMerPayConfig;
import com.hfkj.entity.BsMerPayConfigExample;
import com.hfkj.mer.BsMerPayConfigService;
import com.hfkj.sysenum.mer.PayPlatformEnum;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

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

@Service("BsMerPayConfigService")
public class BsMerPayConfigServiceImpl implements BsMerPayConfigService {

    @Resource
    private BsMerPayConfigMapper bsMerPayConfigMapper;

    @Override
    public void create(BsMerPayConfig merPayConfig) {
        bsMerPayConfigMapper.insert(merPayConfig);
    }

    @Override
    public void update(BsMerPayConfig merPayConfig) {
        bsMerPayConfigMapper.updateByPrimaryKeySelective(merPayConfig);
    }

    @Override
    public BsMerPayConfig getMerPay(Long merId, PayPlatformEnum payPlatform) {
        BsMerPayConfigExample example = new BsMerPayConfigExample();
        example.createCriteria()
                .andMerIdEqualTo(merId)
                .andPayPlatformEqualTo(payPlatform.getCode())
                .andStatusNotEqualTo(0);
        List<BsMerPayConfig> list = bsMerPayConfigMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void delete(Long id, Boolean fullDelete) {
        if (fullDelete) {
            bsMerPayConfigMapper.deleteByPrimaryKey(id);
        } else {
            BsMerPayConfig merPayConfig = queryDetail(id);
            merPayConfig.setStatus(0);
            merPayConfig.setUpdateTime(new Date());
            update(merPayConfig);
        }
    }

    @Override
    public BsMerPayConfig queryDetail(Long id) {
        return bsMerPayConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    public BsMerPayConfig findGoodsOrder(Long merId) {
        BsMerPayConfigExample example = new BsMerPayConfigExample();
        BsMerPayConfigExample.Criteria criteria = example.createCriteria();

        criteria.andMerIdEqualTo(merId);

        List<BsMerPayConfig> list = bsMerPayConfigMapper.selectByExample(example);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsMerPayConfig queryDetailByMap(Map<String, Object> map) {
        BsMerPayConfigExample example = new BsMerPayConfigExample();
        BsMerPayConfigExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(map, "merId") != null) {
            criteria.andMerIdEqualTo(MapUtils.getLong(map, "merId"));
        }
        if (MapUtils.getInteger(map, "payPlatform") != null) {
            criteria.andPayPlatformEqualTo(MapUtils.getInteger(map, "payPlatform"));
        }

        criteria.andStatusEqualTo(1);

        List<BsMerPayConfig> list = bsMerPayConfigMapper.selectByExample(example);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<BsMerPayConfig> getList(Map<String, Object> map) {
        BsMerPayConfigExample example = new BsMerPayConfigExample();
        BsMerPayConfigExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(map, "merId") != null) {
            criteria.andMerIdEqualTo(MapUtils.getLong(map, "merId"));
        }
        if (MapUtils.getString(map, "merNo") != null) {
            criteria.andMerNoEqualTo(MapUtils.getString(map, "merNo"));
        }
        criteria.andStatusEqualTo(1);
        return bsMerPayConfigMapper.selectByExample(example);
    }
}
