package com.mvc.controller.setup;

import com.mvc.model.CorrectMap;
import com.mvc.model.ErrorMap;
import com.mvc.model.TSetupModel;
import com.mvc.service.setup.TSetupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Api(tags = "设备施工安装接口")
@RequestMapping("/TSetupAction")
@Controller
public class TSetupAction {

    @Autowired
    private TSetupService tSetupService;

    @ApiOperation("设备安装主表分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findAllTSetup(@RequestBody TSetupModel queryModel) {
        try {
            Map<String, Object> res = tSetupService.findAllTSetup(queryModel);
            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 = "/saveTSetup", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveTSetup(@RequestBody TSetupModel model) {
        try {
            TSetupModel resModel = tSetupService.saveTSetup(model);
            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 = "/updateTSetup", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateTSetup(@RequestBody TSetupModel model) {
        try {
            TSetupModel resModel = tSetupService.updateTSetup(model);
            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 = "/finishTSetup", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> finishTSetup(@RequestBody TSetupModel model) {
        try {
            TSetupModel resModel = tSetupService.finishTSetup(model);
            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 = "/deleteTSetup/{id}/{setupId}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> deleteTSetup(@PathVariable("id") int id, @PathVariable("setupId") String setupId) {
        try {
            int res = tSetupService.deleteTSetup(id, setupId);
            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 = "/downSetupPicture/{id}", method = RequestMethod.GET)
    @ResponseBody
    public void downSetupPicture(HttpServletResponse response, @PathVariable("id") int id) {
        tSetupService.downSetupPicture(response, id);
    }
}
