package com.ningdatech.carapi.company.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.carapi.common.util.ExcelDownUtil;
import com.ningdatech.carapi.company.entity.CompanyCertificate;
import com.ningdatech.carapi.company.entity.CompanyCommonCredit;
import com.ningdatech.carapi.company.entity.dto.*;
import com.ningdatech.carapi.company.entity.vo.CompanyEmployeesVO;
import com.ningdatech.carapi.company.service.ICompanyArchivesService;
import com.ningdatech.carapi.company.service.ICompanyCertificateService;
import com.ningdatech.carapi.company.service.ICompanyCommonCreditService;
import com.ningdatech.carapi.company.service.ICompanyVehicleNotGetOnService;
import com.ningdatech.carapi.user.util.LoginUserUtil;
import com.ningdatech.log.annotation.WebLog;

import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

/**
 * @description: 企业档案管理
 * @author: liushuai
 * @date: 2022/10/25 17:58
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/company-archives")
public class CompanyArchivesController {
    private final ICompanyArchivesService iCompanyArchivesService;
    private final ICompanyCertificateService iCompanyCertificateService;
    private final ICompanyCommonCreditService iCompanyCommonCreditService;

    private final ICompanyVehicleNotGetOnService companyVehicleNotGetOnService;

    @GetMapping
    public PageVo<ResCompanyArchivesDTO> getCompanyList(@Valid CompanyListParamDTO param) {
        return iCompanyArchivesService.getCompanyList(param);
    }

    @GetMapping("/detail")
    public ResCompanyArchivesDetailsDTO getCompanyDetail(@RequestParam(value = "companyId", required = false) Long companyId) {
        return iCompanyArchivesService.getCompanyDetail(companyId);
    }

    @GetMapping("/employees")
    public List<CompanyEmployeesVO> getCompanyEmployees(@RequestParam(value = "companyId", required = false) Long companyId) {
        return iCompanyArchivesService.getCompanyEmployees(companyId);
    }

    @PutMapping
    public void updateCompanyInfo(@RequestBody ReqCompanyArchivesDetailsDTO reqCompanyArchivesDetailsDTO) {
        iCompanyArchivesService.updateCompanyInfo(reqCompanyArchivesDetailsDTO);
    }

    @GetMapping("/employees/count")
    public PageVo<ResCompanyEmployeesCountDTO> getCompanyEmployeesList(CompanyListParamDTO param) {
        return iCompanyArchivesService.getCompanyEmployeesList(param);
    }

    @PostMapping("/certificate")
    public void saveCertificate(@RequestBody CompanyCertificate companyCertificate) {
        iCompanyCertificateService.save(companyCertificate);
    }

    @GetMapping("/certificate")
    public Page<CompanyCertificate> getCompanyCertificateList(@RequestParam(value = "companyId", required = false) Long companyId,
                                                              @RequestParam(value = "pageNumber") int pageNum,
                                                              @RequestParam(value = "pageSize") int pageSize) {
        return iCompanyCertificateService.getCertificates(new Page<>(pageNum, pageSize),companyId);
    }

    @GetMapping("/basic/credit")
    public PageVo<ResCompanyBasicCreditDTO> getCompanyBasicCreditList(CompanyListParamDTO param) {
        return iCompanyArchivesService.getCompanyBasicCreditList(param);
    }

    @GetMapping("/basic/credit/detail")
    public ResCompanyBasicCreditDetailDTO getCompanyBasicCreditDetail(@RequestParam(value = "companyId") Long companyId) {
        return iCompanyArchivesService.getCompanyBasicCreditDetail(companyId);
    }

    @GetMapping("/common/credit")
    public PageVo<ResCompanyCommonCreditDTO> getCompanyCommonCreditList(CompanyListParamDTO param) {
        return iCompanyArchivesService.getCompanyCommonCreditList(param);
    }

    @GetMapping("/common/credit/detail")
    public Page<CompanyCommonCredit> getCompanyCommonCreditDetailList(@RequestParam(value = "companyId", required = false) Long companyId,
                                                                      @RequestParam(value = "pageNumber") int pageNum,
                                                                      @RequestParam(value = "pageSize") int pageSize) {
        return iCompanyCommonCreditService.getCompanyBasicCreditDetail(new Page<>(pageNum, pageSize),companyId);
    }

    @ApiOperation("企业列表导出")
    @GetMapping("/companyListExport")
    public void companyListExport(CompanyListParamDTO req, HttpServletResponse response) {
        ExcelDownUtil.downXls(response, req, iCompanyArchivesService::companyListExport);
    }

    @ApiOperation("企业证书信息列表导出")
    @GetMapping("/companyCertificateInformationListExport")
    public void companyCertificateInformationListExport(CompanyListParamDTO req, HttpServletResponse response) {
        ExcelDownUtil.downXls(response, req, iCompanyArchivesService::companyCertificateInformationListExport);
    }

    @ApiOperation("企业基本信用列表导出")
    @GetMapping("/companyBasicCreditListExport")
    public void companyBasicCreditListExport(CompanyListParamDTO req, HttpServletResponse response) {
        ExcelDownUtil.downXls(response, req, iCompanyArchivesService::companyBasicCreditListExport);
    }

    @ApiOperation("企业公共信用列表导出")
    @GetMapping("/companyCommonCreditListExport")
    public void companyCommonCreditListExport(CompanyListParamDTO req, HttpServletResponse response) {
        ExcelDownUtil.downXls(response, req, iCompanyArchivesService::companyCommonCreditListExport);
    }

    /**
     * 查询车辆未上车行驶记录
     *
     * @param param /
     * @return /
     */
    @ApiOperation(value = "查询车辆未上车行驶记录-驾驶员ID", notes = "查询车辆未上车行驶记录")
    @PostMapping("/vehicle/getNotGetOnList")
    public PageVo<ResNotGetOnCarRecordDTO> getNotOnCarDriver(@Valid @RequestBody ReqNotGetOnCarDTO param) {
        return iCompanyArchivesService.getNotOnCarList(param);
    }

    @ApiOperation(value = "企业待审核列表查询", notes = "企业待审核列表查询")
    @GetMapping("/toBeAuditList")
    public PageVo<ResCompanyArchivesDTO> queryToBeAudit(@Valid @ModelAttribute CompanyAuditPageQuery param) {
        return iCompanyArchivesService.queryToBeAudit(param);
    }

    @ApiOperation(value = "企业已审核列表查询", notes = "企业已审核列表查询")
    @GetMapping("/auditedList")
    public PageVo<ResCompanyArchivesDTO> queryAuditedList(@Valid @ModelAttribute CompanyAuditPageQuery param) {
        return iCompanyArchivesService.queryAuditedList(param);
    }

    @ApiOperation(value = "企业审核", notes = "企业审核")
    @PostMapping("/audit")
    @WebLog
    public Boolean handlerAudit(@Valid @RequestBody CompanyAuditDTO data) {
        return iCompanyArchivesService.audit(data, LoginUserUtil.getUserId());
    }
}
