package com.zhongzheng.controller.conpany;

import java.io.IOException;
import java.util.List;

import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhongzheng.common.annotation.Log;
import com.zhongzheng.common.core.controller.BaseController;
import com.zhongzheng.common.core.domain.AjaxResult;
import com.zhongzheng.common.enums.BusinessType;
import com.zhongzheng.modules.company.vo.CompanyVo;
import com.zhongzheng.modules.company.bo.CompanyQueryBo;
import com.zhongzheng.modules.company.bo.CompanyAddBo;
import com.zhongzheng.modules.company.bo.CompanyEditBo;
import com.zhongzheng.modules.company.service.ICompanyService;
import com.zhongzheng.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

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

    private final ICompanyService iCompanyService;

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


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

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

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

    /**
     * 导入公司
     */
    @ApiOperation("导入公司")
    @PreAuthorize("@ss.hasPermi('modules.company:company:edit')")
    @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 = iCompanyService.batchImport(file);

        return toAjax(excel ? 1: 0);
    }


}
