123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. $data = $productModel->getDataById($param['id']);
  89. //判断权限
  90. $auth_user_ids = $userModel->getUserByPer('crm', 'product', 'read');
  91. if (!in_array($data['owner_user_id'], $auth_user_ids)) {
  92. //无权限
  93. $authData['dataAuth'] = 0;
  94. return resultArray(['data' => $authData]);
  95. }
  96. if (!$data) {
  97. return resultArray(['error' => $productModel->getError()]);
  98. }
  99. return resultArray(['data' => $data]);
  100. }
  101. /**
  102. * 编辑产品
  103. * @author Michael_xu
  104. * @param
  105. * @return
  106. */
  107. public function update()
  108. {
  109. $productModel = model('Product');
  110. $param = $this->param;
  111. $userInfo = $this->userInfo;
  112. $param['user_id'] = $userInfo['id'];
  113. # 检查产品图片
  114. if (!empty($param['cover_images']) && count(explode(',', $param['cover_images'])) > 9) {
  115. return resultArray(['error' => '最多只能上次9张产品图片!']);
  116. }
  117. # 检查产品详情图片
  118. if (!empty($param['details_images']) && count(explode(',', $param['details_images'])) > 9) {
  119. return resultArray(['error' => '最多只能上次9张产品详情图片!']);
  120. }
  121. if ($productModel->updateDataById($param, $param['id'])) {
  122. return resultArray(['data' => '编辑成功']);
  123. } else {
  124. return resultArray(['error' => $productModel->getError()]);
  125. }
  126. }
  127. /**
  128. * 产品上架、下架
  129. * @author Michael_xu
  130. * @param
  131. * @return
  132. */
  133. public function status()
  134. {
  135. $param = $this->param;
  136. $userInfo = $this->userInfo;
  137. $data = [];
  138. $data['status'] = ($param['status'] == '上架') ? '上架' : '下架';
  139. $data['update_time'] = time();
  140. if (!is_array($param['id'])) {
  141. $productIds[] = $param['id'];
  142. } else {
  143. $productIds = $param['id'] ? : [];
  144. }
  145. if (!$productIds) {
  146. return resultArray(['error' => '参数错误']);
  147. }
  148. $res = db('crm_product')->where(['product_id' => ['in',$productIds]])->update($data);
  149. if (!$res) {
  150. return resultArray(['error' => '操作失败']);
  151. }
  152. return resultArray(['data' => $data['status'].'成功']);
  153. }
  154. /**
  155. * 产品导入模板
  156. * @author Michael_xu
  157. * @param string $save_path 本地保存路径 用于错误数据导出,在 Admin\Model\Excel::batchImportData()调用
  158. * @return
  159. */
  160. public function excelDownload($save_path = '')
  161. {
  162. $param = $this->param;
  163. $userInfo = $this->userInfo;
  164. $excelModel = new \app\admin\model\Excel();
  165. // 导出的字段列表
  166. $fieldModel = new \app\admin\model\Field();
  167. $fieldParam['types'] = 'crm_product';
  168. $fieldParam['action'] = 'excel';
  169. $field_list = $fieldModel->field($fieldParam);
  170. $excelModel->excelImportDownload($field_list, 'crm_product', $save_path);
  171. }
  172. /**
  173. * 产品导出
  174. * @author Michael_xu
  175. * @param
  176. * @return
  177. */
  178. public function excelExport()
  179. {
  180. $param = $this->param;
  181. $userInfo = $this->userInfo;
  182. $param['user_id'] = $userInfo['id'];
  183. if ($param['product_id']) {
  184. $param['product_id'] = ['condition' => 'in','value' => $param['product_id'],'form_type' => 'text','name' => ''];
  185. }
  186. $excelModel = new \app\admin\model\Excel();
  187. // 导出的字段列表
  188. $fieldModel = new \app\admin\model\Field();
  189. $field_list = $fieldModel->getIndexFieldConfig('crm_product', $userInfo['id']);
  190. // 文件名
  191. $file_name = '5kcrm_product_'.date('Ymd');
  192. $model = model('Product');
  193. $temp_file = $param['temp_file'];
  194. unset($param['temp_file']);
  195. $page = $param['page'] ?: 1;
  196. unset($param['page']);
  197. unset($param['export_queue_index']);
  198. return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function($page, $limit) use ($model, $param, $field_list) {
  199. $param['page'] = $page;
  200. $param['limit'] = $limit;
  201. $data = $model->getDataList($param);
  202. $data['list'] = $model->exportHandle($data['list'], $field_list, 'product');
  203. return $data;
  204. });
  205. }
  206. /**
  207. * 产品数据导入
  208. * @author Michael_xu
  209. * @param
  210. * @return
  211. */
  212. public function excelImport()
  213. {
  214. $param = $this->param;
  215. $userInfo = $this->userInfo;
  216. $excelModel = new \app\admin\model\Excel();
  217. $param['types'] = 'crm_product';
  218. $param['create_user_id'] = $userInfo['id'];
  219. $param['owner_user_id'] = $param['owner_user_id'] ? : $userInfo['id'];
  220. $file = request()->file('file');
  221. $res = $excelModel->batchImportData($file, $param, $this);
  222. return resultArray(['data' => $excelModel->getError()]);
  223. }
  224. /**
  225. * 删除
  226. *
  227. * @return void
  228. * @author Ymob
  229. * @datetime 2019-10-24 13:44:31
  230. */
  231. public function delete()
  232. {
  233. $id_list = (array) $this->param['id'];
  234. $id_list = array_map('intval', $id_list);
  235. $productModel = model('Product');
  236. // 错误信息
  237. $delIds = [];
  238. $error_message = [];
  239. // 过滤后的ID
  240. $id_list_filter = ProductModel::where(['product_id' => ['IN', $id_list]])->column('product_id');
  241. $diff = array_diff($id_list, $id_list_filter);
  242. if (!empty($diff)) {
  243. foreach ($diff as $key => $val) {
  244. $error_message[] = sprintf('ID为 %d 的产品删除失败,错误原因:数据不存在或已删除。', $val);
  245. }
  246. array_unshift($error_message, '数据已更新,刷新页面后重试!');
  247. return resultArray(['error' => $error_message]);
  248. }
  249. //数据权限判断
  250. $userModel = new \app\admin\model\User();
  251. $auth_user_ids = $userModel->getUserByPer('crm', 'product', 'delete');
  252. foreach ($id_list as $k => $v) {
  253. $isDel = true;
  254. //数据详情
  255. $data = $productModel->getDataById($v);
  256. if (!in_array($data['owner_user_id'], $auth_user_ids)) {
  257. $isDel = false;
  258. $errorMessage[] = '名称为' . $data['name'] . '的产品删除失败,错误原因:无权操作';
  259. }
  260. if ($isDel) {
  261. $delIds[] = $v;
  262. }
  263. }
  264. if ($delIds) {
  265. // 开启事务
  266. ProductModel::startTrans();
  267. // 软删除数据
  268. $res = ProductModel::destroy(['product_id' => ['IN', $delIds]]);
  269. if ($res == count($delIds)) {
  270. // 事务提交
  271. ProductModel::commit();
  272. // 删除关联附件
  273. (new FileModel)->delRFileByModule('crm_product', $delIds);
  274. // 操作记录
  275. (new ActionRecordModel)->delDataById('crm_product', $delIds);
  276. // 添加删除记录
  277. actionLog($delIds, '', '', '');
  278. return resultArray(['data' => '删除成功']);
  279. } else {
  280. // 事务回滚
  281. ProductModel::rollback();
  282. return resultArray(['error' => '删除失败']);
  283. }
  284. }
  285. if ($errorMessage) {
  286. return resultArray(['error' => $errorMessage]);
  287. } else {
  288. return resultArray(['data' => '删除成功']);
  289. }
  290. }
  291. /**
  292. * 系统信息
  293. *
  294. * @return \think\response\Json
  295. * @throws \think\db\exception\DataNotFoundException
  296. * @throws \think\db\exception\ModelNotFoundException
  297. * @throws \think\exception\DbException
  298. */
  299. public function system()
  300. {
  301. if (empty($this->param['id'])) return resultArray(['error' => '参数错误!']);
  302. $productModel = new \app\crm\model\Product();
  303. $data = $productModel->getSystemInfo($this->param['id']);
  304. return resultArray(['data' => $data]);
  305. }
  306. /**
  307. * table标签栏数量
  308. *
  309. * @return \think\response\Json
  310. * @throws \think\Exception
  311. */
  312. public function count()
  313. {
  314. if (empty($this->param['product_id'])) return resultArray(['error' => '参数错误!']);
  315. # 附件
  316. $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();
  317. return resultArray(['data' => ['fileCount' => $fileCount]]);
  318. }
  319. /**
  320. * 转移
  321. *
  322. * @return \think\response\Json
  323. * @throws \think\Exception
  324. * @throws \think\exception\PDOException
  325. */
  326. public function transfer()
  327. {
  328. if (empty($this->param['product_id']) || !is_array($this->param['product_id'])) return resultArray(['error' => '产品参数错误!']);
  329. if (empty($this->param['owner_user_id'])) return resultArray(['error' => '请选择要变更的负责人']);
  330. $productModel = new \app\crm\model\Product();
  331. if (!$productModel->transfer($this->param)) return resultArray(['error' => '操作失败!']);
  332. return resultArray(['data' => '操作成功!']);
  333. }
  334. }