package com.xhpc.user.service.impl;

import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.user.domain.XhpcCommunityPersonnel;
import com.xhpc.user.domain.XhpcCustomersPersonnel;
import com.xhpc.user.mapper.XhpcCommonMapper;
import com.xhpc.user.service.IXhpcCommonService;
import com.xhpc.user.service.IXhpcCommunityService;
import com.xhpc.user.service.IXhpcCustomersService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yuyang
 * @date 2021/12/29 11:11
 */
@Service
public class XhpcCommonServiceImpl implements IXhpcCommonService {

    @Autowired
    private XhpcCommonMapper xhpcCommonMapper;
    @Autowired
    private IXhpcCommunityService xhpcCommunityService;
    @Autowired
    private IXhpcCustomersService xhpcCustomersService;


    @Override
    public Map<String, Object> getLandUser(String phone,Long userId, Integer type,String serialNumber,String tenantId) {
        if(UserTypeUtil.USER_TYPE.equals(type)){
            return xhpcCommonMapper.getAppUser(phone,userId,tenantId);
        }else if(UserTypeUtil.COMMUNIT_TYPE.equals(type)){
            return xhpcCommonMapper.getCommunityUser(phone,userId,serialNumber,tenantId);
        }else if(UserTypeUtil.CUSTOMERS_TYPE.equals(type)){
            return xhpcCommonMapper.getCustomersUser(phone,userId,serialNumber,tenantId);
        }
        return new HashMap<>();
    }

    @Override
    public AjaxResult insertUserBalance(BigDecimal money, Long userId, Integer userType, String tenantId, Integer type,Integer status,String remark,Long moneyOrderId) {
        if(UserTypeUtil.USER_TYPE.equals(userType)){
            //C端用户暂不考虑

        }else if(UserTypeUtil.COMMUNIT_TYPE.equals(userType)){
            Map<String, Object> communityUser = xhpcCommonMapper.getCommunityUser(null, userId, null, tenantId);
            if(communityUser!=null){
                if(UserTypeUtil.INSERT_BALANCE.equals(status)){
                    XhpcCommunityPersonnel xhpcCommunityPersonnel=new XhpcCommunityPersonnel();
                    xhpcCommunityPersonnel.setCommunityPersonnelId(Long.valueOf(communityUser.get("appUserId").toString()));
                    xhpcCommunityPersonnel.setRechargeMoney(money);
                    xhpcCommunityPersonnel.setRemark(remark);
                    xhpcCommunityPersonnel.setRechargeType(type);
                    xhpcCommunityPersonnel.setTenantId(tenantId);
                    xhpcCommunityPersonnel.setMoneyOrderId(moneyOrderId);
                    return xhpcCommunityService.addRecharge(xhpcCommunityPersonnel);
                }
            }
        }else if(UserTypeUtil.CUSTOMERS_TYPE.equals(userType)){
            Map<String, Object> customersUser = xhpcCommonMapper.getCustomersUser(null, userId, null, tenantId);
            if(customersUser!=null){
                if(UserTypeUtil.INSERT_BALANCE.equals(status)){
                    XhpcCustomersPersonnel xhpcCustomersPersonnel =new XhpcCustomersPersonnel();
                    xhpcCustomersPersonnel.setCustomersPersonnelId(Long.valueOf(customersUser.get("appUserId").toString()));
                    xhpcCustomersPersonnel.setRechargeMoney(money);
                    xhpcCustomersPersonnel.setRemark(remark);
                    xhpcCustomersPersonnel.setRechargeType(type);
                    xhpcCustomersPersonnel.setTenantId(tenantId);
                    xhpcCustomersPersonnel.setMoneyOrderId(moneyOrderId);
                    return xhpcCustomersService.addRecharge(xhpcCustomersPersonnel);
                }
            }
        }
        return AjaxResult.error();
    }
}
