package com.zcxk.controller;

import com.zcxk.common.FileUploadUtil;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.LoginUser;
import com.zcxk.model.ResultStatus;
import com.zcxk.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;
import java.util.List;

import org.springframework.web.bind.annotation.*;
import com.zcxk.entity.ComdisplayMapEntity;
import com.zcxk.service.ComdisplayMapService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 综合展示图片页面控制器
 * @author wyy
 * @date 2021-02-24 09:23
 */
@RestController
@RequestMapping("/comdismap")
@Api(tags = "综合展示--图片")
public class ComdisplayMapController{
    @Autowired
    private ComdisplayMapService comdisplayMapService;
    @Value("${SMS.sys_config_path}")
    private String baseDir;
    /**
     * 新增
     */
    @ApiOperation(value = "综合展示图片")
    @RequestMapping(value = "addComDisImage", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<String> addComDisImage(@ApiParam(value = "综合展示图片", required = true) @RequestParam("avatarfile") MultipartFile file) {
        String avatar = "";
        if (!file.isEmpty()) {
            try {
                avatar = FileUploadUtil.uploadWeb(baseDir, file);
            } catch (IOException e) {
                return new AjaxMessage<>(ResultStatus.ERROR, e.getMessage());
            }
        }
        return new AjaxMessage<>(ResultStatus.OK, avatar);
    }
    /**
     * 新增
     */
    @ApiOperation(value = "综合展示图片")
    @RequestMapping(value = "addComDisImages", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<String> addComDisImages(@ApiParam(value = "综合展示图片", required = true) @RequestParam("avatarfile")MultipartFile[] files) {
        String avatar = "";
        for (MultipartFile file : files) {
            if (!file.isEmpty()) {
                try {
                    avatar += (FileUploadUtil.uploadWeb2(baseDir, file)+",");
                } catch (IOException e) {
                    return new AjaxMessage<>(ResultStatus.ERROR, e.getMessage());
                }
            }
        }
        return new AjaxMessage<>(ResultStatus.OK, avatar);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/findMap", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询地图路径")
    public AjaxMessage<ComdisplayMapEntity> findMap() {
        LoginUser currentUser = UserUtil.getCurrentUser();
        ComdisplayMapEntity comdisplayMap = new ComdisplayMapEntity();
        comdisplayMap.setTenantId(currentUser.getTenantId());
        List<ComdisplayMapEntity> result = comdisplayMapService.findList(comdisplayMap);
        if(result.size()>0)
        return new AjaxMessage<>(ResultStatus.OK, result.get(0));
        else
            return new AjaxMessage<>(ResultStatus.OBJECT_ERROR);
    }
    /**
     * 新增
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "保存或修改")
    public AjaxMessage<Integer> save(@ApiParam(value = "地图信息", required = true) @RequestBody ComdisplayMapEntity comdisplayMap) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        comdisplayMap.setStatus(1);
        comdisplayMap.setTenantId(currentUser.getTenantId());
        int result = comdisplayMapService.saveOrUpdate(comdisplayMap) ? 1 : 0;
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 修改保存综合展示图片
     */
    @ApiOperation(value = "单个编辑")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public  AjaxMessage<Integer>  editComdisplayMap(@ApiParam(value = "地图信息", required = true) @RequestBody ComdisplayMapEntity comdisplayMap) {
        int result = comdisplayMapService.updateComdisplayMapById(comdisplayMap) ? 1 : 0;
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 删除
     */
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<Integer> del(@ApiParam(value = "IDS", required = true) @RequestBody Long[] ids) {
        int result = comdisplayMapService.delComdisplayMapByIds(ids) ? 1 : 0;
        return new AjaxMessage<>(ResultStatus.OK, result);
    }
}
