| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- declare(strict_types=1);
- namespace app\admin\controller\goods;
- use app\common\controller\Backend;
- use app\admin\traits\Actions;
- use think\annotation\route\Group;
- use think\annotation\route\Route;
- use think\facade\Db;
- use app\common\model\ImportSku;
- use app\common\model\ShopList;
- use app\common\model\StockConfig;
- use app\common\model\ShopDelivery;
- use app\admin\service\JuShuiTanService;
- use app\common\model\CustomerSpec;
- use app\common\model\ProductConfig;
- use app\common\model\ImportList as ImportListModel;
- use app\common\model\ShopList as ShopListModel;
- #[Group("goods/import_list")]
- class ImportList extends Backend
- {
- //app\admin\controller\goods\ImportList
- use Actions {
- index as private _index;
- del as private _del;
- }
- protected function _initialize()
- {
- parent::_initialize();
- $this->model = new ImportListModel();
- // $this->assign('shopList', ShopList::where('status', 1)->column('name', 'id'));
- $this->assign('typeList', StockConfig::where('type_id', 'variety_name')->order('sort desc')->column('title', 'id'));
- $this->assign('packingList', StockConfig::where('type_id', 'packing_box')->column('title', 'id'));
- }
- /**
- * 查看
- */
- #[Route('GET,JSON', 'index')]
- public function index()
- {
- if (false === $this->request->isAjax()) {
- return $this->fetch();
- }
- if ($this->request->post('selectpage')) {
- return $this->selectpage();
- }
- [$where, $order, $limit, $with] = $this->buildparams();
-
- $list = $this->model
- ->withJoin($with, 'left')
- //如果没有使用operate filter过滤的情况下,推荐使用with关联,可以提高查询效率
- //->with($with)
- ->where($where)
- ->order($order)
- ->paginate($limit)
- ->each(function ($item, $key) {
- if(empty($item['type_id'])){
- $item['type_id']=0;
- }
- $trade_from_list=[];
- if($item['type_id']==1){
- $feng_su_trade_from_list=site_config("addonsd.feng_su_trade_from_list");
- $trade_from_list=$feng_su_trade_from_list;
- }else{
- $ju_shui_tan_trade_from_list=site_config("addonsd.ju_shui_tan_trade_from_list");
- $trade_from_list=$ju_shui_tan_trade_from_list;
- }
-
-
- $trade_from=$item['trade_from'];
- $trade_from=isset($trade_from_list[$trade_from])?$trade_from_list[$trade_from]:$item['trade_from'];
- $item['trade_from']=$trade_from;
- // $item['trade_from']=empty($item['trade_from'])?'无':$trade_from_list[$trade_from];
-
-
- return $item;
- });
- $result = ['total' => $list->total(), 'rows' => $list->items()];
- return json($result);
- }
- /**
- * 关联店铺
- */
- #[Route('GET,POST', 'shops')]
- public function shops()
- {
- if (false === $this->request->isPost()) {
- $ids = $this->request->get('ids/s', '');
- $shopList=new ShopList();
- $sql_restul=$shopList->alias('s')
- ->join("yun_customer c", "s.customer_id = c.id", "INNER")
- ->where(['s.status' => 1, 'c.status' => 1])
- ->field(['s.id','s.name','c.name'=>'nickname'])
- ->select();
- $arr=[];
- foreach ($sql_restul as $item) {
- $arr[$item['id']]=$item['name'].'(客户:'.$item['nickname'].')';
- }
-
- $this->assign('shopList',$arr );
- return $this->fetch();
- }
- $params = array_merge($this->request->post("row/a"), $this->postParams);
- if (empty($params)) {
- $this->error(__('提交的参数不能为空'));
- }
- if (!$this->request->checkToken('__token__', ['__token__' => $this->request->post('__token__')])) {
- $this->error(__('token错误,请刷新页面重试'));
- }
- foreach ($params as &$value) {
- if (is_array($value)) {
- $value = implode(',', $value);
- }
- if ($value === '') {
- $value = null;
- }
- }
- $ids = $this->request->get('ids/s', '');
- if (empty($params['shop_id'])) $this->error(__('请选择店铺'));
- $result = false;
- Db::startTrans();
- try {
- //更新店铺
- ShopList::where('id', $params['shop_id'])->update(['shop_id' => $ids]);
- //更新状态
- $result = $this->model->where('shop_id', $ids)->update(['status' => 2]);
- if ($this->callback) {
- $callback = $this->callback;
- $callback($this->model);
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result === false) {
- $this->error(__('没有新增任何数据'));
- }
- $this->success();
- }
- /**
- * 关联规格
- */
- #[Route('GET,POST', 'specs')]
- public function specs(mixed $row = null)
- {
- $ids = $this->request->param('ids');
- if (!$row || is_array($row)) {
- $row = $this->model->find($ids);
- }
- if (!$row) {
- $this->error(__('没有找到记录'));
- }
- if (count($this->volidateFields) > 0) {
- foreach ($this->volidateFields as $field => $value) {
- if ($row[$field] != $value) {
- $this->error(__('没有操作权限'));
- }
- }
- }
- if (false === $this->request->isPost()) {
- $shop_id=$row['shop_id'];
- $shopListModel=new ShopListModel();
-
- $rows = $shopListModel::where('shop_id', $shop_id)->where('status', 1)->find();
- $this->assign('rows', $rows->type_spec??'');
- $this->assign('row', $row);
- return $this->fetch();
- }
- $params = $this->request->post("");
- if (empty($params['all_data'])) $this->error(__('请选择规格'));
- $count = 0;
- Db::startTrans();
- try {
- //插入规格
- ImportSku::insertSpecs($row['shop_id'], $row['sku_id'], (int)$params['all_data'][0]['type_id'], (int)$params['all_data'][0]['id']);
- //查看有多少规格
- $list = $this->model::where('shop_id', $row['shop_id'])->where('sku_id', $row['sku_id'])->where('status', 2)->select();
- $shopList = new ShopList();
- $productConfig = new ProductConfig();
- $shopDelivery = new ShopDelivery();
- $customerSpec = new CustomerSpec();
- $importSku = new ImportSku();
- foreach ($list as $item) {
- //插入发货数据
- JuShuiTanService::setAdditionalPrice(
- $importSku,
- $shopList,
- $productConfig,
- $shopDelivery,
- $customerSpec,
- $row['shop_id'],
- $row['sku_id'],
- $params['all_data'][0]['type_id'],
- $params['all_data'][0]['id'],
- $params['all_data'][0]['name'],
- $row
- );
- $count += $item->save(['status' => 3]);
- }
- if ($this->callback) {
- $callback = $this->callback;
- $callback($ids);
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($count) {
- return resp_json(200, '操作成功');
- }
- $this->error(__('没有数据被更新'));
- }
- //删除
- #[Route("GET,POST", "del")]
- public function del()
- {
- //通过定义callback回调函数来执行删除后的操作
- $this->callback = function ($ids) {};
- return $this->_del();
- }
- }
|