/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.platform.wrapper;

import cn.hutool.core.util.NumberUtil;
import org.springblade.common.cache.DictBizCache;
import org.springblade.common.cache.DictCache;
import org.springblade.common.enums.DictBizEnum;
import org.springblade.common.enums.DictEnum;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.entity.UserApp;
import org.springblade.modules.system.service.IUserService;
import org.springblade.modules.system.vo.UserAppVO;

import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author Chill
 */
public class UserAppWrapper extends BaseEntityWrapper<UserApp, UserAppVO> {

	public static UserAppWrapper build() {
		return new UserAppWrapper();
	}

	private static final IUserService userService;

	static {
		userService = SpringUtil.getBean(IUserService.class);
	}

	@Override
	public UserAppVO entityVO(UserApp entity) {
		UserAppVO vo = Objects.requireNonNull(BeanUtil.copy(entity, UserAppVO.class));
		User user = userService.getById(vo.getUserId());
		if (user != null){
			vo.setRealName(user.getRealName());
			vo.setUsername(user.getAccount());
			vo.setCode(user.getCode());
			vo.setBirthday(user.getBirthday());
			vo.setName(user.getName());
			vo.setSexDisplayName(DictCache.getValue(DictEnum.SEX,user.getSex()));
			vo.setEnableAuthDisplayName(DictCache.getValue(DictEnum.YES_NO,vo.getEnableAuth()));
			vo.setStateDisplayName(DictBizCache.getValue(DictBizEnum.USER_STATE, vo.getState()));
		}
		return vo;
	}

	/**
	 * 信誉分规则
	 *
	 *  1.完成交易信誉分加30分
	 * 	2.少于上一天抢的次数减30分 （日终）
	 * 	3.到达500分完成一场加5分，减少一场减30分
	 *
	 * @param userId 用户
	 * @param creditScore 信誉积分
	 * @return boolean
	 */
	public synchronized boolean creditScore(Long userId, Integer creditScore){
		UserApp userApp = new UserApp();
		userApp.setUserId(userId);
		userApp.setCreditScore(creditScore);
		return userApp.updateById();
	}

	/**
	 * 判断绿色积分
	 * @param ticket 绿色积分数
	 * @param price 金额
	 * @return  boolean
	 */
	public boolean isTicket(Double ticket, Double price){
		if (ticket < 100){
			return false;
		}
		double d = NumberUtil.mul(price, Double.valueOf("0.03"));
		if (ticket < d){
			return false;
		}
		return true;
	}

}
