123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. # 下次升级
  175. $param = $this->param;
  176. $userInfo = $this->userInfo;
  177. $excelModel = new \app\admin\model\Excel();
  178. // 导出的字段列表
  179. $fieldModel = new \app\admin\model\Field();
  180. $fieldParam['types'] = 'crm_product';
  181. $fieldParam['action'] = 'excel';
  182. $field_list = $fieldModel->field($fieldParam);
  183. $field=[1=>[
  184. 'field'=>'owner_user_id',
  185. 'types'=>'crm_product',
  186. 'name'=>'负责人',
  187. 'form_type'=>'user',
  188. 'default_value'=>'',
  189. 'is_null' => 1,
  190. 'input_tips' =>'',
  191. 'setting' => Array(),
  192. 'is_hidden'=>0,
  193. 'writeStatus' => 1,
  194. 'value' => '']
  195. ];
  196. $first_array = array_splice($field_list, 2,0, $field);
  197. $excelModel->excelImportDownload($field_list, 'crm_product', $save_path);
  198. }
  199. /**
  200. * 产品导出
  201. * @author Michael_xu
  202. * @param
  203. * @return
  204. */
  205. public function excelExport()
  206. {
  207. $param = $this->param;
  208. $userInfo = $this->userInfo;
  209. $param['user_id'] = $userInfo['id'];
  210. $action_name='导出全部';
  211. if ($param['product_id']) {
  212. $param['product_id'] = ['condition' => 'in','value' => $param['product_id'],'form_type' => 'text','name' => ''];
  213. $action_name='导出选中';
  214. }
  215. $excelModel = new \app\admin\model\Excel();
  216. // 导出的字段列表
  217. $fieldModel = new \app\admin\model\Field();
  218. $field_list = $fieldModel->getIndexFieldConfig('crm_product', $userInfo['id'],'','excel');
  219. // 文件名
  220. $file_name = '5kcrm_product_'.date('Ymd');
  221. $model = model('Product');
  222. $temp_file = $param['temp_file'];
  223. unset($param['temp_file']);
  224. $page = $param['page'] ?: 1;
  225. unset($param['page']);
  226. unset($param['export_queue_index']);
  227. RecordActionLog($userInfo['id'],'crm_product','excelexport',$action_name,'','','导出产品');
  228. return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function($page, $limit) use ($model, $param, $field_list) {
  229. $param['page'] = $page;
  230. $param['limit'] = $limit;
  231. $data = $model->getDataList($param);
  232. $data['list'] = $model->exportHandle($data['list'], $field_list, 'product');
  233. return $data;
  234. });
  235. }
  236. /**
  237. * 产品数据导入
  238. * @author Michael_xu
  239. * @param
  240. * @return
  241. */
  242. public function excelImport()
  243. {
  244. $param = $this->param;
  245. $userInfo = $this->userInfo;
  246. $excelModel = new \app\admin\model\Excel();
  247. $param['types'] = 'crm_product';
  248. $param['create_user_id'] = $userInfo['id'];
  249. $param['owner_user_id'] = $param['owner_user_id'] ? : $userInfo['id'];
  250. $file = request()->file('file');
  251. $res = $excelModel->batchImportData($file, $param, $this);
  252. RecordActionLog($userInfo['id'],'crm_product','excel','导入产品','','','导入产品');
  253. return resultArray(['data' => $excelModel->getError()]);
  254. }
  255. /**
  256. * 删除
  257. *
  258. * @return void
  259. * @author Ymob
  260. * @datetime 2019-10-24 13:44:31
  261. */
  262. public function delete()
  263. {
  264. $id_list = (array) $this->param['id'];
  265. $id_list = array_map('intval', $id_list);
  266. $productModel = model('Product');
  267. // 错误信息
  268. $delIds = [];
  269. $error_message = [];
  270. // 过滤后的ID
  271. $id_list_filter = ProductModel::where(['product_id' => ['IN', $id_list]])->column('product_id');
  272. $diff = array_diff($id_list, $id_list_filter);
  273. if (!empty($diff)) {
  274. foreach ($diff as $key => $val) {
  275. $error_message[] = sprintf('ID为 %d 的产品删除失败,错误原因:数据不存在或已删除。', $val);
  276. }
  277. array_unshift($error_message, '数据已更新,刷新页面后重试!');
  278. return resultArray(['error' => $error_message]);
  279. }
  280. //数据权限判断
  281. $userModel = new \app\admin\model\User();
  282. $auth_user_ids = $userModel->getUserByPer('crm', 'product', 'delete');
  283. foreach ($id_list as $k => $v) {
  284. $isDel = true;
  285. //数据详情
  286. $data = $productModel->getDataById($v);
  287. if (!in_array($data['owner_user_id'], $auth_user_ids)) {
  288. $isDel = false;
  289. $errorMessage[] = '名称为' . $data['name'] . '的产品删除失败,错误原因:无权操作';
  290. }
  291. if ($isDel) {
  292. $delIds[] = $v;
  293. }
  294. }
  295. $dataInfo = $productModel->where('product_id',['in',$delIds])->select();
  296. if ($delIds) {
  297. // 开启事务
  298. ProductModel::startTrans();
  299. // 软删除数据
  300. $res = ProductModel::destroy(['product_id' => ['IN', $delIds]]);
  301. if ($res == count($delIds)) {
  302. // 事务提交
  303. ProductModel::commit();
  304. // 删除客户扩展数据
  305. db('crm_product_data')->whereIn('product_id', $delIds)->delete();
  306. // 删除关联附件
  307. (new FileModel)->delRFileByModule('crm_product', $delIds);
  308. // 操作记录
  309. (new ActionRecordModel)->delDataById('crm_product', $delIds);
  310. // 添加删除记录
  311. $userInfo = $this->userInfo;
  312. foreach ($dataInfo as $k => $v) {
  313. RecordActionLog($userInfo['id'], 'crm_contacts', 'delete', $v['name'], '', '', '删除了产品:' . $v['name']);
  314. }
  315. return resultArray(['data' => '删除成功']);
  316. } else {
  317. // 事务回滚
  318. ProductModel::rollback();
  319. return resultArray(['error' => '删除失败']);
  320. }
  321. }
  322. if ($errorMessage) {
  323. return resultArray(['error' => $errorMessage]);
  324. } else {
  325. return resultArray(['data' => '删除成功']);
  326. }
  327. }
  328. /**
  329. * 系统信息
  330. *
  331. * @return \think\response\Json
  332. * @throws \think\db\exception\DataNotFoundException
  333. * @throws \think\db\exception\ModelNotFoundException
  334. * @throws \think\exception\DbException
  335. */
  336. public function system()
  337. {
  338. if (empty($this->param['id'])) return resultArray(['error' => '参数错误!']);
  339. $productModel = new \app\crm\model\Product();
  340. $data = $productModel->getSystemInfo($this->param['id']);
  341. return resultArray(['data' => $data]);
  342. }
  343. /**
  344. * table标签栏数量
  345. *
  346. * @return \think\response\Json
  347. * @throws \think\Exception
  348. */
  349. public function count()
  350. {
  351. if (empty($this->param['product_id'])) return resultArray(['error' => '参数错误!']);
  352. # 附件
  353. $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();
  354. return resultArray(['data' => ['fileCount' => $fileCount]]);
  355. }
  356. /**
  357. * 转移
  358. *
  359. * @return \think\response\Json
  360. * @throws \think\Exception
  361. * @throws \think\exception\PDOException
  362. */
  363. public function transfer()
  364. {
  365. if (empty($this->param['product_id']) || !is_array($this->param['product_id'])) return resultArray(['error' => '产品参数错误!']);
  366. if (empty($this->param['owner_user_id'])) return resultArray(['error' => '请选择要变更的负责人']);
  367. $userModel = new \app\admin\model\User();
  368. $userInfo=$this->userInfo;
  369. $productModel = new \app\crm\model\Product();
  370. if (!$productModel->transfer($this->param)) return resultArray(['error' => '操作失败!']);
  371. $owner_user_info = $userModel->getUserById($this->param['owner_user_id']);
  372. foreach ($this->param['product_id'] as $v){
  373. $product_info=db('crm_product')->where('product_id',$v)->find();
  374. updateActionLog($userInfo['id'], 'crm_product', $v, '', '', '将产品转移给:' . $owner_user_info['realname']);
  375. RecordActionLog($userInfo['id'], 'crm_product', 'transfer',$product_info['name'], '','','将产品:'.$product_info['name'].'转移给:' . $owner_user_info['realname']);
  376. }
  377. return resultArray(['data' => '操作成功!']);
  378. }
  379. }