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.PatrolInspectionSite;
import com.usky.fire.domain.PatrolInspectionSiteContent;
import com.usky.fire.service.PatrolInspectionContentService;
import com.usky.fire.service.PatrolInspectionSiteService;
import com.usky.fire.service.vo.PatrolInspectionAreaVo;
import com.usky.fire.service.vo.PatrolInspectionSiteExportVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 巡查自检-巡检地点
 *
 * @author JCB
 * @since 2022-07-13
 */
@RestController
@RequestMapping("/patrolInspectionSite")
public class PatrolInspectionSiteController {

    @Autowired
    private PatrolInspectionSiteService patrolInspectionSiteService;

    @Autowired
    private PatrolInspectionContentService patrolInspectionContentService;


    /**
     * 巡查自检-巡检地点-左侧树形区域
     *
     * @return
     */
    @GetMapping("areaLeftList")
    public ApiResult<List<PatrolInspectionAreaVo>> areaLeftList() {
        return ApiResult.success(patrolInspectionSiteService.areaLeftList());
    }


    /**
     * 巡查自检-巡检地点-巡检列表
     *
     * @param areaId   区域id
     * @param siteName 地点名称
     * @param pageNum  当前页
     * @param pageSize 每页条数
     * @param id       巡检地点ID
     * @return
     */
    @GetMapping("patrolInspectionSiteList")
    public ApiResult<CommonPage<PatrolInspectionSite>> patrolInspectionSiteList(@RequestParam(value = "areaId", required = false) Integer areaId,
                                                                                @RequestParam(value = "siteName", required = false) String siteName,
                                                                                @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                                                @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                                                                @RequestParam(value = "id", required = false, defaultValue = "0") Integer id) {
        return ApiResult.success(patrolInspectionSiteService.patrolInspectionSiteList(areaId, siteName, pageNum, pageSize, id));
    }

    /**
     * 巡查自检-巡检地点-添加地点-保存
     *
     * @param PatrolInspectionSiteList
     * @return
     */
    @Log(title = "巡检地点-点位分配", businessType = BusinessType.UPDATE)
    @PostMapping("updatePatrolInspectionSiteList")
    public ApiResult<Void> updatePatrolInspectionSiteList(@RequestBody List<PatrolInspectionSite> PatrolInspectionSiteList) {
        patrolInspectionSiteService.updatePatrolInspectionSiteList(PatrolInspectionSiteList);
        return ApiResult.success();
    }

    /**
     * 巡查自检-巡检地点-编辑
     *
     * @param PatrolInspectionSite
     * @return
     */
    @Log(title = "巡检地点", businessType = BusinessType.UPDATE)
    @PostMapping("updatePatrolInspectionSite")
    public ApiResult<Void> updatePatrolInspectionSite(@RequestBody PatrolInspectionSite PatrolInspectionSite) {
        patrolInspectionSiteService.updatePatrolInspectionSite(PatrolInspectionSite);
        return ApiResult.success();
    }

    /**
     * 巡查自检-巡检地点-删除
     *
     * @param id 巡检地点ID
     * @return
     */
    @Log(title = "巡检地点", businessType = BusinessType.DELETE)
    @GetMapping("delPatrolInspectionSite")
    public ApiResult<Void> delPatrolInspectionSite(@RequestParam(value = "id") Integer id) {
        patrolInspectionSiteService.delPatrolInspectionSite(id);
        return ApiResult.success();
    }


    /**
     * 巡查自检-巡检地点-分配内容-列表查询
     *
     * @param pageNum  当前页
     * @param pageSize 每页条数
     * @param siteId   巡检地点ID
     * @return
     */
    @GetMapping("siteContentList")
    public ApiResult<Map<String, Object>> siteContentList(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                          @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                                          @RequestParam(value = "siteId") Integer siteId) {
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("checkedKeys", patrolInspectionSiteService.siteContentList(siteId));
        ajax.put("InspectionContent", patrolInspectionContentService.patrolInspectionContentList(null, pageNum, pageSize, 0));
        return ApiResult.success(ajax);
    }

    /**
     * 巡查自检-巡检地点-分配内容-保存
     *
     * @param siteContentList
     * @return
     */
    @Log(title = "巡检地点-分配内容", businessType = BusinessType.INSERT)
    @PostMapping("addSiteContent")
    public ApiResult<Void> addSiteContent(@RequestBody List<PatrolInspectionSiteContent> siteContentList) {
        patrolInspectionSiteService.addSiteContent(siteContentList);
        return ApiResult.success();
    }

    /**
     * 巡查自检-巡检地点-列表导出
     *
     * @param areaId   区域id
     * @param siteName 地点名称
     * @param idList   地点ID
     * @return
     */
    @Log(title = "巡检地点", businessType = BusinessType.EXPORT)
    @PostMapping("/patrolInspectionSiteListExport")
    public void export(HttpServletResponse response,
                       @RequestParam(required = false) Integer areaId,
                       @RequestParam(value = "siteName", required = false) String siteName,
                       @RequestParam(value = "idList", required = false) String idList) throws IOException {
        List<PatrolInspectionSiteExportVo> list = patrolInspectionSiteService.patrolInspectionSiteListExport(areaId, siteName, idList);
        ExcelUtil<PatrolInspectionSiteExportVo> util = new ExcelUtil<PatrolInspectionSiteExportVo>(PatrolInspectionSiteExportVo.class);
        util.exportExcel(response, list, "巡检地点列表", "巡检地点列表");
    }

    /**
     * 巡检点位-导入
     *
     * @param file 导入文件
     * @return
     * @throws Exception
     */
    @Log(title = "巡检地点", businessType = BusinessType.IMPORT)
    @PostMapping("/patrolInspectionSiteImport")
    public ApiResult<Void> patrolInspectionSiteImport(@RequestParam("file") MultipartFile file){
        patrolInspectionSiteService.patrolInspectionSiteImport(file);
        return ApiResult.success();
    }
}

