package com.iformall.domain.po;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.iformall.domain.po.base.BaseEntity;
import com.iformall.enums.EnumScoreType;
import lombok.Data;
import lombok.EqualsAndHashCode;


import java.util.Date;
import java.util.List;

/**
 * 1. 每天登录首次给成长值
 * 2. 消费金额为0时，不给成长值。
 * 3. 消费金额除以1元，四舍五入给成长值
 * 4. 绑车牌获得成长值
 * 5. 微信昵称授权获取成长值
 * 6. 微信手机授权获取成长值
 * 7. 完善个人信息获取成长值
 */

@TableName(value = "wx_score_rules")
@Data
@EqualsAndHashCode(callSuper = true)
public class WxScoreRules extends BaseEntity {

	@TableField(exist = false)
	public static final int DEFAULT_SCALE = 10;

	@TableField(exist = false)
	private static final String scoreDefaultRules =
			"[" +
					"{\"id\":1,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"每日登陆\"}," +
					"{\"id\":2,\"childs\":[" +
					"	{\"businessId\":1,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
					"	{\"businessId\":2,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
					"	{\"businessId\":3,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
					"	{\"businessId\":4,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
					"	{\"businessId\":5,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
					"	{\"businessId\":7,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
					"	{\"businessId\":8,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
					"	{\"businessId\":9,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
					"	{\"businessId\":10,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
                    "	{\"businessId\":11,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
					"	{\"businessId\":6,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}" +
					"]}," +
					"{\"id\":3,\"limit\":1,\"step\":1,\"score\":0,\"desc\":\"绑定车牌1个\"}," +
					"{\"id\":5,\"limit\":1,\"step\":1,\"score\":0,\"desc\":\"授权个人信息\"}," +
					"{\"id\":6,\"limit\":1,\"step\":1,\"score\":0,\"desc\":\"授权手机号\"}," +
					"{\"id\":7,\"limit\":1,\"step\":1,\"score\":0,\"desc\":\"编辑个人信息\"}" +
			"]";

	@TableField(exist = false)
	private static final String creditDefaultRules =
			"[" +
					"{\"id\":1,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"每日登陆\"}," +
					"{\"id\":2,\"childs\":[" +
					"	{\"businessId\":1,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
					"	{\"businessId\":2,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
					"	{\"businessId\":3,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
					"	{\"businessId\":4,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
					"	{\"businessId\":5,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
					"	{\"businessId\":7,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
					"	{\"businessId\":8,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
					"	{\"businessId\":9,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
					"	{\"businessId\":10,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
                    "	{\"businessId\":11,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}," +
					"	{\"businessId\":6,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}" +
					"]}," +
					"{\"id\":3,\"limit\":1,\"step\":1,\"score\":0,\"desc\":\"绑定车牌1个\"}," +
					"{\"id\":5,\"limit\":1,\"step\":1,\"score\":0,\"desc\":\"授权个人信息\"}," +
					"{\"id\":6,\"limit\":1,\"step\":1,\"score\":0,\"desc\":\"授权手机号\"}," +
					"{\"id\":7,\"limit\":1,\"step\":1,\"score\":0,\"desc\":\"编辑个人信息\"}," +
					"{\"id\":8,\"limit\":1,\"step\":1,\"score\":0,\"desc\":\"编辑个人信息\"}" +
					"]";

	@TableField(exist = false)
	public static final String ID = "id";
	@TableField(exist = false)
	public static final String LIMIT = "limit";
	@TableField(exist = false)
	public static final String STEP = "step";
	@TableField(exist = false)
	public static final String SCORE = "score";


    protected Long id;


    @io.swagger.annotations.ApiModelProperty(value="租户ID",name="tenantId")
	private String tenantId;

	@io.swagger.annotations.ApiModelProperty(value="规则",name="rules")
	private String rules;

	@io.swagger.annotations.ApiModelProperty(value="创建时间",name="createDate")
	private Date createDate;

	@io.swagger.annotations.ApiModelProperty(value="更新时间",name="updateDate")
	private Date updateDate;

	@io.swagger.annotations.ApiModelProperty(value="1成长值 2积分",name="type")
	private Integer type;

	@io.swagger.annotations.ApiModelProperty(value="清除前多少年积分",name="clearYear")
	private Integer clearYear;

	@io.swagger.annotations.ApiModelProperty(value="生日积分倍率",name="scale")
	private Integer scale;

	@io.swagger.annotations.ApiModelProperty(value = "积分锁定0正常1锁定", name = "creditLocked")
	private Integer creditLocked;

	public static String getScoreDefaultRules() {
		return scoreDefaultRules;
	}

	public static String getCreditDefaultRules() {
		return creditDefaultRules;
	}

	public Integer getRule(EnumScoreType scoreType, String element) {
		if (rules == null)
			return null;

		List<JSONObject> list = JSONObject.parseArray(rules, JSONObject.class);
		if (list == null)
			return null;
		if (list.size() == 0)
			return null;

		for (JSONObject jo:list) {
			Integer id = jo.getInteger(WxScoreRules.ID);
			if ( id!= null && id.equals(scoreType.getCode())){
				return jo.getInteger(element);
			}
		}

		return null;
	}

	public JSONObject getRuleObj(EnumScoreType scoreType) {
		if (rules == null)
			return null;

		List<JSONObject> list = JSONObject.parseArray(rules, JSONObject.class);
		if (list == null)
			return null;
		if (list.size() == 0)
			return null;

		for (JSONObject jo:list) {
			Integer id = jo.getInteger(WxScoreRules.ID);
			if ( id!= null && id.equals(scoreType.getCode())){
				return jo;
			}
		}

		return null;
	}


}
