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


import com.neusoft.smart.pos.framework.constants.RoleConstant;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.framework.utils.MapBeanUtils;
import com.neusoft.smart.pos.framework.utils.SignUtils;
import com.neusoft.smart.pos.order.constants.TradeErrorCode;
import com.neusoft.smart.pos.report.constants.ReportErrorCode;
import com.neusoft.smart.pos.report.persistent.domain.OrganizationGroup;
import com.neusoft.smart.pos.trade.persistent.domain.TradeMchInfo;
import com.neusoft.smart.pos.trade.repository.TradeMchInfoRepository;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class RecordReportToolService {
    @Resource
    private ManageableGroupService manageableGroupService;

    @Autowired
    private TradeMchInfoRepository tradeMchInfoRepository;

    public    Integer getOrgGroupId(){
/**
 *
 */
//Integer orgId=208;
        //测试结束
        Integer orgId = RequestHeaderUtil.getOrgId();
        System.out.println("orgId-----------------"+ orgId);
        String role = RequestHeaderUtil.getRole();

        if (!role.equals(RoleConstant.ROLE_ADMIN) && !role.equals(RoleConstant.ROLE_USER)) {
            throw new BusinessCommonException(ReportErrorCode.INSUFFICIENT_AUTHORITY_ERROR);
        }
        OrganizationGroup organizationGroup = manageableGroupService.getManageableGroup(orgId);
        if (null == organizationGroup) {
            throw new BusinessCommonException(ReportErrorCode.QUERY_ORGANIZATION_GROUP_ERROR);
        }

        return organizationGroup.getId();

    }

    public boolean checkMcodeAndSign(Object object,String mcode){

        TradeMchInfo tradeMchInfo = tradeMchInfoRepository.queryTradeMchInfo(mcode);
        if(null == tradeMchInfo){
            throw new BusinessCommonException(TradeErrorCode.TRADE_MCH_INFO_NOT_EXIST);
        }

        //3.校验签名信息
        if(!SignUtils.isSignatureValid(MapBeanUtils.beanToMap(object),tradeMchInfo.getSecretKey())){
            throw new BusinessCommonException(TradeErrorCode.TRADE_MCH_SIGN_VALIDATE_ERROR);
        }
    return true;
    }

    public boolean checkSign(Object bean,String secretKey){
        //3.校验签名信息
        if(!SignUtils.isSignatureValid(MapBeanUtils.beanToMap(bean),secretKey)){
            throw new BusinessCommonException(TradeErrorCode.TRADE_MCH_SIGN_VALIDATE_ERROR);
        }
        return true;
    }

    public    Integer getParentOrgGroupIdByOrgId(Integer orgId){

        System.out.println("orgId================="+ orgId);
        OrganizationGroup organizationGroup = manageableGroupService.getManageableGroup(orgId);
        if (null == organizationGroup) {
            throw new BusinessCommonException(ReportErrorCode.QUERY_ORGANIZATION_GROUP_ERROR);
        }

        return organizationGroup.getId();

    }
}
