MCustomerTypePriceAdjustment.Class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. /**
  3. * 客户类型调价单
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2020/4/8
  7. * Time: 17:50
  8. */
  9. namespace JinDouYun\Model\Price;
  10. use JinDouYun\Cache\PageCache;
  11. use JinDouYun\Cache\PriceCache;
  12. use JinDouYun\Dao\Price\DCustomerPriceEffective;
  13. use JinDouYun\Dao\Price\DCustomerTypePriceAdjustment;
  14. use JinDouYun\Dao\Price\DCustomerTypePriceEffective;
  15. use JinDouYun\Dao\System\DCustomerSource;
  16. use JinDouYun\Model\GoodsManage\MSku;
  17. use JinDouYun\Model\MBaseModel;
  18. use Mall\Framework\Core\ErrorCode;
  19. use Mall\Framework\Core\ResultWrapper;
  20. use Mall\Framework\Core\StatusCode;
  21. class MCustomerTypePriceAdjustment extends MBaseModel
  22. {
  23. private $onlineUserId;
  24. private $onlineEnterpriseId;
  25. private $objDCustomerTypePriceAdjustment;
  26. private $objPriceCache;
  27. /**
  28. * MCustomerTypePriceAdjustment constructor.
  29. * @param $onlineUserId
  30. * @param $onlineEnterpriseId
  31. * @throws \Exception
  32. */
  33. public function __construct($onlineUserId, $onlineEnterpriseId)
  34. {
  35. $this->onlineUserId = $onlineUserId;
  36. $this->onlineEnterpriseId = $onlineEnterpriseId;
  37. parent::__construct($this->onlineEnterpriseId, $this->onlineUserId);
  38. $this->objPriceCache = new PriceCache($this->onlineEnterpriseId);
  39. $this->objDCustomerTypePriceAdjustment = new DCustomerTypePriceAdjustment();
  40. $this->objDCustomerTypePriceAdjustment->setTable($this->objDCustomerTypePriceAdjustment->get_Table() . '_' . $this->onlineEnterpriseId . '_' . date('Y') . '_' . ceil(date('m') / 3));
  41. }
  42. /**
  43. * c
  44. * @param $sheetData
  45. * @return ResultWrapper
  46. */
  47. public function add($sheetData)
  48. {
  49. // 保存调单价数据
  50. $no = createOrderSn(StatusCode::$source['manage'], StatusCode::$orderType['customerTypePriceAdjustment'], $this->onlineUserId);
  51. foreach ($sheetData as $key => $value) {
  52. unset($sheetData[$key]['effective']);
  53. $sheetData[$key]['no'] = $no;
  54. }
  55. $dbResult = $this->objDCustomerTypePriceAdjustment->insert($sheetData, true);
  56. if ($dbResult === false) {
  57. return ResultWrapper::fail($this->objDCustomerTypePriceAdjustment->error(), ErrorCode::$dberror);
  58. }
  59. if (is_array($dbResult)) {
  60. foreach ($dbResult as $key => $id) {
  61. if (isset($sheetData[$key])) {
  62. $sheetData[$key]['id'] = $id;
  63. }
  64. }
  65. }
  66. return ResultWrapper::success($dbResult);
  67. }
  68. /**
  69. *
  70. * 获取所有调价单
  71. */
  72. public function getAll($params,$export = 0)
  73. {
  74. $pageData = pageToOffset($params['page'], $params['pageSize']);
  75. if($export){
  76. $pageData['limit'] = null;
  77. $pageData['offset'] = null;
  78. }
  79. $condition = null;
  80. if ($params['keyword']) {
  81. $condition = " (goodsName like '%" . $params['keyword'] . "%' or goodsCode like '%" . $params['keyword'] . "%' or no like '%" . $params['keyword'] . "%') ";
  82. }
  83. if ($params['startTime'] && $params['endTime']) {
  84. if ($condition !== null) {
  85. $condition .= ' and ';
  86. }
  87. $condition .= " createTime >= " . $params['startTime'] . " and createTime <=" . $params['endTime'];
  88. }
  89. if ($params['effectiveStatus']) {
  90. if ($condition !== null) {
  91. $condition .= ' and ';
  92. }
  93. $condition .= " effectiveStatus = " . $params['effectiveStatus'];
  94. }
  95. if (isset($params['goodsId']) && !empty($params['goodsId'])) {
  96. if ($condition !== null) {
  97. $condition .= ' and ';
  98. }
  99. $condition .= " goodsId = " . $params['goodsId'];
  100. }
  101. if (isset($params['shopId']) && !empty($params['shopId'])){
  102. if ($condition !== null) {
  103. $condition .= ' and ';
  104. }
  105. $condition .= " shopId = " . $params['shopId'];
  106. }
  107. $condition = parent::getShopIdQueryParams($condition);
  108. $total = $this->objDCustomerTypePriceAdjustment->count($condition);
  109. $dbResult = $this->objDCustomerTypePriceAdjustment->select($condition, '*', 'id desc', $pageData['limit'], $pageData['offset']);
  110. if ($dbResult === false) {
  111. return ResultWrapper::fail($this->objDCustomerTypePriceAdjustment->error(), ErrorCode::$dberror);
  112. }
  113. $return = [
  114. 'data' => self::format($dbResult),
  115. 'total' => $total,
  116. ];
  117. //类型调价单导出
  118. if($export){
  119. self::exportPriceSheet($return['data']);
  120. exit;
  121. }
  122. return ResultWrapper::success($return);
  123. }
  124. /**
  125. * 格式化返回数据
  126. */
  127. public function format($data)
  128. {
  129. $skuIds = [];
  130. foreach ($data as $key => &$val) {
  131. if (!is_array($val['salePrice'])) {
  132. $val['salePrice'] = json_decode($val['salePrice'], true);
  133. }
  134. $val['salePrice'] = array_values($val['salePrice']);
  135. foreach ($val['salePrice'] as $row){
  136. $skuIds[] = $row['skuId'];
  137. }
  138. }
  139. $objMSku = new MSku($this->onlineUserId,$this->onlineEnterpriseId);
  140. $skuResult = $objMSku->getSpecNameBySkuId($skuIds);
  141. $allSkuData = [];
  142. if ($skuResult->isSuccess()){
  143. $allSkuData = $skuResult->getData();
  144. }
  145. foreach ($data as &$value){
  146. foreach ($value['salePrice'] as &$row){
  147. $row['unitName'] = isset($allSkuData[$row['skuId']]) ? $allSkuData[$row['skuId']]['unitName'] : '';
  148. $row['specGroup'] = isset($allSkuData[$row['skuId']]) ? $allSkuData[$row['skuId']]['specGroup'] : [];
  149. if (empty($row['specGroup'])){
  150. $value['specType'] = StatusCode::$specType['single'];
  151. }else{
  152. $value['specType'] = StatusCode::$specType['multiple'];
  153. }
  154. }
  155. }
  156. return $data;
  157. }
  158. /**
  159. * 生效操作
  160. * @param $id
  161. * @param $params
  162. * @return ResultWrapper
  163. * @throws \Exception
  164. */
  165. public function effective($id, $params)
  166. {
  167. // 计算客户类型调价单分表
  168. $tableName = 'qianniao_customerTypePriceAdjustmentSheet_' . $this->onlineEnterpriseId . '_' . date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3);
  169. $this->objDCustomerTypePriceAdjustment->setTable($tableName);
  170. $priceAdjustment = $this->objDCustomerTypePriceAdjustment->get($id);
  171. if ($priceAdjustment === false) {
  172. return ResultWrapper::fail($this->objDCustomerTypePriceAdjustment->error(), ErrorCode::$dberror);
  173. }
  174. if (empty($priceAdjustment)) {
  175. return ResultWrapper::fail('要生效的调价单不存在', ErrorCode::$contentNotExists);
  176. }
  177. $this->objDCustomerTypePriceAdjustment->beginTransaction();
  178. // 将调价单变更为生效状态
  179. $dbResult = $this->objDCustomerTypePriceAdjustment->update(
  180. [
  181. 'effectiveStatus' => StatusCode::$auditStatus['auditPass'],
  182. 'effectiveUserName' => $params['effectiveUserName'],
  183. 'effectiveUserId' => $params['effectiveUserId'],
  184. ]
  185. , $id);
  186. if ($dbResult === false) {
  187. $this->objDCustomerTypePriceAdjustment->rollBack();
  188. return ResultWrapper::fail($this->objDCustomerTypePriceAdjustment->error(), ErrorCode::$dberror);
  189. }
  190. // 记录最后生效数据
  191. unset($dbResult);
  192. $objDCustomerPriceEffective = new DCustomerTypePriceEffective();
  193. $objDCustomerPriceEffective->setTable('qianniao_customerTypePriceEffective_' . $this->onlineEnterpriseId);
  194. $data = [
  195. 'no' => $priceAdjustment['no'],
  196. 'goodsId' => $priceAdjustment['goodsId'],
  197. 'shopId' => $priceAdjustment['shopId'],
  198. 'salePrice' => $priceAdjustment['salePrice'],
  199. 'startTime' => $priceAdjustment['startTime'],
  200. 'endTime' => $priceAdjustment['endTime'],
  201. 'customerType' => $priceAdjustment['customerType'],
  202. ];
  203. $dbResult = $objDCustomerPriceEffective->replace($data);
  204. if ($dbResult === false) {
  205. $this->objDCustomerTypePriceAdjustment->rollBack();
  206. return ResultWrapper::fail($objDCustomerPriceEffective->error(), ErrorCode::$dberror);
  207. }
  208. //删除缓存
  209. $this->objPriceCache->delCustomerTypeAdj($priceAdjustment['goodsId'],$priceAdjustment['customerType']);
  210. // 每编辑一件商品需要清除一下小程序商品首页的缓存;
  211. $objPageCache = new PageCache();
  212. $objPageCache->delPage();
  213. $this->objDCustomerTypePriceAdjustment->commit();
  214. return ResultWrapper::success($dbResult);
  215. }
  216. /**
  217. * 获取指定客户类型的可用的调价单
  218. * @param int $shopId 店铺id
  219. * @param int $customerType 客户类型
  220. * @param array $goodsIds 店铺下商品集合
  221. * @return ResultWrapper
  222. */
  223. public function getCustomerTypePriceByIds($shopId, $customerType, $goodsIds)
  224. {
  225. $objDCustomerTypePriceEffective = new DCustomerTypePriceEffective();
  226. $objDCustomerTypePriceEffective->setTable('qianniao_customerTypePriceEffective_' . $this->onlineEnterpriseId);
  227. $where = 'shopId = ' . $shopId . ' and customerType = ' . $customerType. ' and goodsId in ('.implode(',', $goodsIds).')';
  228. $dbResult = $objDCustomerTypePriceEffective->select($where, 'goodsId,salePrice', 'endTime desc');
  229. if ($dbResult === false) {
  230. return ResultWrapper::fail($objDCustomerTypePriceEffective->error(), ErrorCode::$dberror);
  231. }
  232. return ResultWrapper::success($dbResult);
  233. }
  234. /**
  235. * 获取指定商品的最后生效的客户类型调价数据
  236. * @param $params
  237. * @return ResultWrapper
  238. */
  239. public function getCustomerTypePriceByGoodsIds($params)
  240. {
  241. $pageData = pageToOffset($params['page'], $params['pageSize']);
  242. $objDCustomerTypePriceEffective = new DCustomerTypePriceEffective();
  243. $objDCustomerTypePriceEffective->setTable('qianniao_customerTypePriceEffective_' . $this->onlineEnterpriseId);
  244. $conditon = [
  245. 'goodsId' => $params['goodsId'],
  246. ];
  247. $dbResult = $objDCustomerTypePriceEffective->select($conditon, 'id,customerType,salePrice', 'id desc',$pageData['limit'],$pageData['offset']);
  248. if ($dbResult === false) {
  249. return ResultWrapper::fail($objDCustomerTypePriceEffective->error(), ErrorCode::$dberror);
  250. }
  251. $count = $objDCustomerTypePriceEffective->count($conditon);
  252. if ($count === false) {
  253. return ResultWrapper::fail($objDCustomerTypePriceEffective->error(), ErrorCode::$dberror);
  254. }
  255. $data = self::formatCustomerType($dbResult);
  256. if(!$data->isSuccess()){
  257. return ResultWrapper::fail($objDCustomerTypePriceEffective->error(), ErrorCode::$dberror);
  258. }
  259. $dbResult = $data->getData();
  260. foreach ($dbResult as $key =>$value){
  261. $dbResult[$key]['salePrice'] = json_decode($value['salePrice']);
  262. }
  263. $return = [
  264. 'data' => $dbResult,
  265. 'total' => $count,
  266. ];
  267. return ResultWrapper::success($return);
  268. }
  269. /**
  270. * 类型调价单导出方法
  271. * @param $priceSheetData
  272. */
  273. private static function exportPriceSheet($priceSheetData)
  274. {
  275. //导出到本地
  276. header("Content-type:application/vnd.ms-excel");
  277. header("Content-Disposition:filename=调价单明细表记录.csv");
  278. header('Cache-Control: max-age=0');
  279. $fp = fopen('php://output', 'a');
  280. $head = ['调价单编码','客户类型','商品编码', '商品名称', '调价时间', '调整规格','调整市场价格','调整人','相关店铺', '状态']; //定义标题
  281. foreach ($head as $i => $v) {
  282. $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8'); //将中文标题转换编码,否则乱码
  283. }
  284. fputcsv($fp, $head);
  285. $limit = 10000;
  286. $num = 0;//计数器
  287. foreach ($priceSheetData as $v) {
  288. //调整规格与价格
  289. $price = '';
  290. $unitName = '';
  291. if(!empty($v['salePrice'])){
  292. foreach ($v['salePrice'] as $key =>$value){
  293. $price = $value['price'];
  294. $unitName = $value['unitName'];
  295. }
  296. }
  297. //循环数据
  298. $num++;
  299. if ($num == $limit) {
  300. ob_flush();//释放内存
  301. flush();
  302. }
  303. $rows['no'] = isset($v['no']) ? $v['no'] : '';//调价单编码
  304. $rows['customerName'] = isset($v['customerName']) ? $v['customerName'] : '';//客户类型
  305. $rows['goodsCode'] = isset($v['goodsCode']) ? $v['goodsCode'] : '';//商品编码
  306. $rows['goodsName'] = isset($v['goodsName']) ? $v['goodsName'] : '';//商品名称
  307. $rows['createTime'] = isset($v['createTime']) ? date('Y-m-d H:i:s',$v['createTime']) : '';//调价时间
  308. $rows['unitName'] = $unitName;//调整规格
  309. $rows['price'] = $price;//调整市场价格
  310. //$rows['ladderPrice'] = isset($v['ladderPrice']) ? $v['ladderPrice'] : "否";//阶梯价
  311. //$rows['salePrice'] = isset($v['salePrice']) ? $v['salePrice'] : '';//销售价
  312. $rows['createUserName'] = isset($v['createUserName']) ? $v['createUserName'] : '';//调整人
  313. $rows['shopName'] = isset($v['shopName']) ? $v['shopName'] : '';//相关店铺
  314. $rows['effectiveStatus'] = $v['effectiveStatus'] === 2 ? '生效' : '未生效';//状态
  315. foreach ($rows as $kk => $vv) {
  316. $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码
  317. }
  318. fputcsv($fp, $rs);
  319. $rows = [];
  320. }
  321. exit;
  322. }
  323. /**
  324. * 删除指定商品的最后生效的客户类型调价数据
  325. * @param array $id
  326. * @return ResultWrapper
  327. */
  328. public function delCustomerTypePrice($params)
  329. {
  330. $objDCustomerTypePriceEffective = new DCustomerTypePriceEffective();
  331. $objDCustomerTypePriceEffective->setTable('qianniao_customerTypePriceEffective_' . $this->onlineEnterpriseId);
  332. $dbResult = $objDCustomerTypePriceEffective->select($params['id']);
  333. if ($dbResult === false) {
  334. return ResultWrapper::fail($objDCustomerTypePriceEffective->error(), ErrorCode::$dberror);
  335. }
  336. if(empty($dbResult)){
  337. return ResultWrapper::fail('要作废的调价单不存在', ErrorCode::$contentNotExists);
  338. }
  339. foreach ($dbResult as $key => $value){
  340. $value['salePrice'] = json_decode($value['salePrice'], true);
  341. foreach ($value['salePrice'] as $k => $v){
  342. if($v['skuId'] == $params['skuId']){
  343. unset($value['salePrice'][$k]);
  344. continue;
  345. }
  346. }
  347. $value['salePrice'] = json_encode( $value['salePrice']);
  348. $updateDbResult = $objDCustomerTypePriceEffective->update(['salePrice'=>$value['salePrice']], $value['id']);
  349. if ($updateDbResult === false) {
  350. return ResultWrapper::fail($objDCustomerTypePriceEffective->error(), ErrorCode::$dberror);
  351. }
  352. }
  353. return ResultWrapper::success($updateDbResult);
  354. }
  355. /**
  356. * 格式化客户类型名称
  357. * @return array
  358. */
  359. public function formatCustomerType($data)
  360. {
  361. //根据客户类型id进行渲染类型名称
  362. $customerTypeIdBindCustomerTypeName = [];
  363. $customerTypeIds = [];
  364. foreach ($data as $k => $v){
  365. if(!empty($v['customerType'])){ //客户类型id ”customerType“
  366. $customerTypeIds[] = $v['customerType'];
  367. }
  368. }
  369. if(!empty($customerTypeIds)){
  370. $objDCustomerSource = new DCustomerSource();
  371. $customerSourceData = $objDCustomerSource->select($customerTypeIds, 'id,name');
  372. if($customerSourceData == false){
  373. return ResultWrapper::fail($objDCustomerSource->error(), ErrorCode::$dberror);
  374. }
  375. if(!empty($customerSourceData)){
  376. foreach ($customerSourceData as $k => $v){
  377. $customerTypeIdBindCustomerTypeName[$v['id']] = $v['name'];
  378. }
  379. }
  380. // 循环主数据,进行对应的字段映射操作
  381. foreach ($data as $key => $value){
  382. $data[$key]['customerTypeName'] = isset($customerTypeIdBindCustomerTypeName[$value['customerType']]) ? $customerTypeIdBindCustomerTypeName[$value['customerType']] :'';
  383. }
  384. }
  385. return ResultWrapper::success($data);
  386. }
  387. }