package cn.com.ty.lift.ud.payment.service.impl;

import cn.com.ty.lift.ud.payment.controller.query.UserBillQuery;
import cn.com.ty.lift.ud.payment.mapper.entity.UserBillEntityVo;
import cn.com.ty.lift.ud.userInfo.mapper.entity.UserInfoEntity;
import cn.com.ty.lift.ud.userInfo.service.IUserInfoService;
import cn.com.xwy.boot.web.dto.RestResponse;
import cn.com.xwy.util.collection.ListUtil;
import lombok.Data;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.ty.lift.ud.payment.mapper.entity.UserBillEntity;
import cn.com.ty.lift.ud.payment.mapper.UserBillMapper;
import cn.com.ty.lift.ud.payment.service.IUserBillService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户账单流水 -ServiceImpl
 *
 * @author Xson
 * @date 2020-04-23
 */
@Service
@Transactional
public class UserBillServiceImpl extends ServiceImpl<UserBillMapper, UserBillEntity> implements IUserBillService {

    @Autowired
    private UserBillMapper userBillMapper;

    @Autowired
    private IUserInfoService iUserInfoService;

    /**
     * 查询最近一次的余额
     * 
     * @param expert
     * @return
     */
    @Override
    public BigDecimal getLatestBalance(Long expert) {
        List<BigDecimal> balanceList = userBillMapper.getLatestBalance(expert);
        if (ListUtil.isNotEmpty(balanceList)) {
            return balanceList.get(0);
        } else {
            //上一条不存在，去user_info 里面查
            UserInfoEntity userId = iUserInfoService.getByUserId(expert);
            if(userId.getBalance() == null){
                return new BigDecimal("0");
            }
            return userId.getBalance();
        }

    }

    @Override
    public RestResponse selectByExample(UserBillQuery query) {
        IPage page = query.page();
        List<UserBillEntity> list = userBillMapper.selectByQuery(query.condition(), query);

        Double outcome = userBillMapper.sumByType(query.condition(),1);
        Double income = userBillMapper.sumByType(query.condition(),2);
        if (list != null && list.size() > 0){
            list.get(0).setIncome(income);
            list.get(0).setOutcome(outcome);
        }

        int total = userBillMapper.countByQuery(query.condition());
        page.setRecords(list);
        page.setTotal(total);
        return RestResponse.success(page, "Queried successfully");
    }

    /**
     *
     * @param userId
     * @param businessType
     * @return
     */
    @Override
    public UserBillEntity getByUserIdAndBusinessType(Long userId, Integer businessType) {
        List<UserBillEntity> list = userBillMapper.getByUserIdAndBusinessType(userId, businessType);
        if(ListUtil.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    /**
     * 根据订单号找到之前的 用户流水记录
     * @param orders
     * @return
     */
    @Override
    public UserBillEntity getByFlowNum(String orders) {
        return userBillMapper.getByFlowNum(orders);
    }

    @Override
    public RestResponse getDetail(Long id) {
        UserBillEntityVo detail = userBillMapper.getDetail(id);
        return RestResponse.success(detail);
    }

    @Data
    class ToAmount {

        private Integer type;

        private Double amount ;
    }
}
