Business.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. namespace addons\qingdongams\model;
  3. use think\Db;
  4. use think\Exception;
  5. use think\Model;
  6. use traits\model\SoftDelete;
  7. /**
  8. * 商机管理
  9. */
  10. class Business Extends Model
  11. {
  12. use SoftDelete;
  13. // 表名,不含前缀
  14. protected $name = 'qingdongams_business';
  15. // 开启自动写入时间戳字段
  16. protected $autoWriteTimestamp = 'int';
  17. // 定义时间戳字段名
  18. protected $createTime = 'createtime';
  19. protected $updateTime = 'updatetime';
  20. protected $deleteTime = 'deletetime';
  21. //创建商机
  22. public static function createBusiness($params) {
  23. //自定义字段
  24. $other = [];
  25. foreach ($params as $name => $val) {
  26. if (strstr($name, 'other_') !== false) {
  27. if(is_array($val)){
  28. $other[$name] = implode(',',$val);
  29. }else{
  30. $other[$name] = $val;
  31. }
  32. unset($params[$name]);
  33. }else{
  34. if(empty($params[$name])){
  35. $params[$name]=NULL;
  36. }
  37. }
  38. }
  39. $product = [];
  40. if (isset($params['product']) && $params['product']) {
  41. $product = $params['product'];
  42. unset($params['product']);
  43. foreach ($product as $tkey => $t) {
  44. unset($product[$tkey]['id']);
  45. if($t['number'] <=0){
  46. throw new Exception('产品数量必须大于0');
  47. }
  48. }
  49. }
  50. $customer=Customer::where(['id'=>$params['customer_id']])->find();
  51. if(empty($customer)){
  52. throw new Exception('客户不存在');
  53. }
  54. $params['owner_staff_id'] = $customer->owner_staff_id;
  55. $staff = Staff::info();
  56. if (!empty($staff)) {
  57. $params['create_staff_id'] = $staff->id;
  58. }
  59. $Model = new self;
  60. $result = $Model->allowField(true)->save($params);
  61. if (false === $result) {
  62. // 验证失败 输出错误信息
  63. throw new Exception($Model->getError());
  64. }
  65. $lastId = $Model->id;
  66. $otherModel = new BusinessOther();
  67. if ($otherModel->save(['id' => $lastId, 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)]) === false) {
  68. // 验证失败 输出错误信息
  69. throw new Exception($otherModel->getError());
  70. }
  71. $addProduct = [];
  72. foreach ($product as $v) {
  73. $v['business_id'] = $lastId;
  74. $addProduct[] = $v;
  75. }
  76. if ($addProduct) {
  77. $productModel = new BusinessProduct();
  78. $productModel->allowField(true)->saveAll($addProduct);
  79. }
  80. OperationLog::createLog(OperationLog::BUSINESS_TYPE, $lastId, '创建商机');
  81. return true;
  82. }
  83. //修改商机
  84. public static function updateBusiness($params) {
  85. //自定义字段
  86. $other = [];
  87. foreach ($params as $name => $val) {
  88. if (strstr($name, 'other_') !== false) {
  89. if(is_array($val)){
  90. $other[$name] = implode(',',$val);
  91. }else{
  92. $other[$name] = $val;
  93. }
  94. unset($params[$name]);
  95. }else{
  96. if(empty($params[$name])){
  97. $params[$name]=NULL;
  98. }
  99. }
  100. }
  101. $product = [];
  102. if (isset($params['product'])) {
  103. $product = $params['product'];
  104. unset($params['product']);
  105. if(isset($params['row[product'])){
  106. unset($params['row[product']);
  107. }
  108. if(!is_array($product)){
  109. $product = json_decode($product,true);
  110. }
  111. foreach ($product as $tkey => $t) {
  112. unset($product[$tkey]['id']);
  113. if($t['number'] <=0){
  114. throw new Exception('产品数量必须大于0');
  115. }
  116. }
  117. }
  118. $Model = new self;
  119. // 调用当前模型对应的User验证器类进行数据验证
  120. $result = $Model->allowField(true)->save($params, ['id' => $params['id']]);
  121. if (false === $result) {
  122. // 验证失败 输出错误信息
  123. throw new Exception($Model->getError());
  124. }
  125. $otherModel = new BusinessOther();
  126. if ($otherModel->save(['otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)], ['id' => $params['id']]) === false) {
  127. // 验证失败 输出错误信息
  128. throw new Exception($otherModel->getError());
  129. }
  130. $addProduct = [];
  131. if($product){
  132. foreach ($product as $v) {
  133. $v['business_id'] = $params['id'];
  134. $addProduct[] = $v;
  135. }
  136. }
  137. if ($addProduct) {
  138. $productModel = new BusinessProduct();
  139. $productModel->where(['business_id' => $params['id']])->delete();
  140. $productModel->allowField(true)->saveAll($addProduct);
  141. }
  142. return true;
  143. }
  144. //获取更新时间
  145. public function getUpdatetimeAttr($value) {
  146. return date('Y-m-d H:i:s', $value);
  147. }
  148. //负责人
  149. public function ownerStaff() {
  150. return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name,img');
  151. }
  152. //客户
  153. public function customer() {
  154. return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,name,follow');
  155. }
  156. //商机
  157. public function businessOther() {
  158. return $this->hasOne(BusinessOther::class, 'id', 'id');
  159. }
  160. //
  161. public function staff() {
  162. return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name,img,department_id,post');
  163. }
  164. //产品
  165. public function product(){
  166. return $this->hasMany(BusinessProduct::class,'business_id','id')->with('productOne');
  167. }
  168. /**
  169. * 批量转移商机
  170. */
  171. public static function batchTransfer($ids, $staff_id) {
  172. Db::startTrans();
  173. try {
  174. if (Business::where(['id' => ['in',$ids]])->update([
  175. 'owner_staff_id' => $staff_id,
  176. 'updatetime' => time()
  177. ]) == false) {
  178. throw new Exception('修改失败');
  179. }
  180. $staff = Staff::get($staff_id);
  181. foreach ($ids as $id){
  182. OperationLog::createLog(OperationLog::BUSINESS_TYPE, $id, '将商机转移给:' . $staff['name']);
  183. }
  184. Db::commit();
  185. } catch (Exception $e) {
  186. Db::rollback();
  187. throw new Exception($e->getMessage());
  188. }
  189. return true;
  190. }
  191. public static function getList() {
  192. return self::where(['owner_staff_id'=>['in',Staff::getMyStaffIds()]])->field('id,name')->select();
  193. }
  194. /**
  195. * 推进商机
  196. */
  197. public static function batchStatus($params) {
  198. Db::startTrans();
  199. try {
  200. (new self())->save(['status'=>$params['type']],['id'=>$params['id']]);
  201. $ret = array(
  202. 'business_id'=>$params['id'],
  203. 'type'=>$params['type'],
  204. 'remark'=>$params['remark'],
  205. 'file'=>$params['file'],
  206. );
  207. $result = BusinessStatus::create($ret);
  208. if ($result == false) {
  209. throw new Exception('推进失败');
  210. }
  211. Db::commit();
  212. } catch (Exception $e) {
  213. Db::rollback();
  214. throw new Exception($e->getMessage());
  215. }
  216. return true;
  217. }
  218. /**
  219. * 导入商机
  220. * @param $data
  221. * @return bool
  222. */
  223. public static function importBusiness($data) {
  224. $addBusiness = [];
  225. $addOther = [];
  226. $addLog=[];
  227. foreach ($data as $params) {
  228. //自定义字段
  229. $other = [];
  230. foreach ($params as $name => $val) {
  231. if (strstr($name, 'other_') !== false) {
  232. if(is_array($val)){
  233. $other[$name] = implode(',',$val);
  234. }else{
  235. $other[$name] = $val;
  236. }
  237. unset($params[$name]);
  238. }else{
  239. if(empty($params[$name])){
  240. $params[$name]=NULL;
  241. }
  242. }
  243. }
  244. $other['id'] = $params['id'];
  245. $params['next_time'] = date('Y-m-d H:i:s');
  246. $params['createtime'] = time();
  247. $params['updatetime'] = time();
  248. $addOther[] = ['id' => $params['id'], 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)];
  249. $addLog[] = [
  250. 'content' => '导入商机',
  251. 'operation_type' => 2,
  252. 'operation_id' => 30,
  253. 'relation_type' => OperationLog::BUSINESS_TYPE,
  254. 'relation_id' => $params['id'],
  255. 'createtime' => time()
  256. ];
  257. $addBusiness[] = $params;
  258. }
  259. $customer = new self;
  260. // 调用当前模型对应的User验证器类进行数据验证
  261. $result = $customer->allowField(true)->insertAll($addBusiness);
  262. $otherModel = new BusinessOther();
  263. $otherModel->allowField(true)->insertAll($addOther);
  264. $logModel = new OperationLog();
  265. $logModel->allowField(true)->insertAll($addLog);
  266. return true;
  267. }
  268. }