zoukankan      html  css  js  c++  java
  • 重构代码压缩

    最近,使用人员反应图片无法删除,跟踪进去看到代码长长的,无法直视

    源代码如下

      1   try 
      2                     {
      3                         //string deleteprodcutid = Request["ProductID"];
      4                         int deleteprodcutid = Util.TrimIntNull(Request["sysno"]);
      5                         int i = int.Parse(Request["i"]);
      6                         ProductBasicInfo deleteproductInfo =
      7                             Icson.BLL.Basic.ProductManager.GetInstance().LoadBasic(deleteprodcutid);
      8 
      9                         //FTP服务器用户名和密码
     10                         string ftpUserName = AppConfig.FTPUserName;
     11                         string ftpPassword = AppConfig.FTPPassword;
     12 
     13                         //图片保存路径,作为拼接图片路径用
     14                         string ftpProductPath50 = AppConfig.FTPProductPath50;//原图保存路径
     15                         string ftpProductPath100 = AppConfig.FTPProductPath100;//色图
     16                         string ftpProductPath160 = AppConfig.FTPProductPath160;//小图
     17                         string ftpProductPath330 = AppConfig.FTPProductPath330;//中图
     18                         string ftpProductPath800 = AppConfig.FTPProductPath800;//大图
     19 
     20                         string ftpProductPath60 = AppConfig.FTPProductPath60;
     21                         string ftpProductPath80 = AppConfig.FTPProductPath80;
     22                         string ftpProductPath120 = AppConfig.FTPProductPath120;
     23                         string ftpProductPath180 = AppConfig.FTPProductPath180;
     24                         string ftpProductPath200 = AppConfig.FTPProductPath200;
     25                         string ftpProductPath240 = AppConfig.FTPProductPath240;
     26                         string ftpProductPath460 = AppConfig.FTPProductPath460;
     27 
     28                         //图片地址全名包括路径
     29                         string img50 = AppConst.StringNull;//原图保存路径
     30                         string img100 = AppConst.StringNull;//色图
     31                         string img160 = AppConst.StringNull;//小图
     32                         string img330 = AppConst.StringNull;//中图
     33                         string img800 = AppConst.StringNull;//大图
     34                         string img60 = AppConst.StringNull;
     35                         string img80 = AppConst.StringNull;
     36                         string img120 = AppConst.StringNull;
     37                         string img180 = AppConst.StringNull;
     38                         string img200 = AppConst.StringNull;
     39                         string img240 = AppConst.StringNull;
     40                         string img460 = AppConst.StringNull;
     41                         string current50 = AppConst.StringNull;
     42                         string current100 = AppConst.StringNull;
     43                         string current160 = AppConst.StringNull;
     44                         string current330 = AppConst.StringNull;
     45                         string current800 = AppConst.StringNull;
     46                         string current60 = AppConst.StringNull;
     47                         string current80 = AppConst.StringNull;
     48                         string current120 = AppConst.StringNull;
     49                         string current180 = AppConst.StringNull;
     50                         string current200 = AppConst.StringNull;
     51                         string current240 = AppConst.StringNull;
     52                         string current460 = AppConst.StringNull;
     53                         
     54                         string last50 = AppConst.StringNull;
     55                         string last100 = AppConst.StringNull;
     56                         string last160 = AppConst.StringNull;
     57                         string last330 = AppConst.StringNull;
     58                         string last800 = AppConst.StringNull;
     59 
     60                         string last60 = AppConst.StringNull;
     61                         string last80 = AppConst.StringNull;
     62                         string last120 = AppConst.StringNull;
     63                         string last180 = AppConst.StringNull;
     64                         string last200 = AppConst.StringNull;
     65                         string last240 = AppConst.StringNull;
     66                         string last460 = AppConst.StringNull;
     67 
     68                         FTPHelper fTPHelper = new FTPHelper(ftpProductPath50, ftpUserName, ftpPassword);
     69                         
     70                         if (i == deleteproductInfo.MultiPicNum)
     71                         {
     72                             //处理的是显示在最后的图片
     73                             if (i == 1)
     74                             {
     75                                 //delete剩下的唯一的图片
     76                                 img50 = deleteprodcutid + ".jpg";
     77                                 img100 = deleteprodcutid + ".jpg";
     78                                 img160 = deleteprodcutid + ".jpg";
     79                                 img330 = deleteprodcutid + ".jpg";
     80                                 img800 = deleteprodcutid + ".jpg";
     81 
     82                                 img60 = deleteprodcutid + ".jpg";
     83                                 img80 = deleteprodcutid + ".jpg";
     84                                 img120 = deleteprodcutid + ".jpg";
     85                                 img180 = deleteprodcutid + ".jpg";
     86                                 img200 = deleteprodcutid + ".jpg";
     87                                 img240 = deleteprodcutid + ".jpg";
     88                                 img460 = deleteprodcutid + ".jpg";
     89 
     90                                 //当前图片
     91                                 current50 = ftpProductPath50 + img50;
     92                                 current100 = ftpProductPath100 + img100;
     93                                 current160 = ftpProductPath160 + img160;
     94                                 current330 = ftpProductPath330 + img330;
     95                                 current800 = ftpProductPath800 + img800;
     96 
     97                                 current60 = ftpProductPath60 + img60;
     98                                 current80 = ftpProductPath80 + img80;
     99                                 current120 = ftpProductPath120 + img120;
    100                                 current180 = ftpProductPath180 + img180;
    101                                 current200 = ftpProductPath200 + img200;
    102                                 current240 = ftpProductPath240 + img240;
    103                                 current460 = ftpProductPath460 + img460;
    104 
    105                                 //直接delete最后的一张图片
    106                                 //原图
    107                                 if (!fTPHelper.DeleteFile(current50))
    108                                 {
    109                                     throw new BizException("删除原图失败");
    110                                 }
    111                                
    112                                 //色图
    113                                 if (!fTPHelper.DeleteFile(current100))
    114                                 {
    115                                     throw new BizException("删除色图失败");
    116                                 }
    117                               
    118                                 //小图
    119                                 if (!fTPHelper.DeleteFile(current160))
    120                                 {
    121                                     throw new BizException("删除小图失败");
    122                                 }
    123                               
    124                                 //中图
    125                                 if (!fTPHelper.DeleteFile(current330))
    126                                 {
    127                                     throw new BizException("删除中图失败");
    128                                 }
    129                               
    130                                 //大图
    131                                 if (!fTPHelper.DeleteFile(current800))
    132                                 {
    133                                     throw new BizException("删除大图失败");
    134                                 }
    135                                
    136                                 //60*60
    137                                 if (!fTPHelper.DeleteFile(current60))
    138                                 {
    139                                     throw new BizException("删除60*60图片失败");
    140                                 }
    141                               
    142                                 //80*80
    143                                 if (!fTPHelper.DeleteFile(current80))
    144                                 {
    145                                     throw new BizException("删除80*80图片失败");
    146                                 }
    147                                 
    148                                 //120*120
    149                                 if (!fTPHelper.DeleteFile(current120))
    150                                 {
    151                                     throw new BizException("删除120*120图片失败");
    152                                 }
    153                                
    154                                 //180*180
    155                                 if (!fTPHelper.DeleteFile(current180))
    156                                 {
    157                                     throw new BizException("删除180*180图片失败");
    158                                 }
    159                              
    160                                 //200*200
    161                                 if (!fTPHelper.DeleteFile(current200))
    162                                 {
    163                                     throw new BizException("删除200*200图片失败");
    164                                 }
    165                                
    166                                 //240*240
    167                                 if (!fTPHelper.DeleteFile(current240))
    168                                 {
    169                                     throw new BizException("删除240*240图片失败");
    170                                 }
    171                              
    172                                 //460*460
    173                                 if (!fTPHelper.DeleteFile(current460))
    174                                 {
    175                                     throw new BizException("删除460*460图片失败");
    176                                 }
    177                                
    178                             }
    179                             else
    180                             {
    181                                 //图片数量大于1,并且不是delete最后显示的图片 
    182                                 img50 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    183                                 img100 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    184                                 img160 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    185                                 img330 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    186                                 img800 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    187 
    188                                 img60 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    189                                 img80 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    190                                 img120 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    191                                 img180 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    192                                 img200 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    193                                 img240 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    194                                 img460 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    195 
    196 
    197                                 //当前图片
    198                                 current50 = ftpProductPath50 + img50;
    199                                 current100 = ftpProductPath100 + img100;
    200                                 current160 = ftpProductPath160 + img160;
    201                                 current330 = ftpProductPath330 + img330;
    202                                 current800 = ftpProductPath800 + img800;
    203 
    204                                 current60 = ftpProductPath60 + img60;
    205                                 current80 = ftpProductPath80 + img80;
    206                                 current120 = ftpProductPath120 + img120;
    207                                 current180 = ftpProductPath180 + img180;
    208                                 current200 = ftpProductPath200 + img200;
    209                                 current240 = ftpProductPath240 + img240;
    210                                 current460 = ftpProductPath460 + img460; 
    211                                 
    212                                 last50 = ftpProductPath50 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    213                                 last100 = ftpProductPath100 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    214                                 last160 = ftpProductPath160 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    215                                 last330 = ftpProductPath330 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    216                                 last800 = ftpProductPath800 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    217 
    218                                 last60 = ftpProductPath60 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    219                                 last80 = ftpProductPath80 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    220                                 last120 = ftpProductPath120 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    221                                 last180 = ftpProductPath180 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    222                                 last200 = ftpProductPath200 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    223                                 last240 = ftpProductPath240 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    224                                 last460 = ftpProductPath460 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    225 
    226                                 //原图
    227                                 if (!fTPHelper.DeleteFile(current50))
    228                                 {
    229                                     throw new BizException("删除原图失败");
    230                                 }
    231                               
    232                                 //色图
    233                                 if (!fTPHelper.DeleteFile(current100))
    234                                 {
    235                                     throw new BizException("删除色图失败");
    236                                 }
    237                              
    238                                 //小图
    239                                 if (!fTPHelper.DeleteFile(current160))
    240                                 {
    241                                     throw new BizException("删除小图失败");
    242                                 }
    243                             
    244                                 //中图
    245                                 if (!fTPHelper.DeleteFile(current330))
    246                                 {
    247                                     throw new BizException("删除中图失败");
    248                                 }
    249                               
    250                                 //大图
    251                                 if (!fTPHelper.DeleteFile(current800))
    252                                 {
    253                                     throw new BizException("删除大图失败");
    254                                 }
    255                                
    256                                 //60*60
    257                                 if (!fTPHelper.DeleteFile(current60))
    258                                 {
    259                                     throw new BizException("删除60*60图片失败");
    260                                 }
    261                             
    262                                 //80*80
    263                                 if (!fTPHelper.DeleteFile(current80))
    264                                 {
    265                                     throw new BizException("删除80*80图片失败");
    266                                 }
    267                               
    268                                 //120*120
    269                                 if (!fTPHelper.DeleteFile(current120))
    270                                 {
    271                                     throw new BizException("删除120*120图片失败");
    272                                 }
    273                               
    274                                 //180*180
    275                                 if (!fTPHelper.DeleteFile(current180))
    276                                 {
    277                                     throw new BizException("删除180*180图片失败");
    278                                 }
    279                                
    280                                 //200*200
    281                                 if (!fTPHelper.DeleteFile(current200))
    282                                 {
    283                                     throw new BizException("删除200*200图片失败");
    284                                 }
    285                                
    286                                 //240*240
    287                                 if (!fTPHelper.DeleteFile(current240))
    288                                 {
    289                                     throw new BizException("删除240*240图片失败");
    290                                 }
    291                               
    292                                 //460*460
    293                                 if (!fTPHelper.DeleteFile(current460))
    294                                 {
    295                                     throw new BizException("删除460*460图片失败");
    296                                 }
    297                                
    298                             }
    299 
    300                         }
    301                         else
    302                         {
    303                             //拼接图片路径
    304                             if (i == 1)
    305                             {
    306                                 img50 = deleteprodcutid + ".jpg";
    307                                 img100 =  deleteprodcutid + ".jpg";
    308                                 img160 =  deleteprodcutid + ".jpg";
    309                                 img330 = deleteprodcutid + ".jpg";
    310                                 img800 = deleteprodcutid + ".jpg";
    311 
    312                                 img60 = deleteprodcutid + ".jpg";
    313                                 img80 = deleteprodcutid + ".jpg";
    314                                 img120 = deleteprodcutid + ".jpg";
    315                                 img180 = deleteprodcutid + ".jpg";
    316                                 img200 = deleteprodcutid + ".jpg";
    317                                 img240 = deleteprodcutid + ".jpg";
    318                                 img460 = deleteprodcutid + ".jpg";
    319                             }
    320                             else
    321                             {
    322                                 img50 =  (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    323                                 img100 =(i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    324                                 img160 =  (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    325                                 img330 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    326                                 img800 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    327 
    328                                 img60 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    329                                 img80 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    330                                 img120 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    331                                 img180 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    332                                 img200 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    333                                 img240 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    334                                 img460 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    335                             }
    336 
    337                             //当前图片
    338                             current50 = ftpProductPath50 + img50;
    339                             current100 = ftpProductPath100 + img100;
    340                             current160 = ftpProductPath160 + img160;
    341                             current330 = ftpProductPath330 + img330;
    342                             current800 = ftpProductPath800 + img800;
    343 
    344                             current60 = ftpProductPath60 + img60;
    345                             current80 = ftpProductPath80 + img80;
    346                             current120 = ftpProductPath120 + img120;
    347                             current180 = ftpProductPath180 + img180;
    348                             current200 = ftpProductPath200 + img200;
    349                             current240 = ftpProductPath240 + img240;
    350                             current460 = ftpProductPath460 + img460; 
    351 
    352                             //最后一张图片
    353                             last50 = ftpProductPath50 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    354                             last100 = ftpProductPath100 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    355                             last160 = ftpProductPath160 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    356                             last330 = ftpProductPath330 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    357                             last800 = ftpProductPath800 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    358 
    359                             last60 = ftpProductPath60 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    360                             last80 = ftpProductPath80 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    361                             last120 = ftpProductPath120 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    362                             last180 = ftpProductPath180 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    363                             last200 = ftpProductPath200 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    364                             last240 = ftpProductPath240 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    365                             last460 = ftpProductPath460 + (deleteproductInfo.MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    366                           
    367                             //原图
    368                             if (!fTPHelper.DeleteFile(current50))
    369                             {
    370                                 throw new BizException("删除原图失败");
    371                             }
    372                             if (!fTPHelper.RenameFileName(img50,last50))
    373                             {
    374                                 throw new BizException("删除原图失败");
    375                             }
    376 
    377                             //色图
    378                             if (!fTPHelper.DeleteFile(current100))
    379                             {
    380                                 throw new BizException("删除色图失败");
    381                             }
    382                             if (!fTPHelper.RenameFileName(img100, last100))
    383                             {
    384                                 throw new BizException("删除色图失败");
    385                             }
    386 
    387                             //小图
    388                             if (!fTPHelper.DeleteFile(current160))
    389                             {
    390                                 throw new BizException("删除小图失败");
    391                             }
    392                             if (!fTPHelper.RenameFileName(img160, last160))
    393                             {
    394                                 throw new BizException("删除小图失败");
    395                             }
    396 
    397                             //中图
    398                             if (!fTPHelper.DeleteFile(current330))
    399                             {
    400                                 throw new BizException("删除中图失败");
    401                             }
    402                             if (!fTPHelper.RenameFileName(img330, last330))
    403                             {
    404                                 throw new BizException("删除中图失败");
    405                             }
    406 
    407                             //大图
    408                             if (!fTPHelper.DeleteFile(current800))
    409                             {
    410                                 throw new BizException("删除大图失败");
    411                             }
    412                             if (!fTPHelper.RenameFileName(img800, last800))
    413                             {
    414                                 throw new BizException("删除大图失败");
    415                             }
    416 
    417                             //60*60
    418                             if (!fTPHelper.DeleteFile(current60))
    419                             {
    420                                 throw new BizException("删除60*60图片失败");
    421                             }
    422                             if (!fTPHelper.RenameFileName(img60, last60))
    423                             {
    424                                 throw new BizException("删除60*60图片失败");
    425                             }
    426 
    427                             //80*80
    428                             if (!fTPHelper.DeleteFile(current80))
    429                             {
    430                                 throw new BizException("删除80*80图片失败");
    431                             }
    432                             if (!fTPHelper.RenameFileName(img80, last80))
    433                             {
    434                                 throw new BizException("删除80*80图片失败");
    435                             }
    436 
    437                             //120*120
    438                             if (!fTPHelper.DeleteFile(current120))
    439                             {
    440                                 throw new BizException("删除120*120图片失败");
    441                             }
    442                             if (!fTPHelper.RenameFileName(img120, last120))
    443                             {
    444                                 throw new BizException("删除120*120图片失败");
    445                             }
    446 
    447                             //180*180
    448                             if (!fTPHelper.DeleteFile(current180))
    449                             {
    450                                 throw new BizException("删除180*180图片失败");
    451                             }
    452                             if (!fTPHelper.RenameFileName(img180, last180))
    453                             {
    454                                 throw new BizException("删除180*180图片失败");
    455                             }
    456 
    457                             //200*200
    458                             if (!fTPHelper.DeleteFile(current200))
    459                             {
    460                                 throw new BizException("删除200*200图片失败");
    461                             }
    462                             if (!fTPHelper.RenameFileName(img200, last200))
    463                             {
    464                                 throw new BizException("删除200*200图片失败");
    465                             }
    466 
    467                             //240*240
    468                             if (!fTPHelper.DeleteFile(current240))
    469                             {
    470                                 throw new BizException("删除240*240图片失败");
    471                             }
    472                             if (!fTPHelper.RenameFileName(img240, last240))
    473                             {
    474                                 throw new BizException("删除240*240图片失败");
    475                             }
    476 
    477                             //460*460
    478                             if (!fTPHelper.DeleteFile(current460))
    479                             {
    480                                 throw new BizException("删除460*460图片失败");
    481                             }
    482                             if (!fTPHelper.RenameFileName(img460, last460))
    483                             {
    484                                 throw new BizException("删除460*460图片失败");
    485                             }
    486 
    487                         }
    488                         
    489                         deleteproductInfo.MultiPicNum--;
    490                         ProductManager.GetInstance().UpdateBasicInfo(deleteproductInfo);
    491                         
    492                         result = "success";
    493                     }
    494                     catch (Exception ex)
    495                     {
    496                         result = "error";
    497                     }
    View original Code

    大概500行,检查发现 fTPHelper.DeleteFile删除不存在的图片报错,将不会更新数据库。于是忽略异常,几经测试,发现有2个条件下没有更新数据库,估计代码太长,程序员忘记写了。实在无法忍耐,于是决定重构

    先理清逻辑,把几个相似的字段用对象类属性表示,提取几个函数,得到代码如下

      1   try
      2                         {
      3                             //string deleteprodcutid = context.Request["ProductID"];
      4                             int deleteprodcutid = Util.TrimIntNull(context.Request["sysno"]);
      5                             int i = int.Parse(context.Request["i"]);
      6                             ProductBasicInfo deleteproductInfo =
      7                                 Icson.BLL.Basic.ProductManager.GetInstance().LoadBasic(deleteprodcutid);
      8 
      9                             //FTP服务器用户名和密码
     10                             string ftpUserName = AppConfig.FTPUserName;
     11                             string ftpPassword = AppConfig.FTPPassword;
     12 
     13                             //图片保存路径,作为拼接图片路径用
     14                             ftpProductPath ftpPath = new ftpProductPath();
     15 
     16                             FTPHelper fTPHelper = new FTPHelper(ftpPath.ftpProductPath50, ftpUserName, ftpPassword);
     17 
     18                             //当前图片名称
     19                             Img img = new Img(i, deleteprodcutid);
     20 
     21                             //当前图片和路径
     22                             Current cur = new Current(ftpPath, img);
     23 
     24                             //直接delete图片
     25                             DeleteAllFile(cur, fTPHelper);
     26 
     27                             if (i != deleteproductInfo.MultiPicNum)
     28                             {
     29                                 //不是最后一张,则把最后一张的图片重命名为删掉的图片名称。
     30                                 //最后一张图片和路径
     31                                 Last last = new Last(ftpPath, deleteproductInfo.MultiPicNum, deleteprodcutid);
     32                                 RenameAllFileName(img, last, fTPHelper);
     33                             }
     34 
     35                             deleteproductInfo.MultiPicNum--;
     36                             ProductManager.GetInstance().UpdateBasicInfo(deleteproductInfo);
     37 
     38                             result = "success";
     39                         }
     40                         catch (Exception ex)
     41                         {
     42                             result = "error";
     43                         }
     44 
     45  /// <summary>
     46         /// 重命名图片
     47         /// </summary>
     48         /// <param name="img">命名后名称组</param>
     49         /// <param name="last">命名前名称组</param>
     50         /// <param name="fTPHelper">ftp帮助类</param>
     51         public void RenameAllFileName(Img img, Last last, FTPHelper fTPHelper)
     52         {
     53             //原图
     54             if (!fTPHelper.RenameFileName(img.img50, last.last50))
     55             {
     56                 throw new BizException("重命名原图失败");
     57             }
     58 
     59             //色图
     60             if (!fTPHelper.RenameFileName(img.img100, last.last100))
     61             {
     62                 throw new BizException("重命名色图失败");
     63             }
     64 
     65             //小图
     66             if (!fTPHelper.RenameFileName(img.img160, last.last160))
     67             {
     68                 throw new BizException("重命名小图失败");
     69             }
     70 
     71             //中图
     72             if (!fTPHelper.RenameFileName(img.img330, last.last330))
     73             {
     74                 throw new BizException("重命名中图失败");
     75             }
     76 
     77             //大图
     78             if (!fTPHelper.RenameFileName(img.img800, last.last800))
     79             {
     80                 throw new BizException("重命名大图失败");
     81             }
     82 
     83             //60*60
     84             if (!fTPHelper.RenameFileName(img.img60, last.last60))
     85             {
     86                 throw new BizException("重命名60*60图片失败");
     87             }
     88 
     89             //80*80
     90             if (!fTPHelper.RenameFileName(img.img80, last.last80))
     91             {
     92                 throw new BizException("重命名80*80图片失败");
     93             }
     94 
     95             //120*120
     96             if (!fTPHelper.RenameFileName(img.img120, last.last120))
     97             {
     98                 throw new BizException("重命名120*120图片失败");
     99             }
    100 
    101             //180*180
    102             if (!fTPHelper.RenameFileName(img.img180, last.last180))
    103             {
    104                 throw new BizException("重命名180*180图片失败");
    105             }
    106 
    107             //200*200
    108             if (!fTPHelper.RenameFileName(img.img200, last.last200))
    109             {
    110                 throw new BizException("重命名200*200图片失败");
    111             }
    112 
    113             //240*240
    114             if (!fTPHelper.RenameFileName(img.img240, last.last240))
    115             {
    116                 throw new BizException("重命名240*240图片失败");
    117             }
    118 
    119             //460*460
    120             if (!fTPHelper.RenameFileName(img.img460, last.last460))
    121             {
    122                 throw new BizException("重命名460*460图片失败");
    123             }
    124         }
    125         /// <summary>
    126         /// 删除图片
    127         /// </summary>
    128         /// <param name="cur">要删除的图片(带路径)</param>
    129         /// <param name="fTPHelper">ftp帮助类</param>
    130         public void DeleteAllFile(Current cur, FTPHelper fTPHelper)
    131         {
    132             //原图
    133             if (!fTPHelper.DeleteFile(cur.current50))
    134             {
    135                 throw new BizException("删除原图失败");
    136             }
    137 
    138             //色图
    139             if (!fTPHelper.DeleteFile(cur.current100))
    140             {
    141                 throw new BizException("删除色图失败");
    142             }
    143 
    144             //小图
    145             if (!fTPHelper.DeleteFile(cur.current160))
    146             {
    147                 throw new BizException("删除小图失败");
    148             }
    149 
    150             //中图
    151             if (!fTPHelper.DeleteFile(cur.current330))
    152             {
    153                 throw new BizException("删除中图失败");
    154             }
    155 
    156             //大图
    157             if (!fTPHelper.DeleteFile(cur.current800))
    158             {
    159                 throw new BizException("删除大图失败");
    160             }
    161 
    162             //60*60
    163             if (!fTPHelper.DeleteFile(cur.current60))
    164             {
    165                 throw new BizException("删除60*60图片失败");
    166             }
    167 
    168             //80*80
    169             if (!fTPHelper.DeleteFile(cur.current80))
    170             {
    171                 throw new BizException("删除80*80图片失败");
    172             }
    173 
    174             //120*120
    175             if (!fTPHelper.DeleteFile(cur.current120))
    176             {
    177                 throw new BizException("删除120*120图片失败");
    178             }
    179 
    180             //180*180
    181             if (!fTPHelper.DeleteFile(cur.current180))
    182             {
    183                 throw new BizException("删除180*180图片失败");
    184             }
    185 
    186             //200*200
    187             if (!fTPHelper.DeleteFile(cur.current200))
    188             {
    189                 throw new BizException("删除200*200图片失败");
    190             }
    191 
    192             //240*240
    193             if (!fTPHelper.DeleteFile(cur.current240))
    194             {
    195                 throw new BizException("删除240*240图片失败");
    196             }
    197 
    198             //460*460
    199             if (!fTPHelper.DeleteFile(cur.current460))
    200             {
    201                 throw new BizException("删除460*460图片失败");
    202             }
    203 
    204         }
    205 
    206 /// <summary>
    207     /// 配置文件获取ftp路径
    208     /// </summary>
    209     public class ftpProductPath
    210     {
    211         //图片保存路径,作为拼接图片路径用
    212         public string ftpProductPath50 = AppConfig.FTPProductPath50;//原图保存路径
    213         public string ftpProductPath100 = AppConfig.FTPProductPath100;//色图
    214         public string ftpProductPath160 = AppConfig.FTPProductPath160;//小图
    215         public string ftpProductPath330 = AppConfig.FTPProductPath330;//中图
    216         public string ftpProductPath800 = AppConfig.FTPProductPath800;//大图
    217 
    218         public string ftpProductPath60 = AppConfig.FTPProductPath60;
    219         public string ftpProductPath80 = AppConfig.FTPProductPath80;
    220         public string ftpProductPath120 = AppConfig.FTPProductPath120;
    221         public string ftpProductPath180 = AppConfig.FTPProductPath180;
    222         public string ftpProductPath200 = AppConfig.FTPProductPath200;
    223         public string ftpProductPath240 = AppConfig.FTPProductPath240;
    224         public string ftpProductPath460 = AppConfig.FTPProductPath460;
    225     }
    226     /// <summary>
    227     /// 图片名称类
    228     /// </summary>
    229     public class Img
    230     {
    231         public Img()
    232         {
    233             img50 = AppConst.StringNull;//原图保存路径
    234             img100 = AppConst.StringNull;//色图
    235             img160 = AppConst.StringNull;//小图
    236             img330 = AppConst.StringNull;//中图
    237             img800 = AppConst.StringNull;//大图
    238             img60 = AppConst.StringNull;
    239             img80 = AppConst.StringNull;
    240             img120 = AppConst.StringNull;
    241             img180 = AppConst.StringNull;
    242             img200 = AppConst.StringNull;
    243             img240 = AppConst.StringNull;
    244             img460 = AppConst.StringNull;
    245         }
    246 
    247         public Img(int i, int deleteprodcutid)
    248         {
    249             if (i == 1)
    250             {
    251                 img50 = deleteprodcutid + ".jpg";
    252                 img100 = deleteprodcutid + ".jpg";
    253                 img160 = deleteprodcutid + ".jpg";
    254                 img330 = deleteprodcutid + ".jpg";
    255                 img800 = deleteprodcutid + ".jpg";
    256 
    257                 img60 = deleteprodcutid + ".jpg";
    258                 img80 = deleteprodcutid + ".jpg";
    259                 img120 = deleteprodcutid + ".jpg";
    260                 img180 = deleteprodcutid + ".jpg";
    261                 img200 = deleteprodcutid + ".jpg";
    262                 img240 = deleteprodcutid + ".jpg";
    263                 img460 = deleteprodcutid + ".jpg";
    264             }
    265             else
    266             {
    267                 img50 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    268                 img100 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    269                 img160 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    270                 img330 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    271                 img800 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    272 
    273                 img60 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    274                 img80 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    275                 img120 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    276                 img180 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    277                 img200 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    278                 img240 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    279                 img460 = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    280             }
    281         }
    282 
    283         public string img50;//原图保存路径
    284         public string img100;//色图
    285         public string img160;//小图
    286         public string img330;//中图
    287         public string img800;//大图
    288         public string img60;
    289         public string img80;
    290         public string img120;
    291         public string img180;
    292         public string img200;
    293         public string img240;
    294         public string img460;
    295     }
    296 
    297     /// <summary>
    298     /// 当前图片(带路径)
    299     /// </summary>
    300     public class Current
    301     {
    302         public Current()
    303         {
    304             current50 = AppConst.StringNull;
    305             current100 = AppConst.StringNull;
    306             current160 = AppConst.StringNull;
    307             current330 = AppConst.StringNull;
    308             current800 = AppConst.StringNull;
    309             current60 = AppConst.StringNull;
    310             current80 = AppConst.StringNull;
    311             current120 = AppConst.StringNull;
    312             current180 = AppConst.StringNull;
    313             current200 = AppConst.StringNull;
    314             current240 = AppConst.StringNull;
    315             current460 = AppConst.StringNull;
    316         }
    317 
    318         public Current(ftpProductPath ftpPath, Img img)
    319         {
    320             current50 = ftpPath.ftpProductPath50 + img.img50;
    321             current100 = ftpPath.ftpProductPath100 + img.img100;
    322             current160 = ftpPath.ftpProductPath160 + img.img160;
    323             current330 = ftpPath.ftpProductPath330 + img.img330;
    324             current800 = ftpPath.ftpProductPath800 + img.img800;
    325 
    326             current60 = ftpPath.ftpProductPath60 + img.img60;
    327             current80 = ftpPath.ftpProductPath80 + img.img80;
    328             current120 = ftpPath.ftpProductPath120 + img.img120;
    329             current180 = ftpPath.ftpProductPath180 + img.img180;
    330             current200 = ftpPath.ftpProductPath200 + img.img200;
    331             current240 = ftpPath.ftpProductPath240 + img.img240;
    332             current460 = ftpPath.ftpProductPath460 + img.img460;
    333         }
    334 
    335         public string current50;
    336         public string current100;
    337         public string current160;
    338         public string current330;
    339         public string current800;
    340         public string current60;
    341         public string current80;
    342         public string current120;
    343         public string current180;
    344         public string current200;
    345         public string current240;
    346         public string current460;
    347     }
    348     
    349     /// <summary>
    350     /// 最后一张图片(带路径)
    351     /// </summary>
    352     public class Last
    353     {
    354         public Last()
    355         {
    356             last50 = AppConst.StringNull;
    357             last100 = AppConst.StringNull;
    358             last160 = AppConst.StringNull;
    359             last330 = AppConst.StringNull;
    360             last800 = AppConst.StringNull;
    361 
    362             last60 = AppConst.StringNull;
    363             last80 = AppConst.StringNull;
    364             last120 = AppConst.StringNull;
    365             last180 = AppConst.StringNull;
    366             last200 = AppConst.StringNull;
    367             last240 = AppConst.StringNull;
    368             last460 = AppConst.StringNull;
    369         }
    370 
    371         public Last(ftpProductPath ftp, int MultiPicNum, int deleteprodcutid)
    372         {
    373             last50 = ftp.ftpProductPath50 + (MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    374             last100 = ftp.ftpProductPath100 + (MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    375             last160 = ftp.ftpProductPath160 + (MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    376             last330 = ftp.ftpProductPath330 + (MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    377             last800 = ftp.ftpProductPath800 + (MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    378 
    379             last60 = ftp.ftpProductPath60 + (MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    380             last80 = ftp.ftpProductPath80 + (MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    381             last120 = ftp.ftpProductPath120 + (MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    382             last180 = ftp.ftpProductPath180 + (MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    383             last200 = ftp.ftpProductPath200 + (MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    384             last240 = ftp.ftpProductPath240 + (MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    385             last460 = ftp.ftpProductPath460 + (MultiPicNum - 1).ToString() + "_" + deleteprodcutid + ".jpg";
    386         }
    387 
    388         public string last50;
    389         public string last100;
    390         public string last160;
    391         public string last330;
    392         public string last800;
    393 
    394         public string last60;
    395         public string last80;
    396         public string last120;
    397         public string last180;
    398         public string last200;
    399         public string last240;
    400         public string last460;
    401     }
    View Code

    然后发现还可以进一步压缩,使用属性类换成数组,使用循环删除重命名,重构得到代码如下

      1   try
      2                         {
      3                             //图片保存路径,作为拼接图片路径用,顺序不能变
      4                             string[] ftpPathArray ={AppConfig.FTPProductPath50,AppConfig.FTPProductPath100,AppConfig.FTPProductPath160,
      5                                                         AppConfig.FTPProductPath330,AppConfig.FTPProductPath800,AppConfig.FTPProductPath60,
      6                                                    AppConfig.FTPProductPath80,AppConfig.FTPProductPath120,AppConfig.FTPProductPath180,
      7                                                    AppConfig.FTPProductPath200,AppConfig.FTPProductPath240,AppConfig.FTPProductPath460 };
      8 
      9                             int deleteprodcutid = Util.TrimIntNull(context.Request["sysno"]);
     10                             int i = int.Parse(context.Request["i"]);
     11                             ProductBasicInfo deleteproductInfo =
     12                                 Icson.BLL.Basic.ProductManager.GetInstance().LoadBasic(deleteprodcutid);
     13 
     14                             //FTP服务器用户名和密码
     15                             string ftpUserName = AppConfig.FTPUserName;
     16                             string ftpPassword = AppConfig.FTPPassword;
     17 
     18                             FTPHelper fTPHelper = new FTPHelper(AppConfig.FTPProductPath50, ftpUserName, ftpPassword);
     19 
     20                             //当前图片名称
     21                             string imgStr;
     22                             if (i == 1)
     23                             {
     24                                 imgStr = deleteprodcutid + ".jpg";
     25                             }
     26                             else
     27                             {
     28                                 imgStr = (i - 1).ToString() + "_" + deleteprodcutid + ".jpg";
     29                             }
     30 
     31                             //当前图片和路径
     32                             string[] curArray = CurrentArray(ftpPathArray, imgStr);
     33 
     34                             //直接delete图片
     35                             DeleteAllFile(curArray, fTPHelper);
     36 
     37                             if (i != deleteproductInfo.MultiPicNum)
     38                             {
     39                                 //不是最后一张,则把最后一张的图片重命名为删掉的图片名称。
     40                                 //最后一张图片和路径
     41                                 string[] lastArray = LastArray(ftpPathArray, deleteproductInfo.MultiPicNum, deleteprodcutid.ToString());
     42 
     43                                 RenameAllFileName(imgStr, lastArray, fTPHelper);
     44                             }
     45 
     46                             deleteproductInfo.MultiPicNum--;
     47                             ProductManager.GetInstance().UpdateBasicInfo(deleteproductInfo);
     48 
     49                             result = "success";
     50                         }
     51                         catch (Exception ex)
     52                         {
     53                             result = "error";
     54                         }
     55 
     56  /// <summary>
     57         /// 当前图片带路径 数组表示
     58         /// </summary>
     59         /// <param name="ftpPathArray"></param>
     60         /// <param name="imgStr"></param>
     61         /// <returns></returns>
     62         public string[] CurrentArray(string[] ftpPathArray, string imgStr)
     63         {
     64             for (int i = 0; i < ftpPathArray.Length; i++)
     65             {
     66                 ftpPathArray[i] = ftpPathArray[i] + imgStr;
     67             }
     68             return ftpPathArray;
     69         }
     70         /// <summary>
     71         /// 最后一张图片带路径 数组表示
     72         /// </summary>
     73         /// <param name="ftpPathArray"></param>
     74         /// <param name="MultiPicNum"></param>
     75         /// <param name="deleteprodcutid"></param>
     76         /// <returns></returns>
     77         public string[] LastArray(string[] ftpPathArray, int MultiPicNum, string deleteprodcutid)
     78         {
     79             string picNum = (MultiPicNum - 1).ToString();
     80             for (int i = 0; i < ftpPathArray.Length; i++)
     81             {
     82                 ftpPathArray[i] = ftpPathArray[i] + picNum.ToString() + "_" + deleteprodcutid + ".jpg";
     83             }
     84             return ftpPathArray;
     85         }
     86         /// <summary>
     87         /// 重命名图片
     88         /// </summary>
     89         /// <param name="img">命名后名称组</param>
     90         /// <param name="last">命名前名称组</param>
     91         /// <param name="fTPHelper">ftp帮助类</param>
     92         public void RenameAllFileName(string imgStr, string[] lastArray, FTPHelper fTPHelper)
     93         {
     94             for (int i = 0; i < lastArray.Length; i++)
     95             {
     96                 if (!fTPHelper.RenameFileName(imgStr, lastArray[i]))
     97                 {
     98                     throw new BizException("重命名失败:" + lastArray[i]);
     99                 }
    100             }
    101         }
    102 
    103         /// <summary>
    104         /// 删除图片
    105         /// </summary>
    106         /// <param name="curArray">要删除的图片(带路径)</param>
    107         /// <param name="fTPHelper">ftp帮助类</param>
    108         public void DeleteAllFile(string[] curArray, FTPHelper fTPHelper)
    109         {
    110             for (int i = 0; i < curArray.Length; i++)
    111             {
    112                 if (!fTPHelper.DeleteFile(curArray[i]))
    113                 {
    114                     throw new BizException("删除失败:" + curArray[i]);
    115                 }
    116             }
    117         }
    View last code


    成功地从500行压缩到120行,而且逻辑清晰,也不会漏写代码了

    总结,基础要打好,用哪种数组,还是对象属性类,是否能迭代,内部运行机制是什么,都有区分。

    这里是固定的长度,不需要排序,所以直接用数组比较好。

  • 相关阅读:
    利用Masscan批量生成随机ip地址表
    国光大力推荐(安利)Deepin15.4
    开启你的kali linux DIY之旅
    Docker入门简明教程
    Deepin Linux下的Metasploit安装及优化
    Hexo博客配置笔记
    Mac OS下开启自带的apache服务
    PentestBox简明使用教程
    Pentester中的XSS详解
    信息收集之网站镜像克隆
  • 原文地址:https://www.cnblogs.com/tobase/p/3088602.html
Copyright © 2011-2022 走看看