'use strict';

function convertMemberProfile(config, conns) {
  if (!config.memberProfile.convert) {
    return Promise.resolve();
  }
  return new Promise(async (resolve, reject) => {

    function fetchMemberData() {
      console.log('[MemberProfile][MySQL] Fetching profiles.');
      return new Promise((resolve, reject) => {
        conns.mysql.query({
          sql: 'SELECT * FROM cbs_common_member_profile' + (config.memberProfile.skipFourZero ? ' WHERE NOT (gender = 0 AND birthyear = 0 AND birthmonth = 0 AND birthday = 0)' : ''),
        }, (err, res) => {
          if (err) reject(err);
          else resolve(res);
        });
      })
    }

    function fetchArchviedMemberData() {
      if (config.memberProfile.skipArchiveTable) {
        return Promise.resolve([]);
      }
      console.log('[MemberProfile][MySQL] Fetching archvied profiles.');
      return new Promise((resolve, reject) => {
        conns.mysql.query({
          sql: 'SELECT * FROM cbs_common_member_profile_archive' + (config.memberProfile.skipFourZero ? ' WHERE NOT (gender = 0 AND birthyear = 0 AND birthmonth = 0 AND birthday = 0)' : ''),
        }, (err, res) => {
          if (err)
            reject(err);
          else
            resolve(res);
        });
      })
    }

    function removeEmptyFileds(data) {
      console.log('[MemberProfile][undef] Deleting all empty fileds in member profile...');
      return new Promise((resolve, reject) => {
        data.forEach(row => {
          Object.keys(row).forEach(key => {
            if (row[key] == '')
              delete row[key];
          })
        });
        resolve(data);
      });
    }

    function cleanupMongo() {
      if (!config.memberProfile.cleanup) {
        return Promise.resolve();
      }
      console.log('[MemberProfile][MongoDB] Deleting all data in common_member.');
      return new Promise((resolve, reject) => {
        conns.mongo.collection('common_member').deleteMany({}, (err, res) => {
          if (err) {
            reject(err);
          }
          else {
            resolve();
          }
        })
      })
    }

    function insertMongo(data) {
      return new Promise((resolve, reject) => {
        console.log('[MemberProfile][MongoDB] Inserting data into MongoDB...');
        conns.mongo.collection('common_member').insertMany(data, (err, res) => {
          if (err) {
            reject(err);
          }
          else {
            resolve();
          }
        });
      });
    }

    try {
      await cleanupMongo();
      let resSet = (await fetchMemberData()).concat(await fetchArchviedMemberData());
      resSet = await removeEmptyFileds(resSet);
      await insertMongo(resSet);
      resolve();
    } catch (err) {
      console.log(err);
      reject(err);
    }
  });
}

module.exports = convertMemberProfile;
@@ -358,32 +358,44 @@
 %7B%0A          sql:
+%0A           
  'SELECT * FROM 
@@ -419,16 +419,141 @@
 _profile
+ ' + %0A            'INNER JOIN cbs_ucenter_members ON ' +%0A            'cbs_common_member_profile.uid = cbs_ucenter_members.uid
 ' + (con
@@ -1087,16 +1087,28 @@
     sql:
+%0A           
  'SELECT
@@ -1148,16 +1148,149 @@
 _archive
+ ' + %0A            'INNER JOIN cbs_ucenter_members ON ' +%0A            'cbs_common_member_profile_archive.uid = cbs_ucenter_members.uid
 ' + (con
@@ -1865,16 +1865,16 @@
 w%5Bkey%5D;%0A
-
         
@@ -1873,24 +1873,463 @@
           %7D)
+;%0A          typeof row%5B'constellation'%5D !== 'undefined' && delete row%5B'constellation'%5D;%0A          typeof row%5B'zodiac'%5D !== 'undefined' && delete row%5B'zodiac'%5D;%0A          row%5B'device'%5D = row%5B'field1'%5D;%0A          delete row%5B'field1'%5D;%0A          row%5B'credentials'%5D = %7B%0A            password: row%5B'password'%5D,%0A            type: 'discuz',%0A            salt: row%5B'salt'%5D,%0A          %7D%0A          delete row%5B'password'%5D;%0A          delete row%5B'salt'%5D;
 %0A        %7D);

Update member module.