package com.hfkj.service.impl;

import com.hfkj.channel.lakala.LaKaLaLedgerService;
import com.hfkj.channel.lakala.LaKaLaMerService;
import com.hfkj.dao.BsMerLedgerReceiverMapper;
import com.hfkj.entity.BsMerLedgerReceiver;
import com.hfkj.entity.BsMerLedgerReceiverExample;
import com.hfkj.service.BsMerLedgerReceiverService;
import com.hfkj.sysenum.MerLedgerReceiverStatusEnum;
import com.hfkj.sysenum.PlatformTypeEnum;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @className: BsMerLedgerReceiverServiceImpl
 * @author: HuRui
 * @date: 2023/4/19
 **/
@Service("merLedgerReceiverService")
public class BsMerLedgerReceiverServiceImpl implements BsMerLedgerReceiverService {

    @Resource
    private BsMerLedgerReceiverMapper merLedgerReceiverMapper;

    @Resource
    private LaKaLaLedgerService laKaLaLedgerService;

    @Override
    public void editMerLedgerReceiver(BsMerLedgerReceiver merLedgerReceiver) {
        if (merLedgerReceiver.getId() == null) {
            merLedgerReceiver.setCreateTime(new Date());
            merLedgerReceiver.setUpdateTime(new Date());
            merLedgerReceiverMapper.insert(merLedgerReceiver);
        } else {
            merLedgerReceiver.setUpdateTime(new Date());
            merLedgerReceiverMapper.updateByPrimaryKey(merLedgerReceiver);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void merLedgerReceiverApply(BsMerLedgerReceiver merLedgerReceiver) {
        editMerLedgerReceiver(merLedgerReceiver);

        if (merLedgerReceiver.getPlatformType().equals(PlatformTypeEnum.type1.getNumber())) {
            laKaLaLedgerService.applyBind(merLedgerReceiver);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void merLedgerReceiverDel(BsMerLedgerReceiver merLedgerReceiver) {
        editMerLedgerReceiver(merLedgerReceiver);

        if (merLedgerReceiver.getPlatformType().equals(PlatformTypeEnum.type1.getNumber())) {
            laKaLaLedgerService.applyUnBind(merLedgerReceiver.getMerId(), merLedgerReceiver.getReceiverNo());
        }
    }

    @Override
    public BsMerLedgerReceiver getMerLedgerReceiverById(Long id) {
        return merLedgerReceiverMapper.selectByPrimaryKey(id);
    }

    @Override
    public BsMerLedgerReceiver getMerLedgerReceiver(Long merId, String receiverNo) {
        BsMerLedgerReceiverExample example = new BsMerLedgerReceiverExample();
        example.createCriteria().andMerIdEqualTo(merId).andReceiverNoEqualTo(receiverNo).andStatusNotEqualTo(0);
        List<BsMerLedgerReceiver> list = merLedgerReceiverMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<BsMerLedgerReceiver> getMerLedgerReceiverByMer(Long merId, PlatformTypeEnum platformType) {
        BsMerLedgerReceiverExample example = new BsMerLedgerReceiverExample();
        example.createCriteria().andMerIdEqualTo(merId).andStatusNotEqualTo(0).andPlatformTypeEqualTo(platformType.getNumber());
        return merLedgerReceiverMapper.selectByExample(example);
    }

    @Override
    public List<BsMerLedgerReceiver> getMerLedgerReceiverList(Map<String, Object> param) {
        BsMerLedgerReceiverExample example = new BsMerLedgerReceiverExample();
        BsMerLedgerReceiverExample.Criteria criteria = example.createCriteria()
                .andStatusNotEqualTo(MerLedgerReceiverStatusEnum.status0.getNumber());

        if (MapUtils.getInteger(param, "platformType") != null) {
            criteria.andPlatformTypeEqualTo(MapUtils.getInteger(param, "platformType"));
        }

        if (MapUtils.getInteger(param, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(param, "status"));
        }

        if (MapUtils.getBoolean(param, "receiverNoNotNull") != null) {
            criteria.andReceiverNoIsNotNull();
        }

        example.setOrderByClause("create_time desc");
        return merLedgerReceiverMapper.selectByExample(example);
    }

}
