package com.jsowell.pile.service.impl;

import com.jsowell.common.enums.DelFlagEnum;
import com.jsowell.common.util.DateUtils;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.ClearingBillInfo;
import com.jsowell.pile.dto.GetClearingBillDTO;
import com.jsowell.pile.mapper.ClearingBillInfoMapper;
import com.jsowell.pile.service.ClearingBillInfoService;
import com.jsowell.pile.vo.web.MerchantClearingBillVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class ClearingBillInfoServiceImpl implements ClearingBillInfoService {

    @Resource
    private ClearingBillInfoMapper clearingBillInfoMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return clearingBillInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ClearingBillInfo record) {
        return clearingBillInfoMapper.insert(record);
    }

    @Override
    public int insertSelective(ClearingBillInfo record) {
        return clearingBillInfoMapper.insertSelective(record);
    }

    @Override
    public ClearingBillInfo selectByPrimaryKey(Integer id) {
        return clearingBillInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ClearingBillInfo record) {
        return clearingBillInfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ClearingBillInfo record) {
        return clearingBillInfoMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<ClearingBillInfo> selectByMerchantId(String merchantId, String billStatus) {
        return clearingBillInfoMapper.selectByMerchantId(merchantId, billStatus, null);
    }

    @Override
    public void updateStatus(List<Integer> clearingBillIds, String billStatus, String withdrawCode) {
        if (CollectionUtils.isEmpty(clearingBillIds) || StringUtils.isBlank(billStatus)) {
            return;
        }
        clearingBillInfoMapper.updateStatus(clearingBillIds, billStatus, withdrawCode);
    }

    /**
     * 后管用 查询运营商账单列表
     *
     * @param dto
     * @return
     */
    @Override
    public List<MerchantClearingBillVO> getMerchantClearingBillList(GetClearingBillDTO dto) {
        return clearingBillInfoMapper.getMerchantClearingBillList(dto);
    }

    @Override
    public int insertOrUpdate(ClearingBillInfo record) {
        return clearingBillInfoMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(ClearingBillInfo record) {
        if (record.getId() != null) {
            record.setUpdateTime(DateUtils.getNowDate());
        } else {
            record.setCreateTime(DateUtils.getNowDate());
        }
        return clearingBillInfoMapper.insertOrUpdateSelective(record);
    }

    @Override
    public int updateBatch(List<ClearingBillInfo> list) {
        return clearingBillInfoMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<ClearingBillInfo> list) {
        return clearingBillInfoMapper.updateBatchSelective(list);
    }

    @Override
    public int batchInsert(List<ClearingBillInfo> list) {
        return clearingBillInfoMapper.batchInsert(list);
    }

    @Override
    public ClearingBillInfo selectByMerchantIdAndTradeDate(String merchantId, String tradeDate) {
        ClearingBillInfo result = null;
        List<ClearingBillInfo> list = clearingBillInfoMapper.selectByMerchantId(merchantId, null, tradeDate);
        if (CollectionUtils.isNotEmpty(list)) {
            result = list.get(0);
            if (list.size() > 1) {
                // 原来存在多条清分账单，逻辑删除掉
                ClearingBillInfo finalResult = result;
                List<ClearingBillInfo> collect = list.stream()
                        .filter(x -> !Objects.equals(x.getId(), finalResult.getId()))
                        .collect(Collectors.toList());
                for (ClearingBillInfo clearingBillInfo : collect) {
                    clearingBillInfo.setDelFlag(DelFlagEnum.DELETE.getValue());
                }
                clearingBillInfoMapper.updateBatch(collect);
            }
        }
        return result;
    }

    @Override
    public List<ClearingBillInfo> selectByWithdrawCode(String withdrawCode) {
        return clearingBillInfoMapper.selectByWithdrawCode(withdrawCode);
    }
}

