ProductCategory.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. SystemActionLog($param['create_user_id'], 'crm_product','customer', $this->category_id, 'save',$param['name'] , '', '','添加了产品分类:'.$param['name']);
  61. return $data;
  62. } else {
  63. $this->error = '添加失败';
  64. return false;
  65. }
  66. }
  67. /**
  68. * 编辑产品分类主表信息
  69. * @author Michael_xu
  70. * @param
  71. * @return
  72. */
  73. public function updateDataById($param, $category_id = '')
  74. {
  75. $dataInfo = $this->get($category_id);
  76. if (!$dataInfo) {
  77. $this->error = '数据不存在或已删除';
  78. return false;
  79. }
  80. $param['category_id'] = $category_id;
  81. // 自动验证
  82. $validate = validate($this->name);
  83. if (!$validate->check($param)) {
  84. $this->error = $validate->getError();
  85. return false;
  86. }
  87. $user_id=$param['user_id'];
  88. unset($param['user_id']);
  89. if ($this->allowField(true)->save($param, ['category_id' => $category_id])) {
  90. # 系统操作日志
  91. SystemActionLog($user_id, 'crm_product','customer', $category_id, 'update',$dataInfo['name'] , '', '','编辑了产品分类:'.$dataInfo['name']);
  92. return true;
  93. } else {
  94. $this->error = '编辑失败';
  95. return false;
  96. }
  97. }
  98. /**
  99. * [delDataById 根据id删除数据]
  100. * @param string $param['id'] [主键]
  101. * @param boolean $delSon [是否删除子孙数据]
  102. * @return [type] [description]
  103. */
  104. public function delDataById($param = '', $delSon = false)
  105. {
  106. if (!$param['id']) {
  107. $this->error = '删除失败';
  108. return false;
  109. }
  110. //分类下已有产品,则不能删除
  111. $resDel = true;
  112. if (db('crm_product')->where(['category_id' => $param['id']])->find()) {
  113. $resDel = false;
  114. }
  115. if ($delSon && is_numeric($param['id'])) {
  116. $childIds = $this->getAllChild($param['id']);
  117. if($childIds){
  118. if (db('crm_product')->where(['category_id' => ['in',$childIds]])->find()) {
  119. $resDel = false;
  120. }
  121. }
  122. }
  123. if ($resDel === false) {
  124. $this->error = '请先移除该类型及子类下的相关产品';
  125. return false;
  126. }
  127. $data=db('crm_product_category')->where('category_id' , $param['id'])->find();
  128. //提交事务
  129. $this->startTrans();
  130. try {
  131. $this->where(['category_id' => $param['id']])->delete();
  132. if ($delSon && is_numeric($param['id'])) {
  133. // 删除子孙
  134. $childIds = $this->getAllChild($param['id']);
  135. if ($childIds) {
  136. $this->where('category_id', 'in', $childIds)->delete();
  137. }
  138. }
  139. # 系统操作日志
  140. SystemActionLog($param['user_id'], 'crm_product','customer', $param['id'], 'update',$data['name'] , '', '','删除了产品分类:'.$data['name']);
  141. $this->commit();
  142. return true;
  143. } catch(\Exception $e) {
  144. $this->error = '删除失败';
  145. $this->rollback();
  146. return false;
  147. }
  148. }
  149. /**
  150. * 产品分类id
  151. * @author Michael_xu
  152. * @param
  153. * @return
  154. */
  155. public function getIdByStr($category_id_arr)
  156. {
  157. if ($category_id_arr) {
  158. $category_id = end($category_id_arr);
  159. return $category_id ? : '';
  160. } else {
  161. return '';
  162. }
  163. }
  164. }