MPrintNum.Class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * 单据打印统计模型
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2020/4/17
  7. * Time: 16:55
  8. */
  9. namespace JinDouYun\Model\System;
  10. use JinDouYun\Cache\SystemCache;
  11. use JinDouYun\Controller\Common\Logger;
  12. use JinDouYun\Dao\System\DPrintNum;
  13. use Mall\Framework\Core\ErrorCode;
  14. use Mall\Framework\Core\ResultWrapper;
  15. class MPrintNum
  16. {
  17. private $onlineUserId;
  18. private $onlineEnterpriseId;
  19. private $objDPrintNum;
  20. /**
  21. * MPrintNum constructor.
  22. * @param $enterpriseId
  23. */
  24. public function __construct($enterpriseId)
  25. {
  26. $this->onlineEnterpriseId = $enterpriseId;
  27. $this->objDPrintNum = new DPrintNum('default');
  28. }
  29. /**
  30. * @param int $objectNo 单据编号
  31. * @param int $objectType 单据类型 @var StatusCode::$orderType
  32. * @return ResultWrapper
  33. */
  34. public function printIncr($objectNo, $objectType = 0)
  35. {
  36. $dbResult = $this->objDPrintNum->get(['objectNo' => $objectNo, 'enterpriseId' => $this->onlineEnterpriseId]);
  37. if ($dbResult === false) {
  38. return ResultWrapper::fail($this->objDPrintNum->error(), ErrorCode::$dberror);
  39. }
  40. if (isset($dbResult['id'])) {
  41. //增加打印次数
  42. $dbResult = $this->objDPrintNum->set_inc('printNum', ['id' => $dbResult['id']]);
  43. } else {
  44. //从未打印过
  45. $dbResult = $this->objDPrintNum->insert(
  46. [
  47. 'objectNo' => $objectNo,
  48. 'printNum' => 1,
  49. 'enterpriseId' => $this->onlineEnterpriseId
  50. ]
  51. );
  52. }
  53. $objSystemCache = new SystemCache();
  54. $objSystemCache->delObjectPrintNum($this->onlineEnterpriseId,$objectNo);
  55. return ResultWrapper::success($dbResult);
  56. }
  57. /**
  58. * Doc: (des="获取打印次数")
  59. * User: XMing
  60. * Date: 2020/11/14
  61. * Time: 12:12 下午
  62. * @param $objectNo
  63. * @param $objectType
  64. * @return int
  65. * @throws \Exception
  66. */
  67. public function getObjectPrintNum($objectNo,$objectType = 0): int
  68. {
  69. $objSystemCache = new SystemCache();
  70. $printNum = $objSystemCache->getObjectPrintNum($this->onlineEnterpriseId,$objectNo);
  71. if ($printNum != false && $printNum != 0){
  72. return $printNum;
  73. }
  74. $printNum = $this->objDPrintNum->get_field('printNum', ['objectNo' => $objectNo, 'enterpriseId' => $this->onlineEnterpriseId]);
  75. if ($printNum === false || empty($printNum)) {
  76. return 0;
  77. }
  78. $objSystemCache->setObjectPrintNum($this->onlineEnterpriseId,$objectNo,(int)$printNum);
  79. return (int)$printNum;
  80. }
  81. /**
  82. * Doc: (des="批量获取打印次数")
  83. * User: XMing
  84. * Date: 2020/11/14
  85. * Time: 12:27 下午
  86. * @param $objectNos
  87. * @return array
  88. * @throws \Exception
  89. */
  90. public function getObjectPrintNumByNos($objectNos)
  91. {
  92. $objSystemCache = new SystemCache();
  93. $map = [];
  94. $notNos = [];
  95. foreach ($objectNos as $no){
  96. $printNum = $objSystemCache->getObjectPrintNum($this->onlineEnterpriseId,$no);
  97. if ($printNum != false && $printNum != 0){
  98. $map[] = [
  99. 'objectNo' => $no,
  100. 'printNum' => (int)$printNum,
  101. ];
  102. continue;
  103. }
  104. $notNos[] = $no;
  105. }
  106. if (!empty($notNos)){
  107. $printNumLists = $this->objDPrintNum->select(['objectNo' => $objectNos, 'enterpriseId' => $this->onlineEnterpriseId],'objectNo,printNum');
  108. if ($printNumLists === false){
  109. Logger::logs(E_USER_ERROR,'sql错误',__CLASS__,__LINE__,$this->objDPrintNum->error());
  110. }else{
  111. foreach ($printNumLists as $list){
  112. $map[] = [
  113. 'objectNo' => $list['objectNo'],
  114. 'printNum' => $list['printNum'],
  115. ];
  116. if ($list['printNum'] != 0){
  117. $objSystemCache->setObjectPrintNum($this->onlineEnterpriseId,$list['objectNo'],(int)$list['printNum']);
  118. }
  119. }
  120. }
  121. }
  122. return $map;
  123. }
  124. }