Browse Source

计算积分的方法

sugangqiang 2 years ago
parent
commit
e8f95c81d9

+ 1 - 0
app/admin/view/integral_mgr/save_item.html

@@ -52,6 +52,7 @@
                                     <option value="">请选择</option>
                                     <option value="">请选择</option>
                                     <option value="1">仅首次可获积分</option>
                                     <option value="1">仅首次可获积分</option>
                                     <option value="2">新增可获积分</option>
                                     <option value="2">新增可获积分</option>
+                                    <option value="3">新增可获积分(较上年度)</option>
                                 </select>
                                 </select>
                             </div>
                             </div>
                             <label class="col-sm-2 control-label spacing">计量单位</label>
                             <label class="col-sm-2 control-label spacing">计量单位</label>

+ 6 - 2
app/common/api/IntegralItemApi.php

@@ -7,7 +7,7 @@ use app\common\model\IntegralItem;
 class IntegralItemApi {
 class IntegralItemApi {
 
 
     public static function getOne($id) {
     public static function getOne($id) {
-        return IntegralItem::alias("i")->leftJoin("new_integral_project p", "p.id=i.projectId")->field("i.*,p.type,if(stepNeedAmount > 0,2,1) as plan")->findOrEmpty($id);
+        return IntegralItem::alias("i")->leftJoin("new_integral_project p", "p.id=i.projectId")->field("i.*,p.type,if(stepNeedAmount > 0,if(stepIsYear=1,3,2),1) as plan")->findOrEmpty($id);
     }
     }
 
 
     public static function getList($params) {
     public static function getList($params) {
@@ -49,9 +49,13 @@ class IntegralItemApi {
         $data["fstGainPoints"] = $params["fstGainPoints"];
         $data["fstGainPoints"] = $params["fstGainPoints"];
         $data["fileTypeId"] = $params["fileTypeId"] ? implode(",", $params["fileTypeId"]) : null;
         $data["fileTypeId"] = $params["fileTypeId"] ? implode(",", $params["fileTypeId"]) : null;
         $data["active"] = $params["active"];
         $data["active"] = $params["active"];
-        if ($params["plan"] == 2) {
+        $data["stepIsYear"] = 0; //通用,可以当作较上次新增
+        if (in_array($params["plan"], [2, 3])) {
             $data["stepNeedAmount"] = $params["stepNeedAmount"];
             $data["stepNeedAmount"] = $params["stepNeedAmount"];
             $data["stepGainPoints"] = $params["stepGainPoints"];
             $data["stepGainPoints"] = $params["stepGainPoints"];
+            if ($params["plan"] == 3) {
+                $data["stepIsYear"] = 1; //较上年度新增
+            }
         } else {
         } else {
             $data["stepNeedAmount"] = 0;
             $data["stepNeedAmount"] = 0;
             $data["stepGainPoints"] = 0;
             $data["stepGainPoints"] = 0;

+ 92 - 20
app/common/api/IntegralRecordApi.php

@@ -103,14 +103,17 @@ class IntegralRecordApi {
         return $info;
         return $info;
     }
     }
 
 
-    public static function calIntegral($enterprise_id, $cardType, $cardNumber, $itemId) {
+    public static function calIntegral($enterprise_id, $cardType, $cardNumber, $itemId, $amount) {
+        $returnObj = new \stdClass();
+        $returnObj->amount = $amount;
         $item = getCacheById("IntegralItem", $itemId);
         $item = getCacheById("IntegralItem", $itemId);
-        $projectMax = 0; //项目总上限
-        $itemMax = 0; //标准总上限
+        $projectRemainderPoints = 0; //项目剩余总上限
+        $itemRemainderPoints = 0; //标准剩余总上限
+        $points = 0; //当前可获得积分
         if ($item) {
         if ($item) {
             $project = getCacheById("IntegralProject", $item["projectId"]);
             $project = getCacheById("IntegralProject", $item["projectId"]);
-            $projectMax = $project["max"];
-            $itemMax = $item["max"];
+            $projectRemainderPoints = $project["max"]; //初始化项目可获得剩余积分
+            $itemRemainderPoints = $item["max"]; //初始化标准可获得剩余积分
             if ($project["limit"] == 1) {
             if ($project["limit"] == 1) {
                 //项目下所有规则总上限
                 //项目下所有规则总上限
                 $where = [];
                 $where = [];
@@ -124,36 +127,105 @@ class IntegralRecordApi {
                     $startTime = date("Y-01-01 00:00:00");
                     $startTime = date("Y-01-01 00:00:00");
                     $endTime = date("Y-12-31 23:59:59");
                     $endTime = date("Y-12-31 23:59:59");
                     $where[] = ["r.createTime", "between", [$startTime, $endTime]];
                     $where[] = ["r.createTime", "between", [$startTime, $endTime]];
+                    $returnObj->projectYearly = 1;
                 }
                 }
                 $totalPoints = Db::table("new_integral_detail")->alias("d")->leftJoin("new_integral_record r", "r.id=d.record_id")->leftJoin("new_integral_item i", "i.id=d.item_id")->where($where)->sum("d.point");
                 $totalPoints = Db::table("new_integral_detail")->alias("d")->leftJoin("new_integral_record r", "r.id=d.record_id")->leftJoin("new_integral_item i", "i.id=d.item_id")->where($where)->sum("d.point");
-                $projectMax -= $totalPoints;
+                $projectRemainderPoints -= $totalPoints;
+                $returnObj->projectMaxPoints = $project["max"];
+                $returnObj->projectRemainderPoints = $projectRemainderPoints;
             }
             }
             if ($item["limit"] == 1) {
             if ($item["limit"] == 1) {
                 //规则上限
                 //规则上限
+                $where = [];
+                $where[] = ["r.enterprise_id", "=", $enterprise_id];
+                $where[] = ["r.card_type", "=", $cardType];
+                $where[] = ["r.card_number", "=", $cardNumber];
+                $where[] = ["r.checkState", ">=", IntegralState::SUCCESS];
+                $where[] = ["d.item_id", "=", $itemId];
+                if ($item["yearly"] == 1) {
+                    //年度重置只算当年度
+                    $startTime = date("Y-01-01 00:00:00");
+                    $endTime = date("Y-12-31 23:59:59");
+                    $where[] = ["r.createTime", "between", [$startTime, $endTime]];
+                    $returnObj->itemYearly = 1;
+                }
+                $totalPoints = Db::table("new_integral_detail")->alias("d")->leftJoin("new_integral_record r", "r.id=d.record_id")->where($where)->sum("d.point");
+                $itemRemainderPoints -= $totalPoints;
+                $returnObj->itemMaxPoints = $item["max"];
+                $returnObj->itemRemainderPoints = $itemRemainderPoints;
             }
             }
-
-            if ($project["limit"] == 2) {
-                //项目没有设置上限
-                if ($item["limit"] == 2) {
-                    //标准没有设置上限
+            $where = [];
+            $where[] = ["r.enterprise_id", "=", $enterprise_id];
+            $where[] = ["r.card_type", "=", $cardType];
+            $where[] = ["r.card_number", "=", $cardNumber];
+            $where[] = ["r.checkState", ">=", IntegralState::SUCCESS];
+            $where[] = ["d.item_id", "=", $itemId];
+            $fstGain = Db::table("new_integral_detail")->alias("d")->leftJoin("new_integral_record r", "r.id=d.record_id")->where($where)->field("d.*")->order("createTime asc")->find();
+            if ($item["stepNeedAmount"] && $item["stepGainPoints"]) {
+                $returnObj->stepNeedAmount = $item["stepNeedAmount"];
+                $returnObj->stepGainPoints = $item["stepGainPoints"];
+                if ($fstGain) {
+                    $returnObj->fstGainRecordId = $fstGain["record_id"];
+                    $returnObj->fstGainDetailId = $fstGain["id"];
+                    //有配置增量积分
+                    if ($item["stepIsYear"] == 1) {
+                        //较上年度增量
+                        $where = [];
+                        $where[] = ["r.enterprise_id", "=", $enterprise_id];
+                        $where[] = ["r.card_type", "=", $cardType];
+                        $where[] = ["r.card_number", "=", $cardNumber];
+                        $where[] = ["r.checkState", ">=", IntegralState::SUCCESS];
+                        $where[] = ["d.item_id", "=", $itemId];
+                        $startTime = date("Y-m-d 00:00:00", strtotime("first day of last year"));
+                        $endTime = date("Y-12-31 23:59:59", strtotime("last day of last year"));
+                        $where[] = ["r.createTime", "between", [$startTime, $endTime]];
+                        $lastYearTotalAmount = Db::table("new_integral_detail")->alias("d")->leftJoin("new_integral_record r", "r.id=d.record_id")->where($where)->sum("d.amount");
+                        $newGainAmount = $amount - $lastYearTotalAmount;
+                        $times = floor($newGainAmount / $item["stepNeedAmount"]);
+                        $returnObj->stepIsYear = 1;
+                        $returnObj->lastYearTotalAmount = $lastYearTotalAmount;
+                        $returnObj->newGainAmount = $newGainAmount;
+                    } else {
+                        $times = floor($amount / $item["stepNeedAmount"]);
+                        $returnObj->stepAmount = $amount;
+                    }
+                    $points = $item["stepGainPoints"] * $times;
+                    $returnObj->times = $times;
+                    $returnObj->points = $returnObj->stepPoints = $points;
                 } else {
                 } else {
-                    //标准有设置上限,并检查标准有没有年度重置
-                    if ($item["yearly"] == 1) {
-                        //标准有年度重置
+                    //首次
+                    $returnObj->fstNeedAmount = $item["fstNeedAmount"];
+                    $returnObj->fstGainPoints = $item["fstGainPoints"];
+                    $returnObj->fstAmount = $amount >= $item["fstNeedAmount"] ? $item["fstNeedAmount"] : $amount;
+                    $returnObj->fstPoints = $amount >= $item["fstNeedAmount"] ? $item["fstGainPoints"] : 0;
+                    if ($item["stepIsYear"] != 1) {
+                        //未设置较上年度增量时,首次还需计算增量部分
+                        $stepAmount = $amount - $item["fstNeedAmount"];
+                        if ($stepAmount > 0) {
+                            $times = floor($stepAmount / $item["stepNeedAmount"]);
+                            $returnObj->stepAmount = $stepAmount;
+                            $returnObj->times = $times;
+                            $returnObj->stepPoints = $item["stepGainPoints"] * $times;
+                        }
                     } else {
                     } else {
-                        //标准没有重置
+                        $returnObj->stepIsYear = 1;
                     }
                     }
+                    $returnObj->points = $returnObj->stepPoints ? $returnObj->fstPoints + $returnObj->stepPoints : $returnObj->fstPoints;
                 }
                 }
             } else {
             } else {
-                //有设置上限,并检查有没有年度重置
-                if ($project["yearly"] == 1) {
-                    //有年度重置
+                //仅首次可得积分
+                if ($fstGain) {
+                    $returnObj->points = 0;
+                    $returnObj->msg = "仅首次能获得积分";
                 } else {
                 } else {
-                    //没有重置
+                    $returnObj->fstNeedAmount = $item["fstNeedAmount"];
+                    $returnObj->fstGainPoints = $item["fstGainPoints"];
+                    $returnObj->fstAmount = $amount >= $item["fstNeedAmount"] ? $item["fstNeedAmount"] : $amount;
+                    $returnObj->points = $returnObj->fstPoints = $amount >= $item["fstNeedAmount"] ? $item["fstGainPoints"] : 0;
                 }
                 }
             }
             }
         }
         }
-        return 0;
+        return $returnObj;
     }
     }
 
 
 }
 }

+ 1 - 0
public/static/modular/talentIdentify/integralMgr/IntegralItemMgr_info.js

@@ -158,6 +158,7 @@ IntegralItemMgrInfo.onIntegralPlanChange = function () {
             $('#integraMgrInfoForm').bootstrapValidator('removeField', "stepGainPoints");
             $('#integraMgrInfoForm').bootstrapValidator('removeField', "stepGainPoints");
             break;
             break;
         case "2":
         case "2":
+        case "3":
             $(".planb").css("display", "block");
             $(".planb").css("display", "block");
             $('#integraMgrInfoForm').bootstrapValidator('addField', "stepNeedAmount", {validators: {notEmpty: {message: '请填写每新增量'}}});
             $('#integraMgrInfoForm').bootstrapValidator('addField', "stepNeedAmount", {validators: {notEmpty: {message: '请填写每新增量'}}});
             $('#integraMgrInfoForm').bootstrapValidator('addField', "stepGainPoints", {validators: {notEmpty: {message: '请填写每新增获得积分'}}});
             $('#integraMgrInfoForm').bootstrapValidator('addField', "stepGainPoints", {validators: {notEmpty: {message: '请填写每新增获得积分'}}});