package com.sv.service.oms;

import com.github.pagehelper.PageHelper;
import com.sv.entity.VenuePrice;
import com.sv.exception.oms.OmsException;
import com.sv.mapper.VenuePriceMapper;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.service.impl.BaseServiceImpl;
import net.sf.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * Service - 场馆对应价格表
 *
 * @author lihong
 * @since 2018-08-22
 */
@Service("venuePriceServiceOms")
@Transactional(readOnly = true)
public class VenuePriceService extends BaseServiceImpl {

	private final Logger logger = LoggerFactory.getLogger(VenuePriceService.class);

	@Resource
	private VenuePriceMapper venuePriceMapper;

	/**
	 * 创建场馆对应价格表
	 *
	 * @param venuePrice 场馆对应价格表
	 */
	@Transactional(readOnly = false)
    public void save(VenuePrice venuePrice) {
		if (venuePrice.getId() != null && venuePrice.getId() > 0) {
			// 更新
			venuePriceMapper.update(venuePrice);
		} else {
			// 新建
			venuePriceMapper.insert(venuePrice);
		}
	}

	/**
	 * 更新场馆对应价格表
 	 *
	 * @param venuePrice 场馆对应价格表
	 */
	@Transactional
    public void update(VenuePrice venuePrice) {
		venuePriceMapper.update(venuePrice);
	}

	/**
	 * 删除场馆对应价格表
	 *
	 * @param id 编号
   	 * @return 删除数量
	 */
	@Transactional(readOnly = false)
    public Integer delete(Integer id) {
		if (id == null || id <= 0) {
			return 0;
		}
		return venuePriceMapper.delete(id);
	}

	/**
	 * 删除场馆对应价格表
	 *
	 * @param ids 编号数组
	 * @return 删除数量
	 */
	@Transactional
    public Integer deleteByIds(Integer[] ids) {
		if (ids == null || ids.length == 0) {
			return 0;
		}
		return venuePriceMapper.deleteByIds(ids);
	}

	/**
	 * 查询场馆对应价格表
	 *
	 * @param id 编号
	 * @return 场馆对应价格表
	 */
	public VenuePrice findById(Integer id) {
		return venuePriceMapper.findById(id);
	}

	/**
	 * 查询场馆对应价格表
	 *
	 * @param pagination 分页信息
	 * @return 分页结果
	 */
	public Pagination findPage(Pagination pagination) {
		PageHelper.startPage(pagination.getPage(), pagination.getPageSize());
		PageHelper.orderBy("id desc");
		pagination.setQueryResult(venuePriceMapper.findAll());
		return pagination;
	}

	/**
	 * 查询场馆对应的价格
	 */
	public List<VenuePrice> findPrice(Integer venueId){
		return venuePriceMapper.findPrice(venueId);
	}

	/**
	 * 判断当前时间是否已设置
	 */
	public void checkTime(JSONArray jsonArray) {
		for (int i =0;i<jsonArray.size();i++){
			String priceDate = jsonArray.getJSONObject(i).getString("priceDate");
			net.sf.json.JSONArray json = JSONArray.fromObject(priceDate);
			DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
			LocalDateTime ldt = LocalDateTime.parse("2018-08-08 "+ json.get(0),df);
			LocalTime starTime = ldt.toLocalTime();
			LocalDateTime end = LocalDateTime.parse("2018-08-08 "+ json.get(1),df);
			LocalTime endTime = end.toLocalTime();
			for (int j =0;j<jsonArray.size();j++){
				if (i == j){
					continue;
				}
				String time = jsonArray.getJSONObject(j).getString("priceDate");
				net.sf.json.JSONArray timeJson = JSONArray.fromObject(time);
				DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
				LocalDateTime star = LocalDateTime.parse("2018-08-08 "+ timeJson.get(0),dtf);
				LocalTime checkStarTime = star.toLocalTime();

				boolean compare = checkStarTime.isAfter(starTime);
				boolean compareTme = checkStarTime.isBefore(endTime);

				if (compare && compareTme) {
					throw new OmsException("时间段设置有冲突！请检查后再试");
				}
			}
		}
	}

	/**
	 * 保存场馆对应的价格
	 */
	@Transactional
	public void saveVenuePrice(String priceListStr,Integer venueId,Integer venueType,Integer platFormId){
		net.sf.json.JSONArray jsonArray = JSONArray.fromObject(priceListStr);
		// 校验时间段是否冲突
		checkTime(jsonArray);
		delete(venueId);
		for (int i =0;i<jsonArray.size();i++){
			VenuePrice price = new VenuePrice();
			price.setVenueId(venueId);
			price.setVenueTye(venueType);
			price.setPrice(new BigDecimal(jsonArray.getJSONObject(i).getString("price")));
			DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
			String priceDate = jsonArray.getJSONObject(i).getString("priceDate");
			net.sf.json.JSONArray json = JSONArray.fromObject(priceDate);
			LocalDateTime ldt = LocalDateTime.parse("2018-08-08 "+ json.get(0),df);
			price.setStarTime(ldt.toLocalTime());
			LocalDateTime end = LocalDateTime.parse("2018-08-08 "+ json.get(1),df);
			price.setEndTime(end.toLocalTime());
			price.setPlatformId(platFormId);
			save(price);
		}
	}
}

