package com.zcxk.rmcp.web.controller;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.RandomUtil;
import com.zcxk.core.common.util.SnowflakeIdWorker;
import com.zcxk.rmcp.core.dao.DeviceMapper;
import com.zcxk.rmcp.core.entity.Device;
import io.swagger.annotations.Api;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author linqingwei
 * @date 2021-08-17 9:48
 */
@Controller
@ResponseBody
@RequestMapping("/test")
@Api(tags = "cs")
public class TestController {

    @Autowired
    private SnowflakeIdWorker idWorker;
    @Autowired
    private DeviceMapper deviceMapper;

    @PostMapping("test")
    public void addDevice(){


        List<Device> list = new ArrayList<>();
        for (int i = 0; i < 200000; i++) {
            Device device = new Device();
            device.setId(idWorker.nextId());
            device.setTenantId("487170559310123008");
            device.setCategoryId(2);
            device.setCompanyOrgId(266);
            device.setDeptOrgId(0);
            device.setProductId(2);
            device.setDeviceNo(RandomUtil.randomString(10));
            device.setMeterNo(RandomUtil.randomString(10));
            device.setFileNo(RandomUtil.randomString(10));
            device.setSealNo("");
            device.setDeviceStatus(RandomUtil.randomInt(1,4));
            device.setAddress(RandomUtil.randomString(10));
            device.setLng(new BigDecimal("0"));
            device.setLat(new BigDecimal("0"));
            device.setCommunityId(RandomUtil.randomInt(1,3));
            device.setReadData(RandomUtil.randomNumbers(3));
            device.setValveStatus(1);
            device.setLastReceiveTime(LocalDateTime.now());
            device.setUserName("");
            device.setUserPhone("");
            device.setIdCard("");
            device.setOldEndDegree("");
            device.setOldImageUrl("");
            device.setLastAlarmDate(LocalDateTime.now());
            device.setOfflineDate(LocalDateTime.now());
            device.setAcceptDate(LocalDateTime.now());
            device.setInstallDate(LocalDateTime.now());
            device.setIsInstall(0);
            device.setIsAccept(0);
            device.setStatus(1);
            device.setCreateBy("system");
            device.setCreateDate(LocalDateTime.now());
            device.setUpdateBy("system");
            device.setUpdateDate(LocalDateTime.now());
            list.add(device);
        }
        List<List<Device>> subList2 =  ListUtil.split(list, 5000);

        for (List<Device> devices : subList2) {
            System.out.println("===========");
            deviceMapper.batchInsert(devices);
        }


    }
}
