package com.usky.fire.controller.web;


import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.log.annotation.Log;
import com.usky.common.log.enums.BusinessType;
import com.usky.fire.domain.BaseCompany;
import com.usky.fire.domain.DemGridMember;
import com.usky.fire.service.BaseCompanyService;
import com.usky.fire.service.vo.*;
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;
import java.util.Map;

/**
 * <p>
 * 联网单位信息表 前端控制器
 * </p>
 *
 * @author JCB
 * @since 2022-08-23
 */
@RestController
@RequestMapping("/baseCompany")
public class BaseCompanyController {

    @Autowired
    private BaseCompanyService baseCompanyService;

    /**
     * 根据登录的用户查询所关联的单位下拉框
     *
     * @return
     */
    @GetMapping("userCompanySelect")
    public ApiResult<List<BaseCompany>> userCompanySelect() {
        return ApiResult.success(baseCompanyService.userCompanySelect());
    }

    /**
     * 单位管理-单位列表查询
     *
     * @param companyName  单位名称
     * @param organization 组织机构编码
     * @param streetTownName 街镇名称
     * @param companyType 单位类型
     * @param pageNum      当前页
     * @param pageSize     每页条数
     * @return
     */
    @GetMapping("companyList")
    public ApiResult<CommonPage<Object>> companyList(@RequestParam(value = "companyName", required = false) String companyName,
                                                     @RequestParam(value = "organization", required = false) String organization,
                                                     @RequestParam(value = "streetTownName", required = false) String streetTownName,
                                                     @RequestParam(value = "companyType", required = false) String companyType,
                                                     @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return ApiResult.success(baseCompanyService.companyList(companyName, organization, streetTownName,companyType,pageNum, pageSize));
    }

    /**
     * 单位管理-单位列表查询-导出
     *
     * @param companyName  单位名称
     * @param organization 组织机构编码
     * @param exportTitle    文件名
     * @throws IOException
     */
    @PostMapping("companyListExport")
    public void export(HttpServletResponse response,
                                  @RequestParam(value = "companyName", required = false) String companyName,
                                  @RequestParam(value = "organization", required = false) String organization,
                                  @RequestParam(value = "exportTitle") String exportTitle) throws IOException {
        List<CompanyExportVO> list = baseCompanyService.companyListExport(companyName, organization);
        ExcelUtil<CompanyExportVO> util = new ExcelUtil<CompanyExportVO>(CompanyExportVO.class);
        util.exportExcel(response,list,exportTitle,exportTitle);
    }
    /**
     * 单位管理-修改
     *
     * @param companyDataVo
     * @return
     */
    @Log(title = "单位管理-修改", businessType = BusinessType.UPDATE)
    @PutMapping("updateCompany")
    public ApiResult<Void> updateCompany(@RequestBody CompanyDataVo companyDataVo) {
        baseCompanyService.updateCompany(companyDataVo);
        return ApiResult.success();
    }

    /**
     * 单位管理-删除
     *
     * @param id 主键ID
     * @return
     */
    @Log(title = "单位管理-删除", businessType = BusinessType.DELETE)
    @DeleteMapping("delCompany")
    public ApiResult<Void> delCompany(@RequestParam(value = "id") Integer id) {
        baseCompanyService.delCompany(id);
        return ApiResult.success();
    }


    /**
     * 企业自主管理-单位撒点
     *
     * @param streetTown  街镇
     * @param companyType 单位类型（1：重点单位、2：一般单位、3：九小场所、9：其他单位）
     * @return
     */
    @GetMapping("companyScatterer")
    public ApiResult<List<Object>> companyScatterer(@RequestParam(value = "streetTown", required = false) String streetTown,
                                                    @RequestParam(value = "companyType", required = false) String companyType) {
        return ApiResult.success(baseCompanyService.companyScatterer(streetTown, companyType));
    }

    /**
     * 消防执法动态-撒点
     *
     * @param streetTown   街镇
     * @param sattererType 筛选条件
     * @return
     */
    @GetMapping("lawTrendScatterer")
    public ApiResult<List<Map<String, Object>>> lawTrendScatterer(@RequestParam(value = "streetTown", required = false) String streetTown,
                                                                  @RequestParam(value = "sattererType", required = false) String sattererType) {
        return ApiResult.success(baseCompanyService.lawTrendScatterer(streetTown, sattererType));
    }

    /**
     * 消防检查信息-列表查询
     *
     * @param companyName  单位名称
     * @param organization 组织机构代码
     * @param id           主键ID
     * @param pageNum      当前页
     * @param pageSize     每页条数
     * @return
     */
    @GetMapping("fireInspectionData")
    public ApiResult<CommonPage<Map<String, Object>>> fireInspectionData(@RequestParam(value = "companyName", required = false) String companyName,
                                                                         @RequestParam(value = "organization", required = false) String organization,
                                                                         @RequestParam(value = "id", required = false, defaultValue = "0") Integer id,
                                                                         @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                                         @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return ApiResult.success(baseCompanyService.fireInspectionData(companyName, organization, id, pageNum, pageSize));
    }

    /**
     * 消防检查信息-列表查询-导出
     *
     * @param companyName  单位名称
     * @param organization 组织机构代码
     * @param exportTitle  文件名
     * @throws IOException
     */
    @PostMapping("fireInspectionDataExport")
    public void fireInspectionDataExport(HttpServletResponse response,
                       @RequestParam(value = "companyName", required = false) String companyName,
                       @RequestParam(value = "organization", required = false) String organization,
                       @RequestParam(value = "exportTitle") String exportTitle)throws IOException{
        List<BaseCompanyExportVO> list = baseCompanyService.fireInspectionDataExport(companyName, organization);
        ExcelUtil<BaseCompanyExportVO> util = new ExcelUtil<BaseCompanyExportVO>(BaseCompanyExportVO.class);
        util.exportExcel(response, list, exportTitle, exportTitle);
    }

    /**
     * 单位信息采集-新增
     *
     * @param companyAddVO
     * @return
     */
    @Log(title = "单位信息采集-新增", businessType = BusinessType.INSERT)
    @PostMapping("addBaseCompany")
    public ApiResult<Void> addBaseCompany(@RequestBody CompanyAddVO companyAddVO) {
        baseCompanyService.addBaseCompany(companyAddVO);
        return ApiResult.success();
    }

    /**
     * 单位信息采集-名称模糊查询
     *
     * @param companyName   单位名称
     * @return
     */
    @GetMapping("companyByNameSelect")
    public ApiResult<List<CompanyDataVo>> companyByNameSelect(@RequestParam(value = "companyName", required = false) String companyName) {
        return ApiResult.success(baseCompanyService.companyByNameSelect(companyName));
    }

    /**
     * 生成报告单位信息-列表查询
     *
     * @param companyIdList  单位IDlist
     * @return
     */
    @PostMapping("reportCompanyList")
    public ApiResult<List<BaseCompany>> reportCompanyList(@RequestBody List<String> companyIdList) {
        return ApiResult.success(baseCompanyService.companySelect(companyIdList));
    }

}

