ProductCategory.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 产品类别
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\crm\model;
  8. use app\admin\controller\ApiCommon;
  9. use think\Db;
  10. use app\admin\model\Common;
  11. use think\Request;
  12. use think\Validate;
  13. class ProductCategory extends Common
  14. {
  15. /**
  16. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  17. * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
  18. */
  19. protected $name = 'crm_product_category';
  20. protected $createTime = 'create_time';
  21. protected $updateTime = 'update_time';
  22. protected $autoWriteTimestamp = true;
  23. /**
  24. * [getDataList 产品分类list]
  25. * @author Michael_xu
  26. * @param [string] $map [查询条件]
  27. * @param [number] $page [当前页数]
  28. * @param [number] $limit [每页数量]
  29. * @return [array] [description]
  30. */
  31. public function getDataList($type)
  32. {
  33. $cat = new \com\Category('crm_product_category', array('category_id', 'pid', 'name', 'title'));
  34. $data = $cat->getList('', 0, 'category_id');
  35. // 若type为tree,则返回树状结构
  36. if ($type == 'tree') {
  37. $tree = new \com\Tree();
  38. $data = $tree->list_to_tree($data, 'category_id', 'pid', 'child', 0, true, array(''));
  39. }
  40. return $data;
  41. }
  42. /**
  43. * 创建产品分类信息
  44. * @author Michael_xu
  45. * @param
  46. * @return
  47. */
  48. public function createData($param)
  49. {
  50. // 自动验证
  51. $validate = validate($this->name);
  52. if (!$validate->check($param)) {
  53. $this->error = $validate->getError();
  54. return false;
  55. }
  56. if ($this->data($param)->allowField(true)->save()) {
  57. $data = [];
  58. $data['id'] = $this->category_id;
  59. # 系统操作日志
  60. $user=new ApiCommon();
  61. $userInfo=$user->userInfo;
  62. SystemActionLog($userInfo['id'], 'crm_product','customer', $this->category_id, 'save',$param['name'] , '', '','添加了产品分类:'.$param['name']);
  63. return $data;
  64. } else {
  65. $this->error = '添加失败';
  66. return false;
  67. }
  68. }
  69. /**
  70. * 编辑产品分类主表信息
  71. * @author Michael_xu
  72. * @param
  73. * @return
  74. */
  75. public function updateDataById($param, $category_id = '')
  76. {
  77. $dataInfo = $this->get($category_id);
  78. if (!$dataInfo) {
  79. $this->error = '数据不存在或已删除';
  80. return false;
  81. }
  82. $param['category_id'] = $category_id;
  83. // 自动验证
  84. $validate = validate($this->name);
  85. if (!$validate->check($param)) {
  86. $this->error = $validate->getError();
  87. return false;
  88. }
  89. if ($this->allowField(true)->save($param, ['category_id' => $category_id])) {
  90. # 系统操作日志
  91. $user=new ApiCommon();
  92. $userInfo=$user->userInfo;
  93. SystemActionLog($userInfo['id'], 'crm_product','customer', $category_id, 'update',$dataInfo['name'] , '', '','编辑了产品分类:'.$dataInfo['name']);
  94. return true;
  95. } else {
  96. $this->error = '编辑失败';
  97. return false;
  98. }
  99. }
  100. /**
  101. * [delDataById 根据id删除数据]
  102. * @param string $id [主键]
  103. * @param boolean $delSon [是否删除子孙数据]
  104. * @return [type] [description]
  105. */
  106. public function delDataById($id = '', $delSon = false)
  107. {
  108. if (!$id) {
  109. $this->error = '删除失败';
  110. return false;
  111. }
  112. //分类下已有产品,则不能删除
  113. $resDel = true;
  114. if (db('crm_product')->where(['category_id' => $id])->find()) {
  115. $resDel = false;
  116. }
  117. if ($delSon && is_numeric($id)) {
  118. $childIds = $this->getAllChild($id);
  119. if($childIds){
  120. if (db('crm_product')->where(['category_id' => ['in',$childIds]])->find()) {
  121. $resDel = false;
  122. }
  123. }
  124. }
  125. if ($resDel === false) {
  126. $this->error = '请先移除该类型及子类下的相关产品';
  127. return false;
  128. }
  129. $data=db('crm_product_category')->where('category_id' , $id)->find();
  130. //提交事务
  131. $this->startTrans();
  132. try {
  133. $this->where(['category_id' => $id])->delete();
  134. if ($delSon && is_numeric($id)) {
  135. // 删除子孙
  136. $childIds = $this->getAllChild($id);
  137. if ($childIds) {
  138. $this->where('category_id', 'in', $childIds)->delete();
  139. }
  140. }
  141. # 系统操作日志
  142. $user=new ApiCommon();
  143. $userInfo=$user->userInfo;
  144. SystemActionLog($userInfo['id'], 'crm_product','customer', $id, 'update',$data['name'] , '', '','删除了产品分类:'.$data['name']);
  145. $this->commit();
  146. return true;
  147. } catch(\Exception $e) {
  148. $this->error = '删除失败';
  149. $this->rollback();
  150. return false;
  151. }
  152. }
  153. /**
  154. * 产品分类id
  155. * @author Michael_xu
  156. * @param
  157. * @return
  158. */
  159. public function getIdByStr($category_id_arr)
  160. {
  161. if ($category_id_arr) {
  162. $category_id = end($category_id_arr);
  163. return $category_id ? : '';
  164. } else {
  165. return '';
  166. }
  167. }
  168. }