package com.qmth.themis.admin.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.entity.TBAttachment;
import com.qmth.themis.business.entity.TBClientVersion;
import com.qmth.themis.business.entity.TBUser;
import com.qmth.themis.business.enums.UploadFileEnum;
import com.qmth.themis.business.service.TBAttachmentService;
import com.qmth.themis.business.service.TBClientVersionService;
import com.qmth.themis.business.util.OssUtil;
import com.qmth.themis.business.util.ServletUtil;
import com.qmth.themis.common.enums.ExceptionResultEnum;
import com.qmth.themis.common.exception.BusinessException;
import com.qmth.themis.common.util.Result;
import com.qmth.themis.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;

/**
 * @Description: 客户端 前端控制器
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/11/20
 */
@Api(tags = "客户端Controller")
@RestController
@RequestMapping(SystemConstant.PREFIX_URL_ADMIN + "/client")
@Validated
public class TBClientVersionController {
    private final static Logger log = LoggerFactory.getLogger(TBClientVersionController.class);

    @Resource
    TBClientVersionService tbClientVersionService;

    @Resource
    OssUtil ossUtil;

    @Resource
    TBAttachmentService tbAttachmentService;

    @ApiOperation(value = "客户端上传附件接口")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
    @Transactional
    public Result upload(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file,
                         @ApiParam(value = "版本号", required = true) @RequestParam String version) {
        if (Objects.isNull(file) || Objects.equals(file, "")) {
            throw new BusinessException(ExceptionResultEnum.ATTACHMENT_IS_NULL);
        }
        if (Objects.isNull(version) || Objects.equals(version, "")) {
            throw new BusinessException(ExceptionResultEnum.VERSION_IS_NULL);
        }
        Map<String, Object> mapParameter = ossUtil.getAliYunOssPublicDomain().getMap();
        TBAttachment tbAttachment = null;
        try {
            TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
            mapParameter.put(SystemConstant.VERSION, version);
            tbAttachment = tbAttachmentService
                    .saveAttachment(file, ServletUtil.getRequestMd5(), ServletUtil.getRequestPath(), mapParameter,
                            UploadFileEnum.client, tbUser.getOrgId(), tbUser.getId());
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
            if (Objects.nonNull(tbAttachment)) {
                tbAttachmentService.deleteAttachment(mapParameter, UploadFileEnum.client, tbAttachment);
            }
            if (e instanceof BusinessException) {
                throw new BusinessException(e.getMessage());
            } else {
                throw new RuntimeException(e);
            }
        }
        return ResultUtil.ok(true);
    }

    @ApiOperation(value = "客户端版本新增/编辑接口")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
    public Result save(@ApiParam(value = "客户端版本信息", required = true) @RequestBody TBClientVersion tbClientVersion) {
        tbClientVersionService.saveOrUpdate(tbClientVersion);
        return ResultUtil.ok(true);
    }

    @ApiOperation(value = "客户端版本查询接口")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "客户端信息", response = TBClientVersion.class)})
    public Result query(@ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) int pageNumber,
                        @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) int pageSize) {
        return ResultUtil.ok(tbClientVersionService.clientVersionQuery(new Page<>(pageNumber, pageSize)));
    }
}