package com.jeequan.jeepay.mgr.ctrl.config;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateRange;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeequan.jeepay.components.mq.model.CleanTnDayCacheMQ;
import com.jeequan.jeepay.components.mq.vender.IMQSender;
import com.jeequan.jeepay.core.constants.ApiCodeEnum;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.SysLegalDay;
import com.jeequan.jeepay.mgr.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.SysLegalDayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 系统节假日管理类
 *
 * @author zx
 *
 * @date 2023-09-26 09:15
 */
@Slf4j
@RestController
@RequestMapping("/api/sysLegelDay")
public class SysLegalDayController extends CommonCtrl {

	@Autowired private SysLegalDayService sysLegalDayService;
	@Autowired private IMQSender mqSender;

	/**
	 * @Author: zx
	 * @Description: 列表
	 * @Date: 2023-09-26 09:15
	 */
	@PreAuthorize("hasAuthority('ENT_LEGAL_DAY_LIST')")
	@GetMapping
	public ApiRes list() {
		SysLegalDay sysLegalDay = getObject(SysLegalDay.class);

		Page<SysLegalDay> pages = sysLegalDayService.page(getIPage(), SysLegalDay.gw()
				.eq(StringUtils.isNotBlank(sysLegalDay.getDateType()), SysLegalDay::getDateType, sysLegalDay.getDateType())
				.orderByDesc(SysLegalDay::getDateValue)
		);

		return ApiRes.ok(pages);
	}

	/**
	 * @Author: zx
	 * @Description: 新增
	 * @Date: 2023-09-26 09:15
	 */
	@PreAuthorize("hasAuthority('ENT_LEGAL_DAY_ADD')")
	@PostMapping
	public ApiRes add() {
		String dateType = getValStringRequired("dateType");
		JSONArray dateValueArr = JSON.parseArray(getValStringRequired("dateValueArr"));
		if (CollUtil.isEmpty(dateValueArr) || dateValueArr.size() != 2) {
			return ApiRes.customFail("请选择日期区间");
		}

		DateRange range = DateUtil.range(dateValueArr.getDate(0), dateValueArr.getDate(1), DateField.DAY_OF_YEAR);
		List<DateTime> dateValueList = new ArrayList<>();
		while (range.hasNext()) {
			dateValueList.add(range.next());
		}

		if (sysLegalDayService.count(SysLegalDay.gw().in(SysLegalDay::getDateValue, dateValueList)) > 0) {
			throw new BizException("选择的日期区间包含已创建日期");
		}

		List<SysLegalDay> insertList = new LinkedList<>();
		for (DateTime dateValue : dateValueList) {
			SysLegalDay record = new SysLegalDay();
			record.setDateType(dateType);
			record.setDateValue(dateValue);

			insertList.add(record);
		}

		boolean saveResult = sysLegalDayService.saveBatch(insertList);
		if(!saveResult) {
			return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_CREATE);
		}

		// 清空T+N缓存日期
		mqSender.send(CleanTnDayCacheMQ.build());

		return ApiRes.ok();
	}

	/**
	 * @Author: zx
	 * @Description: 删除
	 * @Date: 2023-09-26 09:15
	 */
	@PreAuthorize("hasAuthority('ENT_LEGAL_DAY_DEL')")
	@DeleteMapping("/{recordId}")
	public ApiRes delete(@PathVariable("recordId") String recordId) {
		boolean result = sysLegalDayService.removeById(recordId);
		if (!result) {
			return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_DELETE);
		}

		// 清空T+N缓存日期
		mqSender.send(CleanTnDayCacheMQ.build());

		return ApiRes.ok();
	}

}
