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


import com.neusoft.smart.pos.dto.trade.request.PostBillSettlementCycleRequest;
import com.neusoft.smart.pos.dto.trade.request.PutBillSettlementCycleRequest;
import com.neusoft.smart.pos.dto.trade.response.GetBillSettlementCycleResponse;
import com.neusoft.smart.pos.dto.trade.response.PostBillSettlementCycleResponse;
import com.neusoft.smart.pos.dto.trade.response.PutBillSettlementCycleResponse;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.order.constants.TradeErrorCode;
import com.neusoft.smart.pos.trade.persistent.domain.BillSettlementCycle;
import com.neusoft.smart.pos.trade.repository.BillSettlementCycleRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

@Service("billSettlementCycleService")
public class BillSettlementCycleService {

	private final static Logger logger = LoggerFactory.getLogger(BillSettlementCycleService.class);
	
	@Autowired
	private BillSettlementCycleRepository billSettlementCycleRepository;
	
	/**
	 * 商户结算周期信息查询方法
	 * @return
	 */
	public GetBillSettlementCycleResponse queryBillSettlementCycle(Integer orgId) {
		GetBillSettlementCycleResponse billSettlementCyclesResponse = new GetBillSettlementCycleResponse();
		BillSettlementCycle billSettlementCycle = billSettlementCycleRepository.queryBillSettlementCycleByOrgId(orgId);
		if(null != billSettlementCycle){
			BeanUtils.copyProperties(billSettlementCycle,billSettlementCyclesResponse);
		}else{
			billSettlementCyclesResponse = null;
		}
		return billSettlementCyclesResponse;
	}
	
	/**
	 * 商户结算周期信息保存方法
	 * @return
	 */
	public PostBillSettlementCycleResponse saveBillSettlementCycle(Integer orgId,PostBillSettlementCycleRequest billSettlementCycleRequest) {
		PostBillSettlementCycleResponse billSettlementCyclesResponse = new PostBillSettlementCycleResponse();
		BillSettlementCycle billSettlementCycle = billSettlementCycleRepository.queryBillSettlementCycleByOrgId(orgId);
		if(null != billSettlementCycle){
			throw new BusinessCommonException(TradeErrorCode.BILL_SETTLEMENT_CYCLE_EXIST);
		}
		billSettlementCycle = new BillSettlementCycle();
		BeanUtils.copyProperties(billSettlementCycleRequest,billSettlementCycle);
		billSettlementCycle.setOrgId(orgId);
		if(billSettlementCycle.getBillDayType().equals("1")&&(billSettlementCycle.getBillDay() == null || billSettlementCycle.getBillDay() == 0)){
			throw new BusinessCommonException(TradeErrorCode.BILL_DAY_EMPTY);
		}
		Boolean bool = billSettlementCycleRepository.saveBillSettlementCycleByOrgId(billSettlementCycle);
		billSettlementCyclesResponse.setIsSuccess(bool);
		return billSettlementCyclesResponse;
	}
	
	/**
	 * 商户归档账单信息更新方法
	 * @return
	 */
	public PutBillSettlementCycleResponse updateBillSettlementCycle(Integer orgId, PutBillSettlementCycleRequest billSettlementCycleRequest) {
		PutBillSettlementCycleResponse billSettlementCyclesResponse = new PutBillSettlementCycleResponse();
		BillSettlementCycle billSettlementCycle = billSettlementCycleRepository.queryBillSettlementCycleByOrgId(orgId);
		if(null == billSettlementCycle){
			throw new BusinessCommonException(TradeErrorCode.BILL_SETTLEMENT_CYCLE_NOT_EXIST);
		}
		billSettlementCycle = new BillSettlementCycle();
		BeanUtils.copyProperties(billSettlementCycleRequest,billSettlementCycle);
		if(billSettlementCycle.getBillDayType().equals("1")&&(billSettlementCycle.getBillDay() == null || billSettlementCycle.getBillDay() == 0)){
			throw new BusinessCommonException(TradeErrorCode.BILL_DAY_EMPTY);
		}
		Boolean bool = billSettlementCycleRepository.updateBillSettlementCycleByOrgId(orgId,billSettlementCycle);
		billSettlementCyclesResponse.setIsSuccess(bool);
		return billSettlementCyclesResponse;
	}
	
}
