package com.jeeplus.test.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.common.utils.FileUtils;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.config.properties.FileProperties;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.sys.utils.FileKit;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.test.oss.service.OSSClientService;
import com.jeeplus.test.oss.service.OssService;
import com.jeeplus.test.user.domain.SysUserInfo;
import com.jeeplus.test.user.service.dto.FileUrlDto;
import com.jeeplus.test.user.service.dto.UserInfoDetailDto;
import com.jeeplus.test.user.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;

@Slf4j
@Api(tags ="userInfo")
@RestController
@RequestMapping(value = "/sys/userInfo")
public class UserInfoController {

    private static final String DIRECTORY = "attachment-file/assess";

    private static final String ALIYUNURL = "http://oss.gangwaninfo.com";

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private OSSClientService ossClientService;

    @Resource
    private OssService ossService;

    /**
     * 用户信息列表
     */
    @ApiOperation(value = "用户信息列表")
    @GetMapping(value = "/list")
    public ResponseEntity<IPage<SysUserInfo>> list(Page<SysUserInfo> page, SysUserInfo sysUserInfo) throws Exception{
        QueryWrapper<SysUserInfo> sysUserInfoQueryWrapper = QueryWrapperGenerator.buildQueryCondition(sysUserInfo, SysUserInfo.class);
        IPage<SysUserInfo> list = userInfoService.list(page, sysUserInfoQueryWrapper);
        return ResponseEntity.ok (list);
    }

    @ApiOperation(value = "删除用户信息")
    @GetMapping(value = "/remove")
    public String remove(@RequestParam String id) {
        return userInfoService.remove(id);
    }

    @ApiOperation(value = "根据id获取用户信息")
    @GetMapping(value = "/getUserInfoById")
    public UserInfoDetailDto getUserInfoById(@RequestParam String id) {
        return userInfoService.getUserInfoById(id);
    }

    @ApiOperation(value = "保存、更新数据")
    @PostMapping(value = "/save")
    public ResponseEntity save(@RequestBody UserInfoDetailDto userInfoDetailDto) {
        if (StringUtils.isEmpty(userInfoDetailDto.getId())) {
            return ResponseEntity.ok(userInfoService.add(userInfoDetailDto));
        }else {
            return ResponseEntity.ok(userInfoService.modify(userInfoDetailDto));
        }
    }

    /**
     * 上传文件
     * @return
     * @throws IOException
     */
    @ApiOperation("上传文件")
    @RequestMapping("webupload/upload")
    public ResponseEntity<FileUrlDto> webupload(HttpServletRequest request, MultipartFile file) throws IOException {
        FileUrlDto dto = new FileUrlDto();
        // 文件保存路径
        String fileDir =DIRECTORY+"/"+"userInfoFile"+ossClientService.datePath()+"/"+ System.currentTimeMillis();
        // 判断文件是否为空
        if (!file.isEmpty()) {
            String name = file.getOriginalFilename ();
            ossClientService.uploadFile2OSS(file.getInputStream(), fileDir, name);
            String url = "/" + fileDir +name;
            String lsUrl = ossService.getFileTemporaryLookUrl(ALIYUNURL + url);
            dto.setUrl(url);
            dto.setLsUrl(lsUrl);
            return ResponseEntity.ok(dto);
        }
        return ResponseEntity.ok(null);
    }
}
