package com.cyksj.web.controller.manage.exchange;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.cyksj.common.annotation.Log;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.dto.Result;
import com.cyksj.enums.BusinessType;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.manage.cms.CmsUserMapper;
import com.cyksj.model.entity.CmsUser;
import com.cyksj.model.excel.ExcelExchangeCodeData;
import com.cyksj.model.request.ExchangeCodeReq;
import com.cyksj.model.views.ExchangeCodeView;
import com.cyksj.service.exchange.ExchangeCodeService;
import com.cyksj.web.util.EasyExcelUtils;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/*
 *项目名: netflix
 *文件名: ExchangeCodeController
 *创建者: JavaZou
 *创建时间:2022/11/7 14:37
 */
@RestController
@RequestMapping("/manage/exchange/code")
@RequiredArgsConstructor
public class ExchangeCodeController {

	private final HttpServletRequest request;

	private final BeanSearcher beanSearcher;

	private final ExchangeCodeService exchangeCodeService;

	private final CmsUserMapper cmsUserMapper;

	/**
	 * 生成兑换码 并导出
	 */
	@GetMapping("/createAndExport")
	@SaCheckPermission("exchangeCode:add")
	@Log(module = "兑换码/生成兑换码", businessType = BusinessType.POST, isSaveRequestData = true)
	public void createAndExportCode(ExchangeCodeReq exchangeCodeReq, HttpServletResponse response) {
		if (ObjectUtil.hasEmpty(exchangeCodeReq.getGoodsId(), exchangeCodeReq.getSkuId(), exchangeCodeReq.getNumber())) {
			throw BusinessRuntimeException.getInstance("必填项为空");
		}
		long adminId = StpUtil.getLoginIdAsLong();
		CmsUser cmsUser = cmsUserMapper.selectById(adminId);
		exchangeCodeReq.setOperator(cmsUser.getNickname());
		List<ExcelExchangeCodeData> data =  exchangeCodeService.createExchangeCode(exchangeCodeReq);
		EasyExcelUtils.createExcelStreamMutilByEasyExcel(response, ExcelExchangeCodeData.class, data, "银河兑换码","银河兑换码", ExcelTypeEnum.XLSX, null);
	}

	/**
	 * 兑换码列表
	 */
	@GetMapping("/get")
	@SaCheckPermission("exchangeCode:view")
	public Result<SearchResult<ExchangeCodeView>> getExchangeCodeView() {
		return GatewayResponse.SUCCESS.newBuilder().toResult(beanSearcher.search(ExchangeCodeView.class, MapUtils.flat(request.getParameterMap())));
	}

	/**
	 * 导出兑换码
	 */
	@GetMapping("/export")
	@SaCheckPermission("exchangeCode:export")
	public void exportExchangeCode(HttpServletResponse response) {
		List<ExcelExchangeCodeData> data = beanSearcher.searchAll(ExcelExchangeCodeData.class, MapUtils.flat(request.getParameterMap()));
		EasyExcelUtils.createExcelStreamMutilByEasyExcel(response, ExcelExchangeCodeData.class, data, "银河兑换码","银河兑换码", ExcelTypeEnum.XLSX, null);
	}
}
