package com.zfire.mall.manager.controller.comlist;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.annotation.ZfireList;
import com.zfire.mall.manager.bean.listV2.ComListVO;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.comlist.ComListLogic;
import com.zfire.mall.manager.plus.entity.ComDetailLog;
import com.zfire.mall.manager.plus.entity.ComList;
import com.zfire.mall.manager.utils.CommonUtils;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@RestController
@Api(value = "通用万能列表API", tags ={"通用万能列表API"} )
@RequestMapping(value = "/com/list", produces = "application/json; charset=utf-8")
public class ComListController {

    @Autowired
    ComListLogic comListLogic;


    @ZfireList
    @PostMapping("/list/v2")
    @ApiOperation(value = "万能通用附件列表")
    public ResponseHelper<ComListVO> listV2(
            @RequestBody ZfireParamBean zfireParamBean
    ){
        IPage<ComListVO> list = comListLogic.listV2(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<ComListVO>() {
        });
    }


    @GetMapping("/list")
    @ApiOperation(value = "万能通用附件列表")
    public ResponseHelper<ComListVO> list(
            @ApiParam(required = false, value = "标题") @RequestParam(required = false) String title,
            @ApiParam(required = false, value = "备注") @RequestParam(required = false) String remark,
            @ApiParam(required = false, value = "是否未下载 true=是 false=否") @RequestParam(required = false) Boolean needDown,
            @ApiParam(required = true, value = "页号") @RequestParam(required = true) Integer pageNo,
            @ApiParam(required = true, value = "页大小") @RequestParam(required = true) Integer pageSize,
            HttpServletRequest request
    ){
        IPage<ComList> list = comListLogic.list(title,remark, needDown,pageNo, pageSize);
        return ResponseHelper.success(list);
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除万能通用附件")
    public ResponseHelper list(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) Integer id,
            HttpServletRequest request
    ){
        comListLogic.delete(id,request);
        return ResponseHelper.success();
    }

    @PostMapping("/yonge")
    @ApiOperation(value = "上传万能通用列表附件")
    public ResponseHelper yonge(
            @ApiParam(required = true, value = "标题") @RequestParam(required = true) String title,
            @ApiParam(required = false, value = "备注") @RequestParam(required = false) String remark,
            @ApiParam(required = true, value = "file") @RequestParam(required = true) String file,
            HttpServletRequest request
    ) throws Exception {
        comListLogic.yonge(title,remark,file,request);
        return ResponseHelper.success();
    }

    @GetMapping("/download")
    @ApiOperation(value = "下载万能通用列表附件")
    public ResponseHelper<String> down(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) Integer id,
            HttpServletRequest request,HttpServletResponse response
    ) throws Exception {
        String download = comListLogic.download(id, request, response);
        return ResponseHelper.success(download);
    }

    @GetMapping("/download/log")
    @ApiOperation(value = "下载记录")
    public ResponseHelper<IPage<ComDetailLog>> downlog(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) Integer id,
            @ApiParam(required = true, value = "页号") @RequestParam(required = true) Integer pageNo,
            @ApiParam(required = true, value = "页大小") @RequestParam(required = true) Integer pageSize,
            HttpServletRequest request
    ) throws Exception {
        IPage<ComDetailLog> comDetailLogIPage = comListLogic.logList(id, pageNo, pageSize,request);
        return ResponseHelper.success(comDetailLogIPage);
    }

    @GetMapping("/download/model")
    @ApiOperation("下载文件下发模板")
    public void download(
            HttpServletResponse response
    ) throws IOException {
        CommonUtils.downloadFile("file_notice.xlsx",response);
    }


}
