package com.mvc.controller.app;

import com.mvc.model.CorrectMap;
import com.mvc.model.ErrorMap;
import com.mvc.model.TAicbqSetupModel;
import com.mvc.service.app.AppSetupService;
import com.mvc.service.app.SetupImageType;
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.Map;

@Api(tags = "APP施工安装类型")
@RequestMapping("/AppSetupAction")
@Controller
public class AppSetupAction {

    @Autowired
    private AppSetupService appSetupService;

    @ApiOperation("分页查询设备安装表")
    @RequestMapping(value = "/findalllist/{page}/{row}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> findAllDeviceSetup(@PathVariable("page") Integer page,@PathVariable("rows") Integer rows){
        try{
            Map<String,Object> res = appSetupService.findAllDeviceSetup(page,rows);
            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 = "/findlist/{setupman}/{starttime}/{endtime}/{page}/{rows}",method = RequestMethod.GET)
    public Map<String,Object> findDeviceSetupsBySetupman(@PathVariable("setupman") String setupMan,
                                                         @PathVariable("starttime") String starttime,
                                                         @PathVariable("endtime") String endtime,
                                                         @PathVariable("page") Integer page,
                                                         @PathVariable("rows") Integer rows)
    {
        try{
            Map<String,Object> res = appSetupService.findDeviceSetupsBySetupman(setupMan,starttime,endtime,page,rows);
            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 = "/saveDeviceSetup",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> saveDeviceSetup(@RequestBody TAicbqSetupModel model){
        try {
            TAicbqSetupModel resModel = appSetupService.saveAicbqSetup(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 = "/deleteDeviceSetup/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> deleteDeviceSetup(@PathVariable("id") int id){
        try{
            int res = appSetupService.deleteAicbqSetup(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("更新设备安装表")
    @RequestMapping(value = "/updateDeviceSetup",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateDeviceSetup(@RequestBody TAicbqSetupModel model){
        try {
            TAicbqSetupModel resModel = appSetupService.updateDeviceSetup(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 = "/uploadSetupPicture",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> uploadSetupPicture(@RequestParam("file") MultipartFile file, Integer id,String imgflag) {
        try{
            SetupImageType imageType = SetupImageType.getImageTypeByFlag(imgflag);
            Map<String, Object> resMap = new CorrectMap();
            if(imageType!=null) {
                String imgUrl = appSetupService.upload(file, id,imageType);
                if(imgUrl!=null) {
                    TAicbqSetupModel resModel = appSetupService.updateTAicbqSetup(id,imgflag,imgUrl);
                    resMap.put("res",resModel);
                }else{
                    resMap.put("error","图片Url地址为空");
                }
            }else{
                resMap.put("error", "安装图片类型错误");
            }
            return resMap;
        }catch (Exception e){
            e.printStackTrace();
            Map<String,Object> resMap = new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("清除指定类型的安装照片")
    @RequestMapping(value = "/clearSetupPicture/{id}/{imgflag}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> clearSetupPicture(Integer id,String imgFlag){
        try {
            SetupImageType imageType = SetupImageType.getImageTypeByFlag(imgFlag);
            Map<String,Object> resMap = new CorrectMap();
            if(imageType!=null){
                int res = appSetupService.clearSetupPicture(id,imgFlag);
                resMap.put("res",res);
            }else{
                resMap.put("error","清楚的图片类型错误");
            }
            return resMap;
        }catch (Exception e){
            e.printStackTrace();
            Map<String,Object> resMap = new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }

//    @ApiOperation("测试1")
//    @RequestMapping(value = "/testPictrue",method = RequestMethod.POST)
//    @ResponseBody
//    public Map<String,Object> testPictrue(@RequestBody AppUserModel model) {
//        try{
//            Map<String,Object> resMap = new CorrectMap();
//            resMap.put("res","1");
//            return resMap;
//        }catch (Exception e){
//            e.printStackTrace();
//            Map<String,Object> resMap = new ErrorMap();
//            resMap.put("error",e.getMessage());
//            return resMap;
//        }
//    }
//
//    @ApiOperation("测试2")
//    @RequestMapping(value = "/getPictrue/{id}",method = RequestMethod.GET)
//    @ResponseBody
//    public Map<String,Object> getPictrue(@PathVariable("id") String id) {
//        try{
//            Map<String,Object> resMap = new CorrectMap();
//            resMap.put("res",id);
//            return resMap;
//        }catch (Exception e){
//            e.printStackTrace();
//            Map<String,Object> resMap = new ErrorMap();
//            resMap.put("error",e.getMessage());
//            return resMap;
//        }
//    }

}
