Integral.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace app\job;
  3. use think\queue\Job;
  4. use think\facade\Log;
  5. use think\facade\Db;
  6. use app\common\api\ChuanglanSmsApi;
  7. use app\common\model\MessageRecord;
  8. use app\common\api\TalentLogApi;
  9. use app\common\api\TalentConditionApi;
  10. use app\common\api\TalentState;
  11. use app\enterprise\model\Talent as TalentModel;
  12. use app\common\state\ProjectState;
  13. use app\common\state\IntegralState;
  14. /**
  15. * Description of Integral
  16. *
  17. * @author sgq
  18. */
  19. class Integral {
  20. public function fire(Job $job, $data) {
  21. if ($this->deal($data)) {
  22. $job->delete();
  23. return true;
  24. }
  25. if ($job->attempts() >= 3) {
  26. $job->delete();
  27. return false;
  28. }
  29. $job->release(10); //10秒后重试
  30. }
  31. /**
  32. * 处理业务逻辑
  33. * @param type $data
  34. * @return bool
  35. */
  36. public function deal($data): bool {
  37. switch ($data["type"]) {
  38. case 1:
  39. //发放积分码后增加积分记录并检查达到晋升分没
  40. Db::startTrans();
  41. try {
  42. $mainId = $data["id"];
  43. $where = [];
  44. $where[] = ["ir.id", "=", $mainId];
  45. $where[] = ["ir.checkState", "=", IntegralState::SUCCESS];
  46. $integralInfo = Db::table("new_integral_record")->alias("ir")->leftJoin("un_enterprise e", "e.id=ir.enterprise_id")->field("ir.*,e.type as enterpriseType")->where($where)->find();
  47. $where = [];
  48. $where[] = ["mainId", "=", $mainId];
  49. $where[] = ["mainType", "=", "integral"];
  50. $integralLog = Db::table("new_integral_log")->where($where)->find();
  51. if ($integralInfo && !$integralLog) {
  52. $name = $integralInfo["name"]; //姓名
  53. $cardType = $integralInfo["card_type"]; //身份证类型
  54. $cardNumber = $integralInfo["card_number"]; //身份证
  55. $type = $integralInfo["enterpriseType"]; //企业类型
  56. $lastPoints = 0; //上次积分
  57. $levelAndPoints = [
  58. 1 => 6000,
  59. 2 => 4000,
  60. 3 => 2000,
  61. 4 => 1000,
  62. 5 => 500,
  63. 6 => 200,
  64. 7 => 100
  65. ];
  66. $levelAndNames = [
  67. 1 => "第一层次",
  68. 2 => "第二层次",
  69. 3 => "第三层次",
  70. 4 => "第四层次",
  71. 5 => "第五层次",
  72. 6 => "第六层次",
  73. 7 => "第七层次",
  74. ];
  75. $gainPoints = $integralInfo["totalPoints"]; //增加积分
  76. $desc = sprintf("积分申请通过,增加积分%d。", $gainPoints); //详情
  77. /* 查询最后一条积分日志 */
  78. $where = [];
  79. $where[] = ["name", "=", $name];
  80. $where[] = ["card_type", "=", $cardType];
  81. $where[] = ["card_number", "=", $cardNumber];
  82. $last_log = Db::table("new_integral_log")->where($where)->order("createTime desc")->find();
  83. $lastPoints = $last_log["nowPoints"] ?: 0;
  84. $nowPoints = $lastPoints + $gainPoints;
  85. $level = 1;
  86. while ($levelPoints = $levelAndPoints[$level]) {
  87. if ($nowPoints >= $levelPoints) {
  88. break;
  89. }
  90. $level++;
  91. }
  92. if ($level >= 1 && $level <= 7) {
  93. //积分达到人才标准,检查是否需要增加人才库记录或者晋升
  94. $lastTalentLevel = $last_log["talentLevel"] ?: 8;
  95. if ($level != $lastTalentLevel && $level < $lastTalentLevel) {
  96. //层级变动,并且当前层级小于日志最后记录的层级,则为晋升
  97. /* 身份证提取有用内容填充 */
  98. if ($cardType == 1) {
  99. $sex = substr($cardNumber, 16, 1);
  100. if ($sex % 2 == 0) {
  101. $newTalentInfo["sex"] = 2;
  102. } else {
  103. $newTalentInfo["sex"] = 1;
  104. }
  105. $year = substr($cardNumber, 6, 4);
  106. $month = substr($cardNumber, 10, 2);
  107. $day = substr($cardNumber, 12, 2);
  108. $birthday = $year . "-" . $month . "-" . $day;
  109. $newTalentInfo["birthday"] = $birthday;
  110. }
  111. /* 生成人才码 */
  112. switch ($type) {
  113. case 2:
  114. $_prefix_type = "IC";
  115. $subindex += strlen($_prefix_type);
  116. break;
  117. }
  118. $year = date("Y");
  119. $no_prefix = $_prefix_type . $year . $level;
  120. $where = [];
  121. $where[] = ["certificateNo", "like", $no_prefix . "%"];
  122. $max_no = Db::table("new_talent_info")->where($where)->max("certificateNo", false);
  123. if (!$max_no) {
  124. $max_no = $no_prefix . "0001";
  125. } else {
  126. $new_no = intval(substr($max_no, $subindex)) + 1;
  127. $max_no = $no_prefix . str_pad($new_no, 4, "0", STR_PAD_LEFT);
  128. }
  129. /* 写入人才申报表 */
  130. $newTalentInfo["enterprise_id"] = $integralInfo["enterprise_id"];
  131. $newTalentInfo["name"] = $name;
  132. $newTalentInfo["card_type"] = $cardType;
  133. $newTalentInfo["card_number"] = $cardNumber;
  134. $newTalentInfo["phone"] = $integralInfo["phone"];
  135. $newTalentInfo["email"] = $integralInfo["email"];
  136. $newTalentInfo["checkState"] = TalentState::CERTIFICATED;
  137. $newTalentInfo["certificateNo"] = $max_no;
  138. $newTalentInfo["isEffect"] = 1;
  139. $newTalentInfo["isPublic"] = 5;
  140. $newTalentInfo["talent_arrange"] = $level;
  141. $newTalentInfo["createTime"] = date("Y-m-d H:i:s");
  142. $newTalentId = Db::table("new_talent_info")->insertGetId($newTalentInfo);
  143. var_dump($newTalentId);return false;
  144. /* 积分申报的最后一条日志中提取有用内容填充下面的人才日志部分字段 */
  145. $ir_last_log = TalentLogApi::getLastLog($mainId, ProjectState::INTEGRAL, ["active", "=", 1]);
  146. /* 写入人才申报日志表 */
  147. $talent_log["last_state"] = 0;
  148. $talent_log["id"] = getStringId();
  149. $talent_log["state"] = $talent_log["new_state"] = TalentState::CERTIFICATED;
  150. $talent_log["type"] = ProjectState::TALENT;
  151. $talent_log["mainId"] = $newTalentId;
  152. $talent_log["companyId"] = $ir_last_log["companyId"];
  153. $talent_log["active"] = 1;
  154. $talent_log["description"] = sprintf("人才积分达到人才层次(%s)晋升标准,人才码为:%s", $levelAndNames[$level], $max_no);
  155. $talent_log["createUser"] = $ir_last_log["createUser"];
  156. $talent_log["createTime"] = date("Y-m-d H:i:s");
  157. Db::table("new_talent_checklog")->insert($talent_log);
  158. }
  159. }
  160. $log["id"] = getStringId();
  161. $log["type"] = $type;
  162. $log["mainType"] = "integral";
  163. $log["mainId"] = $mainId;
  164. $log["name"] = $name;
  165. $log["card_type"] = $cardType;
  166. $log["card_number"] = $cardNumber;
  167. $log["talentLevel"] = $level;
  168. $log["lastPoints"] = $lastPoints;
  169. $log["gainPoints"] = $gainPoints;
  170. $log["nowPoints"] = $nowPoints;
  171. $log["description"] = $desc;
  172. $log["createTime"] = date("Y-m-d H:i:s");
  173. Db::table("new_integral_log")->insert($log);
  174. Db::commit();
  175. return true;
  176. }
  177. } catch (\Exception $e) {
  178. Db::rollback();
  179. return false;
  180. }
  181. break;
  182. }
  183. return false;
  184. }
  185. }