package com.zd.airbottle.controller;

import com.zd.airbottle.domain.QpQualificationCertificate;
import com.zd.airbottle.service.IQpQualificationCertificateService;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.poi.ExcelUtil;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 资质证照Controller
 *
 * @author zd
 */
@RestController
@Api(tags = "【资质证照】")
@RequestMapping("/qualification/certificate")
public class QpQualificationCertificateController extends BaseController<QpQualificationCertificate> {
    @Autowired
    private IQpQualificationCertificateService qpQualificationCertificateService;

    /**
     * 小程序端查询资质证照列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "小程序端查询资质证照列表",notes = "前端使用原则：1、参数supplierType供应商类型目前默认气瓶供应商，传 supplierType=1 即可，2、type参数 根据实际业务选择")
    public ResultData<List<QpQualificationCertificate>> list(QpQualificationCertificate qpQualificationCertificate) {
        List<QpQualificationCertificate> qpQualificationCertificates = qpQualificationCertificateService.selectQpQualificationCertificateList(qpQualificationCertificate);
        return ResultData.success(qpQualificationCertificates);
    }

    /**
     * 后台查询资质证照列表
     */
    @PreAuthorize(hasPermi = "airbottle:qualification:certificate:list")
    @GetMapping("/query")
    @ApiOperation(value = "后台查询资质证照列表")
    public TableDataInfo<QpQualificationCertificate> query(QpQualificationCertificate qpQualificationCertificate) {
        startPage("create_time", "descending");
        List<QpQualificationCertificate> list = qpQualificationCertificateService.selectQpQualificationCertificateList(qpQualificationCertificate);
        return getDataTable(list);
    }

    /**
     * 导出资质证照列表
     */
    @ApiOperation(value = "导出资质证照列表")
    @PreAuthorize(hasPermi = "airbottle:qualification:certificate:export")
    @Log(title = "资质证照", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QpQualificationCertificate qpQualificationCertificate) throws IOException {
        List<QpQualificationCertificate> list = qpQualificationCertificateService.selectQpQualificationCertificateList(qpQualificationCertificate);
        ExcelUtil<QpQualificationCertificate> util = new ExcelUtil<>(QpQualificationCertificate.class);
        util.exportExcel(response, list, "资质证照数据");
    }

    /**
     * 获取资质证照详细信息
     */
    @ApiOperation(value = "获取资质证照详细信息")
    @PreAuthorize(hasPermi = "airbottle:qualification:certificate:query")
    @GetMapping(value = "/{id}")
    public ResultData<QpQualificationCertificate> getInfo(@PathVariable("id") Long id) {
        return ResultData.success(qpQualificationCertificateService.selectQpQualificationCertificateById(id));
    }

    /**
     * 检查排序是否重复
     */
    @ApiOperation(value = "检查排序是否重复")
    @PreAuthorize(hasPermi = "airbottle:qualification:certificate:query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "supplierType", value = "供应商类型", required = true, dataTypeClass = Long.class),
            @ApiImplicitParam(name = "type", value = "资质类型", required = true, dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "sort", required = true, dataTypeClass = Long.class, value = "排序号")
    })
    @GetMapping(value = "/check")
    public ResultData<Boolean> getInfo(Long supplierType, Integer type, Long sort) {
        QpQualificationCertificate certificate = new QpQualificationCertificate().setSupplierType(supplierType)
                .setType(type).setSort(sort);
        List<QpQualificationCertificate> certificates = qpQualificationCertificateService.selectQpQualificationCertificateList(certificate);
        if (!certificates.isEmpty()) {
            throw new ServiceException("排序号已使用");
        }
        return ResultData.success();
    }

    /**
     * 新增资质证照
     */
    @ApiOperation(value = "新增资质证照")
    @PreAuthorize(hasPermi = "airbottle:qualification:certificate:add")
    @Log(title = "资质证照", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData<Boolean> add(@RequestBody QpQualificationCertificate qpQualificationCertificate) {
        return ResultData.result(qpQualificationCertificateService.insertQpQualificationCertificate(qpQualificationCertificate));
    }

    /**
     * 修改资质证照
     */
    @ApiOperation(value = "修改资质证照")
    @PreAuthorize(hasPermi = "airbottle:qualification:certificate:edit")
    @Log(title = "资质证照", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData<Boolean> edit(@RequestBody QpQualificationCertificate qpQualificationCertificate) {
        return ResultData.result(qpQualificationCertificateService.updateQpQualificationCertificate(qpQualificationCertificate));
    }

    /**
     * 删除资质证照
     */
    @ApiOperation(value = "删除资质证照")
    @PreAuthorize(hasPermi = "airbottle:qualification:certificate:remove")
    @Log(title = "资质证照", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData<Boolean> remove(@PathVariable Long[] ids) {
        return ResultData.result(qpQualificationCertificateService.deleteQpQualificationCertificateByIds(ids));
    }
}
