123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- <?php
- namespace app\admin\controller;
- use think\facade\Db;
- use think\facade\Validate;
- //公司平台分类毛利
- class CompanyPlatformCatProfit extends Base
- {
- //列表
- public function getList()
- {
- $param = $this->request->only(['page' => 1, 'size' => 10, 'companyNo', 'platform_id', 'cat_name' => '', 'pid' => 0], 'post', 'trim');
- $val = Validate::rule([
- 'companyNo|公司编码' => 'require|length:18',
- 'platform_id|平台id' => 'require|number|gt:0',
- 'pid|父级分类id' => 'require|number|egt:0',
- ]);
- if ($val->check($param) == false) return json_show(1004, $val->getError());
- $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_platform_cat_profit b', "b.cat_id=a.id AND b.is_del=0 AND b.companyNo='{$param["companyNo"]}'AND b.platform_id={$param['platform_id']}")
- ->leftJoin('platform p', "p.id=b.platform_id AND p.is_del=0 ")
- ->where($where)
- ->count('a.id');
- $list = Db::name('cat')
- ->alias('a')
- ->field('a.id cat_id,a.cat_name,a.level,p.platform_name,b.rate,b.order_rate,b.money_rate,b.low_rate,b.sale_rate,b.status')
- ->leftJoin('company_platform_cat_profit b', "b.cat_id=a.id AND b.is_del=0 AND b.companyNo='{$param["companyNo"]}'AND b.platform_id={$param['platform_id']} ")
- ->leftJoin('platform p', 'p.id=b.platform_id AND p.is_del=0')
- ->where($where)
- ->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(['companyNo', 'platform_id', 'cat_id', 'rate', 'order_rate', 'money_rate', 'low_rate', 'sale_rate'], 'post', 'trim');
- $val = Validate::rule([
- 'companyNo|公司编码' => 'require|length:18',
- 'platform_id|平台id' => 'require|number|gt:0',
- 'cat_id|分类id' => 'require|number|gt:0',
- '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());
- $rs = Db::name('company_platform_cat_profit')
- ->field('id')
- ->where([
- 'is_del' => 0,
- 'companyNo' => $param['companyNo'],
- 'platform_id' => $param['platform_id'],
- 'cat_id' => $param['cat_id'],
- ])
- ->findOrEmpty();
- $date = date('Y-m-d H:i:s');
- if (empty($rs)) {
- $rs = Db::name('company_platform_cat_profit')
- ->insert([
- 'companyNo' => $param['companyNo'],
- 'platform_id' => $param['platform_id'],
- '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' => 1,
- 'is_del' => 0,
- 'addtime' => $date,
- 'updatetime' => $date,
- ]);
- } else {
- $rs = Db::name('company_platform_cat_profit')
- ->where(['is_del' => 0, 'id' => $rs['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 delete()
- {
- $param = $this->request->only(['companyNo', 'platform_id', 'cat_id'], 'post', 'trim');
- $val = Validate::rule([
- 'companyNo|公司编码' => 'require|length:18',
- 'platform_id|平台id' => 'require|number|gt:0',
- 'cat_id|分类id' => 'require|number|gt:0',
- ]);
- if ($val->check($param) == false) return json_show(1004, $val->getError());
- $rs = Db::name('company_platform_cat_profit')
- ->where([
- 'is_del' => 0,
- 'companyNo' => $param['companyNo'],
- 'platform_id' => $param['platform_id'],
- '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 copyProfit(){
- $param = $this->request->only(['companyNo', 'platform_id', 'cat_id',"copy_companyNo",'copy_platform_id', 'copy_cat_id',], 'post', 'trim');
- $val = Validate::rule([
- 'companyNo|公司编码' => 'requireIf:platform_id,0|alphaNum|length:18',
- 'platform_id|平台id' => 'requireIf:companyNo,0|number|egt:0',
- 'cat_id|分类id' => 'number|egt:0',
- 'copy_companyNo|源头公司编码' => 'requireWith:companyNo|alphaNum|length:18',
- 'copy_platform_id|源头平台id' => 'requireWith:platform_id|number|egt:0',
- '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_platform_id']!=0){
- $where[]=['platform_id','=',$param['copy_platform_id']];
- }
- if($param['copy_cat_id']!=0){
- $where[]=['cat_id','=',$param['copy_cat_id']];
- }
- if($param['copy_companyNo']==$param['companyNo'] &&$param['copy_platform_id']==$param['platform_id'] &&
- $param['copy_cat_id']==$param['cat_id']){
- $this->error("数据已存在,请删除后在复制");
- }
- $rs = Db::name('company_platform_cat_profit')->where($where)->field("companyNo,platform_id,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['platform_id']!=0){
- $value['platform_id'] = $param['platform_id'];
- }
- 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_platform_cat_profit')->insertAll($temp);
- $temp=[];
- }
- }
- if(empty($temp)==false) Db::name('company_platform_cat_profit')->insertAll($temp);
- Db::commit();
- $this->success("复制成功");
- }catch (\Exception $exception)
- {
- Db::rollback();
- $this->error($exception->getMessage());
- }
- }
- }
|