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


import com.neusoft.smart.pos.dto.trade.request.*;
import com.neusoft.smart.pos.dto.trade.response.*;
import com.neusoft.smart.pos.framework.constants.ApplicationConstants;
import com.neusoft.smart.pos.framework.constants.CommonErrorCode;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.framework.utils.ListUtils;
import com.neusoft.smart.pos.order.constants.TradeErrorCode;
import com.neusoft.smart.pos.trade.persistent.domain.BillSettlementRule;
import com.neusoft.smart.pos.trade.persistent.domain.BillSettlementRuleDetail;
import com.neusoft.smart.pos.trade.repository.BillSettlementRuleDetailRepository;
import com.neusoft.smart.pos.trade.repository.BillSettlementRuleRepository;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
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 java.util.List;

@Service("billSettlementRuleService")
public class BillSettlementRuleService {

	private final static Logger logger = LoggerFactory.getLogger(BillSettlementRuleService.class);
	
	@Autowired
	private BillSettlementRuleRepository billSettlementRuleRepository;
	
	@Autowired
	private BillSettlementRuleDetailRepository billSettlementRuleDetailRepository;
	
	/**
	 * 商户结算周期信息查询方法
	 * @return
	 */
	public GetBillSettlementRuleResponse queryBillSettlementRule(Integer orgId,Integer ruleId) {
		GetBillSettlementRuleResponse billSettlementRuleResponse = new GetBillSettlementRuleResponse();
		BillSettlementRule billSettlementRule = new BillSettlementRule();
		//1.查询集团
		if(ruleId == 0){
			billSettlementRule = billSettlementRuleRepository.queryOrgDefaultBillRule(orgId);
		}else{
			billSettlementRule = billSettlementRuleRepository.queryBillSettlementRuleById(ruleId);
		}
		if(null != billSettlementRule){
			BeanUtils.copyProperties(billSettlementRule,billSettlementRuleResponse);
			List<BillSettlementRuleDetail> billSettlementRuleDetailList =  billSettlementRuleDetailRepository.queryBillSettlementRuleDetails(billSettlementRuleResponse.getId());
			if(null != billSettlementRuleDetailList){
				List<GetBillSettlementRuleDetailResponse> ruleDetails = ListUtils.convertListToList(billSettlementRuleDetailList,GetBillSettlementRuleDetailResponse.class);
				billSettlementRuleResponse.setRuleDetails(ruleDetails);
			}
		}
		return billSettlementRuleResponse;
	}
	
	/**
	 * 商户结算规则删除方法
	 * @return
	 */
	public DeleteBillSettlementRuleResponse deleteBillSettlementRule(DeleteBillSettlementRuleRequest billSettlementRuleRequest) {
		Integer orgId = RequestHeaderUtil.getOrgId();
		if(null == orgId){
			throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
		}
		Boolean bool = billSettlementRuleRepository.deleteBillSettlementRule(orgId,billSettlementRuleRequest.getRuleId());
		if(bool){
			billSettlementRuleDetailRepository.deleteBillSettlementRule(billSettlementRuleRequest.getRuleId());
			return new DeleteBillSettlementRuleResponse(bool);
		}else{
			throw new BusinessCommonException(TradeErrorCode.PAY_CHANNEL_DELETE_ERROR);
		}
	}
	
	/**
	 * 商户结算规则信息批量保存方法
	 * @return
	 */
	public PostBillSettlementRuleResponse saveBillSettlementRule(Integer orgId, PostBillRuleRequest billRuleRequest) {
		PostBillSettlementRuleResponse billRulesResponse = new PostBillSettlementRuleResponse();
		
		List<Integer> shopIds = billRuleRequest.getShopIds();
		
		//1.遍历批量保存规则数据信息
		BillSettlementRule billSettlementRule = new BillSettlementRule();
		BeanUtils.copyProperties(billRuleRequest,billSettlementRule);
		billSettlementRule.setOrgId(orgId);
		shopIds.forEach(shopId->{
			billSettlementRule.setShopId(shopId);
			//2.保存规则主表信息
			billSettlementRuleRepository.saveBillSettlementRule(billSettlementRule);
			//3.保存规则主体表信息
			List<PostBillRuleDetailRequest> billRuleDetailList = billRuleRequest.getBillRuleDetailList();
			billRuleDetailList.forEach(billRuleDetail ->{
				BillSettlementRuleDetail billSettlementRuleDetail = new BillSettlementRuleDetail();
				BeanUtils.copyProperties(billRuleDetail,billSettlementRuleDetail);
				billSettlementRuleDetail.setBillRuleId(billSettlementRule.getId());
				billSettlementRuleDetailRepository.saveBillSettlemenDetailRule(billSettlementRuleDetail);
			});
		});
		

		billRulesResponse.setIsSuccess(ApplicationConstants.TRUE);
		return billRulesResponse;
	}

	/**
	 * 商户结算规则信息更新方法
	 * @return
	 */
	public PutBillSettlementRuleResponse updateBillSettlementRule(Integer orgId, PutBillRuleRequest billRuleRequest) {
		PutBillSettlementRuleResponse billRulesResponse = new PutBillSettlementRuleResponse();
		
		//1.更新规则数据信息
		BillSettlementRule billSettlementRule = new BillSettlementRule();
		BeanUtils.copyProperties(billRuleRequest,billSettlementRule);
		billSettlementRule.setOrgId(orgId);
		//2.保存规则主表信息
		boolean bool = billSettlementRuleRepository.updateBillSettlemenDetailRule(billRuleRequest.getRuleId(),billSettlementRule);
		//3.保存规则主体表信息，首先删除原有规则主体信息
		if(bool){
			billSettlementRuleDetailRepository.deleteBillSettlementRule(billRuleRequest.getRuleId());
			List<PutBillRuleDetailRequest> billRuleDetailList = billRuleRequest.getBillRuleDetailList();
			billRuleDetailList.forEach(billRuleDetail ->{
				BillSettlementRuleDetail billSettlementRuleDetail = new BillSettlementRuleDetail();
				BeanUtils.copyProperties(billRuleDetail,billSettlementRuleDetail);
				billSettlementRuleDetail.setBillRuleId(billRuleRequest.getRuleId());
				billSettlementRuleDetailRepository.saveBillSettlemenDetailRule(billSettlementRuleDetail);
			});
		}
		billRulesResponse.setIsSuccess(bool);
		return billRulesResponse;
	}
	
	/**
	 * 商户结算规则信息更新方法
	 * @return
	 */
	public PutBillSettlementRuleSortResponse updateBillSettlementRuleSort(Integer orgId, PutBillSettlementRuleSortRequest billRuleSortRequest) {
		PutBillSettlementRuleSortResponse billRuleSortResponse = new PutBillSettlementRuleSortResponse();
		
		//1.更新规则数据排序信息
		billRuleSortRequest.getBillRuleSortDetailList().forEach(billRuleSortDetail->{
			billSettlementRuleRepository.updateBillSettlemenDetailRule(orgId,billRuleSortDetail.getRuleId(),billRuleSortDetail.getSort());
		});
		
		billRuleSortResponse.setIsSuccess(ApplicationConstants.TRUE);
		return billRuleSortResponse;
	}
	
	/**
	 * 商户结算周期信息查询方法
	 * @return
	 */
	public GetBillSettlementRulesResponse queryBillSettlementRules(Integer orgId) {
		GetBillSettlementRulesResponse billSettlementRulesResponse = new GetBillSettlementRulesResponse();
		
		//1.根据集团查询规则列表
		List<BillSettlementRule> billSettlementRuleList = billSettlementRuleRepository.queryBillSettlementRules(orgId);
		List<GetBillSettlementRuleResponse> billSettlementRules = ListUtils.convertListToList(billSettlementRuleList,GetBillSettlementRuleResponse.class);
		
		//2.根据规则列表查询规则明细信息
		billSettlementRules.forEach(billSettlementRule->{
			List<BillSettlementRuleDetail> billSettlementRuleDetailList =  billSettlementRuleDetailRepository.queryBillSettlementRuleDetails(billSettlementRule.getId());
			if(null != billSettlementRuleDetailList){
				List<GetBillSettlementRuleDetailResponse> ruleDetails = ListUtils.convertListToList(billSettlementRuleDetailList,GetBillSettlementRuleDetailResponse.class);
				billSettlementRule.setRuleDetails(ruleDetails);
			}
		});
		billSettlementRulesResponse.setBillSettlementRules(billSettlementRules);
		return billSettlementRulesResponse;
	}
}
