IntegralRecordApi.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 getPublicList($params) {
  51. $order = $params["order"];
  52. $offset = $params["offset"];
  53. $limit = $params["limit"];
  54. $where = [];
  55. $where[] = ["e.type", "=", session("user")["type"]];
  56. if ($params["name"]) {
  57. $where[] = ["ir.name", "like", "%" . $params["name"] . "%"];
  58. }
  59. if ($params["checkState"]) {
  60. $where[] = ["ir.checkState", "=", $params["checkState"]];
  61. }
  62. $type = $params["type"];
  63. switch ($type) {
  64. case 1:
  65. case 2:
  66. $where[] = ["ir.checkState", "=", TalentState::REVERIFY_PASS];
  67. break;
  68. case 3: //公示
  69. case 7: //公示预览
  70. $where[] = ["ir.checkState", "=", TalentState::ZX_PASS];
  71. break;
  72. case 4: //公示通过
  73. $where[] = ["ir.checkState", "=", TalentState::ANNOUNCED];
  74. break;
  75. case 5:
  76. case 8: //公布预览
  77. $where[] = ["ir.checkState", "=", TalentState::ANNOUNCED_REVERIFY_PASS];
  78. break;
  79. case 6:
  80. $where[] = ["ir.checkState", "=", TalentState::PUBLISH_PASS];
  81. break;
  82. }
  83. $enterprise_tag_kvs = DictApi::selectByParentCode("enterprise_tag");
  84. $count = IntegralRecord::alias("ir")->leftJoin("un_enterprise e", "e.id=ir.enterprise_id")->where($where)->count();
  85. $list = IntegralRecord::alias("ir")->leftJoin("un_enterprise e", "e.id=ir.enterprise_id")
  86. ->where($where)
  87. ->limit($offset, $limit)
  88. ->order("ir.createTime " . $order)->field("ir.*,e.name as enterpriseName,e.type as enterprise_type,enterpriseTag")->select()->toArray();
  89. foreach ($list as &$item) {
  90. $item["talent_type"] = $item["enterprise_type"] == 1 ? "晋江市现代产业体系人才" : "集成电路优秀人才";
  91. $item["enterprise_tag"] = $enterprise_tag_kvs[$item["enterpriseTag"]];
  92. }unset($item);
  93. return ["total" => $count, "rows" => $list];
  94. }
  95. public static function getListByProcess($params) {
  96. $process = $params["process"] ?: 1;
  97. $where = [];
  98. switch ($process) {
  99. case 1://初审阶段
  100. switch ($params["checkState"]) {
  101. case 1://待审核
  102. $where[] = ["ir.checkState", "=", IntegralState::SUBMIT];
  103. $where[] = ["tl.state", "=", IntegralState::SUBMIT];
  104. break;
  105. case 2://驳回
  106. $where[] = ["tl.new_state", "in", [IntegralState::SAVE]];
  107. $where[] = ["tl.state", "in", [IntegralState::VERIFY_REJECT]];
  108. break;
  109. case 3:
  110. //审核失败
  111. $where[] = ["ir.checkState", "in", [IntegralState::VERIFY_FAIL]];
  112. break;
  113. default:
  114. $where[] = ["tl.state", "in", [IntegralState::SUBMIT, IntegralState::VERIFY_REJECT, IntegralState::VERIFY_FAIL]];
  115. }
  116. break;
  117. case 2://复审阶段
  118. switch ($params["checkState"]) {
  119. case 1://待审核
  120. $where[] = ["tl.state", "=", IntegralState::VERIFY_PASS];
  121. break;
  122. case 3:
  123. //审核失败
  124. $where[] = ["tl.state", "in", [IntegralState::VERIFY_FAIL]];
  125. break;
  126. default:
  127. $where[] = ["tl.state", "in", [IntegralState::VERIFY_PASS, IntegralState::REVERIFY_FAIL]];
  128. }
  129. break;
  130. case 3://复审后征信公示等状态
  131. if ($params["checkState"]) {
  132. $where[] = ["ir.checkState", "=", $params["checkState"]];
  133. } else {
  134. $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]];
  135. }
  136. break;
  137. }
  138. $order = $params["order"] ?: "desc";
  139. $offset = $params["offset"] ?: 0;
  140. $limit = $params["limit"] ?: 10;
  141. $where[] = ["delete", "=", 0];
  142. if ($params["name"]) {
  143. $where[] = ["ir.name", "like", "%{$params['name']}%"];
  144. }
  145. if ($params["card_number"]) {
  146. $where[] = ["ir.card_number", "like", "%{$params['card_number']}%"];
  147. }
  148. if ($params["shareholder"]) {
  149. $where[] = ["ir.shareholder", "=", $params['shareholder']];
  150. }
  151. $count = IntegralRecord::alias("ir")->where($where)
  152. ->leftJoin("(select description,mainId,last_state,new_state,state,createTime from new_talent_checklog where createTime in (select max(createTime) 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")
  153. ->count();
  154. $list = IntegralRecord::alias("ir")->where($where)
  155. ->leftJoin("(select description,mainId,last_state,new_state,state,createTime from new_talent_checklog where createTime in (select max(createTime) 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")
  156. ->field("ir.*,tl.state as real_state,tl.last_state,if(ir.updateTime is not null,ir.updateTime,ir.createTime) as orderTime")->limit($offset, $limit)->order("orderTime " . $order)->select();
  157. foreach ($list as $key => $item) {
  158. $list[$key]["apply_year"] = BatchApi::getOne($item["batch_id"])["batch"];
  159. $tmp_items = [];
  160. foreach ($item["detail"] as $_item) {
  161. $integral_item_info = getCacheById("IntegralItem", $_item["item_id"]);
  162. $tmp_items[] = sprintf("%s(%s%s)", $integral_item_info["name"], $_item["amount"], $integral_item_info["unit"]);
  163. }
  164. $list[$key]["details"] = implode(";", $tmp_items);
  165. $list[$key]["type"] = session("user")["type"];
  166. //$last_log = TalentLogApi::getLastLog($item["id"], \app\common\state\ProjectState::INTEGRAL);
  167. //$list[$key]["real_state"] = $last_log["state"];
  168. //$list[$key]["last_state"] = $last_log["last_state"];
  169. }
  170. return ["total" => $count, "rows" => $list];
  171. }
  172. public static function checkIsEditable($id) {
  173. $info = self::getOne($id);
  174. if (!$info || !in_array($info["checkState"], [0, IntegralState::SAVE]))
  175. return false;
  176. return true;
  177. }
  178. static public function chkIsOwner($id, $uid) {
  179. $info = self::getOne($id);
  180. if ($info["enterprise_id"] != $uid)
  181. return null;
  182. return $info;
  183. }
  184. /**
  185. * 导出
  186. * @param type $process
  187. * @param type $params
  188. * @return type
  189. */
  190. public static function getExportDatas($process, $params) {
  191. $where[] = [];
  192. //特殊字段处理
  193. $fields = [];
  194. $fields[] = "ir.id";
  195. foreach ($params as $param) {
  196. if (!in_array($param, ["enterpriseName", "street", "checkMsg", "project", "year"])) {
  197. $fields[] = "ir." . $param;
  198. }
  199. }
  200. $fields[] = "e.name as enterpriseName";
  201. $fields[] = "e.street";
  202. $fields[] = "tl.description as checkMsg";
  203. $fields[] = "b.batch as year";
  204. if (in_array("card_type", $params)) {
  205. $cardTypes = DictApi::selectByParentCode("card_type");
  206. }
  207. if (in_array("street", $params)) {
  208. $streets = DictApi::selectByParentCode("street");
  209. }
  210. $sex = [1 => "男", 2 => "女"];
  211. $where = [];
  212. $where[] = ["e.type", "=", session("user")["type"]];
  213. switch ($process) {
  214. case 1:
  215. $where = "ir.checkState in (" . IntegralState::SUBMIT . "," . IntegralState::VERIFY_FAIL . ")";
  216. break;
  217. case 2:
  218. $where = "ir.checkState in (" . IntegralState::VERIFY_PASS . "," . IntegralState::REVERIFY_FAIL . ")";
  219. break;
  220. case 3:
  221. $where = "ir.checkState >= " . IntegralState::REVERIFY_PASS;
  222. break;
  223. }
  224. $list = IntegralRecord::alias("ir")
  225. ->field($fields)
  226. ->leftJoin("un_enterprise e", "e.id=ir.enterprise_id")
  227. ->leftJoin("sys_batch b", "b.id=ir.batch_id")
  228. ->leftJoin("(select description,mainId,last_state,new_state,state,createTime from new_talent_checklog where createTime in (select max(createTime) 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")
  229. //->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)")
  230. ->whereRaw($where)
  231. ->select()->toArray();
  232. foreach ($list as &$item) {
  233. $item["card_type"] = $cardTypes[$item["card_type"]];
  234. $item["street"] = $streets[$item["street"]];
  235. $item["sex"] = $sex[$item["sex"]];
  236. $item["checkState"] = IntegralState::getStateName($item["checkState"]);
  237. $detail = \app\common\model\IntegralDetail::where("record_id", $item["id"])->select();
  238. $tmp_items = [];
  239. foreach ($detail as $_item) {
  240. $integral_item_info = getCacheById("IntegralItem", $_item["item_id"]);
  241. $tmp_items[] = sprintf("%s(%s%s)", $integral_item_info["name"], $_item["amount"], $integral_item_info["unit"]);
  242. }
  243. $item["project"] = implode(";", $tmp_items);
  244. }unset($item);
  245. return $list;
  246. }
  247. /**
  248. * 计算积分
  249. * @param type $enterpriseId 企业id
  250. * @param type $cardType 证件类型
  251. * @param type $cardNumber 证件号码
  252. * @param type $itemId 标准id
  253. * @param type $amount 达成数额
  254. * @return \stdClass 完整积分记录对象
  255. */
  256. public static function calIntegral($enterpriseId, $cardType, $cardNumber, $itemId, $amount) {
  257. $returnObj = new \stdClass();
  258. $returnObj->amount = $amount;
  259. $item = getCacheById("IntegralItem", $itemId);
  260. $projectRemainderPoints = 0; //项目剩余总上限
  261. $itemRemainderPoints = 0; //标准剩余总上限
  262. $points = 0; //当前可获得积分
  263. if ($item) {
  264. $project = getCacheById("IntegralProject", $item["projectId"]);
  265. $projectRemainderPoints = $project["max"]; //初始化项目可获得剩余积分
  266. $itemRemainderPoints = $item["max"]; //初始化标准可获得剩余积分
  267. if ($project["limit"] == 1) {
  268. //项目下所有规则总上限
  269. $where = [];
  270. $where[] = ["r.enterprise_id", "=", $enterprise_id];
  271. $where[] = ["r.card_type", "=", $cardType];
  272. $where[] = ["r.card_number", "=", $cardNumber];
  273. $where[] = ["r.checkState", ">=", IntegralState::SUCCESS];
  274. $where[] = ["i.projectId", "=", $project["id"]];
  275. if ($project["yearly"] == 1) {
  276. //年度重置只算当年度
  277. $startTime = date("Y-01-01 00:00:00");
  278. $endTime = date("Y-12-31 23:59:59");
  279. $where[] = ["r.createTime", "between", [$startTime, $endTime]];
  280. $returnObj->projectYearly = 1;
  281. }
  282. $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");
  283. $projectRemainderPoints -= $totalPoints;
  284. $returnObj->projectMaxPoints = $project["max"];
  285. $returnObj->projectRemainderPoints = $projectRemainderPoints;
  286. }
  287. if ($item["limit"] == 1) {
  288. //规则上限
  289. $where = [];
  290. $where[] = ["r.enterprise_id", "=", $enterprise_id];
  291. $where[] = ["r.card_type", "=", $cardType];
  292. $where[] = ["r.card_number", "=", $cardNumber];
  293. $where[] = ["r.checkState", ">=", IntegralState::SUCCESS];
  294. $where[] = ["d.item_id", "=", $itemId];
  295. if ($item["yearly"] == 1) {
  296. //年度重置只算当年度
  297. $startTime = date("Y-01-01 00:00:00");
  298. $endTime = date("Y-12-31 23:59:59");
  299. $where[] = ["r.createTime", "between", [$startTime, $endTime]];
  300. $returnObj->itemYearly = 1;
  301. }
  302. $totalPoints = Db::table("new_integral_detail")->alias("d")->leftJoin("new_integral_record r", "r.id=d.record_id")->where($where)->sum("d.point");
  303. $itemRemainderPoints -= $totalPoints;
  304. $returnObj->itemMaxPoints = $item["max"];
  305. $returnObj->itemRemainderPoints = $itemRemainderPoints;
  306. }
  307. $where = [];
  308. $where[] = ["r.enterprise_id", "=", $enterprise_id];
  309. $where[] = ["r.card_type", "=", $cardType];
  310. $where[] = ["r.card_number", "=", $cardNumber];
  311. $where[] = ["r.checkState", ">=", IntegralState::SUCCESS];
  312. $where[] = ["d.item_id", "=", $itemId];
  313. $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();
  314. if ($item["stepNeedAmount"] && $item["stepGainPoints"]) {
  315. $returnObj->stepNeedAmount = $item["stepNeedAmount"];
  316. $returnObj->stepGainPoints = $item["stepGainPoints"];
  317. if ($fstGain) {
  318. $returnObj->fstGainRecordId = $fstGain["record_id"];
  319. $returnObj->fstGainDetailId = $fstGain["id"];
  320. //有配置增量积分
  321. if ($item["stepIsYear"] == 1) {
  322. //较上年度增量
  323. $where = [];
  324. $where[] = ["r.enterprise_id", "=", $enterprise_id];
  325. $where[] = ["r.card_type", "=", $cardType];
  326. $where[] = ["r.card_number", "=", $cardNumber];
  327. $where[] = ["r.checkState", ">=", IntegralState::SUCCESS];
  328. $where[] = ["d.item_id", "=", $itemId];
  329. $startTime = date("Y-m-d 00:00:00", strtotime("first day of last year"));
  330. $endTime = date("Y-12-31 23:59:59", strtotime("last day of last year"));
  331. $where[] = ["r.createTime", "between", [$startTime, $endTime]];
  332. $lastYearTotalAmount = Db::table("new_integral_detail")->alias("d")->leftJoin("new_integral_record r", "r.id=d.record_id")->where($where)->sum("d.amount");
  333. $newGainAmount = $amount - $lastYearTotalAmount;
  334. $times = floor($newGainAmount / $item["stepNeedAmount"]);
  335. $returnObj->stepIsYear = 1;
  336. $returnObj->lastYearTotalAmount = $lastYearTotalAmount;
  337. $returnObj->newGainAmount = $newGainAmount;
  338. } else {
  339. $times = floor($amount / $item["stepNeedAmount"]);
  340. $returnObj->stepAmount = $amount;
  341. }
  342. $points = $item["stepGainPoints"] * $times;
  343. $returnObj->times = $times;
  344. $returnObj->points = $returnObj->stepPoints = $points;
  345. } else {
  346. //首次
  347. $returnObj->fstNeedAmount = $item["fstNeedAmount"];
  348. $returnObj->fstGainPoints = $item["fstGainPoints"];
  349. $returnObj->fstAmount = $amount >= $item["fstNeedAmount"] ? $item["fstNeedAmount"] : $amount;
  350. $returnObj->fstPoints = $amount >= $item["fstNeedAmount"] ? $item["fstGainPoints"] : 0;
  351. if ($item["stepIsYear"] != 1) {
  352. //未设置较上年度增量时,首次还需计算增量部分
  353. $stepAmount = $amount - $item["fstNeedAmount"];
  354. if ($stepAmount > 0) {
  355. $times = floor($stepAmount / $item["stepNeedAmount"]);
  356. $returnObj->stepAmount = $stepAmount;
  357. $returnObj->times = $times;
  358. $returnObj->stepPoints = $item["stepGainPoints"] * $times;
  359. }
  360. } else {
  361. $returnObj->stepIsYear = 1;
  362. }
  363. $returnObj->points = $returnObj->stepPoints ? $returnObj->fstPoints + $returnObj->stepPoints : $returnObj->fstPoints;
  364. }
  365. } else {
  366. //仅首次可得积分
  367. if ($fstGain) {
  368. $returnObj->points = 0;
  369. $returnObj->msg = "仅首次能获得积分";
  370. } else {
  371. $returnObj->fstNeedAmount = $item["fstNeedAmount"];
  372. $returnObj->fstGainPoints = $item["fstGainPoints"];
  373. $returnObj->fstAmount = $amount >= $item["fstNeedAmount"] ? $item["fstNeedAmount"] : $amount;
  374. $returnObj->points = $returnObj->fstPoints = $amount >= $item["fstNeedAmount"] ? $item["fstGainPoints"] : 0;
  375. }
  376. }
  377. $point1 = $project["limit"] == 1 ? ($returnObj->projectRemainderPoints > $returnObj->points ? $returnObj->points : $returnObj->projectRemainderPoints) : $returnObj->points;
  378. $point2 = $item["limit"] == 1 ? ($returnObj->itemRemainderPoints > $returnObj->points ? $returnObj->points : $returnObj->itemRemainderPoints) : $returnObj->points;
  379. $returnObj->theoretical = $returnObj->points; //理论值
  380. $returnObj->points = $point1 > $point2 ? $point2 : $point1; //实际值,当有限额时,会与理论值有偏差
  381. }
  382. return $returnObj;
  383. }
  384. }