package com.neusoft.smart.pos.trade.controller;


import com.neusoft.smart.pos.dto.trade.request.*;
import com.neusoft.smart.pos.dto.trade.response.*;
import com.neusoft.smart.pos.framework.constants.CommonErrorCode;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.trade.service.TradeStatisticsService;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(value = "交易统计使用rest接口", description = "交易统计使用rest接口")
@RestController
@RequestMapping(path = "/tradeStatistics")
@Validated
public class TradeStatisticsController {

	@Autowired
	private TradeStatisticsService tradeStatisticsService;

	@ApiOperation(value = "支付渠道金额统计查询接口(交易统计接口)", notes = "支付渠道金额统计查询接口(交易统计接口)")
	@GetMapping("/payChannelSummary")
	public ResponseData<GetPayChannelSummaryResponse> queryPayChannelSummary(@Valid @ModelAttribute GetPayChannelSummaryRequest payChannelSummaryRequest, BindingResult bindingResult) {
		return new ResponseData<>(tradeStatisticsService.queryPayChannelSummary(payChannelSummaryRequest));
	}

	@ApiOperation(value = "交易日报统计接口", notes = "交易日报统计接口")
	@GetMapping("/daily")
	public ResponseData<GetDailyResponse> queryPayDaily(@RequestParam("orgId") Integer orgId
			, @RequestParam(value="shopIds",defaultValue = "") List<Integer> shopIds
			, @RequestParam(value="startTime",defaultValue = "") String startTime
			, @RequestParam(value="endTime",defaultValue = "") String endTime
			, @RequestParam("offset") Integer offset
			, @RequestParam("limit") Integer limit
			, @RequestParam("isPage") Boolean isPage) {
		val dailyRequest = new GetDailyRequest();
		dailyRequest.setOrgId(orgId);
		dailyRequest.setShopIds(shopIds);
		dailyRequest.setStartTime(startTime);
		dailyRequest.setEndTime(endTime);
		dailyRequest.setOffset(offset);
		dailyRequest.setLimit(limit);
		dailyRequest.setIsPage(isPage);
		return new ResponseData<>(tradeStatisticsService.queryPayDaily(dailyRequest));
	}

	@ApiOperation(value = "支付渠道支付占比统计查询接口(交易占比统计接口)", notes = "支付渠道支付占比统计查询接口(交易占比统计接口)")
	@GetMapping("/payChannelProportion")
	public ResponseData<GetPayChannelProportionResponse> queryPayChannelProportion(@Valid @ModelAttribute GetPayChannelProportionRequest payChannelProportionRequest, BindingResult bindingResult) {
		return new ResponseData<>(tradeStatisticsService.queryPayChannelProportion(payChannelProportionRequest));
	}

	@ApiOperation(value = "POS端日交易汇总查询接口", notes = "POS端日交易汇总查询接口")
	@GetMapping("/payChannelSummaryForPos")
	public ResponseData<GetPayChannelSummaryForPosResponse> queryPayChannelSummaryForPos(
			  @RequestParam(value="serialNo",defaultValue = "") String serialNo
			, @RequestParam(value="dateTime",defaultValue = "") String dateTime
			, @RequestParam(value="endTime",defaultValue = "") String endTime
			, @RequestParam(value="startTime",defaultValue = "") String startTime
			, @RequestParam(value="timeMode",defaultValue = "2") String timeMode) {
		return new ResponseData<>(tradeStatisticsService.queryPayChannelSummaryForPos(serialNo,dateTime,startTime,endTime,timeMode));
	}

	@ApiOperation(value = "门店交易统计查询接口(交易统计接口)", notes = "门店交易统计查询接口(交易统计接口)")
	@GetMapping("/shopTradeSummary")
	public ResponseData<GetShopTradeSummaryResponse> queryShopTradeSummary(@RequestParam("orgId") Integer orgId
			, @RequestParam(value="shopIds",defaultValue = "") List<Integer> shopIds
			, @RequestParam(value="startTime",defaultValue = "") String startTime
			, @RequestParam(value="endTime",defaultValue = "") String endTime
			, @RequestParam(value="dateTime",defaultValue = "") String dateTime
			, @RequestParam(value="timeMode",defaultValue = "") String timeMode
	) {
		GetShopTradeSummaryRequest shopTradeSummaryRequest = new GetShopTradeSummaryRequest();
		shopTradeSummaryRequest.setOrgId(orgId);
		shopTradeSummaryRequest.setShopIds(shopIds);
		shopTradeSummaryRequest.setStartTime(startTime);
		shopTradeSummaryRequest.setEndTime(endTime);
		shopTradeSummaryRequest.setDateTime(dateTime);
		shopTradeSummaryRequest.setTimeMode(timeMode);
		return new ResponseData<>(tradeStatisticsService.queryShopTradeSummary(shopTradeSummaryRequest));
	}

	@ApiOperation(value = "交接班统计查询接口(交接班统计接口)", notes = "交接班统计查询接口(交接班统计接口)")
	@GetMapping("/shiftTradeSummary")
	public ResponseData<GetShiftTradeSummaryResponse> queryShiftTradeSummary(@RequestParam("orgId") Integer orgId
			, @RequestParam(value="shopIds",defaultValue = "") List<Integer> shopIds
			, @RequestParam(value="customerNumber",defaultValue = "") String customerNumber
			, @RequestParam(value="startTime",defaultValue = "") String startTime
			, @RequestParam(value="endTime",defaultValue = "") String endTime
			, @RequestParam(value="dateTime",defaultValue = "") String dateTime
			, @RequestParam(value="timeMode",defaultValue = "") String timeMode
	) {
		GetShopTradeSummaryRequest shopTradeSummaryRequest = new GetShopTradeSummaryRequest();
		if(StringUtils.isEmpty(orgId)) {
			//从header中获取集团ID
			orgId = RequestHeaderUtil.getOrgIdByRole(shopTradeSummaryRequest.getOrgId());
		}
        //从header中获取店铺ID
        if (null == shopIds|| shopIds.size() == 0) {
            shopTradeSummaryRequest.setShopIds(RequestHeaderUtil.getStoreIds());
        }
		shopTradeSummaryRequest.setOrgId(orgId);
		shopTradeSummaryRequest.setShopIds(shopIds);
		shopTradeSummaryRequest.setStartTime(startTime);
		shopTradeSummaryRequest.setEndTime(endTime);
		shopTradeSummaryRequest.setDateTime(dateTime);
		shopTradeSummaryRequest.setTimeMode(timeMode);
		return new ResponseData<>(tradeStatisticsService.queryShiftTradeSummary(shopTradeSummaryRequest, customerNumber));
	}
	
	@ApiOperation(value = "支付方式交易统计查询接口", notes = "支付方式交易统计查询接口")
	@GetMapping("/payTypeTradeSummary")
	public ResponseData<GetPayTypeTradeSummaryResponse> queryPayTypeTradeSummary(
			  @RequestParam("orgId") Integer orgId
			, @RequestParam(value="shopIds",defaultValue = "") List<Integer> shopIds
			, @RequestParam(value="startTime",defaultValue = "") String startTime
			, @RequestParam(value="endTime",defaultValue = "") String endTime
	) {
		GetPayTypeTradeSummaryRequest payTypeTradeSummaryRequest = new GetPayTypeTradeSummaryRequest();
		payTypeTradeSummaryRequest.setOrgId(orgId);
		payTypeTradeSummaryRequest.setShopIds(shopIds);
		payTypeTradeSummaryRequest.setStartTime(startTime);
		payTypeTradeSummaryRequest.setEndTime(endTime);
		return new ResponseData<>(tradeStatisticsService.queryPayTypeTradeSummary(payTypeTradeSummaryRequest));
	}
}
	
