package com.hfkj.service.impl;

import com.hfkj.common.utils.DateUtil;
import com.hfkj.dao.BsTradeSettleMapper;
import com.hfkj.entity.BsTradeSettle;
import com.hfkj.entity.BsTradeSettleExample;
import com.hfkj.service.BsTradeSettleService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * @className: BsTradeSettleServiceImpl
 * @author: HuRui
 * @date: 2023/8/4
 **/
@Service("tradeSettleService")
public class BsTradeSettleServiceImpl implements BsTradeSettleService {

    @Resource
    private BsTradeSettleMapper tradeSettleMapper;

    @Override
    public void editData(BsTradeSettle tradeSettle) {
        if (tradeSettle.getId() == null) {
            tradeSettle.setStatus(1);
            tradeSettle.setCreateTime(new Date());
            tradeSettleMapper.insert(tradeSettle);
        } else {
            tradeSettleMapper.updateByPrimaryKey(tradeSettle);
        }
    }

    @Override
    public BsTradeSettle getDetailById(Long id) {
        return tradeSettleMapper.selectByPrimaryKey(id);
    }

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

        if (StringUtils.isNotBlank(MapUtils.getString(param, "merNo"))) {
            criteria.andMerNoEqualTo(MapUtils.getString(param, "merNo"));
        }

        if (MapUtils.getLong(param, "settleTimeS") != null) {
            criteria.andSettleDateGreaterThanOrEqualTo(new Date(MapUtils.getLong(param, "settleTimeS")));
        }

        if (MapUtils.getLong(param, "settleTimeE") != null) {
            criteria.andSettleDateLessThanOrEqualTo(new Date(MapUtils.getLong(param, "settleTimeE")));
        }

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

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

        example.setOrderByClause("trade_time desc");
        return tradeSettleMapper.selectByExample(example);
    }

    @Override
    public Map<String, Object> getTradeSettleCount(Map<String, Object> param) {
        param.put("settleTimeS", DateUtil.date2String(new Date(MapUtils.getLong(param, "settleTimeS")), DateUtil.Y_M_D));
        param.put("settleTimeE", DateUtil.date2String(new Date(MapUtils.getLong(param, "settleTimeE")), DateUtil.Y_M_D));
        return tradeSettleMapper.queryTradeSettleCount(param);
    }


    @Override
    public List<Map<String, Object>> getTradeSettleDayCount(Map<String, Object> param) {
        param.put("settleTimeS", DateUtil.date2String(new Date(MapUtils.getLong(param, "settleTimeS")), DateUtil.Y_M_D));
        param.put("settleTimeE", DateUtil.date2String(new Date(MapUtils.getLong(param, "settleTimeE")), DateUtil.Y_M_D));
        return tradeSettleMapper.queryTradeSettleDayCount(param);
    }
}
