package com.mvc.controller.setup;

import com.alibaba.fastjson.JSON;
import com.mvc.model.CorrectMap;
import com.mvc.model.ErrorMap;
import com.mvc.model.TSetupPicModel;
import com.mvc.service.setup.TSetupPicService;
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 org.springframework.web.multipart.MultipartFile;

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

@Api(tags = "设备安装照片接口")
@RequestMapping("/TSetupPicAction")
@Controller
public class TSetupPicAction {

    @Autowired
    private TSetupPicService tSetupPicService;

    @ApiOperation("根据设备安装业务ID查询相关图片")
    @RequestMapping(value = "/listBySetupId/{setupId}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> findSetupPicsBySetupId(@PathVariable("setupId") String setupId){
        try{
            List<TSetupPicModel> resList = tSetupPicService.findSetupPicsBySetupId(setupId);
            Map<String,Object> resMap = new CorrectMap();
            resMap.put("res",resList);
            return resMap;
        }catch (Exception e){
            e.printStackTrace();
            Map<String,Object> resMap = new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("新增设备安装照片")
    @RequestMapping(value = "/saveTSetupPic",method =  RequestMethod.POST)
    @ResponseBody
    //public Map<String,Object> saveTSetupPic(@RequestParam("file") MultipartFile file,TSetupPicModel model){
    public Map<String,Object> saveTSetupPic(@RequestParam("file") MultipartFile file,@RequestParam("modelJson") String modelJson){
        try{
            TSetupPicModel model = JSON.parseObject(modelJson,TSetupPicModel.class);
            Map<String,Object> resMap = new CorrectMap();
            //PicType picType = PicType.getElement(model.getPicType());
            TSetupPicModel setupPicModel = tSetupPicService.saveTSetupPic(file,model);
            resMap.put("res",setupPicModel);
            return resMap;
        }catch (Exception e){
            e.printStackTrace();
            Map<String,Object> resMap = new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("更新设备安装照片")
    @RequestMapping(value = "/updateTSetupPic",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateTSetupPic(@RequestParam("file") MultipartFile file,@RequestParam("modelJson") String modelJson){
        try {
            TSetupPicModel model = JSON.parseObject(modelJson,TSetupPicModel.class);
            Map<String,Object> resMap = new CorrectMap();
            TSetupPicModel setupPicModel = tSetupPicService.updateTSetupPic(file,model);
            resMap.put("res",setupPicModel);
            return resMap;
        }catch (Exception e){
            e.printStackTrace();
            Map<String,Object> resMap = new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("删除设备安装照片")
    @RequestMapping(value = "/deleteTSetupPic",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> deleteTSetupPic(@RequestBody TSetupPicModel model){
        try {
            int res = tSetupPicService.deleteTSetupPic(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;
        }
    }

}
