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




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.report.dto.*;
import com.neusoft.smart.pos.report.dto.RechargeRecordResponse.MemberRecharge;
import com.neusoft.smart.pos.report.dto.RefundRecordResponse.MemberRefund;
import com.neusoft.smart.pos.report.dto.ConsumptionDetailsResponse.MemberConsumption;
import com.neusoft.smart.pos.report.dto.BusinessGroupResponse.MemberBusinessGroup;
import com.neusoft.smart.pos.report.dto.PersonaConsumptionResponse.MemberPersona;
import com.neusoft.smart.pos.report.persistent.domain.*;
import com.neusoft.smart.pos.report.persistent.repository.*;
import com.neusoft.smart.pos.report.persistent.domain.RechargeRecordExample;


import lombok.val;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

import java.util.List;

@Service
public class RecordReportService {


    @Resource
    private RechargeRecordRepository rechargeRecordRepository;

    @Resource
    private RefundRecordRepository refundRecordRepository;
    @Resource
    private ConsumptionDetailsRepository consumptionDetailsRepository;

    @Resource
    private BusinessGroupRepository businessGroupRepository;

    @Resource
    private PersonaConsumptionRepository personaConsumptionRepository;




    /**
     * 充值记录查获
     * @param rechargeRecordRequest 查询请求
     * @return 充值记录查获
     */
    public RechargeRecordResponse queryRechargeRecord (RechargeRecordRequest rechargeRecordRequest){
        //1.校验参数信息
//        validateQueryMemberOrdersParams(rechargeRecordRequest.getMcode(),rechargeRecordRequest.getSign());
        //2.查询商户号信息
//        TradeMchInfo tradeMchInfo = tradeMchInfoRepository.queryTradeMchInfo(rechargeRecordRequest.getMcode());
//        if(null == tradeMchInfo){
//            throw new BusinessCommonException(TradeErrorCode.TRADE_MCH_INFO_NOT_EXIST);
//        }
//
//        //3.校验签名信息
//        if(!SignUtils.isSignatureValid(MapBeanUtils.beanToMap(rechargeRecordRequest),tradeMchInfo.getSecretKey())){
//            throw new BusinessCommonException(TradeErrorCode.TRADE_MCH_SIGN_VALIDATE_ERROR);
//        }
        return queryMemberOrdersHandle(rechargeRecordRequest);

    }


    /**
     * 退款记录查询
     * @param refundRecordRequest 查询请求
     * @return 充值记录查获
     */

    public RefundRecordResponse queryRefundRecord (RefundRecordRequest refundRecordRequest){
        //1.校验参数信息
//        validateQueryMemberOrdersParams(refundRecordRequest.getMcode(),refundRecordRequest.getSign());
        //2.查询商户号信息
//        TradeMchInfo tradeMchInfo = tradeMchInfoRepository.queryTradeMchInfo(refundRecordRequest.getMcode());
//        if(null == tradeMchInfo){
//            throw new BusinessCommonException(TradeErrorCode.TRADE_MCH_INFO_NOT_EXIST);
//        }
//
//        //3.校验签名信息
//        if(!SignUtils.isSignatureValid(MapBeanUtils.beanToMap(refundRecordRequest),tradeMchInfo.getSecretKey())){
//            throw new BusinessCommonException(TradeErrorCode.TRADE_MCH_SIGN_VALIDATE_ERROR);
//        }
//      return refundRecordRepository.getList(refundRecordRequest);

        //1.整理数据查询条件
        RefundRecordExample refundRecordExample =  removeRefundRecord(refundRecordRequest);
        //2.查询订单总记录条数
        val orderCount = refundRecordRepository.queryMemberOrderDetailsCount(refundRecordExample);
        System.out.println("55555"+orderCount);
        //3.查询员工订单列表数据
        val ccc =refundRecordRepository.getList(refundRecordExample);
        List<MemberRefund> memberOrderList = ListUtils.convertListToList(ccc, MemberRefund.class);
        RefundRecordResponse refundRecordResponse = new RefundRecordResponse();
        refundRecordResponse.setTotal(orderCount);
        refundRecordResponse.setRow(memberOrderList);
        return refundRecordResponse;
    }

    /**
     * 微信、支付宝消费明细查询
     * @param consumptionDetailsRequest 查询请求
     * @return 微信、支付宝消费明细
     */

    public  ConsumptionDetailsResponse queryConsumptionDetails (ConsumptionDetailsRequest consumptionDetailsRequest){
        //1.校验参数信息
//        validateQueryMemberOrdersParams(consumptionDetailsRequest.getMcode(),consumptionDetailsRequest.getSign());
        //2.查询商户号信息
//        TradeMchInfo tradeMchInfo = tradeMchInfoRepository.queryTradeMchInfo(consumptionDetailsRequest.getMcode());
//        if(null == tradeMchInfo){
//            throw new BusinessCommonException(TradeErrorCode.TRADE_MCH_INFO_NOT_EXIST);
//        }
//
//        //3.校验签名信息
//        if(!SignUtils.isSignatureValid(MapBeanUtils.beanToMap(consumptionDetailsRequest),tradeMchInfo.getSecretKey())){
//            throw new BusinessCommonException(TradeErrorCode.TRADE_MCH_SIGN_VALIDATE_ERROR);
//        }


        //1.整理数据查询条件
        ConsumptionDetailsExample consumptionDetailsExample =  removeConsumptionDetails(consumptionDetailsRequest);
        //2.查询订单总记录条数
        val orderCount = consumptionDetailsRepository.queryMemberOrderDetailsCount(consumptionDetailsExample);
        System.out.println("55555"+orderCount);
        //3.查询员工订单列表数据
        val ccc =consumptionDetailsRepository.getList(consumptionDetailsExample);
        List<MemberConsumption> memberOrderList = ListUtils.convertListToList(ccc, MemberConsumption.class);
        ConsumptionDetailsResponse consumptionDetailsResponse = new ConsumptionDetailsResponse();
        consumptionDetailsResponse.setTotal(orderCount);
        consumptionDetailsResponse.setRow(memberOrderList);
        return consumptionDetailsResponse;
    }

    /**
     * 营业组餐收入汇
     * @param businessGroupRequest 查询请求
     * @return 营业组餐收入汇
     */

    public BusinessGroupResponse queryBusinessGroup (BusinessGroupRequest businessGroupRequest){
        //1.校验参数信息
//        validateQueryMemberOrdersParams(businessGroupRequest.getMcode(),businessGroupRequest.getSign());
        //2.查询商户号信息
//        TradeMchInfo tradeMchInfo = tradeMchInfoRepository.queryTradeMchInfo(businessGroupRequest.getMcode());
//        if(null == tradeMchInfo){
//            throw new BusinessCommonException(TradeErrorCode.TRADE_MCH_INFO_NOT_EXIST);
//        }
//
//        //3.校验签名信息
//        if(!SignUtils.isSignatureValid(MapBeanUtils.beanToMap(businessGroupRequest),tradeMchInfo.getSecretKey())){
//            throw new BusinessCommonException(TradeErrorCode.TRADE_MCH_SIGN_VALIDATE_ERROR);
//        }


        //1.整理数据查询条件
        BusinessGroupExample businessGroupExample =  removeBusinessGroup(businessGroupRequest);
        //2.查询订单总记录条数
        val orderCount = businessGroupRepository.queryMemberOrderDetailsCount(businessGroupExample);

        //3.查询员工订单列表数据
        val ccc =businessGroupRepository.getList(businessGroupExample);
        List<MemberBusinessGroup> memberOrderList = ListUtils.convertListToList(ccc, MemberBusinessGroup.class);
        BusinessGroupResponse businessGroupResponse = new BusinessGroupResponse();
        businessGroupResponse.setTotal(orderCount);
        businessGroupResponse.setRow(memberOrderList);

        return  businessGroupResponse;
    }

    /**
     * 个人消费记录查询
     * @param personaConsumptionRequest 查询请求
     * @return 个人消费记录查询
     */

    public PersonaConsumptionResponse queryPersonaConsumption (PersonaConsumptionRequest personaConsumptionRequest){
        //1.校验参数信息
//        validateQueryMemberOrdersParams(personaConsumptionRequest.getMcode(),personaConsumptionRequest.getSign());
        //2.查询商户号信息
//        TradeMchInfo tradeMchInfo = tradeMchInfoRepository.queryTradeMchInfo(personaConsumptionRequest.getMcode());
//        if(null == tradeMchInfo){
//            throw new BusinessCommonException(TradeErrorCode.TRADE_MCH_INFO_NOT_EXIST);
//        }
//
//        //3.校验签名信息
//        if(!SignUtils.isSignatureValid(MapBeanUtils.beanToMap(personaConsumptionRequest),tradeMchInfo.getSecretKey())){
//            throw new BusinessCommonException(TradeErrorCode.TRADE_MCH_SIGN_VALIDATE_ERROR);
//        }


        //1.整理数据查询条件
        PersonaConsumptionExample personaConsumptionExample =  removePersonaConsumption(personaConsumptionRequest);
        //2.查询订单总记录条数

        val orderCount = personaConsumptionRepository.queryMemberOrderDetailsCount(personaConsumptionExample);

        //3.查询员工订单列表数据
        val ccc = personaConsumptionRepository.getList(personaConsumptionExample);
        List<MemberPersona> memberOrderList = ListUtils.convertListToList(ccc, MemberPersona.class);
        PersonaConsumptionResponse personaConsumptionResponse = new PersonaConsumptionResponse();
        personaConsumptionResponse.setTotal(orderCount);
        personaConsumptionResponse.setRow(memberOrderList);

        return  personaConsumptionResponse;
    }


    /**
     * 查询交易数据参数校验
     * @param  mcode，sign
     *
     */
    private void validateQueryMemberOrdersParams(String mcode,String sign) {
        //商户号不能为空
        if (StringUtils.isEmpty(mcode)) {
            throw new BusinessCommonException(TradeErrorCode.TRADE_MCH_MCODE_EMPTY);
        }
        //签名不能为空
        if (StringUtils.isEmpty(sign)) {
            throw new BusinessCommonException(TradeErrorCode.TRADE_MCH_SIGN_EMPTY);
        }

//        //分页数据校验
//        if (rechargeRecordRequest.getIsPage() == null) {
//            throw new BusinessCommonException(TradeErrorCode.TRADE_MCH_IS_PAGE);
//        }
//        if (rechargeRecordRequest.getIsPage()) {
//            if (rechargeRecordRequest.getOffset() == null) {
//                throw new BusinessCommonException(TradeErrorCode.TRADE_MCH_OFFSET);
//            }
//            if (rechargeRecordRequest.getLimit() == null) {
//                throw new BusinessCommonException(TradeErrorCode.TRADE_MCH_LIMIT);
//            }
//        }




    }

    /**
     * 查询充值记录查获
     * @param rechargeRecordRequest
     * @return
     */
    private RechargeRecordResponse queryMemberOrdersHandle (RechargeRecordRequest rechargeRecordRequest){
        //1.整理数据查询条件
        RechargeRecordExample recordExample =  removeRecharge(rechargeRecordRequest);
        //2.查询订单总记录条数
        val orderCount = rechargeRecordRepository.queryMemberOrderDetailsCount(recordExample);
System.out.println("55555"+orderCount);
        //3.查询员工订单列表数据
        val ccc =rechargeRecordRepository.getList(recordExample);
System.out.println("recordExample======"+recordExample);
System.out.println("return cccc+++++"+ccc);
//        PaginationExtendBean<MemberRecharge> paginationExtendBean = new PaginationExtendBean<>();
//        paginationExtendBean.setTotal(orderCount);
            List<MemberRecharge> memberOrderList = ListUtils.convertListToList(ccc, MemberRecharge.class);
//        paginationExtendBean.setRows(memberOrderList);

        RechargeRecordResponse rechargeRecordResponse = new RechargeRecordResponse();
        rechargeRecordResponse.setTotal(orderCount);
        rechargeRecordResponse.setRow(memberOrderList);


        return rechargeRecordResponse;
    }

    /**
     * 去掉验证信息removeRecharge
     * @param rechargeRecordRequest
     * @return
     */
    private  RechargeRecordExample removeRecharge (RechargeRecordRequest rechargeRecordRequest){
        RechargeRecordExample rechargeRecordExample = new RechargeRecordExample();
//       System.out.println("Date "+new Date(System.currentTimeMillis()));
        rechargeRecordExample.setOrgGroupId(rechargeRecordRequest.getOrgGroupId());
        rechargeRecordExample.setOrgName(rechargeRecordRequest.getOrgName());
        rechargeRecordExample.setSerialNo(rechargeRecordRequest.getSerialNo());
        rechargeRecordExample.setOrderNumber(rechargeRecordRequest.getOrderNumber());
        rechargeRecordExample.setMemberName(rechargeRecordRequest.getMemberName());

        rechargeRecordExample.setDeviceName(rechargeRecordRequest.getDeviceName());
        rechargeRecordExample.setCardNum(rechargeRecordRequest.getCardNum());
        rechargeRecordExample.setMemberCardNo(rechargeRecordRequest.getMemberCardNo());
        rechargeRecordExample.setPayTypeName(rechargeRecordRequest.getPayTypeName());
        rechargeRecordExample.setOrderStatus(rechargeRecordRequest.getOrderStatus());

        rechargeRecordExample.setChargeOrg(rechargeRecordRequest.getChargeOrg());

        rechargeRecordExample.setStartTime(rechargeRecordRequest.getStartTime());
        rechargeRecordExample.setEndTime(rechargeRecordRequest.getEndTime());
        rechargeRecordExample.setLimit(rechargeRecordRequest.getLimit());
        rechargeRecordExample.setOffset(rechargeRecordRequest.getOffset());
        return rechargeRecordExample;
    }

    /**
     * 去掉验证信息removeRefundRecord
     * @param refundRecordRequest
     * @return
     */
    private  RefundRecordExample removeRefundRecord (RefundRecordRequest refundRecordRequest){
        RefundRecordExample refundRecordExample = new RefundRecordExample();
        refundRecordExample.setOrgGroupId(refundRecordRequest.getOrgGroupId());
        refundRecordExample.setOrgName(refundRecordRequest.getOrgName());
        refundRecordExample.setSerialNo(refundRecordRequest.getSerialNo());
        refundRecordExample.setPath(refundRecordRequest.getPath());
        refundRecordExample.setStartTime(refundRecordRequest.getStartTime());
        refundRecordExample.setEndTime(refundRecordRequest.getEndTime());
        refundRecordExample.setLimit(refundRecordRequest.getLimit());
        refundRecordExample.setOffset(refundRecordRequest.getOffset());
        return refundRecordExample;
    }
    /**
     * removeBusinessGroup
     * @param businessGroupRequest
     * @return
     */
    private  BusinessGroupExample removeBusinessGroup (BusinessGroupRequest businessGroupRequest){
        BusinessGroupExample businessGroupExample = new BusinessGroupExample();
        businessGroupExample.setOrgGroupId(businessGroupRequest.getOrgGroupId());
        businessGroupExample.setOrgName(businessGroupRequest.getOrgName());
        businessGroupExample.setStartTime(businessGroupRequest.getStartTime());
        businessGroupExample.setEndTime(businessGroupRequest.getEndTime());
        businessGroupExample.setLimit(businessGroupRequest.getLimit());
        businessGroupExample.setOffset(businessGroupRequest.getOffset());
        return businessGroupExample;
    }

    /**
     * removePersonaConsumption
     * @param personaConsumptionRequest
     * @return
     */
    private  PersonaConsumptionExample removePersonaConsumption (PersonaConsumptionRequest personaConsumptionRequest){
        PersonaConsumptionExample personaConsumptionExample = new PersonaConsumptionExample();
        personaConsumptionExample.setOrgGroupId(personaConsumptionRequest.getOrgGroupId());
        personaConsumptionExample.setEmployeeNumber(personaConsumptionRequest.getEmployeeNumber());
        personaConsumptionExample.setOrgName(personaConsumptionRequest.getOrgName());
        personaConsumptionExample.setStartTime(personaConsumptionRequest.getStartTime());
        personaConsumptionExample.setEndTime(personaConsumptionRequest.getEndTime());
        personaConsumptionExample.setLimit(personaConsumptionRequest.getLimit());
        personaConsumptionExample.setOffset(personaConsumptionRequest.getOffset());
        return personaConsumptionExample;
    }

    /**
     * removeConsumptionDetails
     * @param refundRecordRequest
     * @return
     */
    private  ConsumptionDetailsExample removeConsumptionDetails (ConsumptionDetailsRequest refundRecordRequest){
        ConsumptionDetailsExample rechargeRecordExample = new ConsumptionDetailsExample();
        rechargeRecordExample.setOrgGroupId(refundRecordRequest.getOrgGroupId());
        rechargeRecordExample.setOrgName(refundRecordRequest.getOrgName());
        rechargeRecordExample.setSerialNo(refundRecordRequest.getSerialNo());
        rechargeRecordExample.setStartTime(refundRecordRequest.getStartTime());
        rechargeRecordExample.setEndTime(refundRecordRequest.getEndTime());
        rechargeRecordExample.setMealTimes(refundRecordRequest.getMealTimes());
        rechargeRecordExample.setPayType(refundRecordRequest.getPayType());
        rechargeRecordExample.setLimit(refundRecordRequest.getLimit());
        rechargeRecordExample.setOffset(refundRecordRequest.getOffset());
        return rechargeRecordExample;
    }
}
