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


import com.alibaba.fastjson.JSON;
import com.neusoft.smart.pos.dto.trade.request.DeleteBillSettlementRuleRequest;
import com.neusoft.smart.pos.dto.trade.request.PostBillRuleRequest;
import com.neusoft.smart.pos.dto.trade.request.PutBillRuleRequest;
import com.neusoft.smart.pos.dto.trade.request.PutBillSettlementRuleSortRequest;
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.BillSettlementRuleService;
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.ArrayList;
import java.util.List;

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

	@Autowired
	private BillSettlementRuleService billSettlementRuleService;

	@ApiOperation(value = "结算规则查询使用接口", notes = "使用gateway")
	@GetMapping("/billSettlementRule")
	public ResponseData<GetBillSettlementRuleResponse> queryBillSettlementRule(@RequestParam(value="ruleId",defaultValue = "0") Integer ruleId) {
		Integer orgId = RequestHeaderUtil.getOrgId();
		if (null == orgId) {
			throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
		}
		return new ResponseData<>(billSettlementRuleService.queryBillSettlementRule(orgId,ruleId));
	}
	
	@ApiOperation(value = "结算规则信息删除接口", notes = "未使用gateway")
	@DeleteMapping("/billSettlementRule")
	public ResponseData<DeleteBillSettlementRuleResponse> deleteBillSettlementRule(@Valid DeleteBillSettlementRuleRequest billSettlementRuleRequest, BindingResult bindingResult) {
		return new ResponseData<>(billSettlementRuleService.deleteBillSettlementRule(billSettlementRuleRequest));
	}
	
	@ApiOperation(value = "结算规则信息批量新增使用接口", notes = "未使用gateway")
	@PostMapping("/billSettlementRule")
	public ResponseData<PostBillSettlementRuleResponse> saveBillSettlementRule(@Valid @RequestBody PostBillRuleRequest billRuleRequest, BindingResult bindingResult) {
		Integer orgId = RequestHeaderUtil.getOrgId();
		if (null == orgId) {
			throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
		}
		return new ResponseData<>(billSettlementRuleService.saveBillSettlementRule(orgId,billRuleRequest));
	}

    @ApiOperation(value = "结算规则信息更新使用接口", notes = "未使用gateway")
    @PutMapping("/billSettlementRule")
    public ResponseData<PutBillSettlementRuleResponse> updateBillSettlementRule(@Valid @RequestBody PutBillRuleRequest billRuleRequest, BindingResult bindingResult) {
        Integer orgId = RequestHeaderUtil.getOrgId();
        if (null == orgId) {
            throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
        }
        return new ResponseData<>(billSettlementRuleService.updateBillSettlementRule(orgId,billRuleRequest));
    }
    
    @ApiOperation(value = "结算规则信息更新使用接口", notes = "未使用gateway")
    @PutMapping("/billSettlementRuleSort")
    public ResponseData<PutBillSettlementRuleSortResponse> updateBillSettlementRuleSort(@Valid @RequestBody PutBillSettlementRuleSortRequest billRuleSortRequest, BindingResult bindingResult) {
        Integer orgId = RequestHeaderUtil.getOrgId();
        if (null == orgId) {
            throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
        }
        return new ResponseData<>(billSettlementRuleService.updateBillSettlementRuleSort(orgId,billRuleSortRequest));
    }
    
    @ApiOperation(value = "结算规则列表查询使用接口", notes = "使用gateway")
    @GetMapping("/billSettlementRules")
    public ResponseData<GetBillSettlementRulesResponse> queryBillSettlementRules() {
        Integer orgId = RequestHeaderUtil.getOrgId();
        if (null == orgId) {
            throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
        }
        return new ResponseData<>(billSettlementRuleService.queryBillSettlementRules(orgId));
    }
}

