我习惯把代码写得很整齐

我喜欢写C#代码,不喜欢写JS代码和SQL。相比于C#,总感觉JS和SQL写起来乱糟糟的。

最近做了几个统计页面,不得不拼接很长的SQL,我总是尽量把SQL写的层次分明,容易阅读。

应该还有别的实现方法,比如在数据库中写存储过程、函数,但是我不想维护数据库,在当前数据库中没有什么存储过程和函数的情况下,我总是尽量避免在数据库中写存储过程和函数。

别人实现复杂的统计功能的时候,是不是也是这么做的呢?有什么更好的方法?

using System;
using System.Collections.Generic;
using System.Text;
using Demo.Service.Interface.Admin.Report;
using Demo.Domain.Admin;
using Demo.Const;
using Simpo;

namespace Demo.Service.Admin.Report
{
    /// <summary>
    /// 招生业绩趋势
    /// </summary>
    public class PerformanceTrendService : Service, IPerformanceTrendService
    {
        #region 统计招生业绩趋势
        /// <summary>
        /// 统计招生业绩趋势
        /// </summary>
        /// <param name="schoolAreaId">校区ID</param>
        /// <param name="deptIds">部门ID集合</param>
        /// <param name="employeeUserId">员工ID</param>
        /// <param name="startYear">开始年份</param>
        /// <param name="endYear">结束年份,若为-1,则按月统计</param>
        public List<PerformanceTrend> GetList_Trend(int schoolAreaId, string deptIds, int employeeUserId, int startYear, int endYear)
        {
            StringBuilder sb = new StringBuilder();

            //按年度月度
            if (endYear == Constants.OptionAllVal)//按月度
            {
                sb.Append(string.Format(@"
                    select _Year, _Month,
                    count(CustomerId) as DJKH,
                    sum(IsStudent) as CJKH,

                    (select count(cfr1.Id)
                    from FM_CustomerFollowRecord cfr1
                    join FM_Customer cus1 on cfr1.CustomerId=cus1.Id
                    join Sys_User u1 on cus1.RegistUserId=u1.Id
                    join FM_Employee emp1 on u1.RelationId=emp1.Id
                    join FM_Dept dpt1 on emp1.DeptId=dpt1.Id
                    join FM_SchoolArea scl1 on dpt1.SchoolId=scl1.Id
                    where year(cus1.RegistTime)=t._Year
                    and month(cus1.RegistTime)=t._Month
                    {0} {1} {2} ) as GJCS,

                    ((select sum(psf2.PayAmount)
                    from FM_PayStepFlow psf2
                    join FM_PayFlow pf2 on psf2.PayFlowId=pf2.Id
                    join FM_Customer cus2 on pf2.CustomerCode=cus2.Code
                    join Sys_User u2 on cus2.RegistUserId=u2.Id
                    join FM_Employee emp2 on u2.RelationId=emp2.Id
                    join FM_Dept dpt2 on emp2.DeptId=dpt2.Id
                    join FM_SchoolArea scl2 on dpt2.SchoolId=scl2.Id
                    where psf2.DelFlg<>1
                    and year(cus2.RegistTime)=t._Year
                    and month(cus2.RegistTime)=t._Month
                    {3} {4} {5} ) - 

                    ISNULL((select sum(rf3.RefundAmount)
                    from FM_RefundFlow rf3
                    join FM_Customer cus3 on rf3.CustomerCode=cus3.Code
                    join Sys_User u3 on cus3.RegistUserId=u3.Id
                    join FM_Employee emp3 on u3.RelationId=emp3.Id
                    join FM_Dept dpt3 on emp3.DeptId=dpt3.Id
                    join FM_SchoolArea scl3 on dpt3.SchoolId=scl3.Id
                    where rf3.DelFlg<>1 and rf3.SettleFlg=1
                    and year(cus3.RegistTime)=t._Year
                    and month(cus3.RegistTime)=t._Month
                    {6} {7} {8} ),0)) as CJE

                    from ( ",
                    schoolAreaId != Constants.OptionAllVal ? " and cus1.SchoolAreaId=" + schoolAreaId : "",
                    strUtil.IsNullOrEmpty(deptIds) ? "" : " and dpt1.Id in (" + deptIds + ")",
                    employeeUserId != Constants.OptionAllVal ? " and cus1.RegistUserId=" + employeeUserId : "",

                    schoolAreaId != Constants.OptionAllVal ? " and pf2.SchoolAreaId=" + schoolAreaId : "",
                    strUtil.IsNullOrEmpty(deptIds) ? "" : " and dpt2.Id in (" + deptIds + ")",
                    employeeUserId != Constants.OptionAllVal ? " and cus2.RegistUserId=" + employeeUserId : "",

                    schoolAreaId != Constants.OptionAllVal ? " and rf3.SchoolAreaId=" + schoolAreaId : "",
                    strUtil.IsNullOrEmpty(deptIds) ? "" : " and dpt3.Id in (" + deptIds + ")",
                    employeeUserId != Constants.OptionAllVal ? " and cus3.RegistUserId=" + employeeUserId : ""));
            }
            else//按年度
            {
                sb.Append(string.Format(@"select _Year,
                    count(CustomerId) as DJKH,
                    sum(IsStudent) as CJKH,

                    (select count(cfr1.Id)
                    from FM_CustomerFollowRecord cfr1
                    join FM_Customer cus1 on cfr1.CustomerId=cus1.Id
                    join Sys_User u1 on cus1.RegistUserId=u1.Id
                    join FM_Employee emp1 on u1.RelationId=emp1.Id
                    join FM_Dept dpt1 on emp1.DeptId=dpt1.Id
                    join FM_SchoolArea scl1 on dpt1.SchoolId=scl1.Id
                    where year(cus1.RegistTime)=t._Year
                    {0} {1} {2} ) as GJCS,

                    ((select sum(psf2.PayAmount)
                    from FM_PayStepFlow psf2
                    join FM_PayFlow pf2 on psf2.PayFlowId=pf2.Id
                    join FM_Customer cus2 on pf2.CustomerCode=cus2.Code
                    join Sys_User u2 on cus2.RegistUserId=u2.Id
                    join FM_Employee emp2 on u2.RelationId=emp2.Id
                    join FM_Dept dpt2 on emp2.DeptId=dpt2.Id
                    join FM_SchoolArea scl2 on dpt2.SchoolId=scl2.Id
                    where psf2.DelFlg<>1
                    and year(cus2.RegistTime)=t._Year
                    {3} {4} {5} ) - 

                    ISNULL((select sum(rf3.RefundAmount)
                    from FM_RefundFlow rf3
                    join FM_Customer cus3 on rf3.CustomerCode=cus3.Code
                    join Sys_User u3 on cus3.RegistUserId=u3.Id
                    join FM_Employee emp3 on u3.RelationId=emp3.Id
                    join FM_Dept dpt3 on emp3.DeptId=dpt3.Id
                    join FM_SchoolArea scl3 on dpt3.SchoolId=scl3.Id
                    where rf3.DelFlg<>1 and rf3.SettleFlg=1
                    and year(cus3.RegistTime)=t._Year
                    {6} {7} {8} ),0)) as CJE

                    from ( ",
                    schoolAreaId != Constants.OptionAllVal ? " and cus1.SchoolAreaId=" + schoolAreaId : "",
                    strUtil.IsNullOrEmpty(deptIds) ? "" : " and dpt1.Id in (" + deptIds + ")",
                    employeeUserId != Constants.OptionAllVal ? " and cus1.RegistUserId=" + employeeUserId : "",

                    schoolAreaId != Constants.OptionAllVal ? " and pf2.SchoolAreaId=" + schoolAreaId : "",
                    strUtil.IsNullOrEmpty(deptIds) ? "" : " and dpt2.Id in (" + deptIds + ")",
                    employeeUserId != Constants.OptionAllVal ? " and cus2.RegistUserId=" + employeeUserId : "",

                    schoolAreaId != Constants.OptionAllVal ? " and rf3.SchoolAreaId=" + schoolAreaId : "",
                    strUtil.IsNullOrEmpty(deptIds) ? "" : " and dpt3.Id in (" + deptIds + ")",
                    employeeUserId != Constants.OptionAllVal ? " and cus3.RegistUserId=" + employeeUserId : ""));
            }

            sb.Append(string.Format(@"
                select
                year(cus.RegistTime) as _Year,
                month(cus.RegistTime) as _Month,
                cus.Id as CustomerId,
                cus.IsStudent as IsStudent

                from FM_Customer cus

                left join Sys_User u on cus.RegistUserId=u.Id
                left join FM_Employee emp on u.RelationId=emp.Id
                left join FM_Dept dpt on emp.DeptId=dpt.Id
                left join FM_SchoolArea scl on dpt.SchoolId=scl.Id

                where cus.DelFlg<>{0}", (int)Flag.Yes, DictType.CD08));

            // 校区
            if (schoolAreaId != Constants.OptionAllVal)
            {
                sb.Append(string.Format(" and cus.SchoolAreaId={0} ", schoolAreaId));
            }
            // 部门
            if (!strUtil.IsNullOrEmpty(deptIds))
            {
                sb.Append(string.Format(" and dpt.Id in ({0}) ", deptIds));
            }
            // 登记人
            if (employeeUserId != Constants.OptionAllVal)
            {
                sb.Append(string.Format(" and cus.RegistUserId={0} ", employeeUserId));
            }

            sb.Append(" ) t ");

            //按年度月度
            if (endYear == Constants.OptionAllVal)//按月度
            {
                sb.Append(string.Format(" where t._Year={0} group by t._Year, t._Month ", startYear));
            }
            else//按年度
            {
                sb.Append(" group by t._Year ");
            }

            return this.FindListBySql<PerformanceTrend>(sb.ToString());
        }
        #endregion

        #region 统计招生业绩排行
        /// <summary>
        /// 统计招生业绩排行
        /// </summary>
        /// <param name="schoolAreaId">校区ID</param>
        /// <param name="deptIds">部门ID集合</param>
        /// <param name="employeeUserId">员工ID</param>
        /// <param name="startDate">统计时段开始</param>
        /// <param name="endDate">统计时段结束</param>
        public List<PerformanceRank> GetList_Rank(int schoolAreaId, string deptIds, int employeeUserId, string startDate, string endDate)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(string.Format(@"
                select
                emp.Name as EmployeeName,
                dpt.Name as DeptName,
                count(cus.Id) as DJKH,
                sum(cus.IsStudent) as CJKH,

                (select count(cfr1.Id)
                from FM_CustomerFollowRecord cfr1
                join FM_Customer cus1 on cfr1.CustomerId=cus1.Id
                join Sys_User u1 on cus1.RegistUserId=u1.Id
                join FM_Employee emp1 on u1.RelationId=emp1.Id
                join FM_Dept dpt1 on emp1.DeptId=dpt1.Id
                join FM_SchoolArea scl1 on dpt1.SchoolId=scl1.Id
                where cus1.RegistUserId=cus.RegistUserId
                and cus1.RegistTime>=‘{1}‘
                and cus1.RegistTime<=‘{2}‘
                {3} {4} {5} ) as GJCS,

                ((select sum(psf2.PayAmount)
                from FM_PayStepFlow psf2
                join FM_PayFlow pf2 on psf2.PayFlowId=pf2.Id
                join FM_Customer cus2 on pf2.CustomerCode=cus2.Code
                join Sys_User u2 on cus2.RegistUserId=u2.Id
                join FM_Employee emp2 on u2.RelationId=emp2.Id
                join FM_Dept dpt2 on emp2.DeptId=dpt2.Id
                join FM_SchoolArea scl2 on dpt2.SchoolId=scl2.Id
                where psf2.DelFlg<>1
                and cus2.RegistUserId=cus.RegistUserId
                and cus2.RegistTime>=‘{1}‘
                and cus2.RegistTime<=‘{2}‘
                {6} {7} {8} ) - 

                ISNULL((select sum(rf3.RefundAmount)
                from FM_RefundFlow rf3
                join FM_Customer cus3 on rf3.CustomerCode=cus3.Code
                join Sys_User u3 on cus3.RegistUserId=u3.Id
                join FM_Employee emp3 on u3.RelationId=emp3.Id
                join FM_Dept dpt3 on emp3.DeptId=dpt3.Id
                join FM_SchoolArea scl3 on dpt3.SchoolId=scl3.Id
                where rf3.DelFlg<>1 and rf3.SettleFlg=1
                and cus3.RegistUserId=cus.RegistUserId
                and cus3.RegistTime>=‘{1}‘
                and cus3.RegistTime<=‘{2}‘
                {9} {10} {11} ),0)) as CJE

                from FM_Customer cus

                left join Sys_User u on cus.RegistUserId=u.Id
                left join FM_Employee emp on u.RelationId=emp.Id
                left join FM_Dept dpt on emp.DeptId=dpt.Id
                left join FM_SchoolArea scl on dpt.SchoolId=scl.Id

                where cus.DelFlg<>{0}
                and cus.RegistTime>=‘{1}‘
                and cus.RegistTime<=‘{2}‘", (int)Flag.Yes, startDate + " 00:00:00", endDate + " 23:59:59",
                schoolAreaId != Constants.OptionAllVal ? " and cus1.SchoolAreaId=" + schoolAreaId : "",
                strUtil.IsNullOrEmpty(deptIds) ? "" : " and dpt1.Id in (" + deptIds + ")",
                employeeUserId != Constants.OptionAllVal ? " and cus1.RegistUserId=" + employeeUserId : "",

                schoolAreaId != Constants.OptionAllVal ? " and pf2.SchoolAreaId=" + schoolAreaId : "",
                strUtil.IsNullOrEmpty(deptIds) ? "" : " and dpt2.Id in (" + deptIds + ")",
                employeeUserId != Constants.OptionAllVal ? " and cus2.RegistUserId=" + employeeUserId : "",

                schoolAreaId != Constants.OptionAllVal ? " and rf3.SchoolAreaId=" + schoolAreaId : "",
                strUtil.IsNullOrEmpty(deptIds) ? "" : " and dpt3.Id in (" + deptIds + ")",
                employeeUserId != Constants.OptionAllVal ? " and cus3.RegistUserId=" + employeeUserId : ""));

            // 校区
            if (schoolAreaId != Constants.OptionAllVal)
            {
                sb.Append(string.Format(" and cus.SchoolAreaId={0} ", schoolAreaId));
            }
            // 部门
            if (!strUtil.IsNullOrEmpty(deptIds))
            {
                sb.Append(string.Format(" and dpt.Id in({0}) ", deptIds));
            }
            // 登记人
            if (employeeUserId != Constants.OptionAllVal)
            {
                sb.Append(string.Format(" and cus.RegistUserId={0} ", employeeUserId));
            }

            sb.Append(string.Format(" group by cus.RegistUserId, emp.Name, dpt.Name ", startDate));

            return this.FindListBySql<PerformanceRank>(sb.ToString());
        }
        #endregion

        #region 来源途径分析
        /// <summary>
        /// 来源途径分析
        /// </summary>
        /// <param name="schoolAreaId">校区ID</param>
        /// <param name="stuOrCus">类型</param>
        /// <param name="startDate">统计时段开始</param>
        /// <param name="endDate">统计时段结束</param>
        public List<StuCusSource> GetList_Source(int schoolAreaId, int stuOrCus, string startDate, string endDate)
        {
            StringBuilder sb = new StringBuilder();

            if (stuOrCus == (int)StuOrCus.Customer) //客户
            {
                sb.Append(string.Format(@"
                    select
                    dict.Name as Source,
                    count(cus.Id) as Num

                    from FM_Customer cus

                    join FM_SchoolArea scl on scl.Id=cus.SchoolAreaId
                    join Sys_Dict dict on dict.Type=‘{3}‘ and dict.Code=cus.Source

                    where cus.DelFlg<>{0}
                    and cus.RegistTime>=‘{1}‘
                    and cus.RegistTime<=‘{2}‘", (int)Flag.Yes, startDate + " 00:00:00", endDate + " 23:59:59", DictType.CD02));
            }
            else //学员
            {
                sb.Append(string.Format(@"
                    select
                    dict.Name as Source,
                    count(stu.Id) as Num

                    from FM_Student stu

                    join FM_SchoolArea scl on scl.Id=stu.SchoolAreaId
                    join Sys_Dict dict on dict.Type=‘{3}‘ and dict.Code=stu.Source

                    where stu.DelFlg<>{0}
                    and stu.RegistTime>=‘{1}‘
                    and stu.RegistTime<=‘{2}‘", (int)Flag.Yes, startDate + " 00:00:00", endDate + " 23:59:59", DictType.CD02));
            }

            // 校区
            if (schoolAreaId != Constants.OptionAllVal)
            {
                sb.Append(string.Format(" and scl.Id={0} ", schoolAreaId));
            }

            sb.Append(" group by dict.Name");

            return this.FindListBySql<StuCusSource>(sb.ToString());
        }
        #endregion

    }
}

我习惯把代码写得很整齐

时间: 2024-10-13 23:00:25

我习惯把代码写得很整齐的相关文章

当程序员说“这代码写的可真烂”,他们的意思是“这烂代码不是我写的”。而当他们说这段代码有些“小问题”时,很可能这代码是他们自己写的

英文原文:What Programmers Say vs. What They Mean 你是否听到过同事说“这段代码不言自明”?你的同事的这句话的实际意思是这段代码不需要写注释. 你也许注意到了,很多时候,程序员所说的话的字面意思和其真实的意思是完全不同的.不用惊异,下面你将很快知道这些暧昧的短语和其深层次的意思都是什么. 最近 Imgur 上出现了一张图片,里面列举的程序员的一些专业术语和其含义,它能很好的帮助你理解这些话的真实意思.这里是对其中的精华进行的总结. 典型的程序员之间的对话 当

牢骚 - 你代码写得丑,又不肯用好一点的IDE,这让我很为难啊。

又有人问我代码错误,发过来就是一篇巨丑无比的代码,先不说左大括号转行还和代码写在同一行的谭浩强风格,你这狗啃的一样的缩进是闹哪样!粘进VS2015里面,自动格式化,瞬间赏心悦目,编译错误出了5行,我直接粘给他,他说这些都没有错啊,然后发给了我一个VC6编译错误的截图,错误足是VS2015的三倍多,我定睛一看,MB,少写了一个右大括号,怪不得这么多莫名其妙的错,改过来之后,又是未初始化变量又是scanf少参数,反正都是VC6查不出的错呗,直接粘给他,免得之后运行出错又过来问我,我才不管这些由于用了

看别人写的优秀代码,是对自己的一种提高,看别人写的很恶心的代码,对自己也是一种提高:告诉自己不要这样写(转)

这两天,我做了两件事: 1.重构了系统某个模块的部分代码: 花了一天时间,一个6k多行的java文件,搞到4k行加若干个类文件,恕我能力有限,后面的实在重构不下去了,那是一种3个domain属性名几乎一样100多个字段但是却用同一个copy了三遍的方法来处理的欲哭无泪,那是一种使劲滚鼠标滚轮都滚不到一个方法尾部的绝望(100多个字段的几个类属性equals来,equals去,get来,set去的,这样类型的方法有那么五六个,你说能不多吗)...... 2.做了一个日志处理的小工具: 客户要求把日

我所习惯的代码规范

其实代码规范不是一朝一夕就能养成习惯的,都是通过多敲多练习自己摸索出来的,也是个人习惯而已. 我所习惯的代码规范有以下几条: 1. 首先是空行. 空行可以使自己的代码更加清晰,有理有据,给其他人看的时候也是可以看得明白的,而且自己在找bug的时候,都是可以非常清楚地看出自己的错误,并且及时改正. 规则一:定义变量后要空行.尽可能在定义变量的同时初始化该变量,即遵循就近原则.如果变量的引用和定义相隔比较远,那么变量的初始化就很容易被忘记.若引用了未被初始化的变量,就会导致程序出错. 规则二:每个函

你加班太多是因为你的代码写的烂

今天看见一篇不错的文章,给大家分享一下 作为一名程序员,我渴望我加入的应该要是一支"30%的时间在写代码,而70%的时间在喝着咖啡讨论着如何将产品做好"的团队.我觉得软件工作应该成为一项技术和艺术融合的高智力活动,我们的项目经理应该是一个高度理解质量.范围和进度客观规律的明白人,"高效工作,快乐生活"才应该是我们的座右铭. 可现实情况却是,团队在一边超负荷的做着需求,一边改着没完没了的Bug.过点前夕,项目经理熬着通红通红的眼睛盯着我们整晚整晚的加班,质量专员一遍一

ios 用纯代码写程序的时候,navigationController的导航栏的设置

我们都知道,如果用storyBoard设置导航栏很容易,点击左右item的时候,进入下一个界面,导航栏的颜色是跟上一层的是一样的,用纯代码写的时候,可以在当前控制器,和从当前控制器进入到下一个控制器都用代码实现对导航栏的控制,但是,每次都写代码设置,很麻烦,所以,可以这样: 创建一个MainTabBarController的类,在Appdelegate.m里面完成: - (BOOL)application:(UIApplication *)application didFinishLaunchi

开发(ASP.NET程序)把写代码写至最有面向对象味道

今天看了条博文,瞬间又勾起了对设计模式的兴趣.虽然掌握的不多,但看一个学一个总归是好的.一下是文章中的内容和整理. 首先看原始代码 1 public void show2(string day) 2 { 3 string[] str = new string[] { "09:00", "11:00", "12:00", "13:00", "14:00", "15:00", "1

用代码&quot;写&quot;出扫描线效果图片

我们一般采用photoshop等做图工具制作电视扫描线效果图片:首先做一个黑白相间的图案,然后用这个图案进行填充,再调整图层的模式或者透明度,效果就出来了. 现在我们不用photoshop,用css和javascript来做,方法也很简单! 步骤一. 我们先准备一张图片,以我的图片为例,命名为”青山绿水.jpg”,图片大小为1000X543. 步骤二. 在body内插入一个table表格,为表格设置class属性,值为”tvline”,设置表格的背景图片为事先准备的图片. 步骤三. 建立一个cs

Unity CG 写一个超酷的 ray-marching(shader纯代码写3D)

Unity CG 写一个超酷的 ray-marching(shader纯代码写3D) 1.其实自从看了http://www.shadertoy.com(inigo quilez为其主创始人)上的shader后,让我感到很高兴 2.更重要的是自从我接触了一个叫 inigo quilez 的shader技术后,让我觉得shader情感更深的浓厚了 3.http://www.iquilezles.org/ 哈哈,当然给大家一个崇拜的机会吧,你一定会学到你想学到的技术和秘密 哈哈,邪恶的专栏地址放送,一