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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.common.core.constant.enums.ErrorTypeEnum;
import com.kxs.common.core.exception.GlobalCustomerException;
import com.kxs.common.security.util.SecurityUtils;
import com.kxs.store.api.dto.kxsapp.storeCredit.StoreCreditStatusDTO;
import com.kxs.store.api.model.KxsWarehouseCreditAmount;
import com.kxs.store.api.model.KxsWarehouseLimit;
import com.kxs.store.api.vo.kxsapp.storeCredit.StoreCreditListVO;
import com.kxs.store.biz.constant.enums.StoreStatusEnum;
import com.kxs.store.biz.constant.enums.WarehouseLimitTypeEnum;
import com.kxs.store.biz.mapper.KxsWarehouseCreditAmountMapper;
import com.kxs.store.biz.mapper.KxsWarehouseLimitMapper;
import com.kxs.store.biz.service.KxsWarehouseCreditAmountService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 客小爽分仓担保明细记录表(KxsWarehouseCredit)表服务实现类
 *
 * @author 系统
 * @since 2024-01-08 09:46:03
 */
@Service("kxsWarehouseCreditService")
@RequiredArgsConstructor
public class KxsWarehouseCreditAmountServiceImpl extends ServiceImpl<KxsWarehouseCreditAmountMapper, KxsWarehouseCreditAmount> implements KxsWarehouseCreditAmountService {

    private final KxsWarehouseLimitMapper warehouseLimitMapper;

    @Override
    public List<StoreCreditListVO> getByList() {

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

        return baseMapper.selectByList(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean changeStatus(StoreCreditStatusDTO param) {

        KxsWarehouseCreditAmount creditAmount = getById(param.getId());
        Optional.ofNullable(creditAmount).orElseThrow(() -> new RuntimeException(ErrorTypeEnum.PARAM_ERROR.getDescription()));

        if(!Objects.equals(creditAmount.getStatus(), StoreStatusEnum.C_NEED_CFM.getType())){
            throw new GlobalCustomerException("该记录已操作，请勿重复点击");
        }
        creditAmount.setStatus(param.getStatus());

        baseMapper.updateById(creditAmount);

        if(Objects.equals(param.getStatus(), StoreStatusEnum.C_ON_CFM.getType())){

            KxsWarehouseLimit warehouseLimit = warehouseLimitMapper.selectOne(Wrappers.<KxsWarehouseLimit>lambdaQuery()
                    .eq(KxsWarehouseLimit::getUserId, creditAmount.getToCreditUserId())
                    .eq(KxsWarehouseLimit::getWarehouseType, WarehouseLimitTypeEnum.FC.getType()));
            Optional.ofNullable(warehouseLimit).orElseThrow(() -> new GlobalCustomerException("被担保人分仓不存在"));
            warehouseLimit.setCreditAmount(warehouseLimit.getCreditAmount().add(creditAmount.getCreditAmount()));
            warehouseLimitMapper.updateById(warehouseLimit);
        }

        return Boolean.TRUE;
    }
}

