package com.servicemall.loyalty.service;

import java.util.*;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import com.servicemall.right.data.Coupon;
import com.servicemall.right.service.CouponService;
import com.servicemall.systemcommon.util.DateUtil;
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);
	}

	public static List<Plan> queryPlanList(String dms_code, Pagination pagination){
		List<Plan> planList = new ArrayList<>();
		Plan plan = new Plan();
		plan.setBuyStartTime(DateUtil.getTime());
		plan.setBuyEndTime(DateUtil.getTime());
		plan.setApplyOld("1,2,3");
		plan.setPlanImage("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607427778447&di=b0d30539b93ff6e7394b9c80801e11c7&imgtype=0&src=http%3A%2F%2Fimg000.hc360.cn%2Fm6%2FM04%2F58%2F6A%2FwKhQolWVsnmEGYk8AAAAAIA_Z48561.jpg");
		plan.setId(1l);
		plan.setUniformPrice(300.00);
		plan.setPayTypeFlag(1);
		plan.setPriceTypeFlag(1);
		plan.setFactoryGuidePrice(8888.00);
		plan.setActiveStatus(2);
		plan.setBindingUseStatus(1);
		plan.setBuyAmountLimit(-1);
		plan.setBuyLimitDescription("测试");
		plan.setLoyaltyName("会员权益计划");
		plan.setDescription("测试");
		plan.setSelectedStatus(1);
		planList.add(plan);

		Plan plan2 = new Plan();
		plan2.setBuyStartTime(DateUtil.getTime());
		plan2.setBuyEndTime(DateUtil.getTime());
		plan2.setApplyOld("1,2,3");
		plan2.setPlanImage("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607427778447&di=b0d30539b93ff6e7394b9c80801e11c7&imgtype=0&src=http%3A%2F%2Fimg000.hc360.cn%2Fm6%2FM04%2F58%2F6A%2FwKhQolWVsnmEGYk8AAAAAIA_Z48561.jpg");
		plan2.setId(2l);
		plan2.setUniformPrice(300.00);
		plan2.setPriceTypeFlag(2);
		plan2.setPayTypeFlag(2);
		plan2.setDepositPrice(100.00);
		plan2.setFactoryGuidePrice(8888.00);
		plan2.setActiveStatus(2);
		plan2.setBindingUseStatus(1);
		plan2.setBuyAmountLimit(-1);
		plan2.setBuyLimitDescription("测试");
		plan2.setLoyaltyName("保养特惠计划");
		plan2.setDescription("测试");
		plan2.setSelectedStatus(1);
		planList.add(plan2);
		return planList;
	}


	public static Plan getPlanDetails(String loyalty_id,String dms_code){
		Plan plan = new Plan();
		plan.setBuyStartTime(DateUtil.getTime());
		plan.setBuyEndTime(DateUtil.getTime());
		plan.setApplyOld("1,2,3");
		plan.setPlanImage("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607427778447&di=b0d30539b93ff6e7394b9c80801e11c7&imgtype=0&src=http%3A%2F%2Fimg000.hc360.cn%2Fm6%2FM04%2F58%2F6A%2FwKhQolWVsnmEGYk8AAAAAIA_Z48561.jpg");
		plan.setActivityImage("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607427778447&di=b0d30539b93ff6e7394b9c80801e11c7&imgtype=0&src=http%3A%2F%2Fimg000.hc360.cn%2Fm6%2FM04%2F58%2F6A%2FwKhQolWVsnmEGYk8AAAAAIA_Z48561.jpg");
		plan.setId(1l);
		plan.setUniformPrice(300.00);
		plan.setPriceTypeFlag(1);
		plan.setFactoryGuidePrice(8888.00);
		plan.setActiveStatus(2);
		plan.setBindingUseStatus(1);
		plan.setBuyAmountLimit(-1);
		plan.setBuyLimitDescription("测试");
		plan.setLoyaltyName("会员权益计划");
		plan.setDescription("测试");
		plan.setSelectedStatus(1);
		List<Coupon> couponList = CouponService.queryCouponList(dms_code,null);
		plan.setCouponList(couponList);
		return plan;
	}

}