package com.casic.missiles.modular.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.casic.missiles.core.base.service.IBaseExportService;
import com.casic.missiles.model.response.ResponseData;;
import com.casic.missiles.modular.system.dao.DeviceImeiMapper;
import com.casic.missiles.modular.system.model.DeviceImei;
import com.casic.missiles.modular.system.service.IDeviceImeiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@Slf4j
@Service
public class DeviceImeiService extends ServiceImpl<DeviceImeiMapper, DeviceImei> implements IDeviceImeiService {

    @Resource
    private IBaseExportService exportService;

    @Override
    public ResponseData deviceImeiListPage( Page<Map<String,Object>> page, String devcode) {
        ResponseData responseData = new ResponseData();
        try {
            List<Map<String,Object>>  pageList = this.baseMapper.getImeiListPage(page, devcode);
            page.setRecords(pageList);
            responseData.setMessage("查询成功");
            responseData.setCode(200);
            responseData.setData(page);
        } catch (DataAccessException dex) {
            log.error("主题：查询分页出现异常，设备编号{},异常信息为{}", devcode, dex);
            responseData.setCode(500);
            responseData.setMessage("数据库异常");
        }
        return responseData;
    }

    @Override
    public Object addImei(DeviceImei deviceImei) {
        ResponseData responseData = new ResponseData();
        try {
            this.baseMapper.insert(deviceImei);
            responseData.setMessage("新增成功");
            responseData.setCode(200);
        } catch (DataAccessException dex) {
            log.error("主题：smi新增出现异常，设备编号{},异常信息为{}", deviceImei.getDevcode(), dex);
            responseData.setCode(500);
            responseData.setMessage("数据库异常");
        }
        return responseData;
    }

    @Override
    public Object editImei(DeviceImei deviceImei) {
        ResponseData responseData = new ResponseData();
        if (ObjectUtil.isEmpty(deviceImei.getId())) {
            responseData.setMessage("主键不能为空");
            responseData.setCode(200);
        } else {
            try {
                this.baseMapper.updateById(deviceImei);
                responseData.setMessage("更新成功");
                responseData.setCode(200);
            } catch (DataAccessException dex) {
                log.error("主题：smi更新出现异常，设备编号{},异常信息为{}", deviceImei.getDevcode(), dex);
                responseData.setCode(500);
                responseData.setMessage("数据库异常");
            }
        }
        return responseData;
    }

    @Override
    public Object deleteImei(Long id) {
        ResponseData responseData = new ResponseData();
        try {
            this.baseMapper.deleteById(id);
            responseData.setMessage("删除成功");
            responseData.setCode(200);
        } catch (DataAccessException dex) {
            log.error("主题：smi删除出现异常，主键id:{},异常信息为{}", id, dex);
            responseData.setCode(500);
            responseData.setMessage("数据库异常");
        }
        return responseData;
    }

    public Object importImeiList(MultipartFile uploadFile) {
        ResponseData responseData = new ResponseData();
        if (null == uploadFile) {
            responseData.setCode(500);
            responseData.setMessage("上传失败，无法找到文件！");
            return responseData;
        }
        try {
            List<DeviceImei> deviceImeiList = exportService.importExcel(DeviceImei.class, uploadFile, null, null);
            Map<String, Object> objectMap = this.validate(deviceImeiList);
            //检验通过后写入
            if ((Boolean) objectMap.get("success")) {
                this.addBatchImei(deviceImeiList);
                responseData.setCode(200);
                responseData.setMessage(objectMap.get("msg").toString());
            } else {//校验失败，返回
                responseData.setCode(500);
                responseData.setMessage(objectMap.get("msg").toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
            responseData.setCode(500);
            responseData.setMessage("导入失败");
        }
        return responseData;
    }

    private void addBatchImei(List<DeviceImei> deviceImeiList) {
        deviceImeiList.forEach(
                deviceImei -> this.baseMapper.insert(deviceImei)
        );
    }

    private Map<String, Object> validate(List<DeviceImei> deviceImeiList) {
        Map<String, Object> stringMap = new HashMap<>();
//        try {
//            for (int i = 0; i < deviceImeiList.size(); i++) {
//                if (!StrUtil.isEmpty(deviceImeiList.get(i).getDevcode())) {
//                    stringMap.put("success", false);
//                    stringMap.put("msg", "第" + (i + 2) + "行，不能为空");
//                    return stringMap;
//                } else {//校验设备编号是否在系统中存在
//
//                   List<DeviceImei> deviceImei = this.baseMapper.selectOne(busConfigDTOList.get(i).getDevcode());
//                    if (null == deviceImei) {
//                        stringMap.put("success", false);
//                        stringMap.put("msg", "第" + (i + 2) + "行，设备编号在系统中不存在，请先导入设备");
//                        return stringMap;
//                    } else {
//                        deviceImeiList.get(i).setDeviceId(device.getId());
//                    }
//                }
//                if (0 == deviceImeiList.get(i).getPercent()) {
//                    stringMap.put("success", false);
//                    stringMap.put("msg", "第" + (i + 2) + "行，报警阈值必填且小数位最多保留2位");
//                    return stringMap;
//
//                } else if (!RegUtil.isDecimal2(String.valueOf(deviceImeiList.get(i).getPercent()))) {
//                    stringMap.put("success", false);
//                    stringMap.put("msg", "第" + (i + 2) + "行，报警阈值必填且小数位最多保留2位");
//                    return stringMap;
//                }
//                if (!RegUtil.isInteger(deviceImeiList.get(i).getRetryTimes())) {
//                    stringMap.put("success", false);
//                    stringMap.put("msg", "第" + (i + 2) + "行，重传次数必填且为正整数");
//                    return stringMap;
//                }
//                if (0.0 == deviceImeiList.get(i).getInstallHeight()) {
//                    stringMap.put("success", false);
//                    stringMap.put("msg", "第" + (i + 2) + "行，安装高度必填且小数位最多保留2位");
//                    return stringMap;
//                } else if (!RegUtil.isDecimal2(String.valueOf(deviceImeiList.get(i).getInstallHeight()))) {
//                    stringMap.put("success", false);
//                    stringMap.put("msg", "第" + (i + 2) + "行，安装高度必填且小数位最多保留2位");
//                    return stringMap;
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            stringMap.put("success", false);
//            stringMap.put("msg", "导入异常！");
//        }
        stringMap.put("success", true);
        stringMap.put("msg", "导入成功");
        return stringMap;
    }
}


