package com.xzzn.web.controller.ems;

import com.xzzn.common.annotation.Log;
import com.xzzn.common.core.controller.BaseController;
import com.xzzn.common.core.domain.AjaxResult;
import com.xzzn.common.core.page.TableDataInfo;
import com.xzzn.common.enums.BusinessType;
import com.xzzn.ems.domain.EmsPointConfig;
import com.xzzn.ems.domain.vo.ImportPointTemplateRequest;
import com.xzzn.ems.domain.vo.PointConfigCurveRequest;
import com.xzzn.ems.domain.vo.PointConfigGenerateRecentRequest;
import com.xzzn.ems.domain.vo.PointConfigLatestValueRequest;
import com.xzzn.ems.service.IEmsPointConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/ems/pointConfig")
public class EmsPointConfigController extends BaseController {

    @Autowired
    private IEmsPointConfigService pointConfigService;

    @GetMapping("/list")
    public TableDataInfo list(EmsPointConfig pointConfig) {
        startPage();
        List<EmsPointConfig> list = pointConfigService.selectPointConfigList(pointConfig);
        return getDataTable(list);
    }

    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(pointConfigService.selectPointConfigById(id));
    }

    @Log(title = "点位配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmsPointConfig pointConfig) {
        return toAjax(pointConfigService.insertPointConfig(pointConfig, getUsername()));
    }

    @Log(title = "点位配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmsPointConfig pointConfig) {
        return toAjax(pointConfigService.updatePointConfig(pointConfig, getUsername()));
    }

    @Log(title = "点位配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pointConfigService.deletePointConfigByIds(ids));
    }

    @Log(title = "点位配置", businessType = BusinessType.IMPORT)
    @PostMapping("/importTemplateBySite")
    public AjaxResult importTemplateBySite(@Valid @RequestBody ImportPointTemplateRequest request) {
        return success(pointConfigService.importTemplateBySite(request, getUsername()));
    }

    @Log(title = "点位配置", businessType = BusinessType.IMPORT)
    @PostMapping("/importCsv")
    public AjaxResult importCsv(@RequestParam String siteId,
                                @RequestParam(required = false) Boolean overwrite,
                                @RequestParam("file") MultipartFile file) {
        return success(pointConfigService.importCsvBySite(siteId, overwrite, file, getUsername()));
    }

    @GetMapping("/registerAddress")
    public AjaxResult getRegisterAddress(@RequestParam String siteId,
                                         @RequestParam String deviceCategory,
                                         @RequestParam String deviceId,
                                         @RequestParam String dataKey) {
        return success(pointConfigService.getRegisterAddress(siteId, deviceCategory, deviceId, dataKey));
    }

    @PostMapping("/latestValues")
    public AjaxResult latestValues(@RequestBody PointConfigLatestValueRequest request) {
        return success(pointConfigService.getLatestValues(request));
    }

    @PostMapping("/curve")
    public AjaxResult curve(@RequestBody PointConfigCurveRequest request) {
        return success(pointConfigService.getCurveData(request));
    }

    @Log(title = "点位配置", businessType = BusinessType.INSERT)
    @PostMapping("/generateRecent7Days")
    public AjaxResult generateRecent7Days(@Valid @RequestBody PointConfigGenerateRecentRequest request) {
        return success(pointConfigService.generateRecent7DaysData(request));
    }
}
