MarketLogic.php 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace app\crm\logic;
  3. use think\Db;
  4. use think\Validate;
  5. use app\crm\model\Market;
  6. class MarketLogic{
  7. /**
  8. *
  9. * @param $param
  10. *
  11. * @author alvin guogaobo
  12. * @version 1.0 版本号
  13. * @since 2021/4/27 0027 09:46
  14. */
  15. public function getDataList($param){
  16. $userModel = new \app\admin\model\User();
  17. if($param['search']) {
  18. $where['market_field_id'] = $param['search'];
  19. }
  20. //权限
  21. $a = 'index';
  22. $auth_user_ids = $userModel->getUserByPer('crm', 'market', $a);
  23. $authMap['market.create_user_id'] = ['in', $auth_user_ids];
  24. $list=db('crm_market')
  25. ->alias('market')
  26. ->join('__CRM_MARKET_CATEGORY__ market_category','market_category.r_id=market.market_field_id')
  27. ->where($where)
  28. ->where($authMap)
  29. ->page($param['page'],$param['limit'])
  30. ->order()
  31. ->select();
  32. return $list;
  33. }
  34. /**
  35. *
  36. * @author alvin guogaobo
  37. * @version 1.0 版本号
  38. * @since 2021/4/27 0027 09:46
  39. */
  40. public function marketList(){
  41. $list=db('admin_market')->where('status',1)->field('types,name')->select();
  42. $data=[
  43. 0=>[
  44. 'name'=>'客户',
  45. 'types'=>'crm_customer'
  46. ],
  47. 1=>[
  48. 'name'=>'线索',
  49. 'types'=>'crm_leads'
  50. ]
  51. ];
  52. $data=array_merge($data,$list);
  53. return $data;
  54. }
  55. public function createData($param){
  56. $fieldModel = new \app\admin\model\Field();
  57. $dataInfo = db('crm_market')->where(['name' => $param['name']])->find();
  58. if (isset($dataInfo)) {
  59. // 自动验证
  60. $validateArr = $fieldModel->validateField($this->name); //获取自定义字段验证规则
  61. $validate = new Validate($validateArr['rule'], $validateArr['message']);
  62. $result = $validate->check($param);
  63. if (!$result) {
  64. $this->error = $validate->getError();
  65. return false;
  66. }
  67. }
  68. if (db('crm_market')->data($param)->allowField(true)->isUpdate(false)->save()) {
  69. updateActionLog($param['create_user_id'], 'crm_market', $this->product_id, '', '', '创建了市场活动');
  70. RecordActionLog($param['create_user_id'], 'crm_market', 'save', $param['name'], '', '', '新增了市场活动' . $param['name']);
  71. $data = [];
  72. $data['product_id'] = $this->product_id;
  73. return $data;
  74. } else {
  75. $this->error = '添加失败';
  76. return false;
  77. }
  78. }
  79. /**
  80. * 修改
  81. * @param $param
  82. * @param string $market_id
  83. *
  84. * @author alvin guogaobo
  85. * @version 1.0 版本号
  86. * @since 2021/4/27 0027 15:57
  87. */
  88. public function updateDataById($param,$market_id = ''){
  89. $fieldModel = new \app\admin\model\Field();
  90. $dataInfo = db('crm_market')->where(['market_id' => $market_id])->find();
  91. if (isset($dataInfo)) {
  92. // 自动验证
  93. $validateArr = $fieldModel->validateField($this->name); //获取自定义字段验证规则
  94. $validate = new Validate($validateArr['rule'], $validateArr['message']);
  95. $result = $validate->check($param);
  96. if (!$result) {
  97. $this->error = $validate->getError();
  98. return false;
  99. }
  100. }
  101. $param['market_id'] = $market_id;
  102. if (db('crm_market')->update($param, ['market_id' => $market_id], true)) {
  103. updateActionLog($param['create_user_id'], 'crm_market', $this->market_id, '', '', '编辑了市场活动');
  104. RecordActionLog($param['create_user_id'], 'crm_market', 'update', $param['name'], '', '', '编辑了市场活动' . $param['name']);
  105. $data = [];
  106. $data['market_id'] = $this->market_id;
  107. return $data;
  108. } else {
  109. $this->error = '添加失败';
  110. return false;
  111. }
  112. }
  113. /**
  114. * 删除
  115. * @param $id_list
  116. *
  117. * @author alvin guogaobo
  118. * @version 1.0 版本号
  119. * @since 2021/4/28 0028 10:48
  120. */
  121. public function delete($id_list){
  122. $marketModel = model('Market');
  123. // 错误信息
  124. $delIds = [];
  125. $error_message = [];
  126. // 过滤后的ID
  127. $id_list_filter = db('crm_market')->where(['market_id' => ['IN', $id_list]])->column('market_id');
  128. $diff = array_diff($id_list, $id_list_filter);
  129. if (!empty($diff)) {
  130. foreach ($diff as $key => $val) {
  131. $error_message[] = sprintf('ID为 %d 的活动删除失败,错误原因:数据不存在或已删除。', $val);
  132. }
  133. array_unshift($error_message, '数据已更新,刷新页面后重试!');
  134. return resultArray(['error' => $error_message]);
  135. }
  136. //数据权限判断
  137. $userModel = new \app\admin\model\User();
  138. $auth_user_ids = $userModel->getUserByPer('crm', 'market', 'delete');
  139. foreach ($id_list as $k => $v) {
  140. $isDel = true;
  141. //数据详情
  142. $data = $this->getDataById($v);
  143. if (!in_array($data['create_user_id'], $auth_user_ids)) {
  144. $isDel = false;
  145. $errorMessage[] = '名称为' . $data['name'] . '的活动删除失败,错误原因:无权操作';
  146. }
  147. if ($isDel) {
  148. $delIds[] = $v;
  149. }
  150. }
  151. $dataInfo = db('crm_market')->where('market_id',['in',$delIds])->select();
  152. if ($delIds) {
  153. // 软删除数据
  154. $res = $marketModel::destroy(['market_id' => ['IN', $delIds]]);
  155. if ($res == count($delIds)) {
  156. // 添加删除记录
  157. $userInfo = $this->userInfo;
  158. foreach ($dataInfo as $k => $v) {
  159. RecordActionLog($id_list['user_id'], 'crm_market', 'delete', $v['name'], '', '', '删除了活动:' . $v['name']);
  160. }
  161. return resultArray(['data' => '删除成功']);
  162. } else {
  163. return resultArray(['error' => '删除失败']);
  164. }
  165. }
  166. if ($errorMessage) {
  167. return resultArray(['error' => $errorMessage]);
  168. } else {
  169. return resultArray(['data' => '删除成功']);
  170. }
  171. }
  172. /**
  173. * 基本信息
  174. * @param string $id
  175. * @param int $userId
  176. *
  177. * @author alvin guogaobo
  178. * @version 1.0 版本号
  179. * @since 2021/4/27 0027 18:03
  180. */
  181. public function getDataById($id = '', $userId = 0)
  182. {
  183. $map['market_id'] = $id;
  184. $dataInfo = db('crm_market')->where($map)->find();
  185. if (!$dataInfo) {
  186. $this->error = '暂无此数据';
  187. return false;
  188. }
  189. # 获取封面图片
  190. $dataInfo['cover_images'] = $this->getMarketImages($dataInfo['cover_images']);
  191. # 获取详情图片
  192. $dataInfo['details_images'] = $this->getMarketImages($dataInfo['details_images']);
  193. $userModel = new \app\admin\model\User();
  194. $dataInfo['create_user_id_info'] = $userModel->getUserById($dataInfo['create_user_id']);
  195. $dataInfo['create_time'] = !empty($dataInfo['create_time']) ? date('Y-m-d H:i:s', $dataInfo['create_time']) : null;
  196. $dataInfo['update_time'] = !empty($dataInfo['update_time']) ? date('Y-m-d H:i:s', $dataInfo['update_time']) : null;
  197. // 字段授权
  198. if (!empty($userId)) {
  199. $grantData = getFieldGrantData($userId);
  200. $userLevel = isSuperAdministrators($userId);
  201. foreach ($dataInfo as $key => $value) {
  202. if (!$userLevel && !empty($grantData['crm_market'])) {
  203. $status = getFieldGrantStatus($key, $grantData['crm_market']);
  204. # 查看权限
  205. if ($status['read'] == 0) unset($dataInfo[$key]);
  206. }
  207. }
  208. }
  209. return $dataInfo;
  210. }
  211. /**
  212. * 获取活动图片
  213. * @param $fileIds
  214. *
  215. * @author alvin guogaobo
  216. * @version 1.0 版本号
  217. * @since 2021/4/28 0028 10:36
  218. */
  219. private function getMarketImages($fileIds)
  220. {
  221. $files = Db::name('admin_file')->whereIn('file_id', $fileIds)->select();
  222. foreach ($files as $key => $value) {
  223. $files[$key]['file_path'] = getFullPath($value['file_path']);
  224. $files[$key]['file_path_thumb'] = getFullPath($value['file_path_thumb']);
  225. $files[$key]['size'] = format_bytes($value['size']);
  226. }
  227. return $files;
  228. }
  229. }