package com.hfkj.service.impl;

import com.ccb.wlpt.util.StringUtil;
import com.hfkj.channel.lakala.LaKaLaLedgerService;
import com.hfkj.dao.BsMerLedgerMapper;
import com.hfkj.entity.BsMerLedger;
import com.hfkj.entity.BsMerLedgerExample;
import com.hfkj.service.BsMerLedgerService;
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: BsMerLedgerServiceImpl
 * @author: HuRui
 * @date: 2023/4/18
 **/
@Service("merLedgerService")
public class BsMerLedgerServiceImpl implements BsMerLedgerService {

    @Resource
    private BsMerLedgerMapper merLedgerMapper;

    @Resource
    private LaKaLaLedgerService laKaLaLedgerService;

    @Override
    public void editMerLedger(BsMerLedger merLedger) {
        if (merLedger.getId() == null) {
            merLedger.setCreateTime(new Date());
            merLedger.setUpdateTime(new Date());
            merLedgerMapper.insert(merLedger);
        } else {
            merLedger.setUpdateTime(new Date());
            merLedgerMapper.updateByPrimaryKey(merLedger);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void merLedgerApply(BsMerLedger merLedger) {
        editMerLedger(merLedger);

        if (merLedger.getPlatformType().equals(PlatformTypeEnum.type1.getNumber())) {
            laKaLaLedgerService.applyLedger(merLedger.getId());
        }
    }

    @Override
    public BsMerLedger getMerLedger(Long merId, PlatformTypeEnum platformType) {
        BsMerLedgerExample example = new BsMerLedgerExample();
        example.createCriteria()
                .andMerIdEqualTo(merId)
                .andStatusNotEqualTo(0)
                .andPlatformTypeEqualTo(platformType.getNumber());
        example.setOrderByClause("create_time desc");
        List<BsMerLedger> list = merLedgerMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsMerLedger getMerLedgerByReceiverNo(String receiverNo) {
        BsMerLedgerExample example = new BsMerLedgerExample();
        example.createCriteria().andReceiverNoEqualTo(receiverNo);
        List<BsMerLedger> list = merLedgerMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsMerLedger getMerLedgerById(Long id) {
        return merLedgerMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<BsMerLedger> getMerLedgerList(Map<String, Object> param) {
        BsMerLedgerExample example = new BsMerLedgerExample();
        BsMerLedgerExample.Criteria criteria = example.createCriteria().andStatusNotEqualTo(0);

        if (StringUtils.isNotBlank(MapUtils.getString(param, "merNme"))) {
            criteria.andMerNameLike("%" + MapUtils.getString(param, "merNme") + "%");
        }

        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 merLedgerMapper.selectByExample(example);
    }
}
