|
@@ -103,14 +103,17 @@ class IntegralRecordApi {
|
|
|
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);
|
|
|
- $projectMax = 0; //项目总上限
|
|
|
- $itemMax = 0; //标准总上限
|
|
|
+ $projectRemainderPoints = 0; //项目剩余总上限
|
|
|
+ $itemRemainderPoints = 0; //标准剩余总上限
|
|
|
+ $points = 0; //当前可获得积分
|
|
|
if ($item) {
|
|
|
$project = getCacheById("IntegralProject", $item["projectId"]);
|
|
|
- $projectMax = $project["max"];
|
|
|
- $itemMax = $item["max"];
|
|
|
+ $projectRemainderPoints = $project["max"]; //初始化项目可获得剩余积分
|
|
|
+ $itemRemainderPoints = $item["max"]; //初始化标准可获得剩余积分
|
|
|
if ($project["limit"] == 1) {
|
|
|
//项目下所有规则总上限
|
|
|
$where = [];
|
|
@@ -124,36 +127,105 @@ class IntegralRecordApi {
|
|
|
$startTime = date("Y-01-01 00:00:00");
|
|
|
$endTime = date("Y-12-31 23:59:59");
|
|
|
$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");
|
|
|
- $projectMax -= $totalPoints;
|
|
|
+ $projectRemainderPoints -= $totalPoints;
|
|
|
+ $returnObj->projectMaxPoints = $project["max"];
|
|
|
+ $returnObj->projectRemainderPoints = $projectRemainderPoints;
|
|
|
}
|
|
|
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 {
|
|
|
- //标准有设置上限,并检查标准有没有年度重置
|
|
|
- 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 {
|
|
|
- //标准没有重置
|
|
|
+ $returnObj->stepIsYear = 1;
|
|
|
}
|
|
|
+ $returnObj->points = $returnObj->stepPoints ? $returnObj->fstPoints + $returnObj->stepPoints : $returnObj->fstPoints;
|
|
|
}
|
|
|
} else {
|
|
|
- //有设置上限,并检查有没有年度重置
|
|
|
- if ($project["yearly"] == 1) {
|
|
|
- //有年度重置
|
|
|
+ //仅首次可得积分
|
|
|
+ if ($fstGain) {
|
|
|
+ $returnObj->points = 0;
|
|
|
+ $returnObj->msg = "仅首次能获得积分";
|
|
|
} 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;
|
|
|
}
|
|
|
|
|
|
}
|