Good.php 28 KB

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