Integral.php 11 KB

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