package com.cool.bang.modules.system.controller;

import com.cool.bang.common.base.BaseController;
import com.cool.bang.common.constant.ControllerConstant;
import com.cool.bang.common.web.request.PageDomain;
import com.cool.bang.common.web.response.Result;
import com.cool.bang.common.web.response.module.ResultTable;
import com.cool.bang.modules.system.domain.po.Resource;
import com.cool.bang.modules.system.service.IResourceService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件
 *
 * @author PeiZhi Guo
 * @version 1.0.0
 * 2022-09-08 15:12
 */
@RestController
@RequestMapping(ControllerConstant.UPLOAD + "resource/v1")
public class ResourceController extends BaseController {

    @Value("${cool.file-path}")
    private String filePath;

    private IResourceService resourceService;

    @Autowired
    public ResourceController(IResourceService resourceService) {
        this.resourceService = resourceService;
    }

    @GetMapping
    public ResultTable main(PageDomain pageDomain, Resource param) {
        PageInfo<Resource> resourcePage = resourceService.page(pageDomain, param);
        return pageTable(resourcePage.getList(), resourcePage.getTotal());
    }

    @GetMapping("{id}")
    public Result get(@PathVariable Long id) {
        return success(resourceService.queryById(id));
    }

    @GetMapping("tableId/{id}")
    public Result tableId(@PathVariable Long id) {
        Resource param = new Resource();
        param.setTableId(id);
        return success(resourceService.list(param));
    }

    @PostMapping("upload")
    public Result upload(Resource resource, MultipartFile file) {
        try {
        // 图片普通保存到磁盘, 返回参数为保存的路径
        String url = resourceService.upload(file, "");
        resource.setUrl(url);
        resource.setDelFlag(1);
        resource.setCreateTime(new Date());
        resourceService.save(resource);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure();
        }
        return success(resource);
    }

    @PostMapping
    public Result edit(@RequestBody Resource resource) {
        return decide(resourceService.update(resource));
    }

    @DeleteMapping("{id}")
    public Result remove(@PathVariable Integer id) {
        resourceService.delete(resourceService.queryById(id).getUrl());
        return decide(resourceService.removeById(id));
    }

    @GetMapping("/removeFile")
    public Result removeFile(String url) {
        Map<String, Object> map = new HashMap<>();
        try {
            resourceService.delete(filePath + url);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure("服务器上传异常：" + e.getMessage());
        }
        return success(map);
    }
}
