package com.xhpc.payment.service.impl;

import com.xhpc.common.api.UserTypeService;
import com.xhpc.common.core.constant.StatusConstants;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.core.web.service.BaseService;
import com.xhpc.common.domain.XhpcRefundOrder;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.payment.controller.AlipayPaymentController;
import com.xhpc.payment.domain.XhpcUserAccountStatement;
import com.xhpc.payment.mapper.XhpcUserAccountStatementMapper;
import com.xhpc.payment.service.IXhpcUserAccountStatementService;
import com.xhpc.system.api.model.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户流水信息 服务层
 *
 * @author ruoyi
 */
@Service
public class XhpcUserAccountStatementServiceImpl extends BaseService implements IXhpcUserAccountStatementService {

    @Autowired
    private XhpcUserAccountStatementMapper xhpcUserAccountStatementMapper;
    @Autowired
    private UserTypeService userTypeService;
    @Autowired
    private TokenService tokenService;

    private static final Logger logger = LoggerFactory.getLogger(XhpcUserAccountStatementServiceImpl.class);
    /**
     * 新增 用户流水
     *
     * @param xhpcUserAccountStatement 用户流水
     */
    @Override
    @Transactional
    public int insert(XhpcUserAccountStatement xhpcUserAccountStatement) {
        return xhpcUserAccountStatementMapper.insert(xhpcUserAccountStatement);
    }

    /**
     * 用户流水分页列表
     *
     * @param
     * @return
     */
    @Override
    public List<Map<String, Object>> page(Long appUserId) {
        List<Map<String, Object>> list =new ArrayList<>();
        if(appUserId==null){
            return list;
        }
        return xhpcUserAccountStatementMapper.page(appUserId,0,null);
    }

    /**
     * 用户费用变化产生流水
     *
     * @param id     操作id
     * @param amount 变化金额
     * @param userId 用户id
     * @param type   操作类型（1充值 2退款 3充电）
     */
    public void add(Long id, String amount, String userId, Integer type, Integer source, String tenantId) {
        XhpcUserAccountStatement xhpcUserAccountStatement = new XhpcUserAccountStatement();
        xhpcUserAccountStatement.setAmount(BigDecimal.valueOf(Double.valueOf(amount)));
        if (StatusConstants.FLOWING_WATER_RECHARGE == type) {
            xhpcUserAccountStatement.setRechargeOrderId(id);
        } else if (StatusConstants.FLOWING_WATER_REFUND == type) {
            xhpcUserAccountStatement.setRefundOrderId(id);
        } else {
            xhpcUserAccountStatement.setChargeOrderId(id);
        }
        xhpcUserAccountStatement.setUserId(Long.parseLong(userId));
        xhpcUserAccountStatement.setType(type);
        xhpcUserAccountStatement.setSource(source);
        xhpcUserAccountStatement.setTenantId(tenantId);
        if(source== UserTypeUtil.INTERNET_TYPE && Long.parseLong(userId)>10000){
            Map<String, Object> appUserInfo = xhpcUserAccountStatementMapper.getAppInternetUser(Long.parseLong(userId));
            if (StringUtils.isNotNull(appUserInfo)) {
                String balance = StringUtils.valueOf(appUserInfo.get("balance"));
                BigDecimal surplus = BigDecimal.valueOf(Double.valueOf(balance)).subtract(BigDecimal.valueOf(Double.valueOf(balance)));
                xhpcUserAccountStatement.setRemainingSum(surplus);
            }
        }else{
            Map<String, Object> appUserInfo = xhpcUserAccountStatementMapper.appUserInfo(Long.parseLong(userId));
            if (StringUtils.isNotNull(appUserInfo)) {
                String balance = StringUtils.valueOf(appUserInfo.get("balance"));
                BigDecimal surplus = BigDecimal.valueOf(Double.valueOf(balance)).subtract(BigDecimal.valueOf(Double.valueOf(balance)));
                xhpcUserAccountStatement.setRemainingSum(surplus);
            }
        }
        xhpcUserAccountStatement.setCreateTime(new Date());
        if(type==1){
            xhpcUserAccountStatement.setRemark("充值订单！！！");
        }else if(type==2){
            xhpcUserAccountStatement.setRemark("退款订单！！！");
        }else{
            xhpcUserAccountStatement.setRemark("充电订单！！！");
        }
        xhpcUserAccountStatementMapper.insert(xhpcUserAccountStatement);
    }

    /**
     * 用户流水列表
     *
     * @param  C端用户id
     * @return
     */
    @Override
    public List<Map<String, Object>> list(HttpServletRequest request) {

        List<Map<String, Object>> list =new ArrayList<>();
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userid = loginUser.getUserid();
        String tenantId = loginUser.getTenantId();
        Integer userType = loginUser.getUserType();
        R user = userTypeService.getUser(null, userid, userType, null, tenantId);
        if(user !=null && user.getData() !=null){
            Map<String, Object> userMessage = (Map<String, Object>)user.getData();
            if (userMessage != null) {
                return xhpcUserAccountStatementMapper.list(userid,userType,tenantId);
            }
        }
        return list;
    }
}
