CompanyPlatformCatProfit.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\Db;
  4. use think\facade\Validate;
  5. //公司平台分类毛利
  6. class CompanyPlatformCatProfit extends Base
  7. {
  8. //列表
  9. public function getList()
  10. {
  11. $param = $this->request->only(['page' => 1, 'size' => 10, 'companyNo', 'platform_id', 'cat_name' => '', 'pid' => 0], 'post', 'trim');
  12. $val = Validate::rule([
  13. 'companyNo|公司编码' => 'require|length:18',
  14. 'platform_id|平台id' => 'require|number|gt:0',
  15. 'pid|父级分类id' => 'require|number|egt:0',
  16. ]);
  17. if ($val->check($param) == false) return json_show(1004, $val->getError());
  18. $where = [
  19. ['a.is_del', '=', 0],
  20. ['a.pid', '=', $param['pid']],
  21. ];
  22. if ($param['cat_name'] != '') $where[] = ['a.cat_name', 'like', '%' . $param['cat_name'] . '%'];
  23. $count = Db::name('cat')
  24. ->alias('a')
  25. ->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']}")
  26. ->leftJoin('platform p', "p.id=b.platform_id AND p.is_del=0 ")
  27. ->where($where)
  28. ->count('a.id');
  29. $list = Db::name('cat')
  30. ->alias('a')
  31. ->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')
  32. ->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']} ")
  33. ->leftJoin('platform p', 'p.id=b.platform_id AND p.is_del=0')
  34. ->where($where)
  35. ->order('a.id', 'desc')
  36. ->page($param['page'], $param['size'])
  37. ->select()
  38. ->toArray();
  39. return json_show(0, '获取列表成功', ['count' => $count, 'list' => $list]);
  40. }
  41. //设置
  42. public function set()
  43. {
  44. $param = $this->request->only(['companyNo', 'platform_id', 'cat_id', 'rate', 'order_rate', 'money_rate', 'low_rate', 'sale_rate'], 'post', 'trim');
  45. $val = Validate::rule([
  46. 'companyNo|公司编码' => 'require|length:18',
  47. 'platform_id|平台id' => 'require|number|gt:0',
  48. 'cat_id|分类id' => 'require|number|gt:0',
  49. 'rate|主管利率' => 'require|float|between:0,100',
  50. 'order_rate|预算成本' => 'require|float|between:0,100',
  51. 'money_rate|财务利率' => 'require|float|between:0,100',
  52. 'low_rate|最低利率' => 'require|float|between:0,100',
  53. 'sale_rate|成本售价' => 'require|float|between:0,100',
  54. ]);
  55. if (!$val->check($param)) return json_show(1004, $val->getError());
  56. $rs = Db::name('company_platform_cat_profit')
  57. ->field('id')
  58. ->where([
  59. 'is_del' => 0,
  60. 'companyNo' => $param['companyNo'],
  61. 'platform_id' => $param['platform_id'],
  62. 'cat_id' => $param['cat_id'],
  63. ])
  64. ->findOrEmpty();
  65. $date = date('Y-m-d H:i:s');
  66. if (empty($rs)) {
  67. $rs = Db::name('company_platform_cat_profit')
  68. ->insert([
  69. 'companyNo' => $param['companyNo'],
  70. 'platform_id' => $param['platform_id'],
  71. 'cat_id' => $param['cat_id'],
  72. 'rate' => $param['rate'],
  73. 'order_rate' => $param['order_rate'],
  74. 'money_rate' => $param['money_rate'],
  75. 'low_rate' => $param['low_rate'],
  76. 'sale_rate' => $param['sale_rate'],
  77. 'status' => 1,
  78. 'is_del' => 0,
  79. 'addtime' => $date,
  80. 'updatetime' => $date,
  81. ]);
  82. } else {
  83. $rs = Db::name('company_platform_cat_profit')
  84. ->where(['is_del' => 0, 'id' => $rs['id']])
  85. ->update([
  86. 'rate' => $param['rate'],
  87. 'order_rate' => $param['order_rate'],
  88. 'money_rate' => $param['money_rate'],
  89. 'low_rate' => $param['low_rate'],
  90. 'sale_rate' => $param['sale_rate'],
  91. 'updatetime' => $date
  92. ]);
  93. }
  94. return $rs ? json_show(0, '设置成功') : json_show(1004, '设置失败');
  95. }
  96. //删除
  97. public function delete()
  98. {
  99. $param = $this->request->only(['companyNo', 'platform_id', 'cat_id'], 'post', 'trim');
  100. $val = Validate::rule([
  101. 'companyNo|公司编码' => 'require|length:18',
  102. 'platform_id|平台id' => 'require|number|gt:0',
  103. 'cat_id|分类id' => 'require|number|gt:0',
  104. ]);
  105. if ($val->check($param) == false) return json_show(1004, $val->getError());
  106. $rs = Db::name('company_platform_cat_profit')
  107. ->where([
  108. 'is_del' => 0,
  109. 'companyNo' => $param['companyNo'],
  110. 'platform_id' => $param['platform_id'],
  111. 'cat_id' => $param['cat_id'],
  112. ])
  113. ->update([
  114. 'is_del' => 1,
  115. 'updatetime' => date('Y-m-d H:i:s')
  116. ]);
  117. return $rs ? json_show(0, '删除成功') : json_show(1004, '删除失败');
  118. }
  119. public function copyProfit(){
  120. $param = $this->request->only(['companyNo', 'platform_id', 'cat_id',"copy_companyNo",'copy_platform_id', 'copy_cat_id',], 'post', 'trim');
  121. $val = Validate::rule([
  122. 'companyNo|公司编码' => 'requireIf:platform_id,0|alphaNum|length:18',
  123. 'platform_id|平台id' => 'requireIf:companyNo,0|number|egt:0',
  124. 'cat_id|分类id' => 'number|egt:0',
  125. 'copy_companyNo|源头公司编码' => 'requireWith:companyNo|alphaNum|length:18',
  126. 'copy_platform_id|源头平台id' => 'requireWith:platform_id|number|egt:0',
  127. 'copy_cat_id|源头分类id' => 'requireWith:cat_id|number|egt:0',
  128. ]);
  129. if ($val->check($param) == false)$this->error($val->getError());
  130. $where = [["is_del","=",0]];
  131. if($param['copy_companyNo']!=0){
  132. $where[]=["companyNo","=",$param['copy_companyNo']];
  133. }
  134. if($param['copy_platform_id']!=0){
  135. $where[]=['platform_id','=',$param['copy_platform_id']];
  136. }
  137. if($param['copy_cat_id']!=0){
  138. $where[]=['cat_id','=',$param['copy_cat_id']];
  139. }
  140. if($param['copy_companyNo']==$param['companyNo'] &&$param['copy_platform_id']==$param['platform_id'] &&
  141. $param['copy_cat_id']==$param['cat_id']){
  142. $this->error("数据已存在,请删除后在复制");
  143. }
  144. $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();
  145. if(empty($rs)) $this->error('未找到可复制数据');
  146. Db::startTrans();
  147. try{
  148. $temp=[];
  149. foreach ($rs as $key=>$value){
  150. if($param['platform_id']!=0){
  151. $value['platform_id'] = $param['platform_id'];
  152. }
  153. if($param['cat_id']!=0){
  154. $value['cat_id'] = $param['cat_id'];
  155. }
  156. if($param['companyNo']!=0){
  157. $value['companyNo'] = $param['companyNo'];
  158. }
  159. $temp[]=$value;
  160. if($key%1000==0){
  161. Db::name('company_platform_cat_profit')->insertAll($temp);
  162. $temp=[];
  163. }
  164. }
  165. if(empty($temp)==false) Db::name('company_platform_cat_profit')->insertAll($temp);
  166. Db::commit();
  167. $this->success("复制成功");
  168. }catch (\Exception $exception)
  169. {
  170. Db::rollback();
  171. $this->error($exception->getMessage());
  172. }
  173. }
  174. }