package test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kiisoo.ic.app.IcApplication;
import com.kiisoo.ic.customer.mapper.OpSellerCustomerRelationDOMapper;
import com.kiisoo.ic.job.behavior.CustomerBehaviorJob;
import com.kiisoo.ic.job.detail.CustomerDataJob;
import com.kiisoo.ic.job.entity.DataAccountDO;
import com.kiisoo.ic.job.mapper.DataAccountDOMapper;
import com.kiisoo.ic.store.entity.PoiStoreStaff;
import com.kiisoo.ic.store.mapper.PoiStoreStaffDOMapper;
import com.kiisoo.ic.system.entity.PrivilageUserDO;
import com.kiisoo.ic.system.mapper.PrivilageUserDOMapper;
import com.kiisoo.ic.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = IcApplication.class)
public class JobTest {


    /**
     * 客流定时任务 service
     */
    @Autowired
    private CustomerDataJob customerDataJob;

    @Autowired
    private PrivilageUserDOMapper privilageUserDOMapper;

    @Autowired
    private PoiStoreStaffDOMapper poiStoreStaffDOMapper;

    @Autowired
    private OpSellerCustomerRelationDOMapper opSellerCustomerRelationDOMapper;

    @Test
    public void test() {
        try {
            log.info("<--获取联系客户统计数据定时任务执行-->");

            // 19906239808,
            // 13278869965
            // 19906239800
            String cpUserIds = "17386133351";
            customerDataJob.handle(null);
//            List<String> cpUserIds = dataAccountDOMapper.selectByAccount();
//            customerDataJob.handleList(cpUserIds);
            log.info("<--获取联系客户统计数据定时任务结束-->");
        } catch (Exception e) {
            log.error("获取联系客户统计数据任务出错", e);
        }

    }

    @Test
    public void check() throws IOException {

        List<String> list = FileUtils.readLines(new File("data/1.txt"));
//        List<String> list2 = FileUtils.readLines(new File("data/2.txt"));

//        Map<String, String> stringMap = new HashMap<>();
//        for (int i = 0; i < list2.size(); i++) {
//            String item = list2.get(i);
//            String[] arr = item.split(",");
//            if(stringMap.containsKey(arr[3])){
//                System.out.println("重复" + item);
//                continue;
//            }
//            stringMap.put(arr[3], item);
//        }

        List<Map<String, Object>> customerList = opSellerCustomerRelationDOMapper.selectCustomersByCpUserId();
        Map<String, Object> customerMap = new HashMap<>();
        for (int i = 0; i < customerList.size(); i++) {
            Map<String, Object> item = customerList.get(i);
            customerMap.put(item.get("cpUserId").toString(), item.get("sumCustomer"));
        }

        for (int i = 0; i < list.size(); i++) {
            String item = list.get(i);
            String[] arr = item.split(",");
            String account = arr[0];
            String val = arr[1];

            if(account.equals("#N/A") || val.equals("0")){
                continue;
            }

            Object itemMap = customerMap.get(account);
            if(null == itemMap){
                continue;
            }

            if(val.equals(itemMap.toString())){
               continue;
            }

            System.out.println(item + "|  " + itemMap);

        }

    }

    @Test
    public void test3() throws IOException {
        List<String> list = FileUtils.readLines(new File("check/aaa.txt"));

        Integer num = 0;
        for (int i = 0; i < list.size(); i++) {
           String item =  list.get(i);

           num += Integer.parseInt(item);
        }

        System.out.println(num);

    }

    @Test
    public void test4() throws IOException {
        List<String> list = FileUtils.readLines(new File("check/wx-q.txt"));

        File file = new File("check/ww.txt");

        for (int i = 0; i < list.size(); i++) {
            String item =  list.get(i);
            String arr[] = item.split(",");
            String name = arr[0];
            String sumCustomer = arr[1];

            PrivilageUserDO user = new PrivilageUserDO();
            user.setName(name);
            PrivilageUserDO userDO = privilageUserDOMapper.selectOne(new QueryWrapper<>(user));
            if(null == userDO){
                System.out.println(item);
                continue;
            }

            PoiStoreStaff staff = new PoiStoreStaff();
            staff.setUserId(userDO.getId());
            PoiStoreStaff staffDO = poiStoreStaffDOMapper.selectOne(new QueryWrapper<>(staff));
            if(null == staffDO){
                System.out.println("staffDO=null:" + item);
                continue;
            }

            System.out.println(staffDO.toString());
            FileUtils.write(file, staffDO.toString() + "\n", "utf-8", true);
        }
    }

    @Autowired
    private DataAccountDOMapper dataAccountDOMapper;

    @Test
    public void test5() throws IOException {

        List<String> list = FileUtils.readLines(new File("data/account.txt"));


        int id = 1;
        for (int i = 0; i < list.size(); i++) {
            String item = list.get(i);

            id =  i + 1;
            DataAccountDO entity = new DataAccountDO();
            entity.setAccount(item.trim());
            entity.setId(new Long(id+""));
            dataAccountDOMapper.insert(entity);

        }

    }
}
