USE ChongQing_Chunyi

SELECT * FROM TThirdPartyPayConfig 

SELECT * FROM TWebAppInfo 

UPDATE TThirdPartyPayConfig SET FWeChatPayAppId = 'wxaef11d3b67f8b8fe'
WHERE FWeChatPayMchId  = '1346308701'


SELECT FWeChatPayMchId,FWeChatPayAppId FROM TThirdPartyPayConfig WITH(NOLOCK) WHERE FCompId = 

SELECT * FROM TBillThirdPartyPayment WHERE FDeleted = 0 ORDER BY FFinishTime DESC

SELECT * FROM TMemberSaleBill WHERE FBillId = 5322569948262052768


SELECT * FROM TMemberCard WHERE FCardNo = '60079217'


SELECT * FROM TMemberCardAccount WHERE FCardId = 4633255279930003579


SELECT * FROM TMemberSaleCard WHERE FCardId = '5710100861998603546'

SELECT * FROM TMemberSaleBill WHERE FBillId = 5371868601703256122

SELECT * FROM TMemberCardAccount WHERE FAcctId = 14

SELECT  A.FCardId,A.FAcctId2,A.FAcctExpiredDate2,B.FExpiredDate
 FROM TMemberSaleCard AS A,TMemberCardAccount AS B
WHERE A.FAcctId2 != 0
AND FAcctExpiredDate IS NOT NULL
AND A.FCardId = B.FCardId
AND A.FAcctId2 = B.FAcctId
 

DECLARE @Old int
DECLARE @New INT

SELECT @Old = nULL,@New = 1

IF ISNULL( @Old,0) != @New 
BEGIN
    SELECT 1
END



DROP TABLE #Card
DROP TABLE #NewCard

CREATE TABLE #NewCard
(
    FCardId BIGINT,
    FAcctId BIGINT,
    FDate DATETIME,
    FCurrDate DATETIME
)

SELECT A.FCardId,A.FAcctId,A.FAcctExpiredDate AS FDate ,B.FBillTime
INTO #Card
 FROM TMemberSaleCard AS A,TMemberSaleBill AS B
WHERE A.FAcctExpiredDate IS NOT NULL
AND A.FBillId = B.FBillId
AND A.FAcctId != 3

UNION ALL 
SELECT A.FCardId,A.FAcctId2,A.FAcctExpiredDate2  AS FDate ,B.FBillTime
 FROM TMemberSaleCard  AS A,TMemberSaleBill AS B
WHERE A.FAcctExpiredDate2 IS NOT NULL
AND A.FAcctId2 != 0 
AND A.FAcctId2 != 3
AND A.FBillId = B.FBillId

DECLARE @Date DATETIME
DECLARE @CurrDate DATETIME

DECLARE List CURSOR FOR
SELECT FCardId,FAcctId
 FROM #Card
GROUP BY  FCardId,FAcctId
OPEN LIST

DECLARE @CardId BIGINT,@AcctId BIGINT

FETCH LIST INTO  @CardId,@AcctId
WHILE @@FETCH_STATUS = 0
BEGIN
    SELECT  TOP 1  @Date = FDate FROM #Card WHERE FCardId = @CardId AND FAcctId = @AcctId 
    ORDER BY FBillTime DESC
    SELECT @CurrDate = FExpiredDate FROM TMemberCardAccount  WHERE FCardId = @CardId AND FAcctId = @AcctId
    
    IF ISNULL(@Date,'9999-01-01') != ISNULL(@CurrDate,'9999-01-01')
    BEGIN
        INSERT #NewCard
        SELECT @CardId,@AcctId,@Date,@CurrDate
        PRINT @CardId 
        PRINT @AcctId
        PRINT @Date
        PRINT @CurrDate
        PRINT '-----------------------------'
    END
    
    FETCH LIST INTO  @CardId,@AcctId
END

CLOSE LIST
DEALLOCATE LIST

SELECT * FROM #NewCard

UPDATE A
   SET A.FExpiredDate = B.FDate
 FROM TMemberCardAccount AS A,#NewCard AS B
WHERE A.FCardId = B.FCardId
  AND A.FAcctId = B.FAcctid 