package com.unis.base.server.controller;

import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.QyxypjDcVO;
import com.unis.base.server.entity.QyxypjDcDO;
import com.unis.base.server.service.QyxypjService;
import com.unis.common.utils.ResponseWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Classname QyxypjController
 * @Description 企业信用评价
 * @Date 2023/11/2 09:30
 * @Created by rising123sun
 */
@RestController
@Api(value = "企业信用评价", tags = "企业信用评价")
@Slf4j
public class QyxypjController {

    @Resource
    private QyxypjService qyxypjService;

    // 企业信用评价模版导入
    @PostMapping("/qyxypj/importQyxypjDr")
    @ApiOperation(value = "企业信用评价模版导入")
    public ResponseWrapper importQyxypjDr(@ApiParam(name = "file", value = "文件", required = true) @RequestPart(value = "file") MultipartFile file) {
        try {
            return qyxypjService.importQyxypjDr(file);
        } catch (Exception e) {
            log.error("企业信用评价模版导入异常", e);
            return ResponseWrapper.error("企业信用评价模版导入异常");
        }
    }

    // 分页列表查询
    @PostMapping("/qyxypj/queryPageList")
    @ApiOperation(value = "分页列表查询")
    public ResponseWrapper queryPageList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestBody QyxypjDcVO qyxypjDcVO) {
        try {
            PageInfo<QyxypjDcDO> pageInfos = qyxypjService.queryPageList(pageNum, pageSize, qyxypjDcVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), pageInfos);
        } catch (Exception e) {
            log.error("分页列表查询异常", e);
            return ResponseWrapper.error("分页列表查询异常");
        }
    }

    // 获取当前存在最新评价年度
    @PostMapping("/qyxypj/getNewPjnd")
    @ApiOperation(value = "获取当前存在最新评价年度")
    public ResponseWrapper getNewPjnd() {
        try {
            String newPjnd = qyxypjService.getNewPjnd();
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), newPjnd);
        } catch (Exception e) {
            log.error("获取当前存在最新评价年度异常", e);
            return ResponseWrapper.error("获取当前存在最新评价年度异常");
        }
    }

    // 查询所有数据
    @PostMapping("/qyxypj/queryAllList")
    @ApiOperation(value = "查询所有数据")
    public ResponseWrapper queryAllList(@RequestBody QyxypjDcVO qyxypjDcVO) {
        try {
            List<QyxypjDcDO> list = qyxypjService.queryAllList(qyxypjDcVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), list);
        } catch (Exception e) {
            log.error("查询所有数据异常", e);
            return ResponseWrapper.error("查询所有数据异常");
        }
    }

    // 修改
    @PostMapping("/qyxypj/update")
    @ApiOperation(value = "修改（包含编辑以及提交）")
    public ResponseWrapper update(@RequestBody QyxypjDcDO qyxypjDcDO) {
        try {
            qyxypjService.update(qyxypjDcDO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "操作成功");
        } catch (Exception e) {
            log.error("操作异常", e);
            return ResponseWrapper.error("操作异常");
        }
    }

    // 批量修改
    @PostMapping("/qyxypj/updateBatch")
    @ApiOperation(value = "批量修改")
    public ResponseWrapper updateBatch(@RequestParam(value = "ids", required = false) String ids,
                                       @RequestBody QyxypjDcDO qyxypjDcDO) {
        try {
            qyxypjService.updateBatch(ids, qyxypjDcDO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "操作成功");
        } catch (Exception e) {
            log.error("操作异常", e);
            return ResponseWrapper.error("操作异常");
        }
    }

    // 批量提交
    @PostMapping("/qyxypj/updateBatchTj")
    @ApiOperation(value = "批量提交")
    public ResponseWrapper updateBatchTj(@RequestParam(value = "ids", required = false) String ids) {
        try {
            qyxypjService.updateBatchTj(ids);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "操作成功");
        } catch (Exception e) {
            log.error("操作异常", e);
            return ResponseWrapper.error("操作异常");
        }
    }

    // 删除
    @PostMapping("/qyxypj/delete")
    @ApiOperation(value = "删除")
    public ResponseWrapper delete(@RequestParam(value = "id", required = true) String id) {
        try {
            qyxypjService.delete(id);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "操作成功");
        } catch (Exception e) {
            log.error("操作异常", e);
            return ResponseWrapper.error("操作异常");
        }
    }

    // 报表导出
    @PostMapping("/qyxypj/exportQyxypj")
    @ApiOperation(value = "报表导出")
    public ResponseWrapper exportQyxypj(HttpServletResponse response,
                                        @RequestBody QyxypjDcVO qyxypjDcVO) {
        try {
            return qyxypjService.exportQyxypj(response, qyxypjDcVO);
        } catch (Exception e) {
            log.error("报表导出异常", e);
            return ResponseWrapper.error("报表导出异常");
        }
    }

}
