Good.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\txx\controller;
  4. use app\BaseController;
  5. use app\txx\model\Act;
  6. use app\txx\model\ActGood;
  7. use app\txx\model\YzGood;
  8. use app\txx\model\PlatformYouzan;
  9. use think\App;
  10. use think\Exception;
  11. use think\facade\Db;
  12. use think\facade\Validate;
  13. use think\Request;
  14. class Good extends BaseController
  15. {
  16. protected $uid=0;
  17. protected $uname='';
  18. protected $platformid=37;
  19. /**@param array snArr 商品编号sn数组
  20. * @param string actCode 活动编号
  21. * @return \think\response\Json
  22. * @throws \think\exception\DbException
  23. */
  24. private $acton=[];
  25. public $noble=[];
  26. public $origin_img_host ="http://stock.api.wanyuhengtong.com";
  27. public $ssl_img_host ="https://image.wanyuhengtong.com";
  28. public function __construct(App $app) {
  29. parent::__construct($app);
  30. if($this->request->isCx==1){
  31. $this->uid=$this->request->uid;
  32. $this->uname=$this->request->uname;
  33. }
  34. }
  35. public function AddGood()
  36. {
  37. $post = $this->request->only(["snArr"=>[],"actCode"=>''],"post");
  38. $snArr= $post['snArr'];
  39. if(empty($snArr)) return json_show(1004,"参数错误 snArr 不能为空");
  40. $platformYouzan =new PlatformYouzan();
  41. $skuCodes = array_column($snArr,"skuCode");
  42. $goodArr = $platformYouzan->where(["skuCode"=>$skuCodes,"is_del"=>0])->select()->toArray();
  43. if(empty($goodArr)) return json_show(1005,"未找到商品有效数据");
  44. $actCode= $post['actCode']!=''? trim($post['actCode']):"";
  45. if($actCode=='') return json_show(1004,"参数错误 actCode 不能为空");
  46. // $roundId= $post['roundId']!=''? trim($post['roundId']):"";
  47. // if($roundId=='') return json_show(1004,"参数错误 roundId 不能为空");
  48. $act =new Act();
  49. $isActExit=$act->where(['actCode'=>$actCode,"is_del"=>0])->find();
  50. if($isActExit==false) return json_show(1005,"未找到活动有效数据");
  51. $actGood=new ActGood();
  52. $yzgood= new YzGood();
  53. $yzadd = $yzgood->where(["item_no"=>array_column($snArr, 'skuCode'),"is_del"=>0])->column('detail_url,origin', 'item_no');
  54. Db::startTrans();
  55. try{
  56. $data=[];
  57. foreach ($snArr as $item){
  58. $isExit = $actGood->find(["actCode"=>$actCode,"yz_good_code"=>$item['skuCode'],"roundId"=>$item['roundId']??"","is_del"=>0]);
  59. if($isExit) throw new Exception("活动商品已添加",1006);
  60. $temp=[
  61. "actCode"=>$actCode,
  62. "yz_good_code"=>$item['skuCode'],
  63. "stock_num"=>$item['stock_num'],
  64. "used_num"=>0,
  65. "balance_num"=>$item['stock_num'],
  66. "apply_id"=>$this->uid,
  67. "apply_name"=>$this->uname,
  68. "roundId"=>$item['roundId']??"",
  69. "good_url"=>$yzadd[$item['skuCode']]?$yzadd[$item['skuCode']]['detail_url']??"":"",
  70. "origin_price"=>$yzadd[$item['skuCode']]?$yzadd[$item['skuCode']]['origin']??"":"",
  71. "status"=>1,
  72. "addtime"=>date("Y-m-d H:i:s"),
  73. "updatetime"=>date("Y-m-d H:i:s")
  74. ];
  75. $data[]=$temp;
  76. }
  77. if (empty($data)){
  78. throw new Exception("活动商品不能为空",1006);
  79. }
  80. $isAdd=ActGood::insertAll($data);
  81. if ($isAdd==false){
  82. throw new Exception("活动商品添加失败",1006);
  83. }
  84. $up =$act->where($isActExit->toArray())->update(["status"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  85. if ($up==false){
  86. throw new Exception("活动商品添加失败",1006);
  87. }
  88. Db::commit();
  89. return json_show(0,"活动商品添加成功");
  90. }catch (\Exception $e){
  91. Db::rollback();
  92. return json_show(1008,$e->getMessage());
  93. }
  94. }
  95. /**活动商品列表
  96. * @param int page 页数
  97. * @param int size 每页显示数量
  98. * @param string act_name 活动名称
  99. * @param string actCode 活动编号
  100. * @param string company_name 活动公司名称
  101. * @param string contactor 活动联系人
  102. * @param int act_type 活动类型 暂无
  103. * @param int cat_id 采销分类
  104. * @param string spuCode 商品成本编号
  105. * @param string skuCode 商品上线编号
  106. * @param int exam_status 商品上线审核状态
  107. */
  108. public function list()
  109. {
  110. $param = $this->request->only([
  111. "page"=>1,
  112. "size"=>15,
  113. "act_name"=>'',
  114. "actCode"=>'',
  115. "company_name"=>'',
  116. "contactor"=>'',
  117. "act_type"=>'',
  118. "good_name"=>'',
  119. "cat_id"=>'',
  120. "spuCode"=>'',
  121. "skuCode"=>'',
  122. "status"=>"",
  123. "roundId"=>"",
  124. "exam_status"=>'',
  125. ],"post",'trim');
  126. $condition=[["a.is_del","=",0],["b.is_del","=",0]];
  127. if($param['act_name']!='') $condition[]=["b.act_name","like","%{$param['act_name']}%"];
  128. if($param['actCode']!='') $condition[]=["a.actCode","like","%{$param['actCode']}%"];
  129. if($param['company_name']!='') $condition[]=["b.company_name","like","%{$param['company_name']}%"];
  130. if($param['contactor']!='') $condition[]=["b.contactor","like","%{$param['contactor']}%"];
  131. if($param['good_name']!='') $condition[]=["d.good_name","like","%{$param['good_name']}%"];
  132. if($param['skuCode']!='') $condition[]=["c.skuCode","like","%{$param['skuCode']}%"];
  133. if($param['act_type']!='') $condition[]=["b.act_type","=",$param["act_type"]];
  134. if($param['roundId']!=='') $condition[]=["a.roundId","=",$param["roundId"]];
  135. if($param['status']!=='') $condition[]=["a.status","=",$param["status"]];
  136. if ($param['exam_status'] !== '') {
  137. if($param['exam_status']==1){
  138. $where[] = ['c.exam_status', "=", 6];
  139. }else{
  140. $where[] = ['c.exam_status', "<>", 6];
  141. }
  142. }
  143. $actm=new ActGood();
  144. if($this->request->isCx==1){
  145. $field="a.id,a.actCode,a.yz_good_code,a.apply_id,a.apply_name,a.stock_num,a.used_num,a.balance_num,a.status,
  146. a.addtime,a.updatetime,c.plat_code,b.act_name,b.company_name,b.contactor,b.mobile,b.startTime,b.endTime,c.spuCode,
  147. c.final_price,c.desc,d.good_name,d.good_unit,c.yz_cat_id,d.cat_id,a.roundId,d.brand_id,a.origin_price,a.good_url";
  148. }else{
  149. $field="a.id,a.actCode,a.yz_good_code,a.stock_num,a.used_num,a.balance_num,a.status,a.addtime,a.updatetime,
  150. b.act_name,b.company_name,b.contactor,b.mobile,b.startTime,b.endTime,c.final_price,c.desc,d.good_name,c.spuCode,
  151. d.good_unit,d.cat_id,d.brand_id,a.roundId,a.origin_price,a.good_url";
  152. }
  153. $count =$actm->alias("a")
  154. ->leftJoin("act b","a.actCode=b.actCode")
  155. ->leftJoin("platform_youzan c","a.yz_good_code=c.skuCode")
  156. ->leftJoin("good d","c.spuCode=d.spuCode")
  157. ->where($condition)->count();
  158. $total =ceil($count/$param['size']);
  159. $page = $param['page']>= $total ?intval($total):intval($param['page']);
  160. $list=$actm->alias("a")
  161. ->leftJoin("act b","a.actCode=b.actCode")
  162. ->leftJoin("platform_youzan c","a.yz_good_code=c.skuCode")
  163. ->leftJoin("good d","c.spuCode=d.spuCode")
  164. ->field($field)->where($condition)->page($page,intval($param['size']))
  165. ->order("addtime desc")->select()->toArray();
  166. //6.补充数据,照搬list方法
  167. $all_brand = Db::name('brand')
  168. ->where('is_del', 0)
  169. ->whereIn('id', array_column($list, 'brand_id'))
  170. ->column('brand_name', 'id');
  171. $unit = Db::name("unit")->where(["id" => array_column($list, 'good_unit')])->column('unit', 'id');
  172. foreach ($list as &$item){
  173. $item['unit'] =$unit[$item['good_unit']] ?? "";
  174. $item['brand_name'] = $all_brand[$item['brand_id']]?? "";
  175. $item['stock_total'] = Db::name("good_stock")->where(['spuCode' => $item['spuCode'], "is_del" => 0])->sum("usable_stock");
  176. if($this->request->isCx==1){
  177. $item['cat_info'] = made($item['cat_id'], []);
  178. }else{
  179. unset($item['cat_id']);
  180. unset($item['good_unit']);
  181. unset($item['brand_id']);
  182. }
  183. }
  184. return json_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  185. }
  186. /**
  187. * 保存新建的资源
  188. *
  189. * @param \think\Request $request
  190. * @return \think\Response
  191. */
  192. public function EditGood()
  193. {
  194. $post = $this->request->only(["goodArr"=>[]],"post");
  195. $snArr= $post['goodArr'];
  196. if(empty($snArr)) return json_show(1004,"参数 goodArr 不能为空");
  197. $platformYouzan =new PlatformYouzan();
  198. $goodCode = array_column($snArr,"yz_good_code");
  199. $goodArr = $platformYouzan->where(["skuCode"=>$goodCode,"is_del"=>0])->select()->toArray();
  200. if(empty($goodArr)) return json_show(1005,"未找到商品有效数据");
  201. // $roundId= $post['roundId']!=''? trim($post['roundId']):"";
  202. // if($roundId=='') return json_show(1004,"参数错误 roundId 不能为空");
  203. $actGood=new ActGood();
  204. $yzgood= new YzGood();
  205. $yzadd = $yzgood->where(["item_no"=>array_column($snArr, 'yz_good_code'),"is_del"=>0])->column('detail_url,origin', 'item_no');
  206. Db::startTrans();
  207. try{
  208. $data=[];
  209. foreach ($snArr as $item){
  210. $isExit = $actGood->where(["actCode"=>$item['actCode'],"yz_good_code"=>$item['yz_good_code'],
  211. "is_del"=>0])->find();
  212. if($isExit==false) throw new Exception("活动商品不存在",1006);
  213. $temp=[
  214. "id"=>$isExit['id'],
  215. "stock_num"=>$item['stock_num'],
  216. "balance_num"=>$item['stock_num'],
  217. "roundId"=>$item['roundId']??"",
  218. "good_url"=>$yzadd[$item['yz_good_code']]?$yzadd[$item['yz_good_code']]['detail_url']??"":"",
  219. "origin_price"=>$yzadd[$item['yz_good_code']]?$yzadd[$item['yz_good_code']]['origin']??"":"",
  220. "is_del"=>$item['is_del']??0,
  221. "updatetime"=>date("Y-m-d H:i:s")
  222. ];
  223. $data[]=$temp;
  224. }
  225. $isAdd=$actGood->saveAll($data);
  226. if ($isAdd==false){
  227. throw new Exception("活动商品更新失败",1006);
  228. }
  229. Db::commit();
  230. return json_show(0,"活动商品更新成功");
  231. }catch (\Exception $e){
  232. Db::rollback();
  233. return json_show(1008,$e->getMessage());
  234. }
  235. }
  236. /**
  237. * 显示指定的资源
  238. *
  239. * @param int $id 参数详情id
  240. * @return \think\Response
  241. */
  242. public function GoodInfo()
  243. {
  244. $param =$this->request->only(["id"=>''],"post","trim");
  245. if($param['id']=='') return json_show(1004,"参数 id 不能为空");
  246. if($this->request->isCx==1){
  247. $field="a.id,a.actCode,a.yz_good_code,a.apply_id,a.apply_name,a.stock_num,a.used_num,a.balance_num,a.status,
  248. a.addtime,a.updatetime,c.plat_code,b.act_name,b.company_name,b.contactor,b.mobile,b.startTime,b.endTime,d.spuCode,
  249. c.final_price,c.desc,d.good_name,d.good_unit,c.yz_cat_id,d.cat_id,d.brand_id,a.roundId,a.origin_price,a.good_url";
  250. }else{
  251. $field="a.id,a.actCode,a.yz_good_code,a.stock_num,a.used_num,a.balance_num,a.status,a.addtime,a.updatetime,
  252. b.act_name,b.company_name,b.contactor,b.mobile,b.startTime,b.endTime,c.final_price,c.desc,d.good_name,d.spuCode,
  253. d.good_unit,d.cat_id,d.brand_id,a.roundId,a.origin_price,a.good_url";
  254. }
  255. $actm=new ActGood();
  256. $info=$actm->alias("a")
  257. ->leftJoin("act b","a.actCode=b.actCode")
  258. ->leftJoin("platform_youzan c","a.yz_good_code=c.skuCode")
  259. ->leftJoin("good d","c.spuCode=d.spuCode")
  260. ->field($field)
  261. ->find($param);
  262. if($info==false){
  263. return json_show(1005,"未找到数据");
  264. }
  265. $info['brand_name'] = Db::name('brand')
  266. ->where('is_del', 0)
  267. ->where('id',$info['brand_id'])
  268. ->value('brand_name', '');
  269. $info['unit'] = Db::name("unit")->where("id" ,$info['good_unit'])->value('unit', '');
  270. if($this->request->isCx==1){
  271. $info['cat_info'] =made($info['cat_id']);
  272. }else{
  273. unset($info['good_unit']);
  274. unset($info['brand_id']);
  275. }
  276. $info['stock_total'] = Db::name("good_stock")->where(['spuCode' => $info['spuCode'], "is_del" => 0])->sum("usable_stock");
  277. return json_show(0,"获取成功",$info->toArray());
  278. }
  279. /** 商品链接导入
  280. * @param int act_good_id 商品活动id
  281. * @param array web_url 商品链接url
  282. * @return \think\Response
  283. */
  284. public function GoodUrlImport()
  285. {
  286. $param = $this->request->only(["web_url"=>[]],"post");
  287. $validate=Validate::rule([
  288. 'web_url|商品链接' => 'require|array'
  289. ]);
  290. if($validate->check($param)==false) return json_show(1004,$validate->getError());
  291. $actm=new ActGood();
  292. $web=[];
  293. foreach ($param['web_url'] as $item){
  294. $actGood =$actm->where(["id"=>$item['act_good_id'],"is_del"=>0])->find();
  295. if($actGood==false) return json_show(1005,'未找到商品数据');
  296. $temp=[
  297. "act_good_url"=>$item['act_good_url'],
  298. "act_good_id"=>$item['act_good_id'],
  299. "status"=>1,
  300. "is_del"=>0,
  301. "apply_id"=>$this->uid,
  302. "apply_name"=>$this->uname,
  303. "addtime"=>date("Y-m-d H:i:s"),
  304. "updatetime"=>date("Y-m-d H:i:s")
  305. ];
  306. $web[]=$temp;
  307. }
  308. Db::startTrans();
  309. try{
  310. $isAdd=Db::name("act_goodurl")->insertAll($web);
  311. if( $isAdd<=0 ){
  312. Db::rollback();
  313. return json_show(1006,"商品链接导入失败");
  314. }
  315. $ids = array_unique(array_column($param['web_url'],"act_good_id"));
  316. foreach ($ids as $item){
  317. $actGood =$actm->where(["id"=>$item,"is_del"=>0])->find();
  318. if($actGood==false)throw new Exception("未找到活动商品",1006);
  319. $num =Db::name("act_goodurl")->where(["act_good_id"=>$item,"is_del"=>0])->count();
  320. if($num>$actGood['stock_num'])throw new Exception("导入连接数量超过活动数量",1006);
  321. if($num==$actGood['stock_num']){
  322. $up =$actm->where(["id"=>$item,"is_del"=>0])->update(["status"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  323. if($up==false) return json_show(1005,'商品数据更新失败');
  324. $upunum = $actm->where(["actCode"=>$actGood["actCode"],"is_del"=>0,"status"=>0])->count();
  325. if($upunum==0){
  326. $up1 =Db::name("Act")->where(["actCode"=>$actGood["actCode"],"is_del"=>0])->update(["status"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
  327. if ($up1==false){
  328. throw new Exception("活动数据更新失败",1006);
  329. }
  330. }
  331. }
  332. }
  333. Db::commit();
  334. return json_show(0,'商品链接导入成功');
  335. }catch (\Exception $e){
  336. Db::rollback();
  337. return json_show(1008,$e->getMessage());
  338. }
  339. }
  340. /**
  341. * 保存更新的资源
  342. * @param int $page
  343. * @param int $size
  344. * @param string $act_name
  345. * @param string $actCode
  346. * @param int $act_good_id
  347. * @return \think\Response
  348. */
  349. public function GoodUrlList()
  350. {
  351. $param = $this->request->only([
  352. "page"=>1,
  353. "size"=>15,
  354. "act_name"=>'',
  355. "actCode"=>'',
  356. "act_good_id"=>'',
  357. "good_code"=>'',
  358. "roundId"=>'',
  359. ],"post","trim");
  360. $condition=[["a.is_del","=",0],["b.is_del","=",0],["c.is_del","=",0]];
  361. if($param['act_name']!='')$condition[]=["c.act_name","like","%{$param['act_name']}%"];
  362. if($param['actCode']!='')$condition[]=["c.actCode","like","%{$param['actCode']}%"];
  363. if($param['act_good_id']!='')$condition[]=["a.act_good_id","=",$param['act_good_id']];
  364. if($param['good_code']!='')$condition[]=["b.yz_good_code","=",$param['good_code']];
  365. if($param['roundId']!=='') $condition[]=["b.roundId","=",$param["roundId"]];
  366. $count = Db::name("act_goodurl")->alias("a")
  367. ->leftJoin("act_good b","a.act_good_id=b.id and b.is_del=0")
  368. ->leftJoin("act c","b.actCode=c.actCode and c.is_del=0")
  369. ->where($condition)
  370. ->count();
  371. $total =ceil($count/$param['size']);
  372. $page =$param['page']>=$total ? intval($total) : intval($param['page']);
  373. $list = Db::name("act_goodurl")->alias("a")
  374. ->leftJoin("act_good b","a.act_good_id=b.id and b.is_del=0")
  375. ->leftJoin("act c","b.actCode=c.actCode and c.is_del=0")
  376. ->where($condition)->page($page,intval($param['size']))
  377. ->field("a.id,a.act_good_url,b.roundId,a.act_good_id,a.apply_id,a.apply_name,a.addtime,a.updatetime,b.stock_num")
  378. ->select()->toArray();
  379. foreach ($list as &$item){
  380. $item['web_url_num'] = Db::name("act_goodurl")->where(["act_good_id"=>$item['act_good_id'],"is_del"=>0])->count();
  381. }
  382. return json_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  383. }
  384. /**
  385. * 删除指定资源
  386. *
  387. * @param int $id
  388. * @return \think\Response
  389. */
  390. public function GoodUrlDel()
  391. {
  392. $param = $this->request->only(["web_url_ids"=>[]],"post");
  393. if(empty($param['web_url_ids'])|| $param["web_url_ids"]=="") return json_show(1004,"参数 web_url_ids 不能为空");
  394. $actm=new ActGood();
  395. $web=[];
  396. foreach ($param['web_url_ids'] as $item){
  397. $actGood =Db::name("act_goodurl")->where(["id"=>$item,"is_del"=>0])->find();
  398. if($actGood==false) return json_show(1005,'未找到商品链接数据');
  399. $web[]=$actGood['act_good_id'];
  400. }
  401. $web =array_unique($web);
  402. Db::startTrans();
  403. try{
  404. $isDel=Db::name("act_goodurl")->where(["id"=>$param['web_url_ids']])->update(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  405. if($isDel==false){
  406. throw new Exception("活动商品链接删除失败",1006);
  407. }
  408. foreach ($web as $item){
  409. $actGood =$actm->where(["id"=>$item,"is_del"=>0])->find();
  410. if($actGood==false) return json_show(1005,'未找到商品数据');
  411. $num =Db::name("act_goodurl")->where(["act_good_id"=>$item,"is_del"=>0])->count();
  412. if($num<$actGood['stock_num']){
  413. $up =$actm->where(["id"=>$item,"is_del"=>0])->update(["status"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
  414. if($up==false) return json_show(1005,'商品数据更新失败');
  415. $upunum = $actm->where(["actCode"=>$actGood["actCode"],"is_del"=>0,"status"=>0])->count();
  416. if($upunum!=0){
  417. $up1 =Db::name("Act")->where(["actCode"=>$actGood["actCode"],"is_del"=>0])->update(["status"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  418. if ($up1==false){
  419. throw new Exception("活动数据更新失败",1006);
  420. }
  421. }
  422. }
  423. }
  424. Db::commit();
  425. return json_show(0,'商品链接导入成功');
  426. }catch (\Exception $e){
  427. Db::rollback();
  428. return json_show(1008,$e->getMessage());
  429. }
  430. }
  431. //获取跟有赞对接的平台所属的商品上线信息
  432. public function listByYz()
  433. {
  434. $this->noble=\think\facade\Config::get("noble");
  435. $order=\think\facade\Config::get("order");
  436. $this->acton=$order['order_type'];
  437. $param = $this->request->only([
  438. 'page' => 1,
  439. 'size' => 15,
  440. 'good_name' => '',
  441. 'skucode' => '',
  442. 'plat_code' => '',
  443. 'exam_status' => '',
  444. ], 'post', 'trim');
  445. $where = [["b.is_del", "=", 0],["b.platform_id","=",$this->platformid]];
  446. if ($param['good_name'] !== "") $where[] = ['a.good_name', 'like', '%' . $param['good_name'] . '%'];
  447. if ($param['skucode'] !== '') $where [] = ['b.skucode', 'like', '%' . $param['skucode'] . '%'];
  448. if ($param['plat_code'] !== '') $where[] = ["b.plat_code", "like", '%' . $param['plat_code'] . '%'];
  449. if ($param['exam_status'] !== '') {
  450. if($param['exam_status']==1){
  451. $where[] = ['b.exam_status', "=", 6];
  452. }else{
  453. $where[] = ['b.exam_status', "<>", 6];
  454. }
  455. }
  456. if($this->request->isCx==1){
  457. $field="b.skuCode,b.platform_id platform_code,b.plat_code,b.id as platform_youzan_id,a.cat_id,a.good_name,
  458. a.good_img,a.good_info_img,a.good_thumb_img,b.createrid,a.is_exclusive,a.brand_id,a.supplierNo,a.good_unit,
  459. a.noble_metal,a.companyNo,a.spuCode,a.good_type,b.creater,b.addtime,b.updatetime,b.exam_status,a.createrid purchase_id,
  460. a.creater purchase";
  461. }else{
  462. $field="b.skuCode,b.plat_code,b.id as platform_youzan_id,
  463. a.good_name,a.good_img,a.good_info_img,a.good_thumb_img,a.brand_id,
  464. a.good_unit,b.addtime,b.updatetime,b.exam_status,a.spuCode,
  465. b.sale_price,b.final_price";
  466. }
  467. $count = Db::name('platform_youzan')
  468. ->alias("b")
  469. ->leftJoin("good_basic a", "a.spuCode=b.spuCode")
  470. ->where($where)
  471. ->count();
  472. $total = ceil($count / $param['size']);
  473. $param['page'] = $param['page'] >= $total ? $total : $param['page'];
  474. $list = Db::name('platform_youzan')
  475. ->alias("b")
  476. ->field($field)
  477. //成本表里的创建人,也是线上商品的采购人,为了防止混淆,给creater取个别名
  478. ->leftJoin("good_basic a", "a.spuCode=b.spuCode")
  479. ->where($where)
  480. ->page(intval($param['page']),intval($param['size']))
  481. ->order("b.addtime desc")
  482. ->select()->toArray();
  483. $data = [];
  484. $yzgood = new YzGood();
  485. //6.补充数据,照搬list方法t
  486. $all_brand = Db::name('brand')
  487. ->where('is_del', 0)
  488. ->whereIn('id', array_column($list, 'brand_id'))
  489. ->column('brand_name', 'id');
  490. $yzadd = $yzgood->where(["item_no"=>array_column($list, 'skuCode'),"is_del"=>0])->column('detail_url,origin', 'item_no');
  491. $unit = Db::name("unit")->where(["id" => array_column($list, 'good_unit')])->column('unit', 'id');
  492. if($this->request->isCx==1){
  493. $all_platform =Db::name("platform")->field('id,platform_name')->where(["id" =>array_column($list, 'platform_code')])->column('platform_name', 'id');
  494. $all_supplier = Db::name("supplier")->where(["code" =>array_column($list, 'supplierNo')])->column("name","code");
  495. $itemid_all =Db::name("depart_user")->where(["uid"=>array_column($list, 'purchase_id'),"is_del"=>0])->column("itemid",'uid');
  496. }
  497. foreach ($list as $value) {
  498. // $brand = Db::name("brand")->field('id,brand_name')->where(["id" => $value['brand_id']])->find();
  499. $value["brand_name"] = $all_brand[$value['brand_id']]??"";
  500. // $unit = Db::name("unit")->field('id,unit')->where(["id" => $value['good_unit']])->find();
  501. $value['unit'] = $unit[$value['good_unit']]??"";
  502. $value['exam_status'] = $value['exam_status']==6?1:0;
  503. $value['origin_price'] =$yzadd[$value['skuCode']]? $yzadd[$value['skuCode']]['origin']??"":"";
  504. $value['good_url'] =$yzadd[$value['skuCode']]? $yzadd[$value['skuCode']]['detail_url']??"":"";
  505. $spec = Db::name("good_spec")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->select()->toArray();
  506. $value['stock_total'] = Db::name("good_stock")->where(['spuCode' => $value['spuCode'], "is_del" => 0])->sum("usable_stock");
  507. $speclist = [];
  508. if (!empty($spec)) {
  509. foreach ($spec as $val) {
  510. $temp = [];
  511. $temp['spec_id'] = $val['spec_id'];
  512. $temp['spec_value_id'] = $val['spec_value_id'];
  513. $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
  514. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  515. $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
  516. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  517. $speclist[] = $temp;
  518. }
  519. }
  520. $value['good_img']=str_replace($this->origin_img_host,$this->ssl_img_host,$value['good_img']);
  521. $value['good_info_img']=str_replace($this->origin_img_host,$this->ssl_img_host,$value['good_info_img']);
  522. $value['good_thumb_img']=str_replace($this->origin_img_host,$this->ssl_img_host,$value['good_thumb_img']);
  523. if($this->request->isCx==1){
  524. $value['cat_info'] = made($value['cat_id'], []);
  525. $value['platform_name'] = $all_platform[$value['platform_code']]??"";
  526. $value['supplier_name'] = $all_supplier[$value['supplierNo']]??"";
  527. $value['stock_total'] = Db::name("good_stock")->where(['spuCode' => $value['spuCode'], "is_del" => 0])->sum("usable_stock");
  528. $value['exclusive'] = makeExcluse($value['is_exclusive']);
  529. $value['noble_name'] = isset($value['noble_metal']) && $value['noble_metal'] != 0 ? $this->noble[$value['noble_metal']] : "";
  530. $value['company_name'] = implode('/', array_column(GetPart($itemid_all[$value['purchase_id']]), 'name'));
  531. }else{
  532. unset($value['brand_id']);
  533. unset($value['good_unit']);
  534. unset($value['spuCode']);
  535. }
  536. $value['specinfo'] = $speclist;
  537. $data[] = $value;
  538. }
  539. return json_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  540. }
  541. /**
  542. * @return \think\response\Json
  543. * @throws \think\db\exception\DataNotFoundException
  544. * @throws \think\db\exception\DbException
  545. * @throws \think\db\exception\ModelNotFoundException
  546. */
  547. public function YzGoodInfo(){
  548. $param =$this->request->only(["skuCode"=>''],"post","trim");
  549. if($param['skuCode']=='') return json_show(1004,"参数 skuCode 不能为空");
  550. if($this->request->isCx==1){
  551. $field="b.skuCode,b.platform_id platform_code,b.plat_code,b.id as platform_youzan_id,a.cat_id,a.good_name,
  552. a.good_img,a.good_info_img,a.good_thumb_img,b.createrid,a.is_exclusive,a.brand_id,a.supplierNo,a.good_unit,
  553. a.noble_metal,a.companyNo,a.spuCode,a.good_type,b.creater,b.addtime,b.updatetime,b.exam_status,a.createrid purchase_id,
  554. a.creater purchase";
  555. }else{
  556. $field="b.skuCode,b.plat_code,b.id as platform_youzan_id,
  557. a.good_name,a.good_img,a.good_info_img,a.good_thumb_img,a.brand_id,
  558. a.good_unit,b.addtime,b.updatetime,b.exam_status,a.spuCode,
  559. b.sale_price,b.final_price";
  560. }
  561. $info = Db::name('platform_youzan')
  562. ->alias("b")
  563. ->field($field)
  564. //成本表里的创建人,也是线上商品的采购人,为了防止混淆,给creater取个别名
  565. ->leftJoin("good_basic a", "a.spuCode=b.spuCode")
  566. ->where(["b.skuCode"=>$param['skuCode'],"b.is_del"=>0])->find();
  567. if($info==false) return json_show(1005,"商品数据未找到");
  568. $yzgood = new YzGood();
  569. $brand = Db::name("brand")->field('id,brand_name')->where(["id" => $info['brand_id']])->find();
  570. $info["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  571. $unit = Db::name("unit")->field('id,unit')->where(["id" => $info['good_unit']])->find();
  572. $info['unit'] = isset($unit['unit']) ? $unit['unit'] : "";
  573. $info['exam_status'] = $info['exam_status']==6?1:0;
  574. $info['origin_price'] =$yzgood->where(["item_no"=>$info['skuCode'],"is_del"=>0])->value("origin",'');
  575. $info['good_url'] =$yzgood->where(["item_no"=>$info['skuCode'],"is_del"=>0])->value("detail_url",'');
  576. $spec = Db::name("good_spec")->where(["spuCode" => $info['spuCode'], "is_del" => 0])->select()->toArray();
  577. $info['stock_total'] = Db::name("good_stock")->where(['spuCode' => $info['spuCode'], "is_del" => 0])->sum("usable_stock");
  578. $speclist = [];
  579. if (!empty($spec)) {
  580. foreach ($spec as $val) {
  581. $temp = [];
  582. $temp['spec_id'] = $val['spec_id'];
  583. $temp['spec_value_id'] = $val['spec_value_id'];
  584. $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
  585. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  586. $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
  587. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  588. $speclist[] = $temp;
  589. }
  590. }
  591. $info['good_img']=str_replace($this->origin_img_host,$this->ssl_img_host,$info['good_img']);
  592. $info['good_info_img']=str_replace($this->origin_img_host,$this->ssl_img_host,$info['good_info_img']);
  593. $info['good_thumb_img']=str_replace($this->origin_img_host,$this->ssl_img_host,$info['good_thumb_img']);
  594. if($this->request->isCx==1){
  595. $info['cat_info'] = made($info['cat_id'], []);
  596. $platform = Db::name("platform")->field('id,platform_name')->where(["id" => $info['platform_code']])->find();
  597. $info['platform_name'] = isset($platform['platform_name']) ? $platform['platform_name'] : "";
  598. $info['platform_code_en'] = isset($platform['platform_code_en']) ? $platform['platform_code_en'] : "";
  599. $supplier = Db::name("supplier")->field('id,name')->where(["code" => $info['supplierNo']])->find();
  600. $info['supplier_name'] = isset($supplier['name']) ? $supplier['name'] : "";
  601. $info['company'] = isset($company['company']) ? $company['company'] : "";
  602. $info['stock_total'] = Db::name("good_stock")->where(['spuCode' => $info['spuCode'], "is_del" => 0])->sum("usable_stock");
  603. $info['exclusive'] = makeExcluse($info['is_exclusive']);
  604. $info['noble_name'] = isset($info['noble_metal']) && $info['noble_metal'] != 0 ? $this->noble[$info['noble_metal']] : "";
  605. $itemid =Db::name("depart_user")->where(["uid"=>$info["purchase_id"],"is_del"=>0])->value("itemid",'');
  606. $info['company_name'] = implode('/', array_column(GetPart($itemid), 'name'));
  607. }else{
  608. unset($info['brand_id']);
  609. unset($info['good_unit']);
  610. unset($info['spuCode']);
  611. }
  612. $info['specinfo'] = $speclist;
  613. return json_show(0,"获取成功",$info);
  614. }
  615. }