123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- namespace app\common\api;
- use app\common\api\DictApi;
- use app\admin\model\Enterprise;
- use think\facade\Db;
- use app\common\model\IntegralRecord;
- use app\common\state\IntegralState;
- /**
- * Description of IntegralRecordApi
- *
- * @author sgq
- */
- class IntegralRecordApi {
- public static function getOne($id) {
- $result = IntegralRecord::where("id", "=", $id)->find();
- $result["enterprise"] = EnterpriseApi::getOne($result["enterprise_id"]);
- $result["enterprise"]["streetName"] = DictApi::findDictByCode($result["enterprise"]["street"])["name"];
- if ($result) {
- $result["items"] = $result->detail;
- }
- return $result;
- }
- public static function getList($params) {
- $where = [];
- $order = $params["order"] ?: "desc";
- $offset = $params["offset"] ?: 0;
- $limit = $params["limit"] ?: 10;
- $where[] = ["e.type", "=", session("user")["type"]];
- if (session("user")["usertype"] == 2) {
- $where[] = ["enterprise_id", "=", session("user")["uid"]];
- }
- $where[] = ["ir.delete", "=", 0];
- $count = IntegralRecord::alias("ir")->leftJoin("un_enterprise e", "e.id=ir.enterprise_id")->where($where)->count();
- $list = IntegralRecord::alias("ir")->leftJoin("un_enterprise e", "e.id=ir.enterprise_id")->where($where)->field("ir.*,if(ir.updateTime is not null,ir.updateTime,ir.createTime) as orderTime")->limit($offset, $limit)->order("orderTime " . $order)->select();
- foreach ($list as $key => $item) {
- $list[$key]["apply_year"] = BatchApi::getOne($item["batch_id"])["batch"];
- $tmp_items = [];
- foreach ($item["detail"] as $_item) {
- $integral_item_info = getCacheById("IntegralItem", $_item["item_id"]);
- $tmp_items[] = sprintf("%s(%s%s)", $integral_item_info["name"], $_item["amount"], $integral_item_info["unit"]);
- }
- $list[$key]["details"] = implode(",", $tmp_items);
- $list[$key]["type"] = session("user")["type"];
- $last_log = TalentLogApi::getLastLog($item["id"], \app\common\state\ProjectState::INTEGRAL);
- $list[$key]["real_state"] = $last_log["state"];
- $list[$key]["last_state"] = $last_log["last_state"];
- }
- return ["total" => $count, "rows" => $list];
- }
- public static function getListByProcess($params) {
- $process = $params["process"] ?: 1;
- $where = [];
- switch ($process) {
- case 1://初审阶段
- $where[] = ["checkState", "in", [IntegralState::SUBMIT, IntegralState::VERIFY_FAIL]];
- break;
- case 2://复审阶段
- $where[] = ["checkState", "in", [IntegralState::VERIFY_PASS, IntegralState::REVERIFY_FAIL]];
- break;
- case 3://复审后征信公示等状态
- $where[] = ["checkState", "in", [IntegralState::REVERIFY_PASS, IntegralState::ZX_PASS, IntegralState::ZX_FAIL, IntegralState::ANNOUNCED, IntegralState::ANNOUNCED_REVERIFY_PASS, IntegralState::ANNOUNCED_REVERIFY_FAIL, IntegralState::PUBLISH_PASS, IntegralState::PUBLISH_FAIL, IntegralState::SUCCESS]];
- break;
- }
- $order = $params["order"] ?: "desc";
- $offset = $params["offset"] ?: 0;
- $limit = $params["limit"] ?: 10;
- $where[] = ["delete", "=", 0];
- $count = IntegralRecord::where($where)->count();
- $list = IntegralRecord::where($where)->field("*,if(updateTime is not null,updateTime,createTime) as orderTime")->limit($offset, $limit)->order("orderTime " . $order)->select();
- foreach ($list as $key => $item) {
- $list[$key]["apply_year"] = BatchApi::getOne($item["batch_id"])["batch"];
- $tmp_items = [];
- foreach ($item["detail"] as $_item) {
- $integral_item_info = getCacheById("IntegralItem", $_item["item_id"]);
- $tmp_items[] = sprintf("%s(%s%s)", $integral_item_info["name"], $_item["amount"], $integral_item_info["unit"]);
- }
- $list[$key]["details"] = implode(",", $tmp_items);
- $list[$key]["type"] = session("user")["type"];
- $last_log = TalentLogApi::getLastLog($item["id"], \app\common\state\ProjectState::INTEGRAL);
- $list[$key]["real_state"] = $last_log["state"];
- $list[$key]["last_state"] = $last_log["last_state"];
- }
- return ["total" => $count, "rows" => $list];
- }
- public static function checkIsEditable($id) {
- $info = self::getOne($id);
- if (!$info || !in_array($info["checkState"], [0, IntegralState::SAVE]))
- return false;
- return true;
- }
- static public function chkIsOwner($id, $uid) {
- $info = self::getOne($id);
- if ($info["enterprise_id"] != $uid)
- return null;
- return $info;
- }
- /**
- * 计算积分
- * @param type $enterpriseId 企业id
- * @param type $cardType 证件类型
- * @param type $cardNumber 证件号码
- * @param type $itemId 标准id
- * @param type $amount 达成数额
- * @return \stdClass 完整积分记录对象
- */
- public static function calIntegral($enterpriseId, $cardType, $cardNumber, $itemId, $amount) {
- $returnObj = new \stdClass();
- $returnObj->amount = $amount;
- $item = getCacheById("IntegralItem", $itemId);
- $projectRemainderPoints = 0; //项目剩余总上限
- $itemRemainderPoints = 0; //标准剩余总上限
- $points = 0; //当前可获得积分
- if ($item) {
- $project = getCacheById("IntegralProject", $item["projectId"]);
- $projectRemainderPoints = $project["max"]; //初始化项目可获得剩余积分
- $itemRemainderPoints = $item["max"]; //初始化标准可获得剩余积分
- if ($project["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[] = ["i.projectId", "=", $project["id"]];
- if ($project["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->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");
- $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;
- }
- $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 {
- //首次
- $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 ($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;
- }
- }
- $point1 = $project["limit"] == 1 ? ($returnObj->projectRemainderPoints > $returnObj->points ? $returnObj->points : $returnObj->projectRemainderPoints) : $returnObj->points;
- $point2 = $item["limit"] == 1 ? ($returnObj->itemRemainderPoints > $returnObj->points ? $returnObj->points : $returnObj->itemRemainderPoints) : $returnObj->points;
- $returnObj->theoretical = $returnObj->points; //理论值
- $returnObj->points = $point1 > $point2 ? $point2 : $point1; //实际值,当有限额时,会与理论值有偏差
- }
- return $returnObj;
- }
- }
|