package com.xkrs.controller;

import com.xkrs.common.encapsulation.PromptMessageEnum;
import com.xkrs.model.entity.FileDocumentEntity;
import com.xkrs.model.qo.FileDocumentQo;
import com.xkrs.model.validation.FileDocumentQoInsert;
import com.xkrs.model.vo.FileServerResultVo;
import com.xkrs.service.FileDocumentService;
import com.xkrs.service.FileServerService;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Locale;
import java.util.Optional;

import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationErrorList;
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
import static com.xkrs.common.tool.TokenUtil.getTokenUserName;
import static com.xkrs.utils.NumberUtil.isStrNumeric;

/**
 * 文档管理服务
 * @author tajochen
 */
@RestController
@RequestMapping("/api/document")
public class FileDocumentController {

    @Resource
    private FileDocumentService fileDocumentService;

    @Resource
    private FileServerService fileServerService;

    @RequestMapping(value="/get/all",method = RequestMethod.GET)
    public String getAllFiles() {
        // 获取区域信息
        Locale locale = LocaleContextHolder.getLocale();
        Iterable<FileDocumentEntity> list = fileDocumentService.getAllFile();
        return outputEncapsulationObject(PromptMessageEnum.SUCCESS,list,locale);
    }

    @RequestMapping(value="/add",method = RequestMethod.POST)
    @PreAuthorize("hasAnyAuthority('auth_system_manager','auth_general_user','auth_administor')")
    @CrossOrigin
    public String fileUpload(@Validated({FileDocumentQoInsert.class}) @RequestBody FileDocumentQo fileDocumentQo,
                             BindingResult bindingResult, @RequestHeader(value="Authorization") String token) {
        // 获取区域信息
        Locale locale = LocaleContextHolder.getLocale();
        // 验证数据合法性
        if(bindingResult.hasErrors()){
            return outputEncapsulationErrorList(bindingResult.getFieldErrors(),locale);
        }
        // 获取当前用户名
        String userName = getTokenUserName(token);
        fileDocumentService.add(fileDocumentQo,userName);
        return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"",locale);
    }

    @RequestMapping(value="/delete",method = RequestMethod.DELETE)
    @PreAuthorize("hasAnyAuthority('auth_system_manager','auth_administor')")
    @CrossOrigin
    public String fileDelete(@RequestParam("id") String id) {
        //获取区域信息
        Locale locale = LocaleContextHolder.getLocale();
        if(!isStrNumeric(id)){
            return outputEncapsulationObject(PromptMessageEnum.PARAM_ILLEGAL,"参数错误",locale);
        }
        Optional <FileDocumentEntity> sf = fileDocumentService.getById(Integer.parseInt(id));
        if(!sf.isPresent()){
            return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG,"指定id文件不存在",locale);
        }
        try {
            FileServerResultVo fileServerResultVo = fileServerService.deleteFile(sf.get().getMd5(),"");
            fileDocumentService.delete(Integer.parseInt(id));
            return outputEncapsulationObject(PromptMessageEnum.SUCCESS,fileServerResultVo,locale);
        } catch (Exception e) {
            e.printStackTrace();
            return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL,"文件服务器出错",locale);
        }
    }
}
