package com.cyksj.web.controller.corp;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.corp.kf.CorpKfAccountFollowMapper;
import com.cyksj.mapper.corp.kf.CorpKfAccountMapper;
import com.cyksj.model.entity.CorpKfAccount;
import com.cyksj.model.entity.CorpKfAccountFollow;
import com.cyksj.redis.RedisService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/applet/coupon")
@RequiredArgsConstructor
public class CorpKfFrontController {
	private final CorpKfAccountMapper corpKfAccountMapper;

	private final CorpKfAccountFollowMapper corpKfAccountFollowMapper;

	private final RedisService redisService;

	/**
	 * 当前微信客服是否处于值班
	 */
	@GetMapping("/get/duty")
	public Result<Boolean> isDuty(@RequestParam(defaultValue = "1") Integer scene) {
		CorpKfAccount corpKfAccount = corpKfAccountMapper.selectOne(Wrappers.lambdaQuery(CorpKfAccount.class).eq(CorpKfAccount::getScene, scene).last("limit 1"));
		//默认官网
		if (corpKfAccount == null) {
			corpKfAccount = corpKfAccountMapper.selectOne(Wrappers.lambdaQuery(CorpKfAccount.class).eq(CorpKfAccount::getScene, 1).last("limit 1"));
			if (corpKfAccount == null) {
				return GatewayResponse.SUCCESS.newBuilder().toResult(false);
			}
		}
		DateTime now = DateTime.now();
		String thisHourDate = DateUtil.format(now, "HH:mm");
		//星期天为1
		int dayOfWeek = DateUtil.dayOfWeek(now);
		//是否值班期
		List<CorpKfAccountFollow> corpKfAccountFollows = corpKfAccountFollowMapper.selectList(Wrappers.lambdaQuery(CorpKfAccountFollow.class)
				.eq(CorpKfAccountFollow::getOpenKfId, corpKfAccount.getOpenKfId())
				.le(CorpKfAccountFollow::getStartTime, thisHourDate)
				.ge(CorpKfAccountFollow::getEndTime, thisHourDate)
				.eq(CorpKfAccountFollow::getDutyWeek, dayOfWeek));
		return GatewayResponse.SUCCESS.newBuilder().toResult(!corpKfAccountFollows.isEmpty());
	}


	/**
	 * 值班情况
	 */
	@GetMapping("/get/duty/info")
	public Result<List<CorpKfAccountFollow>> dutyInfo(@RequestParam(defaultValue = "1") Integer scene) {
		CorpKfAccount corpKfAccount = corpKfAccountMapper.selectOne(Wrappers.lambdaQuery(CorpKfAccount.class).eq(CorpKfAccount::getScene, scene).last("limit 1"));
		//默认官网
		if (corpKfAccount == null) {
			corpKfAccount = corpKfAccountMapper.selectOne(Wrappers.lambdaQuery(CorpKfAccount.class).eq(CorpKfAccount::getScene, 1).last("limit 1"));
			if (corpKfAccount == null) {
				return GatewayResponse.SUCCESS.newBuilder().toResult();
			}
		}
		//是否值班期
		List<CorpKfAccountFollow> corpKfAccountFollows = corpKfAccountFollowMapper.selectList(Wrappers.lambdaQuery(CorpKfAccountFollow.class)
				.eq(CorpKfAccountFollow::getOpenKfId, corpKfAccount.getOpenKfId())
				.select(CorpKfAccountFollow::getDutyWeek, CorpKfAccountFollow::getStartTime, CorpKfAccountFollow::getEndTime));
		return GatewayResponse.SUCCESS.newBuilder().toResult(corpKfAccountFollows);
	}
}
