package com.iformall.domain.po;

import com.alibaba.fastjson.JSONObject;
import com.iformall.enums.EnumScoreType;
import lombok.Data;

import javax.persistence.*;
import java.util.*;
import javax.persistence.Transient;
import java.util.List;
import javax.persistence.Id;
import java.io.Serializable;

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

@Table(name = "wx_score_rules")
@Data
public class WxScoreRules implements Serializable {
	private static final long serialVersionUID = 1L;

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

	@Transient
	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\":6,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"线上交易1元\"}" +
					"]}," +
					"{\"id\":3,\"limit\":1,\"step\":1,\"score\":0,\"desc\":\"绑定车牌1个\"}," +
					"{\"id\":4,\"limit\":0,\"step\":1,\"score\":0 ,\"desc\":\"连接WIFI1次\"}," +
					"{\"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\":\"编辑个人信息\"}" +
					"]";

	@Transient
	public static final String ID = "id";
	@Transient
	public static final String LIMIT = "limit";
	@Transient
	public static final String STEP = "step";
	@Transient
	public static final String SCORE = "score";

	@Id
    protected Long id;
    
    @Transient
    protected List<Long> ids;
    @Transient
    protected String sortColumns;
  
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public String getSortColumns() {
		return sortColumns;
	}
	 
	public List<Long> getIds() {
		return ids;
	}

	public void setIds(List<Long> ids) {
		this.ids = ids;
	}
	
	/**租户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;

	public String  getTenantId() {
		return tenantId;
	}
	public void setTenantId(String _tenantId) {
		tenantId = _tenantId;
	}

	public static String getScoreDefaultRules() {
		return scoreDefaultRules;
	}

	public static String getCreditDefaultRules() {
		return creditDefaultRules;
	}

	public void setRules(String rules) {
		this.rules = rules;
	}

	public String getRules() {
		return rules;
	}


	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;
	}

	public Date  getCreateDate() {
		return createDate;
	}
	public void setCreateDate(Date _createDate) {
		createDate = _createDate;
	}
	public Date  getUpdateDate() {
		return updateDate;
	}
	public void setUpdateDate(Date _updateDate) {
		updateDate = _updateDate;
	}

	public static enum Field
	{
		Id_ASC("`id` ASC"),Id_DESC("`id` DESC")
			,TenantId_ASC("`tenant_id` ASC"),TenantId_DESC("`tenant_id` DESC")
			,CreateDate_ASC("`create_date` ASC"),CreateDate_DESC("`create_date` DESC")
			,UpdateDate_ASC("`update_date` ASC"),UpdateDate_DESC("`update_date` DESC")
	;
		private String value;
		Field(String value){
			this.value = value;
		}
		public String getValue() {
			return value;
		}
		public void setCol(String value) {
			this.value = value;
		}
		@Override
		public String toString() {
			return this.getValue();
		}
	}
	
	public void setSortColumns(WxScoreRules.Field... fields)
	{
		if (fields == null || fields.length == 0) {
			return;
		}
		for (int k = 0; k < fields.length; k++) {
			if (fields[k] == null) {
				return;
			}
		}
		StringBuilder sb = new StringBuilder(fields[0].toString());
		for (int k = 1; k < fields.length; k++) {
			sb.append(",");
			sb.append(fields[k].toString());
		}
		this.sortColumns = sb.toString();
	}
	
	public void setSortColumns(String sortColumns)
	{
		if (sortColumns == null || "".equals(sortColumns.trim())) {
			return;
		}
		if (sortColumns.contains(",")) {
			String[] cols = sortColumns.split(",");
			java.util.List<Field> fList = new java.util.ArrayList();
			for (int k = 0; k < cols.length; k++) {
				fList.add(Field.valueOf(cols[k]));
			}
			this.setSortColumns(fList.toArray(new Field[fList.size()]));
		} else {
			this.setSortColumns(Field.valueOf(sortColumns));
		}
	}

}
