package com.kxs.store.biz.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.common.core.constant.SecurityConstants;
import com.kxs.common.core.constant.enums.BrandTypeEnum;
import com.kxs.common.core.constant.enums.ErrorTypeEnum;
import com.kxs.common.core.constant.enums.KindTypeEnum;
import com.kxs.common.core.exception.GlobalCustomerException;
import com.kxs.common.core.util.R;
import com.kxs.common.core.util.RetOps;
import com.kxs.common.security.util.SecurityUtils;
import com.kxs.store.api.model.KxsMachineAdvance;
import com.kxs.store.api.model.KxsWarehouseCreditAmount;
import com.kxs.store.api.vo.kxsapp.storeAdvance.AdvanceUserDetailVO;
import com.kxs.store.api.vo.kxsapp.storeLimit.WarehouseAmountVO;
import com.kxs.store.api.vo.kxsapp.userAdvance.AdvanceAmountVO;
import com.kxs.store.biz.constant.enums.WarehouseLimitTypeEnum;
import com.kxs.store.biz.mapper.KxsMachineAdvanceMapper;
import com.kxs.store.biz.mapper.KxsWarehouseCreditAmountMapper;
import com.kxs.store.biz.mapper.KxsWarehouseLimitMapper;
import com.kxs.store.api.model.KxsWarehouseLimit;
import com.kxs.store.biz.service.KxsWarehouseLimitService;
import com.kxs.user.api.feign.RemoteKxsUserService;
import com.kxs.user.api.model.KxsUser;
import io.seata.core.context.RootContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * 客小爽仓库额度表(KxsWarehouseLimit)表服务实现类
 *
 * @author 系统
 * @since 2024-01-08 09:46:02
 */
@Service("kxsWarehouseLimitService")
@RequiredArgsConstructor
@Slf4j
public class KxsWarehouseLimitServiceImpl extends ServiceImpl<KxsWarehouseLimitMapper, KxsWarehouseLimit> implements KxsWarehouseLimitService {

    private final RemoteKxsUserService remoteKxsUserService;

    private final KxsMachineAdvanceMapper kxsMachineAdvanceMapper;

    private final KxsWarehouseCreditAmountMapper kxsWarehouseCreditAmountMapper;

    @Override
    public AdvanceUserDetailVO getUserDetail(String userCode, BigDecimal amount) {

        AdvanceUserDetailVO advanceUserDetailVO = new AdvanceUserDetailVO();

        R<KxsUser> kxsUserR = remoteKxsUserService.loadUserByCode(userCode, SecurityConstants.FROM_IN);
        KxsUser user = RetOps.of(kxsUserR)
                .getData()
                .orElseThrow(() -> new GlobalCustomerException(ErrorTypeEnum.USER_NOT_FOUND.getDescription()));
        advanceUserDetailVO.setUsername(user.getUsername());
        //预发额度
        KxsWarehouseLimit kxsWarehouseLimit = baseMapper.selectOne(Wrappers.<KxsWarehouseLimit>lambdaQuery().eq(KxsWarehouseLimit::getUserId, user.getId()));
        advanceUserDetailVO.setAdvanceAmount(kxsWarehouseLimit.getValidAmount());

        //预发机数量
        Long advanceCount = kxsMachineAdvanceMapper.selectUserByCount(user.getId());
        advanceUserDetailVO.setAdvanceTotal(advanceCount.intValue());

        return advanceUserDetailVO;
    }

    @Override
    public AdvanceAmountVO advanceAmount() {

        Long userId = SecurityUtils.getUser().getId();
        KxsWarehouseLimit kxsWarehouseLimit = baseMapper.selectOne(Wrappers.<KxsWarehouseLimit>lambdaQuery()
                .eq(KxsWarehouseLimit::getUserId, userId)
                .eq(KxsWarehouseLimit::getWarehouseType, WarehouseLimitTypeEnum.XFC.getType()));

        AdvanceAmountVO advanceAmountVO = new AdvanceAmountVO();

        BeanUtils.copyProperties(kxsWarehouseLimit, advanceAmountVO);

        return advanceAmountVO;
    }

    @Override
    public WarehouseAmountVO warehouseAmount() {

        Long userId = SecurityUtils.getUser().getId();

        KxsWarehouseLimit kxsWarehouseLimit = baseMapper.selectOne(Wrappers.<KxsWarehouseLimit>lambdaQuery()
                .eq(KxsWarehouseLimit::getUserId, userId)
                .eq(KxsWarehouseLimit::getWarehouseType, WarehouseLimitTypeEnum.FC.getType()));
        WarehouseAmountVO warehouseAmountVO = new WarehouseAmountVO();
        BeanUtils.copyProperties(kxsWarehouseLimit, warehouseAmountVO);
        //统计为他人担保额度
        KxsWarehouseCreditAmount creditUserAmount = kxsWarehouseCreditAmountMapper.selectOne(Wrappers.query(KxsWarehouseCreditAmount.class)
                .select("IFNULL(SUM(credit_amount), 0) as creditAmount")
                .eq("credit_user_id", userId));
        warehouseAmountVO.setToCreditAmount(creditUserAmount.getCreditAmount());
        warehouseAmountVO.setTotalAmount(NumberUtil.add(kxsWarehouseLimit.getCreditAmount(), kxsWarehouseLimit.getForAmount(), kxsWarehouseLimit.getFixedAmount()));
        return warehouseAmountVO;
    }

    @Override
    public void refreshUserAmount(Integer userId, Integer amount, String kind) {
        //查询用户额度
        KxsWarehouseLimit userLimit = baseMapper.selectOne(Wrappers.<KxsWarehouseLimit>lambdaQuery()
                .eq(KxsWarehouseLimit::getWarehouseType, WarehouseLimitTypeEnum.XFC.getType())
                .eq(KxsWarehouseLimit::getUserId, userId));

        if(KindTypeEnum.ADD.getType().equals(kind)){
            //已用额度增加
            userLimit.setUsedAmount(NumberUtil.add(userLimit.getUsedAmount(), amount));
            //可用额度减少
            userLimit.setValidAmount(NumberUtil.sub(userLimit.getValidAmount(), amount));
        }else{
            //已用额度减少
            userLimit.setUsedAmount(NumberUtil.sub(userLimit.getUsedAmount(), amount));
            //可用额度增加
            userLimit.setValidAmount(NumberUtil.add(userLimit.getValidAmount(), amount));
        }

        baseMapper.updateById(userLimit);
    }

    @Override
    public void refreshWarehouseAmount(Integer userId, int amount, String kind) {

        //查询仓库额度
        KxsWarehouseLimit warehouseLimit = baseMapper.selectOne(Wrappers.<KxsWarehouseLimit>lambdaQuery()
                .eq(KxsWarehouseLimit::getWarehouseType, WarehouseLimitTypeEnum.FC.getType())
                .eq(KxsWarehouseLimit::getUserId, userId));

        if(KindTypeEnum.ADD.getType().equals(kind)){
            //已用额度增加
            warehouseLimit.setUsedAmount(NumberUtil.add(warehouseLimit.getUsedAmount(), amount));
            //可用额度减少
            warehouseLimit.setValidAmount(NumberUtil.sub(warehouseLimit.getValidAmount(), amount));
        }else{
            //已用额度减少
            warehouseLimit.setUsedAmount(NumberUtil.sub(warehouseLimit.getUsedAmount(), amount));
            //可用额度增加
            warehouseLimit.setValidAmount(NumberUtil.add(warehouseLimit.getValidAmount(), amount));
        }

        baseMapper.updateById(warehouseLimit);

    }

    @Override
    public Boolean addUserAdvanceLimit(KxsWarehouseLimit kxsWarehouseLimit) {

        //查询用户预发额度
        KxsWarehouseLimit warehouseLimit = baseMapper.selectOne(Wrappers.<KxsWarehouseLimit>lambdaQuery()
                .eq(KxsWarehouseLimit::getWarehouseType, WarehouseLimitTypeEnum.XFC.getType())
                .eq(KxsWarehouseLimit::getUserId, kxsWarehouseLimit.getUserId()));

        if(warehouseLimit == null){
            kxsWarehouseLimit.setWarehouseType(WarehouseLimitTypeEnum.XFC.getType());
            baseMapper.insert(kxsWarehouseLimit);
        }else{
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }


}

