IntegralRecordApi.php 32 KB

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