package com.hfkj.service.user.impl;

import com.hfkj.dao.BsUserReceiptMapper;
import com.hfkj.entity.BsUserReceipt;
import com.hfkj.entity.BsUserReceiptExample;
import com.hfkj.service.user.BsUserReceiptService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @className: BsUserReceiptServiceImpl
 * @author: HuRui
 * @date: 2024/7/24
 **/
@Service("userReceiptService")
public class BsUserReceiptServiceImpl implements BsUserReceiptService {
    @Resource
    private BsUserReceiptMapper userReceiptMapper;

    @Override
    public void editData(BsUserReceipt data) {
        data.setUpdateTime(new Date());
        if (data.getId() == null) {
            data.setCreateTime(new Date());
            userReceiptMapper.insert(data);
        } else {
            userReceiptMapper.updateByPrimaryKey(data);
        }
    }

    @Override
    public BsUserReceipt getDetail(Long id) {
        return userReceiptMapper.selectByPrimaryKey(id);
    }

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

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

        if (StringUtils.isNotBlank(MapUtils.getString(param, "status"))) {
            List<Integer> status = Arrays.stream(MapUtils.getString(param, "status").split(","))
                    .flatMapToInt(num -> IntStream.of(Integer.parseInt(num))).boxed()
                    .collect(Collectors.toList());
            criteria.andStatusIn(status);
        }

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