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


import com.neusoft.smart.pos.dto.trade.request.GetBillDetailsRequest;
import com.neusoft.smart.pos.dto.trade.request.PutBillAcceptRequest;
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.BillService;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@Api(value = "商户结算信息管理使用rest接口", description = "商户结算信息管理使用rest接口")
@RestController
@Validated
@RequestMapping(path = "/bill")
public class BillController {

	@Autowired
	private BillService billService;

	@ApiOperation(value = "未归档账单数据查询使用接口", notes = "通过gateway")
	@GetMapping("/noAcceptBills")
	public ResponseData<GetNoAcceptBillsResponse> queryNoAcceptBills(
	         @RequestParam(value="orgId",defaultValue = "0") Integer orgId
            ,@RequestParam(value = "shopIds") List<Integer> shopIds
            ,@RequestParam(value="billNumber",defaultValue = "") String billNumber
            ,@RequestParam(value="rebuild",defaultValue = "0") String rebuild) {
		return new ResponseData<>(billService.queryNoAcceptBills(orgId,shopIds,billNumber,rebuild));
	}
	
	@ApiOperation(value = "归档账单数据查询使用接口", notes = "通过gateway")
	@GetMapping("/acceptBills")
	public ResponseData<GetAcceptBillsResponse> queryAcceptBills(
			@RequestParam(value="orgId",defaultValue = "0") Integer orgId
			,@RequestParam(value = "shopIds") List<Integer> shopIds
			,@RequestParam(value="startTime",defaultValue = "") String startTime
			,@RequestParam(value="endTime",defaultValue = "") String endTime) {
		return new ResponseData<>(billService.queryAcceptBills(orgId,shopIds,startTime,endTime));
	}
	
	@ApiOperation(value = "查询账单数据存在状态使用接口", notes = "未通过gateway")
	@GetMapping("/billExistStatus")
	public ResponseData<GetBillExistStatusResponse> queryBillExistStatus(
			@RequestParam(value = "shopId",defaultValue = "0") Integer shopId
			,@RequestParam(value="billNumber",defaultValue = "") String billNumber) {
        Integer orgId = RequestHeaderUtil.getOrgId();
        if (null == orgId) {
            throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
        }
		return new ResponseData<>(billService.queryBillExistStatus(orgId,shopId,billNumber));
	}
    
    @ApiOperation(value = "账单数据归档使用接口", notes = "未通过gateway")
    @PutMapping("/billAccept")
    public ResponseData<PutBillAcceptResponse> updateBillAcceptStatus(@Valid @RequestBody PutBillAcceptRequest billAcceptRequest, BindingResult bindingResult) {
        Integer orgId = RequestHeaderUtil.getOrgId();
        if (null == orgId) {
            throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
        }
        return new ResponseData<>(billService.updateBillAcceptStatus(orgId,billAcceptRequest.getShopId(),billAcceptRequest.getBillNumber()));
    }
    
    @ApiOperation(value = "结算单详情数据查询使用接口", notes = "通过gateway")
    @GetMapping("/billDetails")
    public ResponseData<GetBillDetailsResponse> queryBillDetails(
              @RequestParam(value = "shopId") Integer shopId
            , @RequestParam(value="billNumber") String billNumber
		    , @RequestParam("offset") Integer offset
		    , @RequestParam("limit") Integer limit
		    , @RequestParam("isPage") Boolean isPage) {
	    Integer orgId = RequestHeaderUtil.getOrgId();
	    if (null == orgId) {
		    throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
	    }
	    GetBillDetailsRequest billDetailsRequest = new GetBillDetailsRequest();
	    billDetailsRequest.setOrgId(orgId);
	    billDetailsRequest.setShopId(shopId);
	    billDetailsRequest.setBillNumber(billNumber);
	    billDetailsRequest.setOffset(offset);
	    billDetailsRequest.setLimit(limit);
	    billDetailsRequest.setIsPage(isPage);
        return new ResponseData<>(billService.queryBillDetails(billDetailsRequest));
    }
	
	@ApiOperation(value = "未结算周期编号查询使用接口", notes = "未通过gateway")
	@GetMapping("/noAcceptBillNumbers")
	public ResponseData<GetNoAcceptBillNumbersResponse> queryNoAcceptBillNumbers() {
		Integer orgId = RequestHeaderUtil.getOrgId();
		if (null == orgId) {
			throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
		}
		return new ResponseData<>(billService.queryNoAcceptBillNumbers(orgId));
	}
}

