package com.inspect.metadata.patrolpointmnt.controller;

import com.inspect.metadata.eqpbook.domain.BasedataEqpBook;
import com.inspect.metadata.eqpbook.service.IBasedataEqpBookService;
import com.inspect.metadata.patrolpointmnt.domain.BasedataPatrolPointPreset;
import com.inspect.metadata.patrolpointmnt.service.IBasedataPatrolPointPresetService;
import com.inspect.base.core.utils.poi.ExcelUtil;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.base.core.web.page.TableDataInfo;
import com.inspect.common.log.annotation.Log;
import com.inspect.common.log.enums.BizType;
import com.inspect.common.security.annotation.RequiresPermissions;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

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

@RestController
@RequestMapping({"/patrolpointmnt/patrolpointpreset"})
public class BasedataPatrolPointPresetController extends BaseController {
    private final IBasedataPatrolPointPresetService basedataPatrolPointPresetService;
    private final IBasedataEqpBookService basedataEqpBookService;

    public BasedataPatrolPointPresetController(IBasedataPatrolPointPresetService basedataPatrolPointPresetService, IBasedataEqpBookService basedataEqpBookService) {
        this.basedataPatrolPointPresetService = basedataPatrolPointPresetService;
        this.basedataEqpBookService = basedataEqpBookService;
    }

    @RequiresPermissions({"basedata/patrolpointmnt:patrolpointpreset:list"})
    @GetMapping({"/list"})
    public TableDataInfo list(@RequestParam(value = "patrolpointId", required = false) Long patrolPointId, @RequestParam(value = "noVideo", required = false) Boolean noVideo) {
        //logger.info("[METADATA] basedataPatrolpointPreset: {}", basedataPatrolpointPreset.toString());
        BasedataPatrolPointPreset basedataPatrolPointPreset = new BasedataPatrolPointPreset();
        if(patrolPointId != null) {
            basedataPatrolPointPreset.setPatrolPointId(patrolPointId);
        }
        if(noVideo != null) {
            basedataPatrolPointPreset.setNoVideo(noVideo);
        }

        try {
            this.startPage();
            List<BasedataPatrolPointPreset> list = this.basedataPatrolPointPresetService.selectBasedataPatrolPointPresetList(basedataPatrolPointPreset);
            if(list == null) {
                return this.getDataTable(new ArrayList<>());
            }
            List<BasedataEqpBook> basedataEqpBooks = this.basedataEqpBookService.selectBasedataEqpBookList(new BasedataEqpBook());
            Map<Long, BasedataEqpBook> eqpbookMap = new HashMap<>();
            basedataEqpBooks.forEach((item) -> {
                eqpbookMap.put(item.getEqpBookId(), item);
            });
            List<BasedataPatrolPointPreset> newList = new ArrayList<>();
            List<String> types = Arrays.asList("1", "2", "3", "13");
            list.forEach((item) -> {
                item.setPatrolDeviceName(eqpbookMap.get(item.getEqpBookId()).getPatrolDeviceName());
                if (basedataPatrolPointPreset.isNoVideo()) {
                    if (types.contains(eqpbookMap.get(item.getEqpBookId()).getType())) {
                        newList.add(item);
                    }
                } else {
                    newList.add(item);
                }

            });
            return this.getDataTable(newList);
        } catch (Exception e) {
            e.printStackTrace();
            return this.getDataTable(new ArrayList<>());
        }
    }

    @RequiresPermissions({"basedata/patrolpointmnt:patrolpointpreset:export"})
    @Log(title = "巡检点预置位", businessType = BizType.EXPORT)
    @PostMapping({"/export"})
    public void export(HttpServletResponse response, BasedataPatrolPointPreset basedataPatrolpointPreset) {
        List<BasedataPatrolPointPreset> list = this.basedataPatrolPointPresetService.selectBasedataPatrolPointPresetList(basedataPatrolpointPreset);
        ExcelUtil<BasedataPatrolPointPreset> util = new ExcelUtil<>(BasedataPatrolPointPreset.class);
        util.exportExcel(response, list, "巡检点预置位数据");
    }

    @RequiresPermissions({"basedata/patrolpointmnt:patrolpointpreset:query"})
    @GetMapping({"/{patrolpointPresetId}"})
    public AjaxResult getInfo(@PathVariable("patrolpointPresetId") Long patrolPointPresetId) {
        return AjaxResult.success(this.basedataPatrolPointPresetService.selectBasedataPatrolPointPresetByPatrolPointPresetId(patrolPointPresetId));
    }

    @RequiresPermissions({"basedata/patrolpointmnt:patrolpointpreset:add"})
    @Log(title = "巡检点预置位", businessType = BizType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasedataPatrolPointPreset basedataPatrolpointPreset) {
        logger.info("[METADATA] basedataPatrolpointPreset: {}", basedataPatrolpointPreset);
        return AjaxResult.success(this.basedataPatrolPointPresetService.insertBasedataPatrolPointPreset(basedataPatrolpointPreset));
    }

    @RequiresPermissions({"basedata/patrolpointmnt:patrolpointpreset:edit"})
    @Log(title = "巡检点预置位", businessType = BizType.UPDATE)
    @PostMapping({"/edit"})
    public AjaxResult edit(@RequestBody BasedataPatrolPointPreset basedataPatrolpointPreset) {
        return this.toAjax(this.basedataPatrolPointPresetService.updateBasedataPatrolPointPreset(basedataPatrolpointPreset));
    }

    @RequiresPermissions({"basedata/patrolpointmnt:patrolpointpreset:remove"})
    @Log(title = "巡检点预置位", businessType = BizType.DELETE)
    @PostMapping({"/remove/{patrolpointPresetIds}"})
    public AjaxResult remove(@PathVariable("patrolpointPresetIds") Long[] patrolPointPresetIds) {
        return this.toAjax(this.basedataPatrolPointPresetService.deleteBasedataPatrolPointPresetByPatrolPointPresetIds(patrolPointPresetIds));
    }

    @PostMapping({"/getPatrolpointPresetListByDiveceIdS"})
    public TableDataInfo getPatrolPointPresetListByDeviceIdS(BasedataPatrolPointPreset basedataPatrolpointPreset) {
        List<BasedataPatrolPointPreset> list = this.basedataPatrolPointPresetService.selectBasedataPatrolPointPresetList(basedataPatrolpointPreset);
        return this.getDataTable(list);
    }
}
