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

import com.neusoft.smart.pos.framework.dto.ResponseData;
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.service.RecordReportService;
import com.neusoft.smart.pos.report.service.RecordReportToolService;
import com.neusoft.smart.pos.utils.PosDeviceRedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Api(value = "记录查询报表", description = "记录查询报表接口")
@RestController
@Validated
@RequestMapping(path = "/report")
public class RecordReportGetMcodeController {

    @Resource
    private RecordReportService recordReportService;
    @Resource
    private RecordReportToolService recordReportToolService;


    @Transactional
    @ApiOperation(value = "充值记录查询接口", notes = "充值记录查询接口")
    @GetMapping("/getRechargeRecord")
//    public ResponseData<?> rechargeRecord(@RequestBody RechargeRecordRequest rechargeRecordRequest) {
//
//        return new ResponseData<>(recordReportService.queryRechargeRecord(rechargeRecordRequest));
//    }
    public ResponseData<RechargeRecordGetMcodeResponse> getRechargeRecord(@RequestParam(value="mcode",defaultValue = "") String mcode
                                                                  ,@RequestParam(value="sign",defaultValue = "") String sign
            ,@RequestParam(value="orgName",defaultValue = "") String orgName
           ,@RequestParam(value="path",defaultValue = "") String path
            , @RequestParam(value="serialNo",defaultValue = "") String serialNo
            , @RequestParam(value="startTime",defaultValue = "") String startTime
            , @RequestParam(value="employeeNumber",defaultValue = "") String employeeNumber
            , @RequestParam(value="endTime",defaultValue = "") String endTime
            , @RequestParam(value="offset",defaultValue = "") Integer offset
            , @RequestParam(value="limit",defaultValue = "") Integer limit
            , @RequestParam(value="isPage",defaultValue = "") Boolean isPage) {


        RechargeRecordGetMcodeRequest getMcodRechargeRecordRequest = new RechargeRecordGetMcodeRequest();
        getMcodRechargeRecordRequest.setMcode(mcode);
        getMcodRechargeRecordRequest.setSign(sign);
        getMcodRechargeRecordRequest.setOrgName(orgName);
        getMcodRechargeRecordRequest.setPath(path);
        getMcodRechargeRecordRequest.setEmployeeNumber(employeeNumber);
        getMcodRechargeRecordRequest.setSerialNo(serialNo);
        getMcodRechargeRecordRequest.setStartTime(startTime);
        getMcodRechargeRecordRequest.setEndTime(endTime);
        getMcodRechargeRecordRequest.setOffset(offset);
        getMcodRechargeRecordRequest.setLimit(limit);
        getMcodRechargeRecordRequest.setIsPage(isPage);
       RechargeRecordRequest rechargeRecordRequest = new RechargeRecordRequest();
        if(recordReportToolService.checkMcodeAndSign(getMcodRechargeRecordRequest,mcode)) {

            val deviceInfo = PosDeviceRedisUtils.getDeviceInfoBySerialNo(getMcodRechargeRecordRequest.getMcode());
            if (null == deviceInfo) {
                throw new BusinessCommonException(TradeErrorCode.DEVICE_ORG_EMPTY);
            }

            rechargeRecordRequest.setOrgGroupId(recordReportToolService.getParentOrgGroupIdByOrgId(deviceInfo.getOrganizationId()));
            rechargeRecordRequest.setOrgName(orgName);
            rechargeRecordRequest.setMemberCardNo(employeeNumber);
           rechargeRecordRequest.setChargeOrg(path);
            rechargeRecordRequest.setSerialNo(serialNo);
            rechargeRecordRequest.setStartTime(startTime);
            rechargeRecordRequest.setEndTime(endTime);
            rechargeRecordRequest.setOffset(offset);
            rechargeRecordRequest.setLimit(limit);
            rechargeRecordRequest.setIsPage(isPage);
        }
        RechargeRecordResponse reRecode = recordReportService.queryRechargeRecord(rechargeRecordRequest);
 //System.out.println(reRecode);

        RechargeRecordGetMcodeResponse rechargeRecordGetMcodeResponse = new RechargeRecordGetMcodeResponse();
        List<RechargeRecordGetMcodeResponse.MemberGetRecharge> memberGetRechargeList  = new ArrayList<RechargeRecordGetMcodeResponse.MemberGetRecharge>();
        Iterator<RechargeRecordResponse.MemberRecharge> itRe = reRecode.getRow().iterator();
        while (itRe.hasNext()) {
            RechargeRecordGetMcodeResponse.MemberGetRecharge memberGetRecharge = new RechargeRecordGetMcodeResponse.MemberGetRecharge();
            RechargeRecordResponse.MemberRecharge memberRecharge =itRe.next();
            memberGetRecharge.setOrderNumber(memberRecharge.getOrderNumber());
            memberGetRecharge.setOrderAmount(memberRecharge.getRealPayAmount());
            memberGetRecharge.setBalance(memberRecharge.getBalance());
            memberGetRecharge.setHidNumber(memberRecharge.getCardNum());
            memberGetRecharge.setPath(memberRecharge.getChargeOrg());
            memberGetRecharge.setEmployeeName(memberRecharge.getMemberName());
            memberGetRecharge.setPayTime(memberRecharge.getCreatedAt());
            memberGetRecharge.setOrgName(memberRecharge.getOrgName());
            memberGetRecharge.setSerialNo(memberRecharge.getSerialNo());
            memberGetRecharge.setPre(memberRecharge.getBalanceBefore());
            memberGetRecharge.setOperator(memberRecharge.getOperator());
            memberGetRecharge.setEmployeeNumber(memberRecharge.getMemberCardNo());
            memberGetRechargeList.add(memberGetRecharge);
        }
        System.out.println(memberGetRechargeList);
        rechargeRecordGetMcodeResponse.setRow(memberGetRechargeList);
        rechargeRecordGetMcodeResponse.setTotal(reRecode.getTotal());
        return new ResponseData(rechargeRecordGetMcodeResponse);
    }

    @Transactional
    @ApiOperation(value = "退款记录查询接口", notes = "退款记录查询接口")
    @GetMapping("/getRefundRecord")
    public ResponseData<RefundRecordResponse> getRefundRecord(@RequestParam(value="mcode",defaultValue = "") String mcode
            ,@RequestParam(value="sign",defaultValue = "") String sign
            , @RequestParam(value="path",defaultValue = "") String path
            , @RequestParam(value="orgName",defaultValue = "") String orgName
            , @RequestParam(value="startTime",defaultValue = "") String startTime
            , @RequestParam(value="endTime",defaultValue = "") String endTime
            , @RequestParam(value="serialNo",defaultValue = "") String serialNo
            , @RequestParam(value="refundTime",defaultValue = "") String refundTime
            , @RequestParam(value="offset",defaultValue = "") Integer offset
            , @RequestParam(value="limit",defaultValue = "") Integer limit
            , @RequestParam(value="isPage",defaultValue = "") Boolean isPage) {
        RefundRecordGetMcodeRequest refundRecordGetMcodeRequest = new RefundRecordGetMcodeRequest();
        refundRecordGetMcodeRequest.setMcode(mcode);
        refundRecordGetMcodeRequest.setSign(sign);
        refundRecordGetMcodeRequest.setOrgName(orgName);
        refundRecordGetMcodeRequest.setSerialNo(serialNo);
        refundRecordGetMcodeRequest.setPath(path);
        refundRecordGetMcodeRequest.setStartTime(startTime);
        refundRecordGetMcodeRequest.setEndTime(endTime);
        refundRecordGetMcodeRequest.setIsPage(isPage);
        refundRecordGetMcodeRequest.setLimit(limit);
        refundRecordGetMcodeRequest.setOffset(offset);


        RefundRecordRequest refundRecordRequest = new RefundRecordRequest();
        if(recordReportToolService.checkMcodeAndSign(refundRecordGetMcodeRequest,mcode)) {
            val deviceInfo = PosDeviceRedisUtils.getDeviceInfoBySerialNo(refundRecordGetMcodeRequest.getMcode());
            if (null == deviceInfo) {
                throw new BusinessCommonException(TradeErrorCode.DEVICE_ORG_EMPTY);
            }

            refundRecordRequest.setOrgGroupId(recordReportToolService.getParentOrgGroupIdByOrgId(deviceInfo.getOrganizationId()));
            refundRecordRequest.setOrgName(orgName);
            refundRecordRequest.setSerialNo(serialNo);

            refundRecordRequest.setPath(path);
            refundRecordRequest.setStartTime(startTime);
            refundRecordRequest.setEndTime(endTime);
            refundRecordRequest.setIsPage(isPage);
            refundRecordRequest.setLimit(limit);
            refundRecordRequest.setOffset(offset);
        }
        return new ResponseData(recordReportService.queryRefundRecord(refundRecordRequest));
    }

    @Transactional
    @ApiOperation(value = "微信、支付宝消费明细查询接口", notes = "微信、支付宝消费明细查询接口")
    @GetMapping("/getConsumptionDetails")
    public ResponseData<ConsumptionDetailsResponse> getConsumptionDetails(@RequestParam(value="mcode",defaultValue = "") String mcode
            ,@RequestParam(value="sign",defaultValue = "") String sign
            ,@RequestParam(value="startTime",defaultValue = "") String startTime
           ,@RequestParam(value="endTime",defaultValue = "") String endTime
            , @RequestParam(value="serialNo",defaultValue = "") String serialNo
            , @RequestParam(value="orgName",defaultValue = "") String orgName
            , @RequestParam(value="mealTimes",defaultValue = "") String mealTimes
            , @RequestParam(value="payType",defaultValue = "") String payType
            , @RequestParam(value="offset",defaultValue = "") Integer offset
            , @RequestParam(value="limit",defaultValue = "") Integer limit
            , @RequestParam(value="isPage",defaultValue = "") Boolean isPage) {

        ConsumptionDetailsGetMcodeRequest consumptionDetailsGetMcodeRequestRequest = new  ConsumptionDetailsGetMcodeRequest();
        consumptionDetailsGetMcodeRequestRequest.setMcode(mcode);
        consumptionDetailsGetMcodeRequestRequest.setSign(sign);
        consumptionDetailsGetMcodeRequestRequest.setOrgName(orgName);
        consumptionDetailsGetMcodeRequestRequest.setSerialNo(serialNo);
        consumptionDetailsGetMcodeRequestRequest.setMealTimes(mealTimes);
        consumptionDetailsGetMcodeRequestRequest.setStartTime(startTime);
        consumptionDetailsGetMcodeRequestRequest.setEndTime(endTime);
        consumptionDetailsGetMcodeRequestRequest.setPayType(payType);
        consumptionDetailsGetMcodeRequestRequest.setIsPage(isPage);
        consumptionDetailsGetMcodeRequestRequest.setLimit(limit);
        consumptionDetailsGetMcodeRequestRequest.setOffset(offset);




        ConsumptionDetailsRequest consumptionDetailsRequest = new ConsumptionDetailsRequest();
        if(recordReportToolService.checkMcodeAndSign(consumptionDetailsGetMcodeRequestRequest,mcode)) {
            val deviceInfo = PosDeviceRedisUtils.getDeviceInfoBySerialNo(consumptionDetailsGetMcodeRequestRequest.getMcode());
            if (null == deviceInfo) {
                throw new BusinessCommonException(TradeErrorCode.DEVICE_ORG_EMPTY);
            }

            consumptionDetailsRequest.setOrgGroupId(recordReportToolService.getParentOrgGroupIdByOrgId(deviceInfo.getOrganizationId()));
            consumptionDetailsRequest.setOrgName(orgName);
            consumptionDetailsRequest.setSerialNo(serialNo);

            consumptionDetailsRequest.setMealTimes(mealTimes);
            consumptionDetailsRequest.setStartTime(startTime);
            consumptionDetailsRequest.setEndTime(endTime);
            consumptionDetailsRequest.setPayType(payType);
            consumptionDetailsRequest.setIsPage(isPage);
            consumptionDetailsRequest.setLimit(limit);
            consumptionDetailsRequest.setOffset(offset);
        }
        return new ResponseData(recordReportService.queryConsumptionDetails(consumptionDetailsRequest));
    }

    @Transactional
    @ApiOperation(value = "营业组餐收入汇总查询接口", notes = "营业组餐收入汇总查询接口")
    @GetMapping("/getBusinessGroup")
    public ResponseData getBusinessGroup(@RequestParam(value="mcode",defaultValue = "") String mcode
            ,@RequestParam(value="sign",defaultValue = "") String sign
            ,@RequestParam(value="startTime",defaultValue = "") String startTime
            ,@RequestParam(value="endTime",defaultValue = "") String endTime
            , @RequestParam(value="orgName",defaultValue = "") String orgName
            ,@RequestParam(value="offset",defaultValue = "") Integer offset
            , @RequestParam(value="limit",defaultValue = "") Integer limit
            , @RequestParam(value="isPage",defaultValue = "") Boolean isPage) {
//        System.out.println("sssssss!");
        BusinessGroupGetMcodeRequest businessGroupGetMcodeRequest = new BusinessGroupGetMcodeRequest();
        businessGroupGetMcodeRequest.setMcode(mcode);
        businessGroupGetMcodeRequest.setSign(sign);
        businessGroupGetMcodeRequest.setOrgName(orgName);
        businessGroupGetMcodeRequest.setStartTime(startTime);
        businessGroupGetMcodeRequest.setEndTime(endTime);
        businessGroupGetMcodeRequest.setIsPage(isPage);
        businessGroupGetMcodeRequest.setLimit(limit);
        businessGroupGetMcodeRequest.setOffset(offset);

        BusinessGroupRequest businessGroupRequest = new BusinessGroupRequest();
        if(recordReportToolService.checkMcodeAndSign(businessGroupGetMcodeRequest,mcode)) {
            val deviceInfo = PosDeviceRedisUtils.getDeviceInfoBySerialNo(businessGroupGetMcodeRequest.getMcode());
            if (null == deviceInfo) {
                throw new BusinessCommonException(TradeErrorCode.DEVICE_ORG_EMPTY);
            }

            businessGroupRequest.setOrgGroupId(recordReportToolService.getParentOrgGroupIdByOrgId(deviceInfo.getOrganizationId()));
            businessGroupRequest.setOrgName(orgName);
            businessGroupRequest.setStartTime(startTime);
            businessGroupRequest.setEndTime(endTime);
            businessGroupRequest.setIsPage(isPage);
            businessGroupRequest.setLimit(limit);
            businessGroupRequest.setOffset(offset);
        }
       // return new ResponseData(recordReportService.queryBusinessGroup(businessGroupRequest));
        return new ResponseData(recordReportService.queryBusinessGroup(businessGroupRequest));
    }

    @Transactional
    @ApiOperation(value = "个人消费明细统计接口", notes = "个人消费明细统计接口")
    @GetMapping("/getPersonalonsumption")
    public ResponseData getPersonalonsumption(@RequestParam(value="mcode",defaultValue = "") String mcode
            ,@RequestParam(value="sign",defaultValue = "") String sign
            ,@RequestParam(value="startTime",defaultValue = "") String startTime
            ,@RequestParam(value="endTime",defaultValue = "") String endTime
            , @RequestParam(value="employeeNumber",defaultValue = "") String employeeNumber
            , @RequestParam(value="orgName",defaultValue = "") String orgName
            , @RequestParam(value="offset",defaultValue = "") Integer offset
            , @RequestParam(value="limit",defaultValue = "") Integer limit
            , @RequestParam(value="isPage",defaultValue = "") Boolean isPage) {
        PersonaConsumptionGetMcodeRequest personaConsumptionGetMcodeRequest = new PersonaConsumptionGetMcodeRequest();
        personaConsumptionGetMcodeRequest.setMcode(mcode);
        personaConsumptionGetMcodeRequest.setSign(sign);
        personaConsumptionGetMcodeRequest.setEmployeeNumber(employeeNumber);
        personaConsumptionGetMcodeRequest.setOrgName(orgName);
        personaConsumptionGetMcodeRequest.setStartTime(startTime);
        personaConsumptionGetMcodeRequest.setEndTime(endTime);
        personaConsumptionGetMcodeRequest.setIsPage(isPage);
        personaConsumptionGetMcodeRequest.setLimit(limit);
        personaConsumptionGetMcodeRequest.setOffset(offset);

        PersonaConsumptionRequest personaConsumptionRequest1 = new PersonaConsumptionRequest();
        if(recordReportToolService.checkMcodeAndSign(personaConsumptionGetMcodeRequest,mcode)) {
            val deviceInfo = PosDeviceRedisUtils.getDeviceInfoBySerialNo(personaConsumptionGetMcodeRequest.getMcode());
            if (null == deviceInfo) {
                throw new BusinessCommonException(TradeErrorCode.DEVICE_ORG_EMPTY);
            }
            personaConsumptionRequest1.setOrgGroupId(recordReportToolService.getParentOrgGroupIdByOrgId(deviceInfo.getOrganizationId()));
            personaConsumptionRequest1.setOrgName(orgName);
            personaConsumptionRequest1.setEmployeeNumber(employeeNumber);
            personaConsumptionRequest1.setStartTime(startTime);
            personaConsumptionRequest1.setEndTime(endTime);
            personaConsumptionRequest1.setIsPage(isPage);
            personaConsumptionRequest1.setLimit(limit);
            personaConsumptionRequest1.setOffset(offset);
        }
        return new ResponseData(recordReportService.queryPersonaConsumption(personaConsumptionRequest1));
    }

}
