123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 产品
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\crm\controller;
  8. use app\admin\controller\ApiCommon;
  9. use app\crm\model\Product as ProductModel;
  10. use app\admin\model\File as FileModel;
  11. use app\admin\model\ActionRecord as ActionRecordModel;
  12. use think\Db;
  13. use think\Hook;
  14. use think\Request;
  15. class Product extends ApiCommon
  16. {
  17. /**
  18. * 用于判断权限
  19. * @permission 无限制
  20. * @allow 登录用户可访问
  21. * @other 其他根据系统设置
  22. **/
  23. public function _initialize()
  24. {
  25. $action = [
  26. 'permission'=>['exceldownload'],
  27. 'allow'=>['system','count','read']
  28. ];
  29. Hook::listen('check_auth',$action);
  30. $request = Request::instance();
  31. $a = strtolower($request->action());
  32. if (!in_array($a, $action['permission'])) {
  33. parent::_initialize();
  34. }
  35. }
  36. /**
  37. * 产品列表
  38. * @author Michael_xu
  39. * @return
  40. */
  41. public function index()
  42. {
  43. $productModel = model('Product');
  44. $param = $this->param;
  45. $userInfo = $this->userInfo;
  46. $param['user_id'] = $userInfo['id'];
  47. $data = $productModel->getDataList($param);
  48. return resultArray(['data' => $data]);
  49. }
  50. /**
  51. * 添加产品
  52. * @author Michael_xu
  53. * @param
  54. * @return
  55. */
  56. public function save()
  57. {
  58. $productModel = model('Product');
  59. $param = $this->param;
  60. $userInfo = $this->userInfo;
  61. $param['create_user_id'] = $userInfo['id'];
  62. $param['owner_user_id'] = $userInfo['id'];
  63. # 检查产品图片
  64. if (!empty($param['cover_images']) && count(explode(',', $param['cover_images'])) > 9) {
  65. return resultArray(['error' => '最多只能上次9张产品图片!']);
  66. }
  67. # 检查产品详情图片
  68. if (!empty($param['details_images']) && count(explode(',', $param['details_images'])) > 9) {
  69. return resultArray(['error' => '最多只能上次9张产品详情图片!']);
  70. }
  71. if ($productModel->createData($param)) {
  72. return resultArray(['data' => '添加成功']);
  73. } else {
  74. return resultArray(['error' => $productModel->getError()]);
  75. }
  76. }
  77. /**
  78. * 产品详情
  79. * @author Michael_xu
  80. * @param
  81. * @return
  82. */
  83. public function read()
  84. {
  85. $productModel = model('Product');
  86. $userModel = new \app\admin\model\User();
  87. $param = $this->param;
  88. $userInfo = $this->userInfo;
  89. $data = $productModel->getDataById($param['id'], $userInfo['id']);
  90. //判断权限
  91. $auth_user_ids = $userModel->getUserByPer('crm', 'product', 'read');
  92. if (!in_array($data['owner_user_id'], $auth_user_ids)) {
  93. //无权限
  94. $authData['dataAuth'] = (int)0;
  95. return resultArray(['data' => $authData]);
  96. }
  97. if (!$data) {
  98. return resultArray(['error' => $productModel->getError()]);
  99. }
  100. return resultArray(['data' => $data]);
  101. }
  102. /**
  103. * 编辑产品
  104. * @author Michael_xu
  105. * @param
  106. * @return
  107. */
  108. public function update()
  109. {
  110. $productModel = model('Product');
  111. $param = $this->param;
  112. $userInfo = $this->userInfo;
  113. $param['user_id'] = $userInfo['id'];
  114. # 检查产品图片
  115. if (!empty($param['cover_images']) && count(explode(',', $param['cover_images'])) > 9) {
  116. return resultArray(['error' => '最多只能上次9张产品图片!']);
  117. }
  118. # 检查产品详情图片
  119. if (!empty($param['details_images']) && count(explode(',', $param['details_images'])) > 9) {
  120. return resultArray(['error' => '最多只能上次9张产品详情图片!']);
  121. }
  122. if ($productModel->updateDataById($param, $param['id'])) {
  123. return resultArray(['data' => '编辑成功']);
  124. } else {
  125. return resultArray(['error' => $productModel->getError()]);
  126. }
  127. }
  128. /**
  129. * 产品上架、下架
  130. * @author Michael_xu
  131. * @param
  132. * @return
  133. */
  134. public function status()
  135. {
  136. $param = $this->param;
  137. $userInfo = $this->userInfo;
  138. $data = [];
  139. $data['status'] = ($param['status'] == '上架') ? '上架' : '下架';
  140. $data['update_time'] = time();
  141. $userModel = new \app\admin\model\User();
  142. $owner_user_info = $userModel->getUserById($this->param['owner_user_id']);
  143. if (!is_array($param['id'])) {
  144. $productIds[] = $param['id'];
  145. } else {
  146. $productIds = $param['id'] ? : [];
  147. }
  148. if (!$productIds) {
  149. return resultArray(['error' => '参数错误']);
  150. }
  151. $res = db('crm_product')->where(['product_id' => ['in',$productIds]])->update($data);
  152. if (!$res) {
  153. return resultArray(['error' => '操作失败']);
  154. }
  155. foreach ($productIds as $v){
  156. $product_info=db('crm_product')->where('product_id',$v)->find();
  157. if($param['status'] == '上架'){
  158. updateActionLog($userInfo['id'], 'crm_product', $v, '', '', '将产品上架');
  159. RecordActionLog($userInfo['id'], 'crm_product', 'up',$product_info['name'], '','','将产品上架');
  160. }else{
  161. updateActionLog($userInfo['id'], 'crm_product', $v, '', '', '将产品下架');
  162. RecordActionLog($userInfo['id'], 'crm_product', 'down',$product_info['name'], '','','将产品下架');
  163. }}
  164. return resultArray(['data' => $data['status'].'成功']);
  165. }
  166. /**
  167. * 产品导入模板
  168. * @author Michael_xu
  169. * @param string $save_path 本地保存路径 用于错误数据导出,在 Admin\Model\Excel::batchImportData()调用
  170. * @return
  171. */
  172. public function excelDownload($save_path = '')
  173. {
  174. $param = $this->param;
  175. $userInfo = $this->userInfo;
  176. $excelModel = new \app\admin\model\Excel();
  177. // 导出的字段列表
  178. $fieldModel = new \app\admin\model\Field();
  179. $fieldParam['types'] = 'crm_product';
  180. $fieldParam['action'] = 'excel';
  181. $field_list = $fieldModel->field($fieldParam);
  182. $excelModel->excelImportDownload($field_list, 'crm_product', $save_path);
  183. # 下次升级
  184. // $param = $this->param;
  185. // $userInfo = $this->userInfo;
  186. // $excelModel = new \app\admin\model\Excel();
  187. //
  188. // // 导出的字段列表
  189. // $fieldModel = new \app\admin\model\Field();
  190. // $fieldParam['types'] = 'crm_product';
  191. // $fieldParam['action'] = 'excel';
  192. // $field_list = $fieldModel->field($fieldParam);
  193. // $field=[1=>[
  194. // 'field'=>'owner_user_id',
  195. // 'types'=>'crm_product',
  196. // 'name'=>'负责人',
  197. // 'form_type'=>'user',
  198. // 'default_value'=>'',
  199. // 'is_unique' => 1,
  200. // 'is_null' => 1,
  201. // 'input_tips' =>'',
  202. // 'setting' => Array(),
  203. // 'is_hidden'=>0,
  204. // 'writeStatus' => 1,
  205. // 'value' => '']
  206. // ];
  207. // $first_array = array_splice($field_list, 0, 2);
  208. // $array = array_merge($first_array, $field, $field_list);
  209. // $excelModel->excelImportDownload($array, 'crm_product', $save_path);
  210. }
  211. /**
  212. * 产品导出
  213. * @author Michael_xu
  214. * @param
  215. * @return
  216. */
  217. public function excelExport()
  218. {
  219. $param = $this->param;
  220. $userInfo = $this->userInfo;
  221. $param['user_id'] = $userInfo['id'];
  222. $action_name='导出全部';
  223. if ($param['product_id']) {
  224. $param['product_id'] = ['condition' => 'in','value' => $param['product_id'],'form_type' => 'text','name' => ''];
  225. $action_name='导出选中';
  226. }
  227. $excelModel = new \app\admin\model\Excel();
  228. // 导出的字段列表
  229. $fieldModel = new \app\admin\model\Field();
  230. $field_list = $fieldModel->getIndexFieldConfig('crm_product', $userInfo['id']);
  231. // 文件名
  232. $file_name = '5kcrm_product_'.date('Ymd');
  233. $model = model('Product');
  234. $temp_file = $param['temp_file'];
  235. unset($param['temp_file']);
  236. $page = $param['page'] ?: 1;
  237. unset($param['page']);
  238. unset($param['export_queue_index']);
  239. RecordActionLog($userInfo['id'],'crm_product','excelexport',$action_name,'','','导出产品');
  240. return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function($page, $limit) use ($model, $param, $field_list) {
  241. $param['page'] = $page;
  242. $param['limit'] = $limit;
  243. $data = $model->getDataList($param);
  244. $data['list'] = $model->exportHandle($data['list'], $field_list, 'product');
  245. return $data;
  246. });
  247. }
  248. /**
  249. * 产品数据导入
  250. * @author Michael_xu
  251. * @param
  252. * @return
  253. */
  254. public function excelImport()
  255. {
  256. $param = $this->param;
  257. $userInfo = $this->userInfo;
  258. $excelModel = new \app\admin\model\Excel();
  259. $param['types'] = 'crm_product';
  260. $param['create_user_id'] = $userInfo['id'];
  261. $param['owner_user_id'] = $param['owner_user_id'] ? : $userInfo['id'];
  262. $file = request()->file('file');
  263. $res = $excelModel->batchImportData($file, $param, $this);
  264. RecordActionLog($userInfo['id'],'crm_product','excel','导入产品','','','导入产品');
  265. return resultArray(['data' => $excelModel->getError()]);
  266. }
  267. /**
  268. * 删除
  269. *
  270. * @return void
  271. * @author Ymob
  272. * @datetime 2019-10-24 13:44:31
  273. */
  274. public function delete()
  275. {
  276. $id_list = (array) $this->param['id'];
  277. $id_list = array_map('intval', $id_list);
  278. $productModel = model('Product');
  279. // 错误信息
  280. $delIds = [];
  281. $error_message = [];
  282. // 过滤后的ID
  283. $id_list_filter = ProductModel::where(['product_id' => ['IN', $id_list]])->column('product_id');
  284. $diff = array_diff($id_list, $id_list_filter);
  285. if (!empty($diff)) {
  286. foreach ($diff as $key => $val) {
  287. $error_message[] = sprintf('ID为 %d 的产品删除失败,错误原因:数据不存在或已删除。', $val);
  288. }
  289. array_unshift($error_message, '数据已更新,刷新页面后重试!');
  290. return resultArray(['error' => $error_message]);
  291. }
  292. //数据权限判断
  293. $userModel = new \app\admin\model\User();
  294. $auth_user_ids = $userModel->getUserByPer('crm', 'product', 'delete');
  295. foreach ($id_list as $k => $v) {
  296. $isDel = true;
  297. //数据详情
  298. $data = $productModel->getDataById($v);
  299. if (!in_array($data['owner_user_id'], $auth_user_ids)) {
  300. $isDel = false;
  301. $errorMessage[] = '名称为' . $data['name'] . '的产品删除失败,错误原因:无权操作';
  302. }
  303. if ($isDel) {
  304. $delIds[] = $v;
  305. }
  306. }
  307. $dataInfo = $productModel->where('product_id',['in',$delIds])->select();
  308. if ($delIds) {
  309. // 开启事务
  310. ProductModel::startTrans();
  311. // 软删除数据
  312. $res = ProductModel::destroy(['product_id' => ['IN', $delIds]]);
  313. if ($res == count($delIds)) {
  314. // 事务提交
  315. ProductModel::commit();
  316. // 删除关联附件
  317. (new FileModel)->delRFileByModule('crm_product', $delIds);
  318. // 操作记录
  319. (new ActionRecordModel)->delDataById('crm_product', $delIds);
  320. // 添加删除记录
  321. $userInfo = $this->userInfo;
  322. foreach ($dataInfo as $k => $v) {
  323. RecordActionLog($userInfo['id'], 'crm_contacts', 'delete', $v['name'], '', '', '删除了产品:' . $v['name']);
  324. }
  325. return resultArray(['data' => '删除成功']);
  326. } else {
  327. // 事务回滚
  328. ProductModel::rollback();
  329. return resultArray(['error' => '删除失败']);
  330. }
  331. }
  332. if ($errorMessage) {
  333. return resultArray(['error' => $errorMessage]);
  334. } else {
  335. return resultArray(['data' => '删除成功']);
  336. }
  337. }
  338. /**
  339. * 系统信息
  340. *
  341. * @return \think\response\Json
  342. * @throws \think\db\exception\DataNotFoundException
  343. * @throws \think\db\exception\ModelNotFoundException
  344. * @throws \think\exception\DbException
  345. */
  346. public function system()
  347. {
  348. if (empty($this->param['id'])) return resultArray(['error' => '参数错误!']);
  349. $productModel = new \app\crm\model\Product();
  350. $data = $productModel->getSystemInfo($this->param['id']);
  351. return resultArray(['data' => $data]);
  352. }
  353. /**
  354. * table标签栏数量
  355. *
  356. * @return \think\response\Json
  357. * @throws \think\Exception
  358. */
  359. public function count()
  360. {
  361. if (empty($this->param['product_id'])) return resultArray(['error' => '参数错误!']);
  362. # 附件
  363. $fileCount = Db::name('crm_product_file')->alias('product')->join('__ADMIN_FILE__ file', 'file.file_id = product.file_id', 'LEFT')->where('product_id', $this->param['product_id'])->count();
  364. return resultArray(['data' => ['fileCount' => $fileCount]]);
  365. }
  366. /**
  367. * 转移
  368. *
  369. * @return \think\response\Json
  370. * @throws \think\Exception
  371. * @throws \think\exception\PDOException
  372. */
  373. public function transfer()
  374. {
  375. if (empty($this->param['product_id']) || !is_array($this->param['product_id'])) return resultArray(['error' => '产品参数错误!']);
  376. if (empty($this->param['owner_user_id'])) return resultArray(['error' => '请选择要变更的负责人']);
  377. $userModel = new \app\admin\model\User();
  378. $userInfo=$this->userInfo;
  379. $productModel = new \app\crm\model\Product();
  380. if (!$productModel->transfer($this->param)) return resultArray(['error' => '操作失败!']);
  381. $owner_user_info = $userModel->getUserById($this->param['owner_user_id']);
  382. foreach ($this->param['product_id'] as $v){
  383. $product_info=db('crm_product')->where('product_id',$v)->find();
  384. updateActionLog($userInfo['id'], 'crm_product', $v, '', '', '将产品转移给:' . $owner_user_info['realname']);
  385. RecordActionLog($userInfo['id'], 'crm_product', 'transfer',$product_info['name'], '','','将产品:'.$product_info['name'].'转移给:' . $owner_user_info['realname']);
  386. }
  387. return resultArray(['data' => '操作成功!']);
  388. }
  389. }