IntegralRecordApi.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace app\common\api;
  3. use app\common\api\DictApi;
  4. use app\admin\model\Enterprise;
  5. use think\facade\Db;
  6. use app\common\model\IntegralRecord;
  7. use app\common\state\IntegralState;
  8. /**
  9. * Description of IntegralRecordApi
  10. *
  11. * @author sgq
  12. */
  13. class IntegralRecordApi {
  14. public static function getOne($id) {
  15. $result = IntegralRecord::where("id", "=", $id)->find();
  16. $result["enterprise"] = EnterpriseApi::getOne($result["enterprise_id"]);
  17. $result["enterprise"]["streetName"] = DictApi::findDictByCode($result["enterprise"]["street"])["name"];
  18. if ($result) {
  19. $result["items"] = $result->detail;
  20. }
  21. return $result;
  22. }
  23. public static function getList($params) {
  24. $where = [];
  25. $order = $params["order"] ?: "desc";
  26. $offset = $params["offset"] ?: 0;
  27. $limit = $params["limit"] ?: 10;
  28. $where[] = ["e.type", "=", session("user")["type"]];
  29. if (session("user")["usertype"] == 2) {
  30. $where[] = ["enterprise_id", "=", session("user")["uid"]];
  31. }
  32. $where[] = ["delete", "=", 0];
  33. $count = IntegralRecord::alias("ir")->leftJoin("un_enterprise e", "e.id=ir.enterprise_id")->where($where)->count();
  34. $list = IntegralRecord::where($where)->field("ir.*,if(ir.updateTime is not null,ir.updateTime,ir.createTime) as orderTime")->limit($offset, $limit)->order("orderTime " . $order)->select();
  35. foreach ($list as $key => $item) {
  36. $list[$key]["apply_year"] = BatchApi::getOne($item["batch_id"])["batch"];
  37. $tmp_items = [];
  38. foreach ($item["detail"] as $_item) {
  39. $integral_item_info = getCacheById("IntegralItem", $_item["item_id"]);
  40. $tmp_items[] = sprintf("%s(%s%s)", $integral_item_info["name"], $_item["amount"], $integral_item_info["unit"]);
  41. }
  42. $list[$key]["details"] = implode(",", $tmp_items);
  43. $list[$key]["type"] = session("user")["type"];
  44. $last_log = TalentLogApi::getLastLog($item["id"], \app\common\state\ProjectState::INTEGRAL);
  45. $list[$key]["real_state"] = $last_log["state"];
  46. $list[$key]["last_state"] = $last_log["last_state"];
  47. }
  48. return ["total" => $count, "rows" => $list];
  49. }
  50. public static function getListByProcess($params) {
  51. $process = $params["process"] ?: 1;
  52. $where = [];
  53. switch ($process) {
  54. case 1://初审阶段
  55. $where[] = ["checkState", "in", [IntegralState::SUBMIT]];
  56. break;
  57. case 2://复审阶段
  58. $where[] = ["checkState", "in", [IntegralState::VERIFY_PASS]];
  59. break;
  60. case 3://复审后征信公示等状态
  61. $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]];
  62. break;
  63. }
  64. $order = $params["order"] ?: "desc";
  65. $offset = $params["offset"] ?: 0;
  66. $limit = $params["limit"] ?: 10;
  67. $where[] = ["delete", "=", 0];
  68. $count = IntegralRecord::where($where)->count();
  69. $list = IntegralRecord::where($where)->field("*,if(updateTime is not null,updateTime,createTime) as orderTime")->limit($offset, $limit)->order("orderTime " . $order)->select();
  70. foreach ($list as $key => $item) {
  71. $list[$key]["apply_year"] = BatchApi::getOne($item["batch_id"])["batch"];
  72. $tmp_items = [];
  73. foreach ($item["detail"] as $_item) {
  74. $integral_item_info = getCacheById("IntegralItem", $_item["item_id"]);
  75. $tmp_items[] = sprintf("%s(%s%s)", $integral_item_info["name"], $_item["amount"], $integral_item_info["unit"]);
  76. }
  77. $list[$key]["details"] = implode(",", $tmp_items);
  78. $list[$key]["type"] = session("user")["type"];
  79. $last_log = TalentLogApi::getLastLog($item["id"], \app\common\state\ProjectState::INTEGRAL);
  80. $list[$key]["real_state"] = $last_log["state"];
  81. $list[$key]["last_state"] = $last_log["last_state"];
  82. }
  83. return ["total" => $count, "rows" => $list];
  84. }
  85. public static function checkIsEditable($id) {
  86. $info = self::getOne($id);
  87. if (!$info || !in_array($info["checkState"], [0, IntegralState::SAVE]))
  88. return false;
  89. return true;
  90. }
  91. static public function chkIsOwner($id, $uid) {
  92. $info = self::getOne($id);
  93. if ($info["enterprise_id"] != $uid)
  94. return null;
  95. return $info;
  96. }
  97. public static function calIntegral($enterprise_id, $cardType, $cardNumber, $itemId, $amount) {
  98. $returnObj = new \stdClass();
  99. $returnObj->amount = $amount;
  100. $item = getCacheById("IntegralItem", $itemId);
  101. $projectRemainderPoints = 0; //项目剩余总上限
  102. $itemRemainderPoints = 0; //标准剩余总上限
  103. $points = 0; //当前可获得积分
  104. if ($item) {
  105. $project = getCacheById("IntegralProject", $item["projectId"]);
  106. $projectRemainderPoints = $project["max"]; //初始化项目可获得剩余积分
  107. $itemRemainderPoints = $item["max"]; //初始化标准可获得剩余积分
  108. if ($project["limit"] == 1) {
  109. //项目下所有规则总上限
  110. $where = [];
  111. $where[] = ["r.enterprise_id", "=", $enterprise_id];
  112. $where[] = ["r.card_type", "=", $cardType];
  113. $where[] = ["r.card_number", "=", $cardNumber];
  114. $where[] = ["r.checkState", ">=", IntegralState::SUCCESS];
  115. $where[] = ["i.projectId", "=", $project["id"]];
  116. if ($project["yearly"] == 1) {
  117. //年度重置只算当年度
  118. $startTime = date("Y-01-01 00:00:00");
  119. $endTime = date("Y-12-31 23:59:59");
  120. $where[] = ["r.createTime", "between", [$startTime, $endTime]];
  121. $returnObj->projectYearly = 1;
  122. }
  123. $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");
  124. $projectRemainderPoints -= $totalPoints;
  125. $returnObj->projectMaxPoints = $project["max"];
  126. $returnObj->projectRemainderPoints = $projectRemainderPoints;
  127. }
  128. if ($item["limit"] == 1) {
  129. //规则上限
  130. $where = [];
  131. $where[] = ["r.enterprise_id", "=", $enterprise_id];
  132. $where[] = ["r.card_type", "=", $cardType];
  133. $where[] = ["r.card_number", "=", $cardNumber];
  134. $where[] = ["r.checkState", ">=", IntegralState::SUCCESS];
  135. $where[] = ["d.item_id", "=", $itemId];
  136. if ($item["yearly"] == 1) {
  137. //年度重置只算当年度
  138. $startTime = date("Y-01-01 00:00:00");
  139. $endTime = date("Y-12-31 23:59:59");
  140. $where[] = ["r.createTime", "between", [$startTime, $endTime]];
  141. $returnObj->itemYearly = 1;
  142. }
  143. $totalPoints = Db::table("new_integral_detail")->alias("d")->leftJoin("new_integral_record r", "r.id=d.record_id")->where($where)->sum("d.point");
  144. $itemRemainderPoints -= $totalPoints;
  145. $returnObj->itemMaxPoints = $item["max"];
  146. $returnObj->itemRemainderPoints = $itemRemainderPoints;
  147. }
  148. $where = [];
  149. $where[] = ["r.enterprise_id", "=", $enterprise_id];
  150. $where[] = ["r.card_type", "=", $cardType];
  151. $where[] = ["r.card_number", "=", $cardNumber];
  152. $where[] = ["r.checkState", ">=", IntegralState::SUCCESS];
  153. $where[] = ["d.item_id", "=", $itemId];
  154. $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();
  155. if ($item["stepNeedAmount"] && $item["stepGainPoints"]) {
  156. $returnObj->stepNeedAmount = $item["stepNeedAmount"];
  157. $returnObj->stepGainPoints = $item["stepGainPoints"];
  158. if ($fstGain) {
  159. $returnObj->fstGainRecordId = $fstGain["record_id"];
  160. $returnObj->fstGainDetailId = $fstGain["id"];
  161. //有配置增量积分
  162. if ($item["stepIsYear"] == 1) {
  163. //较上年度增量
  164. $where = [];
  165. $where[] = ["r.enterprise_id", "=", $enterprise_id];
  166. $where[] = ["r.card_type", "=", $cardType];
  167. $where[] = ["r.card_number", "=", $cardNumber];
  168. $where[] = ["r.checkState", ">=", IntegralState::SUCCESS];
  169. $where[] = ["d.item_id", "=", $itemId];
  170. $startTime = date("Y-m-d 00:00:00", strtotime("first day of last year"));
  171. $endTime = date("Y-12-31 23:59:59", strtotime("last day of last year"));
  172. $where[] = ["r.createTime", "between", [$startTime, $endTime]];
  173. $lastYearTotalAmount = Db::table("new_integral_detail")->alias("d")->leftJoin("new_integral_record r", "r.id=d.record_id")->where($where)->sum("d.amount");
  174. $newGainAmount = $amount - $lastYearTotalAmount;
  175. $times = floor($newGainAmount / $item["stepNeedAmount"]);
  176. $returnObj->stepIsYear = 1;
  177. $returnObj->lastYearTotalAmount = $lastYearTotalAmount;
  178. $returnObj->newGainAmount = $newGainAmount;
  179. } else {
  180. $times = floor($amount / $item["stepNeedAmount"]);
  181. $returnObj->stepAmount = $amount;
  182. }
  183. $points = $item["stepGainPoints"] * $times;
  184. $returnObj->times = $times;
  185. $returnObj->points = $returnObj->stepPoints = $points;
  186. } else {
  187. //首次
  188. $returnObj->fstNeedAmount = $item["fstNeedAmount"];
  189. $returnObj->fstGainPoints = $item["fstGainPoints"];
  190. $returnObj->fstAmount = $amount >= $item["fstNeedAmount"] ? $item["fstNeedAmount"] : $amount;
  191. $returnObj->fstPoints = $amount >= $item["fstNeedAmount"] ? $item["fstGainPoints"] : 0;
  192. if ($item["stepIsYear"] != 1) {
  193. //未设置较上年度增量时,首次还需计算增量部分
  194. $stepAmount = $amount - $item["fstNeedAmount"];
  195. if ($stepAmount > 0) {
  196. $times = floor($stepAmount / $item["stepNeedAmount"]);
  197. $returnObj->stepAmount = $stepAmount;
  198. $returnObj->times = $times;
  199. $returnObj->stepPoints = $item["stepGainPoints"] * $times;
  200. }
  201. } else {
  202. $returnObj->stepIsYear = 1;
  203. }
  204. $returnObj->points = $returnObj->stepPoints ? $returnObj->fstPoints + $returnObj->stepPoints : $returnObj->fstPoints;
  205. }
  206. } else {
  207. //仅首次可得积分
  208. if ($fstGain) {
  209. $returnObj->points = 0;
  210. $returnObj->msg = "仅首次能获得积分";
  211. } else {
  212. $returnObj->fstNeedAmount = $item["fstNeedAmount"];
  213. $returnObj->fstGainPoints = $item["fstGainPoints"];
  214. $returnObj->fstAmount = $amount >= $item["fstNeedAmount"] ? $item["fstNeedAmount"] : $amount;
  215. $returnObj->points = $returnObj->fstPoints = $amount >= $item["fstNeedAmount"] ? $item["fstGainPoints"] : 0;
  216. }
  217. }
  218. }
  219. return $returnObj;
  220. }
  221. }