package com.fdage.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.fdage.aop.WebControllerLog;
import com.fdage.constant.ConfigConstant;
import com.fdage.constant.ConstantUrl;
import com.fdage.enums.ResponEnum;
import com.fdage.pojo.TbCollection;
import com.fdage.request.RequestCollection;
import com.fdage.respon.ResponCollection;
import com.fdage.service.ICollectionService;
import com.fdage.util.AjaxJson;
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.apache.commons.lang3.StringUtils;
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.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created by Hb_zzZ on 2019/9/11.
 */
@Api(tags = "文物库模块")
@Controller
@RequestMapping("/zhoushan/collection")
@Slf4j
public class CollectionController {

    @Autowired
    private ICollectionService service;

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

    @Autowired
    ConfigConstant configConstant;

    @PostMapping("timeList")
    @ResponseBody
    @ApiOperation("获取年代列表")
    public AjaxJson timeList(){
        return AjaxJson.success(service.timeList());
    }

    @PostMapping("typeList")
    @ResponseBody
    @ApiOperation("获取文物类别列表")
    public AjaxJson typeList(@RequestBody RequestCollection bo){
        return AjaxJson.success(service.typeList(bo));
    }

    @PostMapping("addCollection")
    @ResponseBody
    @WebControllerLog(description = "文物库-新增文物")
    @ApiOperation("新增文物")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "name", value = "文物名称", dataType = "String"),
//            @ApiImplicitParam(name = "description", value = "文物描述", dataType = "String"),
//            @ApiImplicitParam(name = "timeId", value = "年代id", dataType = "String"),
//            @ApiImplicitParam(name = "typeId", value = "类型id", dataType = "String"),
//            @ApiImplicitParam(name = "modelUrl", value = "文物模型url", dataType = "String"),
//            @ApiImplicitParam(name = "discoveryTime", value = "文物发现时间", dataType = "String"),
//            @ApiImplicitParam(name = "repairTime", value = "文物维修时间", dataType = "String"),
//            @ApiImplicitParam(name = "venue", value = "文物所在场馆", dataType = "String"),
//            @ApiImplicitParam(name = "contentUrl", value = "多媒体内容Url", dataType = "String"),
//            @ApiImplicitParam(name = "pic", value = "4dmodel文物的封面图", dataType = "String"),
//            @ApiImplicitParam(name = "num", value = "藏品编号", dataType = "String"),
//            @ApiImplicitParam(name = "state", value = "状态,0：展示，1：隐藏", dataType = "String")})
    public AjaxJson addCollection(@RequestBody RequestCollection bo){
        if(bo == null || StringUtils.isEmpty(bo.getName())  ||
                bo.getTimeId() == null  || bo.getTypeId() == null){
            return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
        }

        TbCollection collection = new TbCollection();
        BeanUtils.copyProperties(bo, collection);
//        collection.setTypeImages(convertTypeImages(bo.getTypeImages()));
        collection.setUnityUrl("/collection/unity/" + bo.getDirCode());
//        collection.setUnityUrl(configConstant.serverBasePath + "/collection/unity/" + bo.getDirCode());
        service.insert(collection);
        return AjaxJson.success();
    }

    @PostMapping("updateCollection")
    @ResponseBody
    @WebControllerLog(description = "文物库-修改文物")
    @ApiOperation("修改文物")
    public AjaxJson updateCollection(@RequestBody RequestCollection bo){
        if(bo == null || StringUtils.isEmpty(bo.getName()) ||
                bo.getTimeId() == null  || bo.getTypeId() == null ){
            return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
        }

        TbCollection collection = new TbCollection();
        BeanUtils.copyProperties(bo, collection);
        service.update(collection);
        return AjaxJson.success();
    }



    @PostMapping("deleteCollection")
    @ResponseBody
    @WebControllerLog(description = "文物库-删除文物")
    @ApiOperation("删除文物")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文物id", dataType = "String")})
    public AjaxJson deleteCollection(@RequestBody RequestCollection bo){
        if(bo == null || bo.getId() == null){
            return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
        }
        service.deleteById(bo.getId());
        return AjaxJson.success();
    }

    @PostMapping("findById")
    @ResponseBody
    @WebControllerLog(description = "文物库-查询文物库详情")
    @ApiOperation("查询文物库详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文物id", dataType = "String")})
    public AjaxJson findById(@RequestBody RequestCollection bo){
        if(bo == null || bo.getId() == null){
            return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
        }

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

    @PostMapping("list")
    @ResponseBody
    @WebControllerLog(description = "文物库-获取文物列表")
    @ApiOperation("获取文物列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "timeId", value = "年代id", dataType = "String"),
            @ApiImplicitParam(name = "typeId", value = "类型id", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "文物名称", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "String"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "String")})
    public AjaxJson list(@RequestBody RequestCollection bo){
        List<ResponCollection> list = service.findList(bo);
        PageInfo<ResponCollection> pageInfo = new PageInfo<>(list);
        return AjaxJson.success(pageInfo);
    }

    @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(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) ("/collection/" + fileName));
    }


    /**
     * 2021-08-09
     * @param file
     * @param code
     * @return
     */
    @PostMapping("/uploadCode")
    @ApiOperation("上传Unity")
    @ResponseBody
    public AjaxJson uploadCode(MultipartFile file, String code){

        return service.uploadCode(file, code);
    }


    @PostMapping("/importCollection")
    @ResponseBody
    @ApiOperation("导入文物Excel")
    public AjaxJson importCollection(@RequestParam("file") MultipartFile file){
        if(file == null){
            return AjaxJson.failure("参数不能为空");
        }
        String fileName = System.currentTimeMillis() + "_" +  file.getOriginalFilename();
        log.info("导入文件保存地址：" + fileName);
        boolean flag = FileUtil.upload(file, uploadPath, fileName);

        if(!flag){
            return AjaxJson.failure("导入文物数据失败");
        }

        return service.importCollection(uploadPath + "/" + fileName);
    }



    @GetMapping("addVisit/{id}")
    @ResponseBody
    @ApiOperation("保存访问量")
    public AjaxJson addVisit(@PathVariable Long id){
        return service.addVisit(id);
    }
}
