package com.yiqi.app.service.impl;

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

import com.yiqi.common.exception.ServiceException;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.enums.*;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.EnumUtils;
import com.yiqi.order.service.IPayService;
import com.yiqi.recharge.domain.dto.BillMstrOrderDTO;
import com.yiqi.recharge.domain.dto.ConsumeOrderDTO;
import com.yiqi.recharge.service.IRechargePasswordCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.yiqi.app.mapper.AppUserBillMstrMapper;
import com.yiqi.app.domain.AppUserBillMstr;
import com.yiqi.app.service.IAppUserBillMstrService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 用户账单Service业务层处理
 *
 * @author xiaohei
 * @date 2023-11-02
 */
@Service
public class AppUserBillMstrServiceImpl extends ServiceImpl<AppUserBillMstrMapper, AppUserBillMstr> implements IAppUserBillMstrService {

    @Lazy
    @Autowired
    private IRechargePasswordCardService rechargePasswordCardService;
    @Autowired
    private IPayService payService;

    @Override
    public R consumeRecord(ConsumeOrderDTO consumeOrderDTO) {
        if (SysBoolType.YES.getCode().equals(consumeOrderDTO.getIsRefund())) {
            throw new ServiceException("该请求为退款订单，无法处理");
        }
        //只记录日志
        save(buildBillMstr(consumeOrderDTO));
        return R.ok();
    }


    @Override
    public void saveBillMstrOrder(BillMstrOrderDTO billMstrOrderDTO) {
        AppUserBillMstr appUserBillMstr = new AppUserBillMstr();
        appUserBillMstr.buildCreateData();
        appUserBillMstr.setAppUserId(billMstrOrderDTO.getAppUserId());
        appUserBillMstr.setBillType(OrderBillType.pay.getCode());
        appUserBillMstr.setPayTotalAmount(billMstrOrderDTO.getPayTotalAmount().abs());
        appUserBillMstr.setPayType(billMstrOrderDTO.getPayType());
        appUserBillMstr.setRechargePayAmount(billMstrOrderDTO.getRechargePayAmount());
        appUserBillMstr.setGivePayAmount(billMstrOrderDTO.getGivePayAmount());
        appUserBillMstr.setWelfarePayAmount(billMstrOrderDTO.getWelfarePayAmount());
        appUserBillMstr.setIsRefund(SysBoolType.NO.getCode());
        appUserBillMstr.setOrderNo(billMstrOrderDTO.getOrderNo());
        appUserBillMstr.setOrderType(billMstrOrderDTO.getOrderType());
        appUserBillMstr.setOrderId(billMstrOrderDTO.getOrderId());
        appUserBillMstr.setSourceType(billMstrOrderDTO.getSourceType());
        appUserBillMstr.setOrgId(billMstrOrderDTO.getOrgId());
        appUserBillMstr.setCardStoreId(billMstrOrderDTO.getUseBindStoreId());
        appUserBillMstr.setContent(billMstrOrderDTO.getRemark());
        appUserBillMstr.setRechargeCardNo(billMstrOrderDTO.getRechargeCardNo());
        switch (EnumUtils.getEnumByType(billMstrOrderDTO.getOrderType(), OrderType.class)) {
            case cloth: {
                appUserBillMstr.setTitle("洗衣下单");
                break;
            }
            case goods: {
                appUserBillMstr.setTitle("普通商品下单");
                break;
            }
            case life: {
                appUserBillMstr.setTitle("生活服务下单");
                break;
            }
            case car: {
                appUserBillMstr.setTitle("汽车美容下单");
                break;
            }
            case recharge: {
                appUserBillMstr.setBillType(OrderBillType.recharge.getCode());
                appUserBillMstr.setTitle("账户充值");
                break;
            }
        }
        baseMapper.insert(appUserBillMstr);
    }

    /**
     * 退款日志
     * @param billMstrOrderDTO
     */
    @Override
    public void saveRefundBillMstrOrder(BillMstrOrderDTO billMstrOrderDTO) {
        AppUserBillMstr appUserBillMstr = new AppUserBillMstr();
        appUserBillMstr.buildCreateData();
        appUserBillMstr.setAppUserId(billMstrOrderDTO.getAppUserId());
        appUserBillMstr.setBillType(OrderBillType.recharge.getCode());
        appUserBillMstr.setOrderId(billMstrOrderDTO.getOrderId());
        appUserBillMstr.setPayTotalAmount(billMstrOrderDTO.getPayTotalAmount());
        appUserBillMstr.setRechargePayAmount(billMstrOrderDTO.getRechargePayAmount());
        appUserBillMstr.setGivePayAmount(billMstrOrderDTO.getGivePayAmount());
        appUserBillMstr.setPayType(billMstrOrderDTO.getPayType());
        appUserBillMstr.setWelfarePayAmount(billMstrOrderDTO.getWelfarePayAmount());
        appUserBillMstr.setRechargeCardNo(billMstrOrderDTO.getRechargeCardNo());
        appUserBillMstr.setIsRefund(SysBoolType.YES.getCode());
        appUserBillMstr.setOrderNo(billMstrOrderDTO.getOrderNo());
        appUserBillMstr.setOrderType(billMstrOrderDTO.getOrderType());
        appUserBillMstr.setOrgId(billMstrOrderDTO.getOrgId());
        appUserBillMstr.setCardStoreId(billMstrOrderDTO.getUseBindStoreId());
        appUserBillMstr.setSourceType(billMstrOrderDTO.getSourceType());
        appUserBillMstr.setContent(billMstrOrderDTO.getRemark());
        switch (EnumUtils.getEnumByType(billMstrOrderDTO.getOrderType(), OrderType.class)) {
            case cloth: {
                appUserBillMstr.setTitle("洗衣退单");
                break;
            }
            case goods: {
                appUserBillMstr.setTitle("普通商品退单");
                break;
            }
            case life: {
                appUserBillMstr.setTitle("生活服务退单");
                break;
            }
            case car: {
                appUserBillMstr.setTitle("汽车美容退单");
                break;
            }
            case recharge: {
                appUserBillMstr.setBillType(OrderBillType.pay.getCode());
                appUserBillMstr.setTitle("账户充值退款");
                break;
            }
        }
        baseMapper.insert(appUserBillMstr);
    }

    /**
     * 消费日志
     *
     * @param consumeOrderDTO 消费信息
     * @return
     */
    public AppUserBillMstr buildBillMstr(ConsumeOrderDTO consumeOrderDTO) {
        AppUserBillMstr appUserBillMstr = new AppUserBillMstr();
        appUserBillMstr.buildCreateData();
        appUserBillMstr.setAppUserId(consumeOrderDTO.getAppUserId());
        appUserBillMstr.setBillType(OrderBillType.pay.getCode());
        appUserBillMstr.setPayTotalAmount(consumeOrderDTO.getAmount().multiply(new BigDecimal(-1)));
        appUserBillMstr.setRechargePayAmount(BigDecimal.ZERO);
        appUserBillMstr.setGivePayAmount(BigDecimal.ZERO);
        appUserBillMstr.setWelfarePayAmount(BigDecimal.ZERO);

        switch (EnumUtils.getEnumByType(consumeOrderDTO.getOrderType(), OrderType.class)) {
            case cloth: {
                appUserBillMstr.setUsePoint(consumeOrderDTO.getPointAmount());
                appUserBillMstr.setReceivePoint(0);
                appUserBillMstr.setTitle("洗衣订单");
                break;
            }
            case goods: {
                appUserBillMstr.setUsePoint(consumeOrderDTO.getPointAmount());
                appUserBillMstr.setReceivePoint(0);
                appUserBillMstr.setTitle("商品订单");
                break;
            }
            case life: {
                appUserBillMstr.setUsePoint(consumeOrderDTO.getPointAmount());
                appUserBillMstr.setReceivePoint(0);
                appUserBillMstr.setTitle("生活服务");
                break;
            }
            case car: {
                appUserBillMstr.setUsePoint(consumeOrderDTO.getPointAmount());
                appUserBillMstr.setReceivePoint(0);
                appUserBillMstr.setTitle("汽车美容");
                break;
            }
            case recharge: {
                appUserBillMstr.setBillType(OrderBillType.recharge.getCode());
                appUserBillMstr.setReceivePoint(consumeOrderDTO.getPointAmount());
                appUserBillMstr.setUsePoint(0);
                appUserBillMstr.setTitle("充值");
                break;
            }
        }
        appUserBillMstr.setIsRefund(OrderRefundStatus.OK.getCode());
        appUserBillMstr.setOrderId(consumeOrderDTO.getOrderId());
        appUserBillMstr.setOrderNo(consumeOrderDTO.getOrderNo());
        appUserBillMstr.setOrderType(consumeOrderDTO.getOrderType());
        appUserBillMstr.setOrgId(consumeOrderDTO.getStoreId());
        appUserBillMstr.setCardStoreId(consumeOrderDTO.getUseBindStoreId());
        appUserBillMstr.setPayType(consumeOrderDTO.getPayType());
        return appUserBillMstr;
    }

    /**
     * 查询用户账单
     *
     * @param id 用户账单主键
     * @return 用户账单
     */
    @Override
    public AppUserBillMstr selectAppUserBillMstrById(Long id) {
        return baseMapper.selectAppUserBillMstrById(id);
    }

    /**
     * 查询用户账单列表
     *
     * @param appUserBillMstr 用户账单
     * @return 用户账单
     */
    @Override
    public List<AppUserBillMstr> selectAppUserBillMstrList(AppUserBillMstr appUserBillMstr) {
        return baseMapper.selectAppUserBillMstrList(appUserBillMstr);
    }

    /**
     * 新增用户账单
     *
     * @param appUserBillMstr 用户账单
     * @return 结果
     */
    @Override
    public int insertAppUserBillMstr(AppUserBillMstr appUserBillMstr) {
        appUserBillMstr.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertAppUserBillMstr(appUserBillMstr);
    }

    /**
     * 修改用户账单
     *
     * @param appUserBillMstr 用户账单
     * @return 结果
     */
    @Override
    public int updateAppUserBillMstr(AppUserBillMstr appUserBillMstr) {
        appUserBillMstr.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateAppUserBillMstr(appUserBillMstr);
    }

    /**
     * 批量删除用户账单
     *
     * @param ids 需要删除的用户账单主键
     * @return 结果
     */
    @Override
    public int deleteAppUserBillMstrByIds(Long[] ids) {
        return baseMapper.deleteAppUserBillMstrByIds(ids);
    }

    /**
     * 删除用户账单信息
     *
     * @param id 用户账单主键
     * @return 结果
     */
    @Override
    public int deleteAppUserBillMstrById(Long id) {
        return baseMapper.deleteAppUserBillMstrById(id);
    }


//</editor-folder>
}
