if not exists(select top 1 Id from [settings_DictionaryData] where Id=1807580836291917) and not exists(select top 1 Id from settings_Configs WHERE Cname='CrmDataInit' and Name='RewardInit')
begin
	INSERT [settings_DictionaryData] ([Id],[Version],[DictionaryId],[Data],[IsDefault],[Enable],[Sort],[Type]) VALUES ( 1807580836291917,1,190011,N'表扬',1,1,1,2)
	INSERT [settings_DictionaryData] ([Id],[Version],[DictionaryId],[Data],[IsDefault],[Enable],[Sort],[Type]) VALUES ( 1807584512535570,1,190011,N'小功',0,1,2,2)
	INSERT [settings_DictionaryData] ([Id],[Version],[DictionaryId],[Data],[IsDefault],[Enable],[Sort],[Type]) VALUES ( 1807585330657758,1,190011,N'大功',0,1,3,2)
	INSERT [settings_DictionaryData] ([Id],[Version],[DictionaryId],[Data],[IsDefault],[Enable],[Sort],[Type]) VALUES ( 1817469390825106,1,190012,N'警告',1,1,1,2)
	INSERT [settings_DictionaryData] ([Id],[Version],[DictionaryId],[Data],[IsDefault],[Enable],[Sort],[Type]) VALUES ( 1817469886637464,1,190012,N'小过',0,1,2,2)
	INSERT [settings_DictionaryData] ([Id],[Version],[DictionaryId],[Data],[IsDefault],[Enable],[Sort],[Type]) VALUES ( 1817470750577072,1,190012,N'大过',0,1,3,2)
	INSERT INTO [settings_Configs]([Id],[Version],[Cname],[Name],[Value]) VALUES(10008,1,'CrmDataInit','RewardInit','1')
end
if not exists(select top 1 Id from settings_Configs WHERE Cname='CrmDataInit' and Name='IwCountTimeInit')
begin
   update customer_Customer set IwCountTime=OwnerTime
   update customer_InternationalWaters set [State]=0 where [State] is null
   INSERT INTO [settings_Configs]([Id],[Version],[Cname],[Name],[Value]) VALUES(10009,1,'CrmDataInit','IwCountTimeInit','1') 
end
if not exists(select top 1 Id from office_Category WHERE Id=2326142257878887)
begin
  INSERT INTO [office_Category]([Id],[Version],CategoryName,CategoryColor) VALUES(2326142257878887,1,'外勤行动','13') 
end
if not exists(select top 1 Id from office_Category WHERE Id=1519304167389316)
begin
  if not exists(select top 1 Id from settings_Configs WHERE Id=10010)
  begin
  INSERT INTO [office_Category]([Id],[Version],CategoryName,CategoryColor) VALUES(1519304167389316,1,'重要事项','5') 
  INSERT INTO [office_Category]([Id],[Version],CategoryName,CategoryColor) VALUES(1519304196297153,1,'节日活动','7') 
  INSERT INTO [office_Category]([Id],[Version],CategoryName,CategoryColor) VALUES(1519304868004246,1,'待办事项','1')
  INSERT INTO [settings_Configs]([Id],[Version],[Cname],[Name],[Value]) VALUES(10010,1,'CrmDataInit','OfficeCalCategoryInit','1') 
  end
end
else
begin
  if not exists(select top 1 Id from settings_Configs WHERE Cname='CrmDataInit' and Name='OfficeCalCategoryInit')
  begin
    INSERT INTO [settings_Configs]([Id],[Version],[Cname],[Name],[Value]) VALUES(10010,1,'CrmDataInit','OfficeCalCategoryInit','1') 
  end
end
if not exists(select top 1 Id from settings_Configs WHERE Id=10011)
  begin
    alter table market_ContractDetails drop column [No]
	alter table market_ContractDetails add [No]int identity(1,1) not null
	alter table Product_Product drop column [No]
	alter table Product_Product add [No] bigint identity(1,1) not null
	update Product_StorageTotal set ProductNo=p.No from Product_StorageTotal a,Product_Product p where a.ProductId=p.Id
	update Product_StorageOutAndIn set ProductNo=p.No from Product_StorageOutAndIn a,Product_Product p where a.ProductId=p.Id
	update Product_StorageInventory set ProductNo=p.No from Product_StorageInventory a,Product_Product p where a.ProductId=p.Id
	update Product_PurchaseEnquiryProducts set ProductNo=p.No from Product_PurchaseEnquiryProducts a,Product_Product p where a.ProductId=p.Id
	update Product_PurchaseOrderProducts set ProductNo=p.No from Product_PurchaseOrderProducts a,Product_Product p where a.ProductId=p.Id
	INSERT INTO [settings_Configs]([Id],[Version],[Cname],[Name],[Value]) VALUES(10011,1,'CrmDataInit','IndentityNo1Init','1') 
  end
if not exists(select top 1 Id from settings_Configs WHERE Id=10016)
  begin
    update [settings_ExpandoFields] set [Type]='select', [ViewHtml]='{{customerEntity.StageDisplay}}' where Name='StageId' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='select', [ViewHtml]='{{customerEntity.SourceDisplay}}' where Name='SourceId' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='select', [ViewHtml]='{{customerEntity.CompanyCountryDisplay}}-{{customerEntity.CompanyProvinceDisplay}}-{{customerEntity.CompanyCityDisplay}}-{{customerEntity.CompanyAreaDisplay}}' where Name='Area' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='single-text', [ViewHtml]='{{customerEntity.CompanyAddress}}&nbsp;&nbsp;<img ms-if="customerEntity.Lng" src="/Content/Themes/base/Images/ico/dadiandituico.jpg" style="padding-top: 2px;cursor:pointer;" title="点击查看地图" ms-click="detailMap"><img ms-if="!customerEntity.Lng" src="/Content/Themes/base/Images/ico/dadiandituicohui.jpg" style="padding-top: 2px" title="地图未标注">' where Name='CompanyAddress' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='single-text', [ViewHtml]='{{customerEntity.CompanyPostCode}}' where Name='CompanyPostCode' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='single-text', [ViewHtml]='{{customerEntity.FullName}}&nbsp;&nbsp;&nbsp;<span class="red" ms-if="customerEntity.FullNameChangeDate" ms-text="customerEntity.FullNameChangeDate"></span>' where Name='FullName' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='select', [ViewHtml]='{{customerEntity.ScaleDisplay}}' where Name='Scale' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='single-text', [ViewHtml]='{{customerEntity.RegFund}}万' where Name='RegFund' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='multi-radio', [ViewHtml]='' where Name='IsBlackList' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='raty-star', [ViewHtml]='<div style="width: 125px; display: inline;" ms-widget=''raty,valueRaty,valueRatyOpts'' readonly="readonly"></div>' where Name='Value' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='multi-radio', [ViewHtml]='{{customerEntity.IsHot?''是'':''否''}}' where Name='IsHot' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='raty-medal', [ViewHtml]='<div style="width: 125px; display: inline;" ms-widget=''raty,creditRaty,creditRatyOpts'' readonly="readonly"></div>' where Name='Credit' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='processbar', [ViewHtml]='<div style="width: 125px;display: inline;" ms-widget=''processbar,hotprocessbar,hotOpts'' readonly="readonly"></div>' where Name='Temperature' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='select', [ViewHtml]='<span color="#F96400">{{customerEntity.RelationDisplay}}</span>' where Name='RelationId' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='multi-checkbox', [ViewHtml]='{{customerEntity.ServerLabelDisplay}}' where Name='ServerLabel' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='tel', [ViewHtml]='{{customerEntity.CompanyTel1|mobileFormat|html}}' where Name='CompanyTel1' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='tel', [ViewHtml]='{{customerEntity.Tel400}}' where Name='Tel400' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='tel', [ViewHtml]='{{customerEntity.CompanyTel2|mobileFormat|html}}' where Name='CompanyTel2' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='tel', [ViewHtml]='{{customerEntity.Fax}}' where Name='Fax' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='url', [ViewHtml]='{{customerEntity.Homepage|link|html}}' where Name='Homepage' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='url', [ViewHtml]='{{customerEntity.ShopPage|link|html}}' where Name='ShopPage' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='email', [ViewHtml]='<a ms-href="''#/Tools/Email/''+customerEntity.Email+''/send''">{{customerEntity.Email}}</a>' where Name='Email' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='qq', [ViewHtml]='{{customerEntity.QQ}}' where Name='QQ' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='single-text', [ViewHtml]='{{customerEntity.Wangwang}}' where Name='Wangwang' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='single-text', [ViewHtml]='{{customerEntity.OtherContactMethod}}' where Name='OtherContactMethod' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='select', [ViewHtml]='{{customerEntity.IndustryDisplay}}' where Name='Industry' and Module='Customer'
    update [settings_ExpandoFields] set [Type]='single-text', [ViewHtml]='{{customerEntity.Profession}}' where Name='Profession' and Module='Customer'
	INSERT INTO [settings_Configs]([Id],[Version],[Cname],[Name],[Value]) VALUES(10016,1,'CrmDataInit','CustomerExpandoFieldsUpdate3','1') 
  end
  if not exists(select top 1 Id from settings_Configs WHERE Id=10013)
  begin
    update market_DocumentaryOffer set CommentCount=isnull(m.c,0) from market_DocumentaryOffer d left outer join (select COUNT(1) as c,DocumentaryOfferId from market_Comment group by DocumentaryOfferId) m on d.Id=m.DocumentaryOfferId
	INSERT INTO [settings_Configs]([Id],[Version],[Cname],[Name],[Value]) VALUES(10013,1,'CrmDataInit','DocumentaryOfferCommentCount','1') 
  end
  if not exists(select top 1 Id from settings_Configs WHERE Id=10015)
  begin
    update financialAffairs_Assets set Hide=0 where Hide is null
	update financialAffairs_Project set Hide=0 where Hide is null
	INSERT INTO [settings_Configs]([Id],[Version],[Cname],[Name],[Value]) VALUES(10015,1,'CrmDataInit','FinancialAffairsSettings','1') 
  end
if not exists(select top 1 Id from Report_Report where Id = 87)
begin
INSERT [Report_Report] ([Id],[Version],[Module],[Category],[Title],[Url],[Sort],[ActionId]) VALUES ( 87,1,2,1,N'销售分析报表',N'#/Report/SalesComplex',3,1061108564400021)
end
if not exists(select top 1 Id from settings_Configs WHERE Id=10018)
  begin
    update [settings_ExpandoFields] set [Html]='<input ms-duplex="customerEntity.QQ" type="text" data-tipkey="QQ" />' where Name='QQ' and Module='Customer'
	update [settings_ExpandoFields] set [Html]='<input ms-duplex="customerEntity.Email" type="text" data-tipkey="Email" />' where Name='Email' and Module='Customer'
	update [settings_ExpandoFields] set [Html]='<input ms-duplex="customerEntity.Wangwang" type="text" data-tipkey="Wangwang" />' where Name='WangWang' and Module='Customer'
	update [settings_ExpandoFields] set [Html]='<input ms-duplex="customerEntity.CompanyTel1" type="text" data-tipkey="CompanyTel1" />' where Name='CompanyTel1' and Module='Customer'
	update [settings_ExpandoFields] set [Html]='<input ms-duplex="customerEntity.CompanyTel2" type="text" data-tipkey="CompanyTel2" />' where Name='CompanyTel2' and Module='Customer'
    INSERT INTO [settings_Configs]([Id],[Version],[Cname],[Name],[Value]) VALUES(10018,1,'CrmDataInit','CustomerExpandoFieldsUpdate4','1') 
  end
if not exists(select top 1 Id from settings_Configs WHERE Id=10019)
  begin
  update market_Contract set Files=b.Id from market_Contract a left outer join settings_Attachmet b on a.Files='/upload/'+b.Path where a.Files is not null and b.Id is not null
  INSERT INTO [settings_Configs]([Id],[Version],[Cname],[Name],[Value]) VALUES(10019,1,'CrmDataFix','ContractFilesFix','1') 
end
if not exists(select top 1 Id from settings_Configs WHERE Id=10020)
  begin
  update customer_Customer set OwnerFrom='Create' where OwnerFrom is null
  INSERT INTO [settings_Configs]([Id],[Version],[Cname],[Name],[Value]) VALUES(10020,1,'CrmDataFix','CustomerOwnerFrom','1') 
end
if not exists(select top 1 Id from settings_Configs WHERE Id=10021)
  begin
  update financialAffairs_AccountBook set DealPrice=ProductPrice where DealPrice is null
  update financialAffairs_AccountBook set SalesmanId=u.UserId from financialAffairs_AccountBook a left outer join security_User u on a.Salesman=u.FullName where a.SalesmanId is null
  update financialAffairs_AccountBook set ReceiptmanId=u.UserId from financialAffairs_AccountBook a left outer join security_User u on a.Receiptman=u.FullName where a.ReceiptmanId is null
  INSERT INTO [settings_Configs]([Id],[Version],[Cname],[Name],[Value]) VALUES(10021,1,'CrmDataFix','AccountBookFix','1') 
end
if not exists(select top 1 Id from settings_Configs WHERE Id=10022)
  begin
  update office_DailyReport set CommentTotal=isnull(b.c,0) from office_DailyReport a left outer join (select COUNT(Id) as c,DailyReportId as did from office_DailReportDetail where Comments is not null and datalength(Comments)>0 group by DailyReportId) b on a.Id=b.did
  INSERT INTO [settings_Configs]([Id],[Version],[Cname],[Name],[Value]) VALUES(10022,1,'CrmDataFix','DailyReportCommentTotalFix','1') 
end
if not exists(select top 1 Id from settings_Configs WHERE Id=10023)
  begin
  update Product_Product set RoyaltyPrice=GeneralCustomerPrice,RoyaltyType=0,RoyaltyCompute=0,RoyaltyValue=0 where RoyaltyPrice is null
  update market_ContractDetails set ProductName=b.Name,BrandId=c.Id,BrandName=c.Name,[Standard]=b.StartDate,UnitsDisplay=d.Data from
  market_ContractDetails a left outer join Product_Product b on a.Product=b.Id
  left outer join Product_Category c on b.CategoryId=c.Id
  left outer join settings_DictionaryData d on a.Unit=d.Id
  where a.Product is not null
  INSERT INTO [settings_Configs]([Id],[Version],[Cname],[Name],[Value]) VALUES(10023,1,'CrmDataFix','ProductRoyaltyFix','1') 
end
if not exists(select top 1 Id from settings_Configs WHERE Id=10025)
  begin
  update market_ContractDetails set BelongTo=b.BelongTo,NoteType=b.NoteType,Customer=b.Customer from
  market_ContractDetails a left outer join market_Contract b on a.Contract=b.Id
  where b.Id is not null
  INSERT INTO [settings_Configs]([Id],[Version],[Cname],[Name],[Value]) VALUES(10025,1,'CrmDataFix','ContractDetailsBelongToFix2','1') 
end