Good.php 28 KB

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