package com.mvc.controller.taizhang;

import com.mvc.model.*;
import com.mvc.service.app.SetupImageType;
import com.mvc.service.taizhang.DeviceAccService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;


@Api(tags = "设备台账接口")
@RequestMapping("/DeviceAccAction")
@Controller
public class DeviceAccAction {

    @Autowired
    private DeviceAccService deviceAccService;


    @ApiOperation("分页查询设备台账")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findAllDeviceAcc(@RequestBody DeviceAccModel model) {
        try {
            Map<String, Object> res = deviceAccService.findAllDeviceAcc(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }


    @ApiOperation("新增设备台账")
    @RequestMapping(value = "/saveDeviceAcc", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveDeviceAcc(@RequestBody DeviceAccModel deviceAcc) {
        try {
            String errMsg = deviceAccService.checkDataLegitimacy(deviceAcc);
            if (StringUtils.isNotEmpty(errMsg)) {
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", errMsg);
                return resMap;
            }

            List<DeviceAccModel> resModel = deviceAccService.saveDeviceAcc(deviceAcc);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", resModel);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("更新设备台账")
    @RequestMapping(value = "/updateDeviceAcc", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateDeviceAcc(@RequestBody DeviceAccModel deviceAcc) {
        try {
            DeviceAccModel resModel = deviceAccService.updateDeviceAcc(deviceAcc);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", resModel);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("删除设备台账")
    @RequestMapping(value = "/deleteDeviceAcc/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> deleteDeviceAcc(@PathVariable("id") int id) {
        try {
            int res = deviceAccService.deleteDeviceAcc(id);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据SN查询台账")
    @RequestMapping(value = "/findDeviceAccBysn/{deviceSn}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findDeviceAccBysn(@PathVariable("deviceSn") String deviceSn) {
        try {
            DeviceAccModel res = deviceAccService.findDeviceAccBysn(deviceSn);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("导入台账数据")
    @PostMapping(value = "/importDeviceAccData")
    @ResponseBody
    public Map<String, Object> importDeviceAccData(@RequestParam("file") MultipartFile file, @RequestParam String type,
                                              @RequestParam String operator) {
        try {
            String resMsg = deviceAccService.importDeviceAccData(file, type, operator);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", resMsg);
            return resMap;
        } catch (Exception e) {
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }
}
