package com.pj.project.tb_account;

import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.sql.Struct;
import java.util.Date;
import java.util.List;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pj.current.satoken.StpUserUtil;
import com.pj.project.tb_charge_record.TbChargeRecord;
import com.pj.project.tb_charge_record.TbChargeRecordService;
import com.pj.project.tb_costomer.TbCostomer;
import com.pj.project.tb_costomer.TbCostomerService;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pj.utils.sg.*;

import javax.annotation.Resource;

/**
 * Service: tb_account -- 客户账户
 *
 * @author qzy
 */
@Service
public class TbAccountService extends ServiceImpl<TbAccountMapper, TbAccount> implements IService<TbAccount> {

    /**
     * 底层 Mapper 对象
     */
    @Autowired
    TbAccountMapper tbAccountMapper;
    @Resource
    TbCostomerService tbCostomerService;
    @Resource
    TbChargeRecordService tbChargeRecordService;

    public TbAccount getByCustomerId(String customerId) {
        QueryWrapper<TbAccount> ew = new QueryWrapper<>();
        ew.eq("customer_id", customerId);
        TbAccount account = this.getOne(ew);
        if (account == null) {
            account = new TbAccount();
            account.setCustomerId(customerId).setCreateTime(new Date())
                    .setAccountNo("36" + RandomUtil.randomNumbers(6)).setStatus(1);
            this.save(account);
        }
        account.setTotalMoney(Base64.decodeStr(account.getTotalMoney()))
                .setActMoney(Base64.decodeStr(account.getActMoney()))
                .setLockMoney(Base64.decodeStr(account.getActMoney()));
        return account;
    }

    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    List<TbAccount> getList(SoMap so) {
        return tbAccountMapper.getList(so);
    }


    public void recharge(String customerId, String money) {
        TbAccount account = this.getByCustomerId(customerId);
        recharge(account, TbChargeRecord.Ch.PC.getType(), new BigDecimal(money),new BigDecimal(money));
    }

    public void recharge(String id,BigDecimal money,String chargeType,BigDecimal payMoney){
        TbAccount account = this.getById(id);
        recharge(account, chargeType, money,payMoney);
    }
    public void recharge(TbAccount account, String chargeType, BigDecimal money,BigDecimal payMoney) {
        String totalMoney =account.getTotalMoney();
        if (NumberUtil.isNumber(totalMoney)){
            totalMoney=Base64.encode(totalMoney.getBytes());
        }
        BigDecimal total = new BigDecimal(Base64.decodeStr(totalMoney)).add(money);
        account.setTotalMoney(Base64.encode(total.toString()));
        this.updateById(account);
        String customerId = account.getCustomerId();
        TbChargeRecord record = new TbChargeRecord();
        TbCostomer tbCostomer = tbCostomerService.getById(customerId);
        record.setChargeChannel(chargeType).setMoney(money).setPayMoney(payMoney)
                .setCreateTime(new Date()).setNo(DateUtil.format(new Date(), "yyyyMMddHHmmss") + RandomUtil.randomNumbers(4))
                .setCustomerId(customerId).setCustomerName(tbCostomer.getName());
        tbChargeRecordService.save(record);

    }
}
