20221115064704_set_customer_shareing_data.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. use app\model\Customer;
  3. use think\db\exception\PDOException;
  4. use think\facade\Db;
  5. use think\migration\Migrator;
  6. class SetCustomerShareingData extends Migrator
  7. {
  8. /**
  9. * Change Method.
  10. *
  11. * Write your reversible migrations using this method.
  12. *
  13. * More information on writing migrations is available here:
  14. * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
  15. *
  16. * The following commands can be used in this method and Phinx will
  17. * automatically reverse them when rolling back:
  18. *
  19. * createTable
  20. * renameTable
  21. * addColumn
  22. * renameColumn
  23. * addIndex
  24. * addForeignKey
  25. *
  26. * Remember to call "create()" or "update()" and NOT "save()" when working
  27. * with the Table class.
  28. */
  29. public function change()
  30. {
  31. $state = array_merge(Customer::changeState('签单', 'chaos'), Customer::changeState('无效', 'chaos'));
  32. $condition = [
  33. [['designer_id', 'not null', ''], ['state', 'not in', $state]],
  34. [['assigned_personnel', 'not null', ''], ['state', 'not in', $state]],
  35. ];
  36. $org = Db::name('org')->column('path', 'id');
  37. $count = Db::name('customer')->whereOr($condition)->count();
  38. $page = ceil($count / 1000);
  39. for ($p = 1; $p <= $page; $p++) {
  40. $data = Db::name('customer')->whereOr($condition)->page($p, 1000)->select();
  41. foreach ($data as $item) {
  42. $path = $org[$item['org_id']];
  43. $a = explode('-', $path);
  44. $root_id = array_shift($a);
  45. if ($item['designer_id'] != null) {
  46. $saveData = [
  47. 'customer_id' => $item['id'],
  48. 'employee_id' => $item['designer_id'],
  49. 'root_id' => $root_id,
  50. 'addtime' => time()
  51. ];
  52. try {
  53. Db::name('customer_sharing')->insert($saveData);
  54. } catch (PDOException $e) {
  55. print_r($e->getMessage());
  56. }
  57. } else {
  58. $l = explode(',', $item['assigned_personnel']);
  59. foreach ($l as $i) {
  60. $saveData = [
  61. 'customer_id' => $item['id'],
  62. 'employee_id' => $i,
  63. 'root_id' => $root_id,
  64. 'addtime' => time()
  65. ];
  66. try {
  67. Db::name('customer_sharing')->insert($saveData);
  68. } catch (PDOException $e) {
  69. print_r($e->getMessage());
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }