package com.hfkj.service.spread.impl;

import com.hfkj.common.utils.DateUtil;
import com.hfkj.dao.BsUserSpreadAccountRecordMapper;
import com.hfkj.entity.BsUserSpreadAccount;
import com.hfkj.entity.BsUserSpreadAccountRecord;
import com.hfkj.entity.BsUserSpreadAccountRecordExample;
import com.hfkj.service.spread.BsUserSpreadAccountRecordService;
import com.hfkj.service.spread.BsUserSpreadAccountService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * @className: BsUserSpreadAccountRecordServiceImpl
 * @author: HuRui
 * @date: 2025/4/1
 **/
@Service("userSpreadAccountRecordService")
public class BsUserSpreadAccountRecordServiceImpl implements BsUserSpreadAccountRecordService {
    @Resource
    private BsUserSpreadAccountRecordMapper userSpreadAccountRecordMapper;

    @Override
    public void create(BsUserSpreadAccountRecord data) {
        data.setCreateTime(new Date());
        data.setStatus(1);
        userSpreadAccountRecordMapper.insert(data);
    }

    @Override
    public void update(BsUserSpreadAccountRecord data) {
        userSpreadAccountRecordMapper.updateByPrimaryKey(data);
    }

    @Override
    public BsUserSpreadAccountRecord getRecordByOrderNo(String orderNo) {
        BsUserSpreadAccountRecordExample example = new BsUserSpreadAccountRecordExample();
        example.createCriteria().andStatusNotEqualTo(0).andSourceOrderNoEqualTo(orderNo);
        List<BsUserSpreadAccountRecord> list = userSpreadAccountRecordMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

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

        if (MapUtils.getLong(param, "userId") != null) {
            criteria.andUserIdEqualTo(MapUtils.getLong(param, "userId"));
        }

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

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

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

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

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

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

    @Override
    public BigDecimal countRecordAmount(Map<String, Object> param) {
        if (MapUtils.getLong(param, "createTimeS") != null) {
            param.put("createTimeS", DateUtil.date2String(new Date(MapUtils.getLong(param, "createTimeS")), DateUtil.Y_M_D_HMS));
        }

        if (MapUtils.getLong(param, "createTimeE") != null) {
            param.put("createTimeE", DateUtil.date2String(new Date(MapUtils.getLong(param, "createTimeE")), DateUtil.Y_M_D_HMS));
        }

        // 统计
        BigDecimal decimal = userSpreadAccountRecordMapper.countRecordAmount(param);
        if (decimal != null) {
            return decimal;
        } else {
            return new BigDecimal("0");
        }
    }

    @Override
    public int withdrawChance(Long userId) {
        return userSpreadAccountRecordMapper.withdrawChance(userId);
    }
}
