package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.exception.RenException;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.dao.AppSysLoanApplyDao;
import io.renren.dto.AppSysLoanApplyDTO;
import io.renren.entity.AppSysLoanApplyEntity;
import io.renren.interceptor.ThreadLocalLoginUser;
import io.renren.service.AppSysLoanApplyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * APP系统用户贷款申请表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2022-05-11
 */
@Service
public class AppSysLoanApplyServiceImpl extends CrudServiceImpl<AppSysLoanApplyDao, AppSysLoanApplyEntity, AppSysLoanApplyDTO> implements AppSysLoanApplyService {

    @Override
    public QueryWrapper<AppSysLoanApplyEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String appUserId = params.get("app_user_id")+"";

        QueryWrapper<AppSysLoanApplyEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.eq(StringUtils.isNotBlank(appUserId), "app_user_id", appUserId);

        return wrapper;
    }

    @Override
    public void updateLoanStep(Integer step) {
        Long userId = ThreadLocalLoginUser.getUserId();
        AppSysLoanApplyEntity applyEntity = baseDao.selectOne(new QueryWrapper<AppSysLoanApplyEntity>().eq("app_user_id",userId));
        if (applyEntity==null){
            throw new RenException("贷款申请不存在");
        }
        if (step>applyEntity.getStep()) {
            applyEntity.setStep(step);
            baseDao.updateById(applyEntity);
        }
    }
}