/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.shopping.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.constant.CacheBizConstant;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.modules.shopping.entity.ConsignConfig;
import org.springblade.modules.shopping.vo.ConsignConfigVO;
import org.springblade.modules.shopping.mapper.ConsignConfigMapper;
import org.springblade.modules.shopping.service.IConsignConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

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

/**
 * 寄售配置表 服务实现类
 *
 * @author BladeX
 * @since 2022-02-13
 */
@Service
public class ConsignConfigServiceImpl extends ServiceImpl<ConsignConfigMapper, ConsignConfig> implements IConsignConfigService {

	@Autowired
	private BladeRedis bladeRedis;

	@Override
	public IPage<ConsignConfigVO> selectConsignConfigPage(IPage<ConsignConfigVO> page, ConsignConfigVO consignConfig) {
		return page.setRecords(baseMapper.selectConsignConfigPage(page, consignConfig));
	}

	@Override
	public List<ConsignConfig> selectCacheList() {
		List<ConsignConfig> list = bladeRedis.get(CacheBizConstant.CACHE_CONSIGN_CONFIG);
		if (list == null || list.size() == 0){
			list = baseMapper.selectList(Wrappers.<ConsignConfig>lambdaQuery()
				.eq(ConsignConfig::getIsEnabled, 2)
				.eq(ConsignConfig::getIsNow, 2)
				.orderByAsc(ConsignConfig::getStartTime));
			bladeRedis.setEx(CacheBizConstant.CACHE_CONSIGN_CONFIG, list, CacheBizConstant.CACHE_TIME);
		}
		return list;
	}

	@Override
	public Kv selectCacheConsignConfig() {
		Kv kv = Kv.create();
		List<ConsignConfig> list = selectCacheList();
		if (list.size() > 0){
			String todayYmd = DateUtil.format(DateUtil.now(), DateUtil.PATTERN_DATE);
			for (ConsignConfig consignConfig : list) {
				String shims = DateUtil.format(consignConfig.getStartTime(), DateUtil.PATTERN_TIME);
				String hims = DateUtil.format(consignConfig.getEndTime(), DateUtil.PATTERN_TIME);
				String startTime = todayYmd + " " + shims;
				String endTime = todayYmd + " " + hims;
				Date endDate = DateUtil.parse(endTime, DateUtil.PATTERN_DATETIME);
				long secondEnd = DateUtil.between(DateUtil.now(), endDate).getSeconds();
				if (secondEnd > 0) {
					kv.set("startTime", startTime)
						.set("endTime", endTime)
						.set("id",consignConfig.getId());
					return kv;
				}
			}
			ConsignConfig consignConfig = list.get(0);
			DateTime date = cn.hutool.core.date.DateUtil.tomorrow();
			String nextYmd = DateUtil.format(date, DateUtil.PATTERN_DATE);
			String startTime = nextYmd + " " + DateUtil.format(consignConfig.getStartTime(), DateUtil.PATTERN_TIME);
			String endHms = DateUtil.format(consignConfig.getEndTime(), DateUtil.PATTERN_TIME);
			String endTime = nextYmd + " " + endHms;
			kv.set("startTime", startTime)
				.set("endTime", endTime)
				.set("id",list.get(0).getId());
			return kv;
		}
		kv.set("startTime", null)
			.set("endTime", null)
			.set("id",null);
		return kv;
	}

	@Override
	public boolean updateConsignTime(ConsignConfig consignConfig) {
		return baseMapper.updateConsignTime(consignConfig);
	}

}
