package com.zcxk.charge.web.controller;

import com.zcxk.charge.api.enums.ChargeErrorEnum;
import com.zcxk.charge.web.common.FileUploadUtil;
import com.zcxk.charge.core.entity.ComdisplayMapEntity;
import com.zcxk.charge.web.service.ComdisplayMapService;
import com.zcxk.core.common.pojo.AjaxMessage;
import com.zcxk.core.oauth2.pojo.LoginUser;
import com.zcxk.core.oauth2.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 org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 综合展示图片页面控制器
 * @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 addComDisImage(@ApiParam(value = "综合展示图片", required = true) @RequestParam("avatarfile") MultipartFile file) {
        String avatar = "";
        if (!file.isEmpty()) {
            try {
                avatar = FileUploadUtil.uploadWeb(baseDir, file);
            } catch (IOException e) {
                return AjaxMessage.fail(500, e.getMessage());
            }
        }
        return AjaxMessage.success( avatar);
    }

    @ApiOperation(value = "添加综合展示图片")
    @RequestMapping(value = "addComDisImages", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage 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 AjaxMessage.fail(500, e.getMessage());
                }
            }
        }
        return AjaxMessage.success( avatar);
    }

    @RequestMapping(value = "/findMap", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询地图路径")
    public AjaxMessage findMap() {
        LoginUser currentUser = UserUtil.getCurrentUser();
        ComdisplayMapEntity comdisplayMap = new ComdisplayMapEntity();
        comdisplayMap.setTenantId(currentUser.getTenantId());
        List<ComdisplayMapEntity> result = comdisplayMapService.findList(comdisplayMap);
        if(result.size()>0) {
            return AjaxMessage.success( result.get(0));
        }
        return AjaxMessage.success(ChargeErrorEnum.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 AjaxMessage.success( 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 AjaxMessage.success( 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 AjaxMessage.success(result);
    }
}
