package com.servicemall.loyalty.service;

import java.util.Collections;
import java.util.List;
import java.util.UUID;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import team.bangbang.common.CommonMPI;
import team.bangbang.common.data.KeyValue;
import team.bangbang.common.data.Pagination;

import com.servicemall.loyalty.data.Plan;
import com.servicemall.loyalty.mapper.PlanMapper;

import org.springframework.stereotype.Service;

/**
 * 礼遇计划 - Service
 *
 * @author 帮帮组
 * @version 1.0  2020-12-08
 */
@Service
public final class PlanService {
	/* 礼遇计划(Plan)Mapper */
	@Resource
	private PlanMapper _planMapper = null;
	/* 设置static的Mapper对象,主要是为了兼顾Service层的static方法需要 */
	private static PlanMapper planMapper = null;

	@PostConstruct
	public void init() {
		// 设置static的Mapper对象,主要是为了兼顾Service层的static方法需要
		planMapper = _planMapper;
	}

	/**
	 * 得到指定的礼遇计划
	 *
	 * @param id
	 *			指定的编号
	 * @return 礼遇计划
	 */
	public static Plan getObject(Long id) {
		if(planMapper == null) {
			return null;
		}

		// 参数校验

		if(id == null || id == 0L) {
			return null;
		}

		// 查询条件
		Plan form = new Plan();
		form.setId(id);

		return getObject(form, null);
	}

	/**
	 * 查询一条礼遇计划,并转化为相应的POJO对象
	 *
	 * @param plan
	 *			查询条件,不能为null
	 * @param appendix
	 *			附加限定条件
	 * @return 返回结果记录,并转化为相应的POJO对象
	 */
	public static Plan getObject(Plan plan, String appendix) {
		if(planMapper == null) {
			return null;
		}

		// 参数校验
		if(plan == null && (appendix == null || appendix.trim().length() == 0)) {
			return null;
		}

		return planMapper.getObject(plan, appendix);
	}

	/**
	 * 插入一条礼遇计划
	 *
	 * @param data
	 *			插入的数据,不能为null
	 * @return 1:成功 其它:失败
	 */
	public static int insert(Plan data) {
		if(planMapper == null) {
			return 0;
		}

		if (data.getId() == null) {

			// 返回创建的关键字的值,如果是系统自动生成的,则此处不返回
			long id = CommonMPI.generateSequenceId();
			data.setId(id);

		}

		return planMapper.insert(data);
	}

	/**
	 * 删除礼遇计划
	 *
	 * @param where
	 *			删除条件,不能为null
	 * @param appendix
	 *			附加限定条件
	 * @return 成功删除的记录数量
	 */
	public static int delete(Plan where, String appendix) {
		if(planMapper == null) {
			return 0;
		}

		return planMapper.delete(where, appendix);
	}

	/**
	 * 修改礼遇计划
	 *
	 * @param where
	 *			更新条件,不能为null
	 * @param appendix
	 *			附加限定条件
	 * @param data
	 *			更新数据,不能为null
	 * @return 成功修改的记录数量
	 */
	public static int update(Plan where, String appendix, Plan data) {
		if(planMapper == null) {
			return 0;
		}

		return planMapper.update(where, appendix, data);
	}

	/**
	 * 查询多条礼遇计划,并转化为相应的POJO对象列表
	 *
	 * @param where
	 *			更新条件,不能为null
	 * @param appendix
	 *			附加限定条件
	 * @param pagination
	 *			分页参数,如果分页参数为空,表示不分页
	 * @return 返回结果记录,并转化为相应的POJO对象列表
	 */
	public static List<Plan> list(Plan where, String appendix, Pagination pagination) {
		if(planMapper == null) {
			return Collections.emptyList();
		}

		return planMapper.list(where, appendix, pagination);
	}

	/**
	 * 获得符合条件的礼遇计划数量
	 *
	 * @param where
	 *			查询条件,不能为null
	 * @param appendix
	 *			附加限定条件
	 * @return 返回记录数量
	 */
	public static int count(Plan where, String appendix) {
		if(planMapper == null) {
			return 0;
		}

		return planMapper.count(where, appendix);
	}

	/**
	 * 获得售价类型{1:统一价格 2:车型动态价格}列表
	 *
	 * @return 售价类型{1:统一价格 2:车型动态价格}列表
	 */
	public static List<KeyValue> getPriceTypeList() {
		return CommonMPI.getDictionaryList(Plan.priceTypeFlags);
	}

	/**
	 * 获得付款方式{1:定金 2:全款}列表
	 *
	 * @return 付款方式{1:定金 2:全款}列表
	 */
	public static List<KeyValue> getPayTypeList() {
		return CommonMPI.getDictionaryList(Plan.payTypeFlags);
	}

}