IntegralRecordApi.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. use app\common\state\CommonConst;
  9. /**
  10. * Description of IntegralRecordApi
  11. *
  12. * @author sgq
  13. */
  14. class IntegralRecordApi {
  15. public static function getOne($id) {
  16. $result = IntegralRecord::where("id", "=", $id)->find();
  17. $result["enterprise"] = EnterpriseApi::getOne($result["enterprise_id"]);
  18. $result["apply_year"] = BatchApi::getOne($result["batch_id"])["batch"];
  19. $result["enterprise"]["streetName"] = DictApi::findDictByCode($result["enterprise"]["street"])["name"];
  20. if ($result) {
  21. $result["items"] = $result->detail;
  22. $result["tips"] = self::getIntegralRecordByIdCard($result["card_type"], $result["card_number"]);
  23. }
  24. return $result;
  25. }
  26. public static function getList($params) {
  27. $order = $params["order"] ?: "desc";
  28. $offset = $params["offset"] ?: 0;
  29. $limit = $params["limit"] ?: 10;
  30. $where = [];
  31. $where[] = ["e.type", "=", session("user")["type"]];
  32. if (session("user")["usertype"] == 2) {
  33. $where[] = ["enterprise_id", "=", session("user")["uid"]];
  34. }
  35. $where[] = ["ir.delete", "=", 0];
  36. if ($params["apply_year"]) {
  37. $where[] = ["b.batch", "like", "%{$params['apply_year']}%"];
  38. }
  39. if ($params["name"]) {
  40. $where[] = ["ir.name", "like", "%{$params['name']}%"];
  41. }
  42. if ($params["card_number"]) {
  43. $where[] = ["ir.card_number", "like", "%{$params['card_number']}%"];
  44. }
  45. if ($params["phone"]) {
  46. $where[] = ["ir.phone", "like", "%{$params['phone']}%"];
  47. }
  48. if ($params["email"]) {
  49. $where[] = ["ir.email", "like", "%{$params['email']}%"];
  50. }
  51. if ($params["shareholder"]) {
  52. $where[] = ["ir.shareholder", "=", $params['shareholder']];
  53. }
  54. switch ($params["checkState"]) {
  55. case -1:
  56. $where[] = ["tl.state", "in", [IntegralState::VERIFY_FAIL, IntegralState::REVERIFY_FAIL, IntegralState::ZX_FAIL, IntegralState::ANNOUNCED_REVERIFY_FAIL, IntegralState::PUBLISH_FAIL]];
  57. break;
  58. case 1:
  59. $where[] = ["tl.state", "=", IntegralState::SAVE];
  60. break;
  61. case 2:
  62. $where[] = ["tl.state", "in", [IntegralState::VERIFY_REJECT, IntegralState::REVERIFY_REJECT]];
  63. break;
  64. case 3:
  65. $where[] = ["tl.state", "in", [IntegralState::SUBMIT, IntegralState::VERIFY_PASS]];
  66. break;
  67. case 4:
  68. $where[] = ["tl.state", ">=", IntegralState::REVERIFY_PASS];
  69. $where[] = ["tl.state", "not in", [IntegralState::REVERIFY_REJECT, IntegralState::REVERIFY_FAIL]];
  70. break;
  71. case 5:
  72. $where[] = ["tl.state", "=", IntegralState::SUCCESS];
  73. break;
  74. }
  75. $count = IntegralRecord::alias("ir")
  76. ->leftJoin("sys_batch b", "b.id=ir.batch_id")
  77. ->leftJoin("(select description,mainId,last_state,new_state,state,createTime from new_talent_checklog where md5(concat(createTime,mainId,`type`)) in (select md5(concat(max(createTime),mainId,`type`)) from `new_talent_checklog` where `type`=20 and `step` is null and active=1 and typeFileId is null group by mainId,`type`)) tl", "`tl`.`mainId`=ir.id")
  78. ->leftJoin("un_enterprise e", "e.id=ir.enterprise_id")->where($where)->count();
  79. $list = IntegralRecord::alias("ir")
  80. ->leftJoin("sys_batch b", "b.id=ir.batch_id")
  81. ->leftJoin("(select description,mainId,last_state,new_state,state,createTime from new_talent_checklog where md5(concat(createTime,mainId,`type`)) in (select md5(concat(max(createTime),mainId,`type`)) from `new_talent_checklog` where `type`=20 and `step` is null and active=1 and typeFileId is null group by mainId,`type`)) tl", "`tl`.`mainId`=ir.id")
  82. ->leftJoin("un_enterprise e", "e.id=ir.enterprise_id")->where($where)->field("ir.*,b.batch as apply_year,tl.state,tl.new_state,if(ir.updateTime is not null,ir.updateTime,ir.createTime) as orderTime")
  83. ->limit($offset, $limit)
  84. ->order("orderTime " . $order)
  85. ->select();
  86. foreach ($list as $key => $item) {
  87. $tmp_items = [];
  88. foreach ($item["detail"] as $_item) {
  89. $integral_item_info = getCacheById("IntegralItem", $_item["item_id"]);
  90. $tmp_items[] = sprintf("%s(%s%s)", $integral_item_info["name"], $_item["amount"], $integral_item_info["unit"]);
  91. }
  92. $list[$key]["details"] = implode(",", $tmp_items);
  93. $list[$key]["type"] = session("user")["type"];
  94. $last_log = TalentLogApi::getLastLog($item["id"], \app\common\state\ProjectState::INTEGRAL);
  95. $list[$key]["real_state"] = $last_log["state"];
  96. $list[$key]["last_state"] = $last_log["last_state"];
  97. }
  98. return ["total" => $count, "rows" => $list];
  99. }
  100. public static function getPublicList($params) {
  101. $order = $params["order"];
  102. $offset = $params["offset"];
  103. $limit = $params["limit"];
  104. $where = [];
  105. $where[] = ["e.type", "=", session("user")["type"]];
  106. if ($params["name"]) {
  107. $where[] = ["ir.name", "like", "%" . $params["name"] . "%"];
  108. }
  109. switch ($params["checkState"]) {
  110. case 1:
  111. $where[] = ["ir.checkState", "in", [IntegralState::REVERIFY_PASS, IntegralState::ZX_PASS, IntegralState::ANNOUNCED, IntegralState::ANNOUNCED_REVERIFY_PASS, IntegralState::PUBLISH_PASS]];
  112. break;
  113. case 2:
  114. $where[] = ["ir.checkState", "in", [IntegralState::REVERIFY_FAIL, IntegralState::ZX_FAIL, IntegralState::ANNOUNCED_REVERIFY_FAIL, IntegralState::PUBLISH_FAIL]];
  115. break;
  116. }
  117. $type = $params["type"];
  118. switch ($type) {
  119. case 1:
  120. case 2:
  121. $where[] = ["ir.checkState", "=", IntegralState::REVERIFY_PASS];
  122. break;
  123. case 3: //公示
  124. case 7: //公示预览
  125. $where[] = ["ir.checkState", "=", IntegralState::ZX_PASS];
  126. break;
  127. case 4: //公示通过
  128. $where[] = ["ir.checkState", "=", IntegralState::ANNOUNCED];
  129. break;
  130. case 5:
  131. case 8: //公布预览
  132. $where[] = ["ir.checkState", "=", IntegralState::ANNOUNCED_REVERIFY_PASS];
  133. break;
  134. case 6:
  135. $where[] = ["ir.checkState", "=", IntegralState::PUBLISH_PASS];
  136. break;
  137. }
  138. $count = IntegralRecord::alias("ir")->leftJoin("un_enterprise e", "e.id=ir.enterprise_id")->where($where)->count();
  139. $list = IntegralRecord::alias("ir")->leftJoin("un_enterprise e", "e.id=ir.enterprise_id")
  140. ->where($where)
  141. ->limit($offset, $limit)
  142. ->order("ir.createTime " . $order)->field("ir.*,e.name as enterpriseName,e.type as enterprise_type,enterpriseTag")->select()->toArray();
  143. foreach ($list as &$item) {
  144. $item["talent_type"] = $item["enterprise_type"] == 1 ? "晋江市现代产业体系人才" : "集成电路优秀人才";
  145. }unset($item);
  146. return ["total" => $count, "rows" => $list];
  147. }
  148. public static function getListByProcess($params) {
  149. $order = $params["order"] ?: "desc";
  150. $offset = $params["offset"] ?: 0;
  151. $limit = $params["limit"] ?: 10;
  152. $process = $params["process"] ?: 1;
  153. $where = [];
  154. $where[] = ["e.type", "=", session("user")["type"]];
  155. switch ($process) {
  156. case 1://初审阶段
  157. switch ($params["checkState"]) {
  158. case 1://待审核
  159. $where[] = ["ir.checkState", "=", IntegralState::SUBMIT];
  160. $where[] = ["tl.state", "=", IntegralState::SUBMIT];
  161. break;
  162. case 2://驳回
  163. $where[] = ["tl.new_state", "in", [IntegralState::SAVE]];
  164. $where[] = ["tl.state", "in", [IntegralState::VERIFY_REJECT]];
  165. break;
  166. case 3:
  167. //审核失败
  168. $where[] = ["ir.checkState", "in", [IntegralState::VERIFY_FAIL]];
  169. break;
  170. default:
  171. $where[] = ["tl.state", "in", [IntegralState::SUBMIT, IntegralState::VERIFY_REJECT, IntegralState::VERIFY_FAIL]];
  172. }
  173. break;
  174. case 2://复审阶段
  175. switch ($params["checkState"]) {
  176. case 1://待审核
  177. $where[] = ["tl.state", "=", IntegralState::VERIFY_PASS];
  178. break;
  179. case 3:
  180. //审核失败
  181. $where[] = ["tl.state", "in", [IntegralState::VERIFY_FAIL]];
  182. break;
  183. default:
  184. $where[] = ["tl.state", "in", [IntegralState::VERIFY_PASS, IntegralState::REVERIFY_FAIL]];
  185. }
  186. break;
  187. case 3://复审后征信公示等状态
  188. if ($params["checkState"]) {
  189. $where[] = ["ir.checkState", "=", $params["checkState"]];
  190. } else {
  191. $where[] = ["ir.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]];
  192. }
  193. break;
  194. case 4:
  195. return self::getLibraryList($params);
  196. case 5:
  197. return self::getIntegralLogs($params);
  198. break;
  199. }
  200. $where[] = ["ir.delete", "=", 0];
  201. if ($params["apply_year"]) {
  202. $where[] = ["b.batch", "like", "%{$params['apply_year']}%"];
  203. }
  204. if ($params["name"]) {
  205. $where[] = ["ir.name", "like", "%{$params['name']}%"];
  206. }
  207. if ($params["card_number"]) {
  208. $where[] = ["ir.card_number", "like", "%{$params['card_number']}%"];
  209. }
  210. if ($params["phone"]) {
  211. $where[] = ["ir.phone", "like", "%{$params['phone']}%"];
  212. }
  213. if ($params["email"]) {
  214. $where[] = ["ir.email", "like", "%{$params['email']}%"];
  215. }
  216. if ($params["enterprise_id"]) {
  217. $where[] = ["ir.enterprise_id", "=", $params['enterprise_id']];
  218. }
  219. if ($params["shareholder"]) {
  220. $where[] = ["ir.shareholder", "=", $params['shareholder']];
  221. }
  222. $count = IntegralRecord::alias("ir")->where($where)
  223. ->leftJoin("sys_batch b", "b.id=ir.batch_id")
  224. ->leftJoin("un_enterprise e", "e.id=ir.enterprise_id")
  225. ->leftJoin("(select description,mainId,last_state,new_state,state,createTime from new_talent_checklog where md5(concat(createTime,mainId,`type`)) in (select md5(concat(max(createTime),mainId,`type`)) from `new_talent_checklog` where `type`=20 and `step` is null and active=1 and typeFileId is null group by mainId,`type`)) tl", "`tl`.`mainId`=ir.id")
  226. ->count();
  227. $list = IntegralRecord::alias("ir")->where($where)
  228. ->leftJoin("sys_batch b", "b.id=ir.batch_id")
  229. ->leftJoin("un_enterprise e", "e.id=ir.enterprise_id")
  230. ->leftJoin("(select description,mainId,last_state,new_state,state,createTime from new_talent_checklog where md5(concat(createTime,mainId,`type`)) in (select md5(concat(max(createTime),mainId,`type`)) from `new_talent_checklog` where `type`=20 and `step` is null and active=1 and typeFileId is null group by mainId,`type`)) tl", "`tl`.`mainId`=ir.id")
  231. ->field("ir.*,e.name as enterpriseName,b.batch as apply_year,tl.state as real_state,tl.last_state")->limit($offset, $limit)->order("tl.createTime " . $order)->select();
  232. foreach ($list as $key => $item) {
  233. $tmp_items = [];
  234. foreach ($item["detail"] as $_item) {
  235. $integral_item_info = getCacheById("IntegralItem", $_item["item_id"]);
  236. $tmp_items[] = sprintf("%s(%s%s)", $integral_item_info["name"], $_item["amount"], $integral_item_info["unit"]);
  237. }
  238. $list[$key]["details"] = implode(";", $tmp_items);
  239. $list[$key]["type"] = session("user")["type"];
  240. //$last_log = TalentLogApi::getLastLog($item["id"], \app\common\state\ProjectState::INTEGRAL);
  241. //$list[$key]["real_state"] = $last_log["state"];
  242. //$list[$key]["last_state"] = $last_log["last_state"];
  243. }
  244. return ["total" => $count, "rows" => $list];
  245. }
  246. public static function getLibraryList($params) {
  247. $order = $params["order"] ?: "desc";
  248. $offset = $params["offset"] ?: 0;
  249. $limit = $params["limit"] ?: 10;
  250. $where = [];
  251. $where[] = ["e.type", "=", session("user")["type"]];
  252. if ($params["name"]) {
  253. $where[] = ["l.name", "like", "%{$params['name']}%"];
  254. }
  255. if ($params["card_number"]) {
  256. $where[] = ["l.card_number", "like", "%{$params['card_number']}%"];
  257. }
  258. if ($params["enterprise_id"]) {
  259. $where[] = ["l.enterprise_id", "=", $params['enterprise_id']];
  260. }
  261. if ($params["veto"]) {
  262. $having = "if(min_shareholder=1 and enterprise_veto = 1,true,false) or personal_veto = 1";
  263. } else {
  264. $having = "if(min_shareholder=1 and enterprise_veto = 1,false,true) and personal_veto = 0";
  265. }
  266. $count = Db::table("new_integral_log")->alias("l")->where($where)
  267. ->leftJoin("un_enterprise e", "e.id=l.enterprise_id")
  268. //->leftJoin("new_integral_veto iv1", "iv1.enterprise_id=l.enterprise_id")
  269. //->leftJoin("new_integral_veto iv2", "md5(concat(iv2.card_type,iv2.card_number))=md5(concat(l.card_type,l.card_number))")
  270. //->field("min(shareholder) as min_shareholder,if(iv1.`active`=1,iv1.`active`,0) as enterprise_veto,if(iv2.`active`=1,iv2.`active`,0) as personal_veto")
  271. ->group("concat(l.card_type,l.card_number)")
  272. //->having($having)
  273. ->count();
  274. $list = Db::table("new_integral_log")->alias("l")->where($where)
  275. ->leftJoin("un_enterprise e", "e.id=l.enterprise_id")
  276. //->leftJoin("new_integral_veto iv1", "iv1.enterprise_id=ir.enterprise_id")
  277. //->leftJoin("new_integral_veto iv2", "md5(concat(iv2.card_type,iv2.card_number))=md5(concat(ir.card_type,ir.card_number))")
  278. ->group("concat(l.card_type,l.card_number)")
  279. //->having($having)
  280. //->field("min(shareholder) as min_shareholder,ir.name,ir.card_type,ir.card_number,ir.enterprise_id,sum(ir.totalPoints) as totalPoints,e.name as enterpriseName,if(iv1.`active`=1,iv1.`active`,0) as enterprise_veto,iv1.description as enterprise_veto_msg,if(iv2.`active`=1,iv2.`active`,0) as personal_veto,iv2.description as personal_veto_msg")
  281. ->field("l.*,sum(gainPoints) as totalPoints,e.name as enterpriseName")
  282. ->limit($offset, $limit)
  283. //->order("b.batch " . $order)
  284. ->select()->toArray();
  285. foreach ($list as $key => $item) {
  286. $list[$key]["basePoints"] = CommonConst::getLayerPointsByLayer($item["talentLevel"]);
  287. }
  288. return ["total" => $count, "rows" => $list];
  289. }
  290. public static function getIntegralLogs($params) {
  291. $order = $params["order"] ?: "desc";
  292. $offset = $params["offset"] ?: 0;
  293. $limit = $params["limit"] ?: 10;
  294. $where = [];
  295. $where[] = ["e.type", "=", session("user")["type"]];
  296. if ($params["name"]) {
  297. $where[] = ["l.name", "like", "%{$params['name']}%"];
  298. }
  299. if ($params["card_number"]) {
  300. $where[] = ["l.card_number", "like", "%{$params['card_number']}%"];
  301. }
  302. if ($params["enterprise_id"]) {
  303. $where[] = ["l.enterprise_id", "=", $params['enterprise_id']];
  304. }
  305. $count = Db::table("new_integral_log")->alias("l")->where($where)
  306. ->leftJoin("un_enterprise e", "e.id=l.enterprise_id")
  307. ->count();
  308. $list = Db::table("new_integral_log")->alias("l")
  309. ->field("l.*,e.name as enterpriseName")
  310. ->leftJoin("un_enterprise e", "e.id=l.enterprise_id")
  311. ->where($where)
  312. ->limit($offset, $limit)
  313. ->order("l.createTime desc")
  314. ->select()->toArray();
  315. foreach ($list as $key => $item) {
  316. $where = [];
  317. if ($item["mainType"] == "integral") {
  318. $where[] = ["ir.id", "=", $item["mainId"]];
  319. $integralInfo = IntegralRecord::alias("ir")->leftJoin("sys_batch b", "b.id=ir.batch_id")->field("ir.*,b.batch as apply_year")->where($where)->find();
  320. $list[$key]["apply_year"] = $integralInfo["apply_year"];
  321. $list[$key]["shareholder"] = $integralInfo["shareholder"];
  322. $tmp_items = [];
  323. foreach ($integralInfo["detail"] as $_item) {
  324. $integral_item_info = getCacheById("IntegralItem", $_item["item_id"]);
  325. $tmp_items[] = sprintf("%s(%s%s)", $integral_item_info["name"], $_item["amount"], $integral_item_info["unit"]);
  326. }
  327. $list[$key]["details"] = implode(";", $tmp_items);
  328. $list[$key]["first_submit_time"] = $integralInfo["first_submit_time"];
  329. $list[$key]["new_submit_time"] = $integralInfo["new_submit_time"];
  330. } else {
  331. $where[] = ["id", "=", $item["mainId"]];
  332. $talentInfo = Db::table("new_talent_info")->where($where)->find();
  333. $list[$key]["apply_year"] = $talentInfo["apply_year"];
  334. $list[$key]["details"] = $item["description"];
  335. $list[$key]["first_submit_time"] = $talentInfo["first_submit_time"];
  336. $list[$key]["new_submit_time"] = $talentInfo["new_submit_time"];
  337. }
  338. }
  339. return ["total" => $count, "rows" => $list];
  340. }
  341. public static function checkIsEditable($id) {
  342. $info = self::getOne($id);
  343. if (!$info || !in_array($info["checkState"], [0, IntegralState::SAVE]))
  344. return false;
  345. return true;
  346. }
  347. static public function chkIsOwner($id, $uid) {
  348. $info = self::getOne($id);
  349. if ($info["enterprise_id"] != $uid)
  350. return null;
  351. return $info;
  352. }
  353. /**
  354. * 导出
  355. * @param type $process
  356. * @param type $params
  357. * @return type
  358. */
  359. public static function getExportDatas($process, $params) {
  360. $where[] = [];
  361. //特殊字段处理
  362. $fields = [];
  363. $fields[] = "ir.id";
  364. foreach ($params as $param) {
  365. if (!in_array($param, ["enterpriseName", "street", "checkMsg", "project", "year"])) {
  366. $fields[] = "ir." . $param;
  367. }
  368. }
  369. $fields[] = "e.name as enterpriseName";
  370. $fields[] = "e.street";
  371. $fields[] = "tl.description as checkMsg";
  372. $fields[] = "b.batch as year";
  373. if (in_array("card_type", $params)) {
  374. $cardTypes = DictApi::selectByParentCode("card_type");
  375. }
  376. if (in_array("street", $params)) {
  377. $streets = DictApi::selectByParentCode("street");
  378. }
  379. $sex = [1 => "男", 2 => "女"];
  380. $where = [];
  381. $where[] = ["e.type", "=", session("user")["type"]];
  382. switch ($process) {
  383. case 1:
  384. $where = "ir.checkState in (" . IntegralState::SUBMIT . "," . IntegralState::VERIFY_FAIL . ")";
  385. break;
  386. case 2:
  387. $where = "ir.checkState in (" . IntegralState::VERIFY_PASS . "," . IntegralState::REVERIFY_FAIL . ")";
  388. break;
  389. case 3:
  390. $where = "ir.checkState >= " . IntegralState::REVERIFY_PASS;
  391. break;
  392. }
  393. $list = IntegralRecord::alias("ir")
  394. ->field($fields)
  395. ->leftJoin("un_enterprise e", "e.id=ir.enterprise_id")
  396. ->leftJoin("sys_batch b", "b.id=ir.batch_id")
  397. ->leftJoin("(select description,mainId,last_state,new_state,state,createTime from new_talent_checklog where md5(concat(createTime,mainId,`type`)) in (select md5(concat(max(createTime),mainId,`type`)) from `new_talent_checklog` where `type`=20 and `step` is null and active=1 and typeFileId is null group by mainId,`type`)) tl", "`tl`.`mainId`=ir.id")
  398. //->leftJoin("new_talent_checklog tl", "tl.mainId=ti.id and tl.id=(select id from new_talent_checklog where mainId=ti.id and `step` is null and active=1 and typeFileId is null order by createTime desc limit 1)")
  399. ->whereRaw($where)
  400. ->select()->toArray();
  401. foreach ($list as &$item) {
  402. $item["card_type"] = $cardTypes[$item["card_type"]];
  403. $item["street"] = $streets[$item["street"]];
  404. $item["sex"] = $sex[$item["sex"]];
  405. $item["checkState"] = IntegralState::getStateName($item["checkState"]);
  406. $detail = \app\common\model\IntegralDetail::where("record_id", $item["id"])->select();
  407. $tmp_items = [];
  408. foreach ($detail as $_item) {
  409. $integral_item_info = getCacheById("IntegralItem", $_item["item_id"]);
  410. $tmp_items[] = sprintf("%s(%s%s)", $integral_item_info["name"], $_item["amount"], $integral_item_info["unit"]);
  411. }
  412. $item["project"] = implode(";", $tmp_items);
  413. }unset($item);
  414. return $list;
  415. }
  416. public static function getListByIds($ids) {
  417. $where[] = ["id", "in", $ids];
  418. return IntegralRecord::where($where)->select()->toArray();
  419. }
  420. /**
  421. * 计算积分
  422. * @param type $enterpriseId 企业id
  423. * @param type $cardType 证件类型
  424. * @param type $cardNumber 证件号码
  425. * @param type $itemId 标准id
  426. * @param type $amount 达成数额
  427. * @return \stdClass 完整积分记录对象
  428. */
  429. public static function calIntegral($enterprise_id, $cardType, $cardNumber, $itemId, $amount) {
  430. $returnObj = new \stdClass();
  431. $returnObj->amount = $amount;
  432. $item = getCacheById("IntegralItem", $itemId);
  433. $projectRemainderPoints = 0; //项目剩余总上限
  434. $itemRemainderPoints = 0; //标准剩余总上限
  435. $points = 0; //当前可获得积分
  436. if ($item) {
  437. $project = getCacheById("IntegralProject", $item["projectId"]);
  438. $projectRemainderPoints = $project["max"]; //初始化项目可获得剩余积分
  439. $itemRemainderPoints = $item["max"]; //初始化标准可获得剩余积分
  440. if ($project["limit"] == 1) {
  441. //项目下所有规则总上限
  442. $where = [];
  443. $where[] = ["r.enterprise_id", "=", $enterprise_id];
  444. $where[] = ["r.card_type", "=", $cardType];
  445. $where[] = ["r.card_number", "=", $cardNumber];
  446. $where[] = ["r.checkState", ">=", IntegralState::SUCCESS];
  447. $where[] = ["i.projectId", "=", $project["id"]];
  448. if ($project["yearly"] == 1) {
  449. //年度重置只算当年度
  450. $startTime = date("Y-01-01 00:00:00");
  451. $endTime = date("Y-12-31 23:59:59");
  452. $where[] = ["r.createTime", "between", [$startTime, $endTime]];
  453. $returnObj->projectYearly = 1;
  454. }
  455. $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");
  456. $projectRemainderPoints -= $totalPoints;
  457. $returnObj->projectMaxPoints = $project["max"];
  458. $returnObj->projectRemainderPoints = $projectRemainderPoints;
  459. }
  460. if ($item["limit"] == 1) {
  461. //规则上限
  462. $where = [];
  463. $where[] = ["r.enterprise_id", "=", $enterprise_id];
  464. $where[] = ["r.card_type", "=", $cardType];
  465. $where[] = ["r.card_number", "=", $cardNumber];
  466. $where[] = ["r.checkState", ">=", IntegralState::SUCCESS];
  467. $where[] = ["d.item_id", "=", $itemId];
  468. if ($item["yearly"] == 1) {
  469. //年度重置只算当年度
  470. $startTime = date("Y-01-01 00:00:00");
  471. $endTime = date("Y-12-31 23:59:59");
  472. $where[] = ["r.createTime", "between", [$startTime, $endTime]];
  473. $returnObj->itemYearly = 1;
  474. }
  475. $totalPoints = Db::table("new_integral_detail")->alias("d")->leftJoin("new_integral_record r", "r.id=d.record_id")->where($where)->sum("d.point");
  476. $itemRemainderPoints -= $totalPoints;
  477. $returnObj->itemMaxPoints = $item["max"];
  478. $returnObj->itemRemainderPoints = $itemRemainderPoints;
  479. }
  480. $where = [];
  481. $where[] = ["r.enterprise_id", "=", $enterprise_id];
  482. $where[] = ["r.card_type", "=", $cardType];
  483. $where[] = ["r.card_number", "=", $cardNumber];
  484. $where[] = ["r.checkState", ">=", IntegralState::SUCCESS];
  485. $where[] = ["d.item_id", "=", $itemId];
  486. $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();
  487. if ($item["stepNeedAmount"] && $item["stepGainPoints"]) {
  488. $returnObj->stepNeedAmount = $item["stepNeedAmount"];
  489. $returnObj->stepGainPoints = $item["stepGainPoints"];
  490. if ($fstGain) {
  491. $returnObj->fstGainRecordId = $fstGain["record_id"];
  492. $returnObj->fstGainDetailId = $fstGain["id"];
  493. //有配置增量积分
  494. if ($item["stepIsYear"] == 1) {
  495. //较上年度增量
  496. $where = [];
  497. $where[] = ["r.enterprise_id", "=", $enterprise_id];
  498. $where[] = ["r.card_type", "=", $cardType];
  499. $where[] = ["r.card_number", "=", $cardNumber];
  500. $where[] = ["r.checkState", ">=", IntegralState::SUCCESS];
  501. $where[] = ["d.item_id", "=", $itemId];
  502. $startTime = date("Y-m-d 00:00:00", strtotime("first day of last year"));
  503. $endTime = date("Y-12-31 23:59:59", strtotime("last day of last year"));
  504. $where[] = ["r.createTime", "between", [$startTime, $endTime]];
  505. $lastYearTotalAmount = Db::table("new_integral_detail")->alias("d")->leftJoin("new_integral_record r", "r.id=d.record_id")->where($where)->sum("d.amount");
  506. $newGainAmount = $amount - $lastYearTotalAmount;
  507. $times = floor($newGainAmount / $item["stepNeedAmount"]);
  508. $returnObj->stepIsYear = 1;
  509. $returnObj->lastYearTotalAmount = $lastYearTotalAmount;
  510. $returnObj->newGainAmount = $newGainAmount;
  511. } else {
  512. $times = floor($amount / $item["stepNeedAmount"]);
  513. $returnObj->stepAmount = $amount;
  514. }
  515. $points = $item["stepGainPoints"] * $times;
  516. $returnObj->times = $times;
  517. $returnObj->points = $returnObj->stepPoints = $points;
  518. } else {
  519. //首次
  520. $returnObj->fstNeedAmount = $item["fstNeedAmount"];
  521. $returnObj->fstGainPoints = $item["fstGainPoints"];
  522. $returnObj->fstAmount = $amount >= $item["fstNeedAmount"] ? $item["fstNeedAmount"] : $amount;
  523. $returnObj->fstPoints = $amount >= $item["fstNeedAmount"] ? $item["fstGainPoints"] : 0;
  524. if ($item["stepIsYear"] != 1) {
  525. //未设置较上年度增量时,首次还需计算增量部分
  526. $stepAmount = $amount - $item["fstNeedAmount"];
  527. if ($stepAmount > 0) {
  528. $times = floor($stepAmount / $item["stepNeedAmount"]);
  529. $returnObj->stepAmount = $stepAmount;
  530. $returnObj->times = $times;
  531. $returnObj->stepPoints = $item["stepGainPoints"] * $times;
  532. }
  533. } else {
  534. $returnObj->stepIsYear = 1;
  535. }
  536. $returnObj->points = $returnObj->stepPoints ? $returnObj->fstPoints + $returnObj->stepPoints : $returnObj->fstPoints;
  537. }
  538. } else {
  539. //仅首次可得积分
  540. if ($fstGain) {
  541. $returnObj->points = 0;
  542. $returnObj->msg = "仅首次能获得积分";
  543. } else {
  544. $returnObj->fstNeedAmount = $item["fstNeedAmount"];
  545. $returnObj->fstGainPoints = $item["fstGainPoints"];
  546. $returnObj->fstAmount = $amount >= $item["fstNeedAmount"] ? $item["fstNeedAmount"] : $amount;
  547. $returnObj->points = $returnObj->fstPoints = $amount >= $item["fstNeedAmount"] ? $item["fstGainPoints"] : 0;
  548. }
  549. }
  550. $point1 = $project["limit"] == 1 ? ($returnObj->projectRemainderPoints > $returnObj->points ? $returnObj->points : $returnObj->projectRemainderPoints) : $returnObj->points;
  551. $point2 = $item["limit"] == 1 ? ($returnObj->itemRemainderPoints > $returnObj->points ? $returnObj->points : $returnObj->itemRemainderPoints) : $returnObj->points;
  552. $returnObj->theoretical = $returnObj->points; //理论值
  553. $returnObj->points = $point1 > $point2 ? $point2 : $point1; //实际值,当有限额时,会与理论值有偏差
  554. }
  555. return $returnObj;
  556. }
  557. /**
  558. * 获取是否在库及积分情况
  559. * @param type $cardType
  560. * @param type $cardNumber
  561. * @return array $response
  562. */
  563. public static function getIntegralRecordByIdCard($cardType, $cardNumber) {
  564. if ($cardType && $cardNumber) {
  565. $where = [];
  566. $where[] = ["card_type", "=", $cardType];
  567. $where[] = ["card_number", "=", $cardNumber];
  568. $where[] = ["checkState", "=", TalentState::CERTIFICATED];
  569. $talentInfo = Db::table("new_talent_info")->where($where)->find();
  570. $where = [];
  571. $where[] = ["card_type", "=", $cardType];
  572. $where[] = ["card_number", "=", $cardNumber];
  573. $integralLog = Db::table("new_integral_log")->where($where)->order("createTime desc")->find();
  574. if (session("user")["usertype"] == 2 && (($talentInfo && session("user")["uid"] != $talentInfo["enterprise_id"]) || ($integralLog && session("user")["uid"] != $integralLog["enterprise_id"]))) {
  575. $response = [
  576. "deny" => 1
  577. ];
  578. } else {
  579. $response = [
  580. "isTalentInLibrary" => $talentInfo ? 1 : 0,
  581. "levelName" => CommonConst::getLayerNameByLayer($integralLog["talentLevel"]),
  582. "level" => $integralLog["talentLevel"],
  583. "totalPoints" => $integralLog["nowPoints"] ?: 0
  584. ];
  585. }
  586. if ($response["deny"]) {
  587. //不是本企业人员
  588. return "该身份证已经在别的企业申报过优秀人才 / 积分项目";
  589. } else {
  590. $levelTypeName = $response["level"] >= 1 && $response["level"] <= 7 ? "在库人才(" . $response["levelName"] . ")" : $response["levelName"];
  591. return sprintf("该人员为%s,截至目前,已累计%d积分", $levelTypeName, $response["totalPoints"]);
  592. }
  593. }
  594. return "";
  595. }
  596. public static function setPublic($mainId, $state, $msg, $batch = null) {
  597. $data["id"] = $mainId;
  598. $data["checkState"] = $state;
  599. switch ($state) {
  600. case IntegralState::ZX_PASS:
  601. case IntegralState::ZX_FAIL:
  602. $data["isPublic"] = 2;
  603. break;
  604. case IntegralState::ANNOUNCED:
  605. $data["isPublic"] = 3;
  606. $data["publicBatch"] = $batch;
  607. break;
  608. case IntegralState::ANNOUNCED_REVERIFY_PASS:
  609. case IntegralState::ANNOUNCED_REVERIFY_FAIL:
  610. $data["isPublic"] = 3;
  611. break;
  612. case IntegralState::PUBLISH_PASS:
  613. $data["getTime"] = $batch;
  614. $data["isPublic"] = 4;
  615. break;
  616. case IntegralState::PUBLISH_FAIL:
  617. $data["isPublic"] = 4;
  618. break;
  619. }
  620. if (IntegralRecord::update($data)) {
  621. TalentLogApi::write(\app\common\state\ProjectState::INTEGRAL, $mainId, $state, $msg, 1);
  622. return true;
  623. }
  624. return false;
  625. }
  626. }