123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- use app\model\Customer;
- use think\db\exception\PDOException;
- use think\facade\Db;
- use think\migration\Migrator;
- class SetCustomerShareingData extends Migrator
- {
- /**
- * Change Method.
- *
- * Write your reversible migrations using this method.
- *
- * More information on writing migrations is available here:
- * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
- *
- * The following commands can be used in this method and Phinx will
- * automatically reverse them when rolling back:
- *
- * createTable
- * renameTable
- * addColumn
- * renameColumn
- * addIndex
- * addForeignKey
- *
- * Remember to call "create()" or "update()" and NOT "save()" when working
- * with the Table class.
- */
- public function change()
- {
- $state = array_merge(Customer::changeState('签单', 'chaos'), Customer::changeState('无效', 'chaos'));
- $condition = [
- [['designer_id', 'not null', ''], ['state', 'not in', $state]],
- [['assigned_personnel', 'not null', ''], ['state', 'not in', $state]],
- ];
- $org = Db::name('org')->column('path', 'id');
- $count = Db::name('customer')->whereOr($condition)->count();
- $page = ceil($count / 1000);
- for ($p = 1; $p <= $page; $p++) {
- $data = Db::name('customer')->whereOr($condition)->page($p, 1000)->select();
- foreach ($data as $item) {
- $path = $org[$item['org_id']];
- $a = explode('-', $path);
- $root_id = array_shift($a);
- if ($item['designer_id'] != null) {
- $saveData = [
- 'customer_id' => $item['id'],
- 'employee_id' => $item['designer_id'],
- 'root_id' => $root_id,
- 'addtime' => time()
- ];
- try {
- Db::name('customer_sharing')->insert($saveData);
- } catch (PDOException $e) {
- print_r($e->getMessage());
- }
- } else {
- $l = explode(',', $item['assigned_personnel']);
- foreach ($l as $i) {
- $saveData = [
- 'customer_id' => $item['id'],
- 'employee_id' => $i,
- 'root_id' => $root_id,
- 'addtime' => time()
- ];
- try {
- Db::name('customer_sharing')->insert($saveData);
- } catch (PDOException $e) {
- print_r($e->getMessage());
- }
- }
- }
- }
- }
- }
- }
|