package com.jxy.windminer.controller;

import com.jxy.common.core.utils.StringUtils;
import com.jxy.common.core.web.Result.AjaxResult;
import com.jxy.windminer.dto.IdsDto;
import com.jxy.windminer.service.TicketService;
import com.jxy.windminer.vo.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;

/**
 * @Description 统一请求接口
 * @Date 2022/5/27 9:54
 * @Author 杜懿
 */
@RestController
@RequestMapping("/ticket")
public class TicketController {

    @Autowired
    private TicketService ticketService;


        @PostMapping("/uploadFile")
        @ApiOperation(value = "文件上传")
        //@RequiresLogin
        public AjaxResult uploadFile(@RequestParam("file") MultipartFile[] file,HttpServletRequest req){

            String userName = req.getHeader("user_name");
            try {
                return  ticketService.uploadFiles(file,userName);
            } catch (SQLException e) {
                e.printStackTrace();
                return AjaxResult.error("服务器异常，请联系管理员");
            }

        }

    @GetMapping("/downloadFile")
    @ApiOperation(value = "文件批量下载")
    //@RequiresLogin
    public void  download(IdsDto dto, HttpServletRequest request, HttpServletResponse response){

            ticketService.downloadByIds(dto.getIds(),request,response);

    }


    @PostMapping("/submitTicket")
    @ApiOperation(value = "提交工单")
    //@RequiresLogin
    public AjaxResult submitTicket(@RequestBody(required = false) TicketVo vo){
        if(StringUtils.isNull(vo)){vo = new TicketVo();}
        return  ticketService.submitTicket(vo);

    }

    @PostMapping("/resubmitTicket")
    @ApiOperation(value = "工单继续提交 已提交内容补充")
    //@RequiresLogin
    public AjaxResult resubmitTicket(@RequestBody ResubmitTicketVo vo,HttpServletRequest req){
        String userName = req.getHeader("user_name");
        return  ticketService.resubmitTicket(vo,userName);
    }

    @PostMapping("/retractTicket")
    @ApiOperation(value = "已提交工单撤回")
    //@RequiresLogin
    public AjaxResult retractTicket(@RequestBody IdVo vo,HttpServletRequest req){
        String userName = req.getHeader("user_name");
        return  ticketService.retractTicket(vo.getId(),userName);
    }

    @PostMapping("/readTicket")
    @ApiOperation(value = "工单状态设为已读")
    //@RequiresLogin
    public AjaxResult readTicket(@RequestBody IdVo vo,HttpServletRequest req){
        String userName = req.getHeader("user_name");
        return  ticketService.readTicket(vo.getId(),userName);
    }

    @PostMapping("/endTicket")
    @ApiOperation(value = "工单状态设为已解决")
    //@RequiresLogin
    public AjaxResult endTicket(@RequestBody IdVo vo,HttpServletRequest req){
        String userName = req.getHeader("user_name");
        return  ticketService.endTicket(vo.getId(),userName);
    }

    @PostMapping("/getPrivateTicket")
    @ApiOperation(value = "个人工单列表")
    //@RequiresLogin
    public AjaxResult getPrivateTicket(@RequestBody(required = false) StatusVo vo,HttpServletRequest req){
        if(StringUtils.isNull(vo)){
            vo = new StatusVo();
            vo.setStatus(0);
        }
        String userName = req.getHeader("user_name");
        return  ticketService.getPrivateTicketByStatus(vo.getStatus(),userName);
    }

    @PostMapping("/getTicketDetails")
    @ApiOperation(value = "工单详情")
    //@RequiresLogin
    public AjaxResult getTicketDetails(@RequestBody IdVo vo){
        return  ticketService.getTicketDetails(vo.getId());
    }

    @PostMapping("/getUnreadCount")
    @ApiOperation(value = "个人未读工单数")
    //@RequiresLogin
    public AjaxResult getUnreadCount(HttpServletRequest req){
        String userName = req.getHeader("user_name");
        return  ticketService.getUnreadCount(userName);
    }

    @PostMapping("/getTicketList")
    @ApiOperation(value = "后台工单列表")
    //@RequiresLogin
    public AjaxResult getTicket(@RequestBody StatusVo vo){
        return  ticketService.getTicketList(vo.getStatus());
    }

    @PostMapping("/responTicket")
    @ApiOperation(value = "回复工单")
    //@RequiresLogin
    public AjaxResult responTicket(@RequestBody ResponTicketVo vo,HttpServletRequest req){
        String userName = req.getHeader("user_name");
        return  ticketService.responTicket(vo,userName);
    }


}
