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'] . '%']; $count = Db::name('company_cat_profit') ->alias('a') ->leftJoin('cat b', 'b.id=a.cat_id') ->where($where) ->count('a.id'); $list = Db::name('company_cat_profit') ->alias('a') ->leftJoin('cat b', 'b.id=a.cat_id') ->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,b.pid,a.cat_id is_gold') ->where($where) ->order(['a.id' => 'desc']) ->withAttr("is_gold",function ($value){ return cat_is_gold($value);}) ->page($param['page'], $param['size']) ->select() ->toArray(); return json_show(0, '获取成功', ['count' => $count, 'list' => $list]); } public function copyProfit(){ $param = $this->request->only(['companyNo', 'cat_id','copy_companyNo', 'copy_cat_id',], 'post', 'trim'); $val = Validate::rule([ 'companyNo|公司编码' => 'requireIf:cat_id,0|length:18', 'cat_id|分类id' => 'requireIf:cat_id,0|number|egt:0', 'copy_companyNo|源头公司编码' => 'requireWith:companyNo|alphaNum|length:18', 'copy_cat_id|源头分类id' => 'requireWith:cat_id|number|egt:0', ]); if ($val->check($param) == false)$this->error($val->getError()); $where = [['is_del','=',0]]; if($param['copy_companyNo']!=0){ $where[]=['companyNo','=',$param['copy_companyNo']]; } if($param['copy_cat_id']!=0){ $where[]=['cat_id','=',$param['copy_cat_id']]; } if($param['copy_companyNo']==$param['companyNo']&& $param['copy_cat_id']==$param['cat_id']){ $this->error('数据已存在,请删除后在复制'); } $rs = Db::name('company_cat_profit')->where($where)->field('companyNo,cat_id,rate,order_rate,money_rate,low_rate,sale_rate ')->select(); if(empty($rs)) $this->error('未找到可复制数据'); Db::startTrans(); try{ $temp=[]; foreach ($rs as $key=>$value){ if($param['cat_id']!=0){ $value['cat_id'] = $param['cat_id']; } if($param['companyNo']!=0){ $value['companyNo'] = $param['companyNo']; } $temp[]=$value; if($key%1000==0){ Db::name('company_cat_profit')->insertAll($temp); $temp=[]; } } if(empty($temp)==false) Db::name('company_cat_profit')->insertAll($temp); Db::commit(); $this->success('复制成功'); }catch (\Exception $exception) { Db::rollback(); $this->error($exception->getMessage()); } } }