package com.fdage.controller;

import com.fdage.aop.WebControllerLog;
import com.fdage.enums.ResponEnum;
import com.fdage.request.RequestCollection;
import com.fdage.request.RequestExhibition;
import com.fdage.request.RequestInformation;
import com.fdage.respon.ResponCollection;
import com.fdage.respon.ResponExhibition;
import com.fdage.respon.ResponInformation;
import com.fdage.service.ICollectionService;
import com.fdage.service.IExhibitionService;
import com.fdage.service.IInformationService;
import com.fdage.util.AjaxJson;
import com.fdage.util.DateUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
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.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Hb_zzZ on 2020/7/30.
 */
@Api(tags = "w-门户网站")
@Controller
@RequestMapping("/api")
@Slf4j
public class ApiController {

    @Autowired
    private IInformationService informationService;

    @Autowired
    private IExhibitionService exhibitionService;

    @Autowired
    private ICollectionService collectionService;

    @PostMapping("informationList")
    @ResponseBody
    @ApiOperation("获取资讯列表")
    public AjaxJson informationList(){
        List<ResponInformation> list = informationService.findList(null);
        for(ResponInformation information : list){
            if(StringUtils.isNotEmpty(information.getStartTime())){
                information.setStartTime(String.valueOf(DateUtil.convert2CST(information.getStartTime())));
            }
            if(StringUtils.isNotEmpty(information.getEndTime())){
                information.setEndTime(String.valueOf(DateUtil.convert2CST(information.getEndTime())));
            }
            if(StringUtils.isNotEmpty(information.getCreateTime())){
                information.setCreateTime(String.valueOf(DateUtil.convert2CST(information.getCreateTime())));
            }

            information.setUrlArray(information.getContentUrl().replace("\"", "").replace("[", "").replace("]", "").split(","));
        }
        return AjaxJson.success(list);
    }

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

    @PostMapping("exhibitionList")
    @ResponseBody
    @ApiOperation("获取展览方案列表")
    public AjaxJson exhibitionList(){
        List<ResponExhibition> list = exhibitionService.findList(null);
        for(ResponExhibition exhibition : list){
            exhibition.setCreateTime(String.valueOf(DateUtil.convert2CST(exhibition.getCreateTime())));
        }
        return AjaxJson.success(list);
    }

    @PostMapping("collectionList")
    @ResponseBody
    @ApiOperation("获取所有文物列表")
    public AjaxJson collectionList(){
        List<ResponCollection> list = collectionService.findList(null);
        return AjaxJson.success(list);
    }

    @PostMapping("collectionFindById")
    @ResponseBody
    @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(collectionService.findById(bo.getId()));
    }

    @PostMapping("likeCollection")
    @ResponseBody
    @ApiOperation("文物点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文物id", dataType = "String")})
    public AjaxJson likeCollection(@RequestBody RequestCollection bo){
        if(bo.getId() == null){
            return AjaxJson.failure("id不能为空");
        }
        collectionService.addLikeNumById(bo.getId());
        return AjaxJson.success();
    }

    @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 = exhibitionService.getExhibitionById(bo.getId());

        exhibitionService.addOpenNum(bo.getId());
        return AjaxJson.success(responExhibition);
    }

    @PostMapping("typeList")
    @ResponseBody
    @ApiOperation("获取文物类别列表")
    public AjaxJson typeList(){
        RequestCollection bo = new RequestCollection();
        bo.setState(0);
        return AjaxJson.success(collectionService.typeList(bo));
    }

    @PostMapping("searchCollection")
    @ResponseBody
    @ApiOperation("搜索文物")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "展览方案id", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "文物名称", dataType = "String")})
    public AjaxJson searchCollection(@RequestBody RequestCollection bo){
        return AjaxJson.success(collectionService.searchCollection(bo));
    }

    @PostMapping("addSearchNum")
    @ResponseBody
    @ApiOperation("增加文物搜索次数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文物id", dataType = "String")})
    public AjaxJson addSearchNum(@RequestBody RequestCollection bo){
        if(bo.getId() == null){
            return AjaxJson.failure("id不能为空");
        }
        collectionService.addSearchNum(bo.getId());
        return AjaxJson.success();
    }

    @PostMapping("addDownloadNum")
    @ResponseBody
    @ApiOperation("增加文物下载次数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文物id", dataType = "String")})
    public AjaxJson addDownloadNum(@RequestBody RequestCollection bo){
        if(bo.getId() == null){
            return AjaxJson.failure("id不能为空");
        }
        collectionService.addDownloadNum(bo.getId());
        return AjaxJson.success();
    }
}
