package common.controller;


import common.model.DmpIncome;
import common.model.PdAttachment;
import common.model.User;
import common.service.DmpIncomeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import security.annotation.CurrentAccount;
import util.ResultModel;

@Controller
@RequestMapping("{platform}")
public class AttachmnetUploadController {

    @Autowired
    DmpIncomeService dmpIncomeService;

    //上传附件
    @PostMapping("/upload/attachment")
    @ResponseBody
    public ResultModel uploadAttach(@RequestParam("file") MultipartFile[] files, String contractCode, @CurrentAccount User user) {

        String erroNames = "";
        for (MultipartFile file : files) {
            PdAttachment attachment = dmpIncomeService.uploadPdAttach(file, contractCode, user);
            if (attachment.getFaild()) {
                erroNames += " " + attachment.getFileOrginName();
            }
        }

        if (!StringUtils.isEmpty(erroNames)) {
            return ResultModel.ERROR(5001, "文件：" + erroNames + "上传失败");
        }

        return ResultModel.OK();
    }

    //下载附加
    @RequestMapping(value = "/download/attachment/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public ResponseEntity<byte[]> getDownloadResponseEntity(@PathVariable Long id) {
        return dmpIncomeService.downloadAttach(id);
    }

    //删除附件
    @PostMapping("/delete/attachment/{id}")
    @ResponseBody
    public ResultModel deleteAttach(@PathVariable Long id) {
        return ResultModel.OK(dmpIncomeService.deleteAttach(id));
    }


    //附件列表
    @GetMapping("/list/attachment")
    @ResponseBody
    public ResultModel attachList(String code,String startDate,String endDate, @PathVariable String platform) {
        return dmpIncomeService.attachList(code,startDate,endDate, platform);
    }
}
