package com.zhongzheng.controller.recruit;

import com.zhongzheng.common.annotation.Log;
import com.zhongzheng.common.core.controller.BaseController;
import com.zhongzheng.common.core.domain.AjaxResult;
import com.zhongzheng.common.core.page.TableDataInfo;
import com.zhongzheng.common.enums.BusinessType;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.modules.recruit.bo.RecruitCompanyAddBo;
import com.zhongzheng.modules.recruit.bo.RecruitCompanyEditBo;
import com.zhongzheng.modules.recruit.bo.RecruitCompanyQueryBo;
import com.zhongzheng.modules.recruit.bo.RecruitCompanyTradeQueryBo;
import com.zhongzheng.modules.recruit.service.IRecruitCompanyService;
import com.zhongzheng.modules.recruit.service.IRecruitCompanyTradeService;
import com.zhongzheng.modules.recruit.vo.RecruitCompanyTradeVo;
import com.zhongzheng.modules.recruit.vo.RecruitCompanyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 公司管理列Controller
 * 
 * @author change
 * @date 2021-08-12
 */
@Api(value = "公司管理列控制器", tags = {"公司管理列管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/companyCon")
public class RecruitCompanyController extends BaseController {

    private final IRecruitCompanyService iRecruitCompanyService;


    private final IRecruitCompanyTradeService iRecruitCompanyTradeService;


    /**
     * 查询公司管理列列表
     */
    @ApiOperation("查询公司管理列列表")
    @PreAuthorize("@ss.hasPermi('modules.company:company:list')")
    @GetMapping("/list")
    public TableDataInfo<RecruitCompanyVo> list(RecruitCompanyQueryBo bo) {
        startPage();
        List<RecruitCompanyVo> list = iRecruitCompanyService.queryList(bo);
        return getDataTable(list);
    }



    /**
     * 查询行业类别列表
     */
    @ApiOperation("查询行业类别列表")
    @PreAuthorize("@ss.hasPermi('system:trade:list')")
    @GetMapping("/listTrade")
    public TableDataInfo<RecruitCompanyTradeVo> list(RecruitCompanyTradeQueryBo bo) {
        startPage();
        List<RecruitCompanyTradeVo> list = iRecruitCompanyTradeService.queryCompanyTradeVo(bo);
        return getDataTable(list);
    }


    /**
     * 获取公司管理列详细信息
     */
    @ApiOperation("获取公司管理列详细信息")
    @PreAuthorize("@ss.hasPermi('modules.company:company:query')")
    @GetMapping("/{companyId}")
    public AjaxResult<RecruitCompanyVo> getInfo(@PathVariable("companyId" ) Long companyId) {
        return AjaxResult.success(iRecruitCompanyService.queryById(companyId));
    }

    /**
     * 新增公司管理列
     */
    @ApiOperation("新增公司管理列")
    @PreAuthorize("@ss.hasPermi('modules.company:company:add')")
    @Log(title = "公司管理列", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult<Void> add(@RequestBody RecruitCompanyAddBo bo) {
        return toAjax(iRecruitCompanyService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改公司管理列
     */
    @ApiOperation("修改公司管理列")
    @PreAuthorize("@ss.hasPermi('modules.company:company:edit')")
    @Log(title = "公司管理列", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult<Void> edit(@RequestBody RecruitCompanyEditBo bo) {
        return toAjax(iRecruitCompanyService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 导入公司
     */
    @ApiOperation("导入公司")
    @PreAuthorize("@ss.hasPermi('modules.company:company:add')")
    @Log(title = "公司管理列", businessType = BusinessType.INSERT)
    @PostMapping("/importExcel")
    public AjaxResult<Void> exc(MultipartFile file, HttpServletRequest request) throws IOException {

        // 判断文件是否为空
        if (file == null) {
         throw new RuntimeException("文件为空");
        }

        // 2.判断上传内容是否符合要求
        String fileName = file.getOriginalFilename();

        // 判断是否是excel文件
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            throw new RuntimeException("请上传正确格式的文件");
        }

        Boolean excel = iRecruitCompanyService.batchImport(file);

        return toAjax(excel ? 1: 0);
    }


}
