request->only(['page' => 1, 'size' => 10, 'companyNo' => '', 'cat_name' => '', 'pid' => 0], 'post', 'trim'); if ($param['companyNo'] == '') return json_show(1004, '业务公司编号不能为空'); $where = [['a.is_del', '=', 0], ['a.pid', '=', $param['pid']]]; if ($param['cat_name'] !== '') $where[] = ['a.cat_name', 'like', '%' . $param['cat_name'] . '%']; $count = Db::name('cat') ->alias('a') ->leftJoin('company_cat_profit b', 'b.cat_id=a.id AND companyNo=\'' . $param['companyNo'] . '\'') ->where($where) ->count('a.id'); $list = Db::name('cat') ->alias('a') ->field('a.id cat_id,a.cat_name,a.level,b.rate,b.order_rate,b.money_rate,b.low_rate,b.sale_rate,b.status') ->leftJoin('company_cat_profit b', 'b.is_del=0 AND b.cat_id=a.id AND companyNo=\'' . $param['companyNo'] . '\'') ->where($where) ->withAttr('status', function ($val) { return (int)$val; }) ->order('a.id', 'desc') ->page($param['page'], $param['size']) ->select() ->toArray(); return json_show(0, '获取列表成功', ['count' => $count, 'list' => $list]); } //设置毛利率相关 public function set() { $param = $this->request->only(['cat_id', 'companyNo', 'rate', 'order_rate', 'money_rate', 'low_rate', 'sale_rate'], 'post', 'trim'); $val = Validate::rule([ 'cat_id|分类id' => 'require|number|gt:0', 'companyNo|公司编码' => 'require|length:18', 'rate|主管利率' => 'require|float|between:0,100', 'order_rate|预算成本' => 'require|float|between:0,100', 'money_rate|财务利率' => 'require|float|between:0,100', 'low_rate|最低利率' => 'require|float|between:0,100', 'sale_rate|成本售价' => 'require|float|between:0,100', ]); if (!$val->check($param)) return json_show(1004, $val->getError()); $tmp = Db::name('company_cat_profit') ->field('id') ->where(['is_del' => 0, 'companyNo' => $param['companyNo'], 'cat_id' => $param['cat_id']]) ->findOrEmpty(); $date = date('Y-m-d H:i:s'); if (empty($tmp)) { $rs = Db::name('company_cat_profit') ->insert([ 'companyNo' => $param['companyNo'], 'cat_id' => $param['cat_id'], 'rate' => $param['rate'], 'order_rate' => $param['order_rate'], 'money_rate' => $param['money_rate'], 'low_rate' => $param['low_rate'], 'sale_rate' => $param['sale_rate'], 'status' => 0, 'is_del' => 0, 'addtime' => $date, 'updatetime' => $date ]); } else { $rs = Db::name('company_cat_profit') ->where(['is_del' => 0, 'id' => $tmp['id']]) ->update([ 'rate' => $param['rate'], 'order_rate' => $param['order_rate'], 'money_rate' => $param['money_rate'], 'low_rate' => $param['low_rate'], 'sale_rate' => $param['sale_rate'], 'updatetime' => $date ]); } return $rs ? json_show(0, '设置成功') : json_show(1004, '设置失败'); } //启禁用 public function status() { $param = $this->request->only(['cat_id', 'companyNo', 'status'], 'post', 'trim'); $val = Validate::rule([ 'cat_id|分类id' => 'require|number|gt:0', 'companyNo|公司编码' => 'require|length:18', 'status|状态' => 'require|number|in:0,1', ]); if ($val->check($param) == false) return json_show(1004, $val->getError()); $cat = Db::name('cat') ->field('id,level') ->where(['id' => $param['cat_id'], 'is_del' => 0]) ->findOrEmpty(); if (empty($cat)) return json_show(1005, '该分类不存在'); $tmp = Db::name('company_cat_profit') ->field('id,status') ->where(['is_del' => 0, 'companyNo' => $param['companyNo'], 'cat_id' => $param['cat_id']]) ->findOrEmpty(); if (empty($tmp)) return json_show(1004, '该分类尚未设置毛利,无法操作'); if ($tmp['status'] == $param['status']) return json_show(1004, '重复操作'); //要操作的分类id集合 switch ($cat['level']) { case 1: $first_ids = Db::name('cat') ->where(['is_del' => 0, 'pid' => $param['cat_id']]) ->column('id'); $second_ids = Db::name('cat') ->where('is_del', 0) ->whereIn('pid', $first_ids) ->column('id'); $cat_ids = array_merge($first_ids, $second_ids); break; case 2: $cat_ids = Db::name('cat') ->where(['is_del' => 0, 'pid' => $param['cat_id']]) ->column('id'); break; } $cat_ids[] = $param['cat_id']; $rs = Db::name('company_cat_profit') ->where(['is_del' => 0, 'companyNo' => $param['companyNo']]) ->whereIn('cat_id', $cat_ids) ->where('status', '<>', $param['status']) ->update([ 'status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s') ]); return $rs ? json_show(0, '操作成功') : json_show(1004, '操作失败,该记录不存在或重复操作'); } //删除 public function delete() { $param = $this->request->only(['cat_id', 'companyNo'], 'post', 'trim'); $val = Validate::rule([ 'cat_id|分类id' => 'require|number|gt:0', 'companyNo|公司编码' => 'require|length:18', ]); if ($val->check($param) == false) return json_show(1004, $val->getError()); $rs = Db::name('company_cat_profit') ->where(['is_del' => 0, 'companyNo' => $param['companyNo'], 'cat_id' => $param['cat_id']]) ->update([ 'is_del' => 1, 'updatetime' => date('Y-m-d H:i:s') ]); return $rs ? json_show(0, '删除成功') : json_show(1004, '删除失败'); } //筛选指定公司下的所有分类 public function getListByCompany() { $param = $this->request->only(['companyNo', 'cat_name' => '', 'page' => 1, 'size' => 10], 'post', 'trim'); $val = Validate::rule([ 'companyNo|公司编码' => 'require|length:18', 'cat_name|分类名称' => 'max:255' ]); if ($val->check($param) == false) return json_show(1004, $val->getError()); $where = [ ['a.companyNo', '=', $param['companyNo']], ['a.is_del', '=', 0], ['b.level', '=', 3], ]; if ($param['cat_name'] !== '') $where[] = ['b.search', 'like', '%' . $param['cat_name'] . '%']; //如果搜索名字,要把所有符合要求的三级分类id展现出来 // if ($param['cat_name'] != '') { // // $cat_id = Db::name('cat') // ->where(['is_del' => 0]) // ->whereLike('cat_name', '%' . $param['cat_name'] . '%') // ->column('id'); // if (!empty($cat_id)) { // $catid = []; //// $tmp_1 = Db::name('cat') //// ->where(['is_del'=>0,'pid'=>$cat_id,'level'=>3]) //// ->column('id'); //// if(!empty($tmp_1)) $catid = array_merge($catid,$tmp_1); // // $tmp_1 = Db::name('cat') // ->where(['is_del' => 0, 'pid' => $cat_id]) // ->column('id'); // if (!empty($tmp_1)) { // $catid = array_merge($catid, $tmp_1); // $tmp_2 = Db::name('cat') // ->where(['is_del' => 0, 'pid' => $tmp_1]) // ->column('id'); // if (!empty($tmp_2)) { // $catid = array_merge($catid, $tmp_2); // $tmp_3 = Db::name('cat') // ->where(['is_del' => 0, 'pid' => $tmp_2]) // ->column('id'); // if (!empty($tmp_3)) $catid = array_merge($catid, $tmp_3); // } // } // // if ($catid) $where[] = ['a.cat_id', 'in', $catid]; // } // // } $count = Db::name('company_cat_profit') ->alias('a') ->leftJoin('cat b', 'b.id=a.cat_id') // ->leftJoin('cat c', 'b.id=b.pid') // ->leftJoin('cat d', 'd.id=c.pid') ->where($where) ->count('a.id'); $list = Db::name('company_cat_profit') ->alias('a') ->leftJoin('cat b', 'b.id=a.cat_id') // ->leftJoin('cat c', 'c.id=b.pid') // ->leftJoin('cat d', 'd.id=c.pid') ->field('a.id,b.search cat_name,a.cat_id,a.rate,a.order_rate,a.money_rate,a.low_rate,a.sale_rate,a.status') ->where($where) ->order(['a.id' => 'desc']) ->page($param['page'], $param['size']) ->select() ->toArray(); return json_show(0, '获取成功', ['count' => $count, 'list' => $list]); } }