IntegralRecordApi.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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[] = ["ir.delete", "=", 0];
  33. $count = IntegralRecord::alias("ir")->leftJoin("un_enterprise e", "e.id=ir.enterprise_id")->where($where)->count();
  34. $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();
  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, IntegralState::VERIFY_FAIL]];
  56. break;
  57. case 2://复审阶段
  58. $where[] = ["checkState", "in", [IntegralState::VERIFY_PASS, IntegralState::REVERIFY_FAIL]];
  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. /**
  98. * 计算积分
  99. * @param type $enterpriseId 企业id
  100. * @param type $cardType 证件类型
  101. * @param type $cardNumber 证件号码
  102. * @param type $itemId 标准id
  103. * @param type $amount 达成数额
  104. * @return \stdClass 完整积分记录对象
  105. */
  106. public static function calIntegral($enterpriseId, $cardType, $cardNumber, $itemId, $amount) {
  107. $returnObj = new \stdClass();
  108. $returnObj->amount = $amount;
  109. $item = getCacheById("IntegralItem", $itemId);
  110. $projectRemainderPoints = 0; //项目剩余总上限
  111. $itemRemainderPoints = 0; //标准剩余总上限
  112. $points = 0; //当前可获得积分
  113. if ($item) {
  114. $project = getCacheById("IntegralProject", $item["projectId"]);
  115. $projectRemainderPoints = $project["max"]; //初始化项目可获得剩余积分
  116. $itemRemainderPoints = $item["max"]; //初始化标准可获得剩余积分
  117. if ($project["limit"] == 1) {
  118. //项目下所有规则总上限
  119. $where = [];
  120. $where[] = ["r.enterprise_id", "=", $enterprise_id];
  121. $where[] = ["r.card_type", "=", $cardType];
  122. $where[] = ["r.card_number", "=", $cardNumber];
  123. $where[] = ["r.checkState", ">=", IntegralState::SUCCESS];
  124. $where[] = ["i.projectId", "=", $project["id"]];
  125. if ($project["yearly"] == 1) {
  126. //年度重置只算当年度
  127. $startTime = date("Y-01-01 00:00:00");
  128. $endTime = date("Y-12-31 23:59:59");
  129. $where[] = ["r.createTime", "between", [$startTime, $endTime]];
  130. $returnObj->projectYearly = 1;
  131. }
  132. $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");
  133. $projectRemainderPoints -= $totalPoints;
  134. $returnObj->projectMaxPoints = $project["max"];
  135. $returnObj->projectRemainderPoints = $projectRemainderPoints;
  136. }
  137. if ($item["limit"] == 1) {
  138. //规则上限
  139. $where = [];
  140. $where[] = ["r.enterprise_id", "=", $enterprise_id];
  141. $where[] = ["r.card_type", "=", $cardType];
  142. $where[] = ["r.card_number", "=", $cardNumber];
  143. $where[] = ["r.checkState", ">=", IntegralState::SUCCESS];
  144. $where[] = ["d.item_id", "=", $itemId];
  145. if ($item["yearly"] == 1) {
  146. //年度重置只算当年度
  147. $startTime = date("Y-01-01 00:00:00");
  148. $endTime = date("Y-12-31 23:59:59");
  149. $where[] = ["r.createTime", "between", [$startTime, $endTime]];
  150. $returnObj->itemYearly = 1;
  151. }
  152. $totalPoints = Db::table("new_integral_detail")->alias("d")->leftJoin("new_integral_record r", "r.id=d.record_id")->where($where)->sum("d.point");
  153. $itemRemainderPoints -= $totalPoints;
  154. $returnObj->itemMaxPoints = $item["max"];
  155. $returnObj->itemRemainderPoints = $itemRemainderPoints;
  156. }
  157. $where = [];
  158. $where[] = ["r.enterprise_id", "=", $enterprise_id];
  159. $where[] = ["r.card_type", "=", $cardType];
  160. $where[] = ["r.card_number", "=", $cardNumber];
  161. $where[] = ["r.checkState", ">=", IntegralState::SUCCESS];
  162. $where[] = ["d.item_id", "=", $itemId];
  163. $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();
  164. if ($item["stepNeedAmount"] && $item["stepGainPoints"]) {
  165. $returnObj->stepNeedAmount = $item["stepNeedAmount"];
  166. $returnObj->stepGainPoints = $item["stepGainPoints"];
  167. if ($fstGain) {
  168. $returnObj->fstGainRecordId = $fstGain["record_id"];
  169. $returnObj->fstGainDetailId = $fstGain["id"];
  170. //有配置增量积分
  171. if ($item["stepIsYear"] == 1) {
  172. //较上年度增量
  173. $where = [];
  174. $where[] = ["r.enterprise_id", "=", $enterprise_id];
  175. $where[] = ["r.card_type", "=", $cardType];
  176. $where[] = ["r.card_number", "=", $cardNumber];
  177. $where[] = ["r.checkState", ">=", IntegralState::SUCCESS];
  178. $where[] = ["d.item_id", "=", $itemId];
  179. $startTime = date("Y-m-d 00:00:00", strtotime("first day of last year"));
  180. $endTime = date("Y-12-31 23:59:59", strtotime("last day of last year"));
  181. $where[] = ["r.createTime", "between", [$startTime, $endTime]];
  182. $lastYearTotalAmount = Db::table("new_integral_detail")->alias("d")->leftJoin("new_integral_record r", "r.id=d.record_id")->where($where)->sum("d.amount");
  183. $newGainAmount = $amount - $lastYearTotalAmount;
  184. $times = floor($newGainAmount / $item["stepNeedAmount"]);
  185. $returnObj->stepIsYear = 1;
  186. $returnObj->lastYearTotalAmount = $lastYearTotalAmount;
  187. $returnObj->newGainAmount = $newGainAmount;
  188. } else {
  189. $times = floor($amount / $item["stepNeedAmount"]);
  190. $returnObj->stepAmount = $amount;
  191. }
  192. $points = $item["stepGainPoints"] * $times;
  193. $returnObj->times = $times;
  194. $returnObj->points = $returnObj->stepPoints = $points;
  195. } else {
  196. //首次
  197. $returnObj->fstNeedAmount = $item["fstNeedAmount"];
  198. $returnObj->fstGainPoints = $item["fstGainPoints"];
  199. $returnObj->fstAmount = $amount >= $item["fstNeedAmount"] ? $item["fstNeedAmount"] : $amount;
  200. $returnObj->fstPoints = $amount >= $item["fstNeedAmount"] ? $item["fstGainPoints"] : 0;
  201. if ($item["stepIsYear"] != 1) {
  202. //未设置较上年度增量时,首次还需计算增量部分
  203. $stepAmount = $amount - $item["fstNeedAmount"];
  204. if ($stepAmount > 0) {
  205. $times = floor($stepAmount / $item["stepNeedAmount"]);
  206. $returnObj->stepAmount = $stepAmount;
  207. $returnObj->times = $times;
  208. $returnObj->stepPoints = $item["stepGainPoints"] * $times;
  209. }
  210. } else {
  211. $returnObj->stepIsYear = 1;
  212. }
  213. $returnObj->points = $returnObj->stepPoints ? $returnObj->fstPoints + $returnObj->stepPoints : $returnObj->fstPoints;
  214. }
  215. } else {
  216. //仅首次可得积分
  217. if ($fstGain) {
  218. $returnObj->points = 0;
  219. $returnObj->msg = "仅首次能获得积分";
  220. } else {
  221. $returnObj->fstNeedAmount = $item["fstNeedAmount"];
  222. $returnObj->fstGainPoints = $item["fstGainPoints"];
  223. $returnObj->fstAmount = $amount >= $item["fstNeedAmount"] ? $item["fstNeedAmount"] : $amount;
  224. $returnObj->points = $returnObj->fstPoints = $amount >= $item["fstNeedAmount"] ? $item["fstGainPoints"] : 0;
  225. }
  226. }
  227. $point1 = $project["limit"] == 1 ? ($returnObj->projectRemainderPoints > $returnObj->points ? $returnObj->points : $returnObj->projectRemainderPoints) : $returnObj->points;
  228. $point2 = $item["limit"] == 1 ? ($returnObj->itemRemainderPoints > $returnObj->points ? $returnObj->points : $returnObj->itemRemainderPoints) : $returnObj->points;
  229. $returnObj->theoretical = $returnObj->points; //理论值
  230. $returnObj->points = $point1 > $point2 ? $point2 : $point1; //实际值,当有限额时,会与理论值有偏差
  231. }
  232. return $returnObj;
  233. }
  234. }