package com.red.circle.wallet.infra.database.rds.service.bean.impl;

import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.mybatis.service.impl.BaseServiceImpl;
import com.red.circle.tool.core.text.StringUtils;
import com.red.circle.wallet.infra.database.rds.dao.BeanIncomeExpenditureDAO;
import com.red.circle.wallet.infra.database.rds.entity.bean.BeanIncomeExpenditure;
import com.red.circle.wallet.infra.database.rds.service.bean.BeanIncomeExpenditureService;
import com.red.circle.wallet.inner.model.cmd.UserBeanIncomeExpenditurePageQryCmd;
import java.util.Objects;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户豆子收支 服务实现类.
 * </p>
 *
 * @author pengshigang
 * @since 2021-10-14
 */
@Service
public class BeanIncomeExpenditureServiceImpl extends
    BaseServiceImpl<BeanIncomeExpenditureDAO, BeanIncomeExpenditure> implements
    BeanIncomeExpenditureService {

  @Override
  public PageResult<BeanIncomeExpenditure> pageRunningWater(
      UserBeanIncomeExpenditurePageQryCmd query) {

    return query()
        .eq(StringUtils.isNotBlank(query.getSysOrigin()), BeanIncomeExpenditure::getSysOrigin,
            query.getSysOrigin())
        .eq(StringUtils.isNotBlank(query.getOrigin()), BeanIncomeExpenditure::getOrigin,
            query.getOrigin())
        .eq(Objects.nonNull(query.getUserId()), BeanIncomeExpenditure::getUserId,
            query.getUserId())
        .eq(Objects.nonNull(query.getType()), BeanIncomeExpenditure::getType, query.getType())
        .ge(Objects.nonNull(query.getStartTime()), BeanIncomeExpenditure::getCreateTime,
            query.startTimeToLocalDateTime())
        .le(Objects.nonNull(query.getEndTime()), BeanIncomeExpenditure::getCreateTime,
            query.endTimeToLocalDateTime()
        )
        .page(query.getPageQuery());
  }


  public void saveBeanIncome(BeanIncomeExpenditure beanIncomeExpenditure) {
    save(new BeanIncomeExpenditure()
        .setSysOrigin(beanIncomeExpenditure.getSysOrigin())
        .setUserId(beanIncomeExpenditure.getUserId())
        .setAssociateId(beanIncomeExpenditure.getAssociateId())
        .setQuantity(beanIncomeExpenditure.getQuantity())
        .setBalance(beanIncomeExpenditure.getBalance())
        .setType(beanIncomeExpenditure.getType())
        .setOrigin(beanIncomeExpenditure.getOrigin())
        .setOriginName(beanIncomeExpenditure.getOriginName())
    );
  }
}
