package com.fdage.controller;

import com.alibaba.fastjson.JSONObject;
import com.fdage.aop.WebControllerLog;
import com.fdage.enums.ResponEnum;
import com.fdage.pojo.TbExhibition;
import com.fdage.pojo.TbExhibitionCollection;
import com.fdage.request.RequestCollection;
import com.fdage.request.RequestExhibition;
import com.fdage.respon.ResponExhibition;
import com.fdage.respon.ResponInformation;
import com.fdage.service.IExhibitionService;
import com.fdage.util.AjaxJson;
import com.fdage.util.DateUtil;
import com.fdage.util.FileUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

/**
 * Created by Hb_zzZ on 2019/9/12.
 */
@ApiIgnore
@Controller
@RequestMapping("/zhoushan/exhibition")
@Slf4j
@Api(tags = "展示管理模块")
public class ExhibitionController {

    @Value("${upload.exhibition}")
    private String uploadPath;

    @Autowired
    private IExhibitionService service;

    @PostMapping("insertExhibitionCollection")
    @ResponseBody
    @ApiOperation("新增展览方案-推送给文通")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "exhibitionId", value = "展览id", dataType = "String"),
            @ApiImplicitParam(name = "collectionId", value = "文物id", dataType = "String")})
    public AjaxJson insertExhibitionCollection(@RequestBody RequestExhibition bo){
        if(bo == null || bo.getExhibitionId() == null || StringUtils.isEmpty(bo.getCollectionId())){

//        if(bo == null || bo.getExhibitionId() == null){
            return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
        }
        String[] ids = bo.getCollectionId().split(",");

        TbExhibitionCollection exhibitionCollection = new TbExhibitionCollection();
        for(int i = 0, len = ids.length; i < len; i++){
            exhibitionCollection.setExhibitionId(bo.getExhibitionId());
            // 2021-08-16 by owen
//            if (ids.length > 1) {
//                exhibitionCollection.setCollectionId(Long.valueOf(ids[i]));
//            }
            exhibitionCollection.setCollectionId(Long.valueOf(ids[i]));
            service.insertExhibitionCollection(exhibitionCollection);
        }

        //发送sse消息
        SseController.send("data:" + "0\r\n");
        return AjaxJson.success();
    }

    @PostMapping("updateExhibitionCollection")
    @ResponseBody
    @ApiOperation("修改展览方案-推送给文通")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "exhibitionId", value = "展览id", dataType = "String"),
            @ApiImplicitParam(name = "collectionId", value = "文物id", dataType = "String")})
    public AjaxJson updateExhibitionCollection(@RequestBody RequestExhibition bo){
        if(bo == null || bo.getExhibitionId() == null || StringUtils.isEmpty(bo.getCollectionId())){
            return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
        }

        service.deleteExhibitionCollection(bo.getExhibitionId());


        String[] ids = bo.getCollectionId().split(",");
        TbExhibitionCollection exhibitionCollection = new TbExhibitionCollection();
        for(int i = 0, len = ids.length; i < len; i++){
            exhibitionCollection.setExhibitionId(bo.getExhibitionId());
            exhibitionCollection.setCollectionId(Long.valueOf(ids[i]));
            service.insertExhibitionCollection(exhibitionCollection);
        }

        //发送sse消息
        SseController.send("data:" + "0\r\n");
        return AjaxJson.success();
    }

    @PostMapping("addExhibition")
    @ResponseBody
    @WebControllerLog(description = "展示管理-新增展览方案")
    @ApiOperation("新增展览方案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "展览方案名称", dataType = "String"),
            @ApiImplicitParam(name = "exhibitionId", value = "展览id", dataType = "String"),
            @ApiImplicitParam(name = "equipmentId", value = "关联设备id", dataType = "String"),
            @ApiImplicitParam(name = "webUrl", value = "展示封面", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "状态,0：启用，1：禁用", dataType = "String")})
    public AjaxJson addExhibition(@RequestBody RequestExhibition bo){
        if(bo == null || StringUtils.isEmpty(bo.getName()) || bo.getTypeId() == null || bo.getState() == null){
            return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
        }

        TbExhibition exhibition = new TbExhibition();
        BeanUtils.copyProperties(bo, exhibition);
        service.insert(exhibition);
        return AjaxJson.success(exhibition);
    }

    @PostMapping("updateExhibition")
    @ResponseBody
    @WebControllerLog(description = "展示管理-修改展览方案")
    @ApiOperation("修改展览方案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isSend", value = "是否推送给问题，1推送", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "展览方案id", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "展览方案名称", dataType = "String"),
            @ApiImplicitParam(name = "exhibitionId", value = "展览id", dataType = "String"),
            @ApiImplicitParam(name = "equipmentId", value = "关联设备id", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "状态,0：启用，1：禁用", dataType = "String")})
    public AjaxJson updateExhibition(@RequestBody RequestExhibition bo){
        if(bo == null || StringUtils.isEmpty(bo.getName()) || bo.getTypeId() == null || bo.getState() == null){
            return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
        }

        TbExhibition exhibition = new TbExhibition();
        BeanUtils.copyProperties(bo, exhibition);
        exhibition.setCreateTime(new Date());
        service.update(exhibition);
        if(bo.getIsSend() != null && bo.getIsSend() == 1){
            //发送sse消息
            SseController.send("data:" + "0\r\n");
        }
        return AjaxJson.success(exhibition);
    }

    @PostMapping("deleteExhibition")
    @ResponseBody
    @WebControllerLog(description = "展示管理-删除展览方案")
    @ApiOperation("删除展览方案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "展览方案id", dataType = "String")})
    public AjaxJson deleteExhibition(@RequestBody RequestExhibition bo){
        if(bo == null || bo.getId() == null){
            return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
        }

        service.delete(bo.getId());
        return AjaxJson.success();
    }

    @PostMapping("copyExhibition")
    @ResponseBody
    @WebControllerLog(description = "展示管理-复制展览方案")
    @ApiOperation("复制展览方案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "展览方案id", dataType = "String")})
    public AjaxJson copyExhibition(@RequestBody RequestExhibition bo){
        if(bo == null || bo.getId() == null){
            return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
        }

        TbExhibition exhibition = service.findById(bo.getId());
        exhibition.setId(null);
        exhibition.setState(1);
        service.insert(exhibition);

        List<ResponExhibition> list = service.findExhibitionCollection(bo.getId());
        TbExhibitionCollection exhibitionCollection = new TbExhibitionCollection();
        for(ResponExhibition responExhibition : list){
            exhibitionCollection.setExhibitionId(exhibition.getId());
            exhibitionCollection.setCollectionId(responExhibition.getCollectionId());
            service.insertExhibitionCollection(exhibitionCollection);
        }
        return AjaxJson.success();
    }

    @PostMapping("findCollectionByExhibition")
    @ResponseBody
    @ApiOperation("通过展示方案找文物")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "exhibitionId", value = "展览方案id", dataType = "String")})
    public AjaxJson findCollectionByExhibition(@RequestBody RequestExhibition bo){
        if(bo == null || bo.getExhibitionId() == null){
            return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
        }

        return AjaxJson.success(service.findCollectionByExhibition(bo.getExhibitionId()));
    }

    @PostMapping("list")
    @ResponseBody
    @WebControllerLog(description = "展示管理-获取展览方案列表")
    @ApiOperation("获取展览方案列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "方案名称", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "String"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "String")})
    public AjaxJson list(@RequestBody RequestExhibition bo){
        List<ResponExhibition> list = service.findList(bo);
        for(ResponExhibition exhibition : list){
            exhibition.setCreateTime(String.valueOf(DateUtil.convert2CST(exhibition.getCreateTime())));
        }
        PageInfo<ResponExhibition> pageInfo = new PageInfo<>(list);
        return AjaxJson.success(pageInfo);
    }

    @PostMapping("getExhibitionByEquipmentId")
    @ResponseBody
    @ApiOperation("通过设备id获取展示方案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "equipmentId", value = "设备id", dataType = "String")})
    public AjaxJson getExhibitionByEquipmentId(@RequestBody RequestExhibition bo){
        if(bo == null || bo.getEquipmentId() == null){
            return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
        }

        ResponExhibition responExhibition = service.getExhibitionByEquipmentId(bo.getEquipmentId());
        return AjaxJson.success(responExhibition);
    }

    @PostMapping("getExhibitionById")
    @ResponseBody
    @ApiOperation("通过id获取展示方案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "String")})
    public AjaxJson getExhibitionById(@RequestBody RequestExhibition bo){
        if(bo == null || bo.getId() == null){
            return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
        }

        ResponExhibition responExhibition = service.getExhibitionById(bo.getId());
        return AjaxJson.success(responExhibition);
    }

    @PostMapping("/upload")
    @ResponseBody
    @ApiOperation("上传展示方案图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件流", dataType = "String")})
    public AjaxJson upload(@RequestParam(value = "filename", defaultValue = "") String name, @RequestParam("file") MultipartFile file){
        if(file == null){
            return AjaxJson.failure("参数不能为空");
        }
        String fileName = System.currentTimeMillis() + "_";
        if(org.apache.commons.lang3.StringUtils.isNotEmpty(name)){
            fileName = fileName + name;
        }else {
            fileName = fileName + file.getOriginalFilename();
        }
        log.info("图片地址：" + fileName);
        boolean flag = FileUtil.upload(file, uploadPath, fileName);

        if(!flag){
            return AjaxJson.failure("上传图片失败");
        }

//        Map<String, String> map = new HashMap<>();
//        map.put(path + "/" + fileName, "company/" + fileName);
//        uploadToAlibabaService.upload(map);

        return AjaxJson.success((Object) ("/exhibition/" + fileName));
    }

    @PostMapping("typeList")
    @ResponseBody
    @ApiOperation("获取展示类别列表")
    public AjaxJson typeList(){
        return AjaxJson.success(service.typeList());
    }
}
