use taiyuan_yueerwan


select * from imp_member
select * from imp_Member_card




truncate table imp_member_trans_hist

SELECT * FROM imp_member where studentId = '4c0b0e85-5f08-48a3-b0ae-c05432d667fd'

alter table imp_member add idx int identity

delete a
 from imp_member as a,(

select studentId,max(idx) idx FROM imp_member
group by studentId) as b
where a.studentId = b.studentId
and a.idx != b.idx


select  *  from imp_member_trans_hist

select count(1) from (select studentId from imp_member_trans_hist group by studentId) as a

delete imp_member_trans_hist

detal: 
481c40de-5087-4f53-83ba-3c5c07ac5f31

drop table imp_member_list

select  *  from imp_member_card



--会员 
ALTER TABLE imp_member ADD mem_id BIGINT 
ALTER TABLE imp_member ADD mem_no VARCHAR(50)
ALTER TABLE imp_member ADD name VARCHAR(50)
ALTER TABLE imp_member ADD gender INT
ALTER TABLE imp_member ADD mobile VARCHAR(50)
ALTER TABLE imp_member ADD birthday_type VARCHAR(50)
--ALTER TABLE imp_member ADD birthday DATETIME
ALTER TABLE imp_member ADD comp_id BIGINT
ALTER TABLE imp_member ADD memo VARCHAR(MAX)
ALTER TABLE imp_member ADD custom1 VARCHAR(MAX)
ALTER TABLE imp_member ADD custom2 VARCHAR(MAX)
ALTER TABLE imp_member ADD custom3 VARCHAR(MAX)
ALTER TABLE imp_member ADD address VARCHAR(MAX)
ALTER TABLE imp_member ADD point DECIMAL(18,6)
ALTER TABLE imp_member ADD create_date datetime

 

ALTER TABLE imp_member_card ADD mem_id BIGINT 
ALTER TABLE imp_member_card ADD card_id BIGINT 
ALTER TABLE imp_member_card ADD card_type_name VARCHAR(100)
ALTER TABLE imp_member_card ADD card_No VARCHAR(50)
ALTER TABLE imp_member_card ADD balance DECIMAL(18,6)
ALTER TABLE imp_member_card ADD balance2 DECIMAL(18,6)
ALTER TABLE imp_member_card ADD card_type_id BIGINT
ALTER TABLE imp_member_card ADD comp_id BIGINT
ALTER TABLE imp_member_card ADD create_date datetime
ALTER TABLE imp_member_card ADD expire_date datetime
ALTER TABLE imp_member_card ADD pur_times decimal(18,6)
ALTER TABLE imp_member_card ADD remain_times decimal(18,6)
ALTER TABLE imp_member_card ADD card_state int
ALTER TABLE imp_member_card ADD memo varchar(100)

UPDATE imp_member SET mem_id = dbo.FNNewId() WHERE mem_id IS NULL
UPDATE imp_member SET name = StudentName  
UPDATE imp_member SET gender = CASE WHEN SEX = 0 THEN 1 ELSE 0 END
UPDATE imp_member SET comp_id = 1
UPDATE imp_member SET mobile = Tel
UPDATE imp_Member SET custom2 =  guardian
UPDATE imp_Member SET custom3 =  nickname
UPDATE imp_member SET address = Homeaddress
UPDATE imp_member SET create_date = createTime
UPDATE imp_member SET memo = remark
UPDATE imp_member SET birthday_type = 'G'
UPDATE imp_member SET mem_no = CardCode
UPDATE imp_member SET mem_no = mobile where mem_no = ''


UPDATE A 
   SET A.mem_no = A.mem_no + A.name
 FROM imp_member as a
WHERE a.mem_no IN(
  SELECT mem_no FROM imp_member
  GROUP BY mem_no 
  HAVING SUM(1) > 1)
  
   
   
INSERT INTO TMemberCardType(FId,FNo,FName,FValueType,FMaterial,FPrice,FCardFeeAmt,FAcctId,FDeposit,FAcctId2,FDeposit2,FAcctId3,FDeposit3,FTimes,FAlertBalance,FMinRchgAmt,FSalePointMode,FSalePointValue,FRchgPointMode,FRchgPointValue,FPaySmsFee,FTimeCard,FValidityPeriod,FValidityPeriodUnit,FExpiredDate,FDefaultMemo,FCreateTime,FAllowSale,FAllowRchg,FDeptId,FEnabled,FOrder)
SELECT FId =dbo.FNNewId(),  contractkindname,contractkindname as name,1,FMaterial = 2,0,FCardFeeAmt = 0,FAcctId = 3,FDeposit = 0,FAcctId2 = 0,FDeposit2 = 0 ,FAcctId3 = 0,FDeposit3 = 0,
FTimes = 0,FAlertBalance = 0,FMinRchgAmt = 0,FSalePointMode = 0,FSalePointValue = 0 ,FRchgPointMode = 0,FRchgPointValue = 0,FPaySmsFee = 0,FTimeCard = 0,FValidityPeriod = 100,FValidityPeriodUnit = 1,
FExpiredDate = NULL,FDefaultMemo = '',FCreateTime = GETDATE(),1,1,FDeptId = 0,FEnabled =1,FOrder = 1
FROM imp_member_card
WHERE deductmode = 1
GROUP BY contractkindname


UPDATE imp_member_card SET card_id = dbo.FNNewId() where card_id is null
UPDATE imp_member_card SET create_date = beginTime
UPDATE imp_member_card SET expire_date = endTime
UPDATE imp_member_card SET balance = yings_ye
UPDATE imp_member_card SET memo = remark
UPDATE imp_member_card SET comp_id = 1
 

UPDATE A 
   SET A.card_no = B.mem_no,
       a.mem_id = b.mem_Id
  FROM imp_member_card AS A,imp_Member AS B
 WHERE A.studentId = B.studentId

UPDATE A
   SET a.card_type_id = B.FId
  FROM imp_member_card AS A,TMemberCardType AS B
 WHERE A.contractkindname= B.FName
 
UPDATE imp_member_card SET card_state = 1
UPDATE imp_member_card SET card_state = 4 WHERE contractStatus != 1

 

WHERE deductmode = 1

WHERE StudentId = '01caf2dc-093c-4cc1-ae34-4dfdac2f8d9f'


SELECT contractkindname FROM imp_member_card
GROUP BY contractkindname

SELECT* FROM imp_member WHERE StudentId = '01caf2dc-093c-4cc1-ae34-4dfdac2f8d9f'

 INSERT INTO TMember(
    FMemId,
    FMemNo,
    FName,
    FBriefCode,
    FTypeId,
    FCompId,
    FCreateDate,
    FGender,
    FBirthdayType,
    FBirthday,FTelephone,FMobilePhone,FQQ,FWeChat,FIdCard,FEmail,FAddress,FProId,FCPW,FQPW,
    FChargeEmpId,FPoint,FRecvConsSMS,FRecvBulkSMS,FMemo,FCustom1,FCustom2,FCustom3,FCustom4,FCustom5)
SELECT mem_id AS FMemId,
     mem_no  AS FMemNo,
      name AS FName,
      dbo.FNGetFirstPinYin(name),
      0 AS FTypeId,
      comp_id AS FCompId,
        create_date  AS FCreateDate,
      gender AS FGender,
      birthday_type AS FBirthdayType,
      birthday AS FBirthday,
      '' AS FTelephone,
      ISNULL(mobile,'') AS FMobilePhone,
       '' AS FQQ,
       '' AS FWeChat,
       '' AS FIdCard,
       '' AS FEmail,
       '' AS FAddress,
       0 AS FProId,
       '' AS FCPW,
       '' AS FQPW,
       0 AS FChargeEmpId,
       0 AS FPoint,
       1 AS FRecvConsSMS,
       1 AS FRecvBulkSMS,
       memo AS FMemo,
       custom1 AS FCustom1,
       custom2 AS FCustom2,
        custom3 AS FCustom3,'' AS FCustom4,'' AS FCustom5
       from imp_member

  
  
 INSERT INTO TMemberCard(
        FCardId,
        FCardNo,
 
        FTypeId,
        FValueType,
        FCompId,
        FMemId,
        FState,
        FCreateDate,
        FExpiredDate,
        FTotalTimes,
        FUsedTimes,
        FSaleCompId,
        FSaleBillId,
        FMemo)
    SELECT 
        card_id AS FId,
        card_no AS FNo,
 
        card_type_id AS FTypeId,    
        (SELECT FValueType FROM TMemberCardType WHERE FId = card_type_id) AS FValueType,    
        comp_id AS FCompId,
        mem_id AS FMemId,
        card_state AS FState,
        create_date AS FCreateDate,
        expire_date AS FExpiredDate,
        0 AS FTotalTimes,
        0 AS FUsedTimes,
        comp_id AS FSaleCompId,
        0 AS FSaleBillId,
        memo AS FMemo 
   FROM imp_member_card
 WHERE deductmode = 1
 
 
  
  SELECT card_no from imp_member_card
  WHERE deductmode = 1
  
  group by card_no
  having sum(1) > 1
  
 
   SELECT * FROM imp_member_card WHERE card_no = '06614090'

INSERT TMemberCardAccount(FMemId,FId,FCardId,FAcctId,FDeposit,FBalance,FArrear,FCreateDate,FExpiredDate,FMemo)
SELECT mem_id,dbo.FNNewId(),card_id AS card_id,3,balance,balance,0,create_date,NULL AS FExpiredDate,''
FROM imp_member_card
  WHERE deductmode = 1
  

INSERT TMemberCardAcctHist(FId,FMemId,FCardId,FAcctId,FTime,FCardTypeId,FCompId,FBillType,FBizType,FTransId,FTransNo,FInAmount,FOutAmount,FBalance,FArrear,FRecordTime,FMemo)
select dbo.FNNewId(),A.mem_id,A.card_id,3,getdate(),A.card_type_id,A.comp_id,'DR','Import',0,'',A.balance,0,A.balance,0,getdate(),'从老系统中导入'
FROM imp_member_card AS A
  WHERE deductmode = 1
  
 
select dbo.FNNewId() AS id,dbo.FNNewId() AS course_id,   contractkindname AS no, contractkindname AS name 
INTO imp_item
from imp_member_card
where deductmode = 0
group by contractkindname

  

    INSERT  TItem(FId,FNo,FNoOfPad,FName,FAbbr,FTypeId,FBriefCode,FDescription,FPriceMode,FIsService,FStatTimes,FCustom1,FCustom2,FCustom3,FEnabled,FPrice,FVipPrice,FTime,FDctId,FPointMode,FPointValue,FDeptId,FAllowBooking)
    SELECT id,no,name,name,name,0,dbo.FNGetFirstPinYin(name),'''',1,1,1,'''','''','''',1,0,0,0,0,0,0,0,1
      FROM imp_item
 

        INSERT INTO  TCourse(FId,FNo,FName,FBriefCode,FItemId,FPackId,FSequence,FTypeId,FTimes,FFreeTimes,FDurable,FValidityPeriodUnit,FValidityPeriod,FCreateDate,FExpiredDate,FDescription,FEnabled,FPrice,FAmount,FCalcMode)  
        SELECT course_id AS FId,
               I.no + '',
               I.name AS FName,
               '' AS FBriefCode,
               I.id AS FItemId,
               0 AS FPackId,
               1 AS FSequence,
               isnull((SELECT TOP 1 FId FROM TBasicType WHERE FKey = 'CourseType'),0) AS FTypeId,
               1 AS FTimes,
               0 AS FFreeTimes,
               0 AS FDurable,
               0 AS FValidityPeriodUnit,
               0 AS FValidityPeriod, 
               getdate() AS FCreateDate,
               NULL AS FExpiredDate,
               '' AS FDescription,
               1 AS FEnabled,
               0 AS FPrice,
               0 AS FAmount,
               1 AS FCalcMode
          FROM imp_item AS I
    
        
        
        
  
ALTER TABLE imp_member_card ADD Pur_Times DECIMAL(18,6)
ALTER TABLE imp_member_card ADD Used_Pur_Times DECIMAL(18,6)
ALTER TABLE imp_member_card ADD Pur_Amount DECIMAL(18,6) 
ALTER TABLE imp_member_card ADD Item_Id BIGINT  
ALTER TABLE imp_member_card ADD Adjust_Price DECIMAL(18,6)
ALTER TABLE imp_member_card ADD free_times DECIMAL(18,6)
ALTER TABLE imp_member_card ADD used_free_times DECIMAL(18,6)

UPDATE A
   SET A.item_id = B.Id,
       A.card_type_id = B.Course_Id
  FROM imp_member_card AS A,imp_item AS B
 WHERE A.contractkindname = B.name
 
  select * from imp_member_card where deductmode = 0
  
--总次数
UPDATE imp_member_card 
SET pur_Times = classHour,
    free_times = zsclasshour

--还没有用
UPDATE imp_member_card 
   SET used_pur_times = 0, 
       used_free_times = 0
  where classHour_ye = classHour + zsclasshour

--使用次数小余等于赠送次数
UPDATE imp_member_card 
   SET used_pur_times = 0, 
       used_free_times = (classHour + zsclasshour) - classHour_ye 
  where  (classHour + zsclasshour) -  classHour_ye <= free_times
 
--使用次数大余等于赠送次数
UPDATE imp_member_card 
   SET used_pur_times = (classHour + zsclasshour) - classHour_ye - free_times, 
       used_free_times = free_times
  where  (classHour + zsclasshour) - classHour_ye  > free_times
 
 
 UPDATE A
    SET A.FUsedFreeTimes = B.used_free_times ,
        A.FUsedPurTimes = b.used_pur_times
   FROM imp_member_card AS B,TMemberCourse AS A
 WHERE A.FId = B.card_id
 
 
 SELECT classHour , zsclasshour, (classHour + zsclasshour) - classHour_ye , classHour_ye,used_pur_times,USED_free_times FROM imp_member_card
 
 
  SELECT * FROM imp_member WHERE mem_no = '06611562'
  
  SELECT * FROM imp_member_card where mem_Id = 6043687176467639838
  
    SELECT * FROM TMemberCourse where FMemId = 6043687176467639838
  
  
  
  
  INSERT TMemberCourse(FId,FMemId,FCourseId,FMemPackId,FItemId,FCompId,FPurTimes,FPurAmount,FFreeTimes,FUsedPurTimes,FFreeAmount,FPurchaseDate,FExpiredDate,FBizType,FTransId,FArrear,FDurable,FMemo,FCalcMode,FAdjustPrice)
SELECT card_id,mem_id,card_type_id,0,item_id,comp_id,Pur_Times,ISNULL(Pur_Amount,0),free_TIMES,USED_pur_TIMES,0,Create_Date,Expire_Date,'DR.Import',0,0,0,memo,1,ISNULL(Adjust_Price,0)
  FROM imp_member_card
--WHERE FCourseId IS NOT NULL

WHERE deductmode = 0


UPDATE TMemberCourse SET FFreeAmount = 0 
 

    
INSERT INTO TMemberCourseLog(FId,FMemId,FCardId,FPackId,FCourseId,FTime,FInPurTimes,FInFreeTimes,FOutPurTimes,FOutFreeTimes,
                          FCompId,
                          FBillType,FBizType,FBillId,FBillNo,FBillDetailId)

SELECT dbo.FNNewId(),Mem_Id,card_Id,0,card_type_id,FTime = getdate(),Pur_Times,used_pur_times,free_times,used_free_times, 
comp_id,
'DR','Import',0,'',0
FROM imp_member_card
WHERE deductmode = 0




    ALTER TABLE Imp_Member_trans_Hist ADD comp_id BIGINT
    ALTER TABLE Imp_Member_trans_Hist ADD mem_id BIGINT
 
 

    UPDATE A
     SET A.mem_id = B.mem_id,
         A.comp_id = B.comp_id
    FROM Imp_Member_trans_Hist AS A,imp_member AS B
   WHERE A.studentId = B.studentId

  
INSERT TMemberOldTransHist(FId,FMemId,FTime,FCardId,FCompId,FBillType,FBizType,FTransNo,FContent,FPrice,FQuantity,FAmount,FPayment,FEmployee,FMemo)
SELECT DBO.FNNewId(),A.mem_id,A.modifyTime,0,A.comp_Id,'DR','Import','',courseName,payValue,1,payValue,'',isnull('',''),''
FROM  Imp_Member_trans_Hist AS A 
 
     
 