--本科室业务量统计
CREATE PROCEDURE sp_MyDepartmentLoad
    @ModalityType varchar(18), 
    @StatStartDttm datetime,
    @StatEndDttm datetime 
AS
BEGIN
 
DECLARE @sStudy_dttm datetime 
DECLARE @iStudy_key int
DECLARE @fStudyCharge money
DECLARE @sGroupField varchar(128)
DECLARE @sStatistField varchar(128)
DECLARE @fStatistWeight money
DECLARE @iPos int
DECLARE @ilen int


DECLARE crStudy CURSOR FOR

SELECT study_dttm, study_key, study_bodypart, study_charge
FROM study
WHERE study_modalitytype=@ModalityType AND report_state > 0 AND
	study_dttm >= @StatStartDttm AND study_dttm <= @StatEndDttm 

OPEN crStudy

FETCH NEXT FROM crStudy 
INTO @sStudy_dttm, @iStudy_key, @sGroupField, @fStudyCharge

WHILE @@FETCH_STATUS = 0
BEGIN
  SET @sGroupField=RTRIM(LTRIM(@sGroupField))  
  IF LEN(@sGroupField) > 0
  BEGIN
	  SET @iPos=CHARINDEX(' ',@sGroupField)          
	  WHILE @iPos>=1       
	  BEGIN    
		SET @sStatistField=SUBSTRING(@sGroupField,1,@iPos-1)  
		INSERT INTO CC_Statist (study_dttm,statist_field,statist_weight)
			VALUES(@sStudy_dttm,@sStatistField,1)
		SET @iLen=LEN(@sGroupField)  
		SET @sGroupField=SUBSTRING(@sGroupField,@iPos+1,@iLen)  
		SET @sGroupField=RTRIM(LTRIM(@sGroupField))  
		SET @iPos=CHARINDEX(' ',@sGroupField)  
	  END

	  INSERT INTO CC_Statist (study_dttm,study_key,statist_field,statist_weight,study_charge)
		VALUES(@sStudy_dttm,@iStudy_key,@sGroupField,1,@fStudyCharge)    
  END
  	
  FETCH NEXT FROM crStudy 
  INTO @sStudy_dttm,@iStudy_key,@sGroupField,@fStudyCharge
END       

CLOSE crStudy     
DEALLOCATE crStudy 

-- 按部位权重更新cc_statist，仅更新权重不为1的部位，如果dd_bodypart中的权重设为负数，则权重为0
DECLARE crBodyPart CURSOR FOR       
SELECT bodypart_name,CASE WHEN bodypart_charge1>0.0 THEN bodypart_charge1 ELSE 0.0 END       
FROM dd_bodypart WHERE modality_type=@ModalityType AND (bodypart_charge1<1.0 OR bodypart_charge1>1.0)

OPEN crBodyPart      

FETCH NEXT FROM crBodyPart  
INTO @sStatistField,@fStatistWeight 

WHILE @@FETCH_STATUS=0 
BEGIN 
  UPDATE cc_statist SET statist_weight=@fStatistWeight WHERE statist_field=@sStatistField 
  
  FETCH NEXT FROM crBodyPart  
  INTO @sStatistField,@fStatistWeight
END 

CLOSE crBodyPart
DEALLOCATE crBodyPart

END