/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.xrtygis.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.xrtygis.entity.TfIndexAllocation;
import com.tofly.xrtygis.service.TfDeviceArchiveManageService;
import com.tofly.xrtygis.service.TfIndexAllocationService;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.Arrays;
import java.util.List;
import com.haiqiu.tools.excel.utils.ExcelUtils;
/**
 * 指标配置
 *
 * @author admin
 * @date Fri Mar 11 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tfindexallocation")
@Api(tags="指标配置接口")
public class TfIndexAllocationController {

    private final  TfIndexAllocationService tfIndexAllocationService;
    
    private final TfDeviceArchiveManageService tfDeviceArchiveManageService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param tfIndexAllocation 指标配置
    * @return
    */
    @GetMapping("/page" )
    @ToFlyAppLog(title="分页查询" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getTfIndexAllocationPage(Page page, @ModelAttribute TfIndexAllocation tfIndexAllocation) {
        try{
            List<Long> deviceIds = null;
            if(StringUtils.isNotBlank(tfIndexAllocation.getDeviceName())){
                deviceIds = tfDeviceArchiveManageService.getIdList(tfIndexAllocation.getDeviceName());
            }
            Page<TfIndexAllocation> indexAllocationPage = tfIndexAllocationService.page(page, Wrappers.query(tfIndexAllocation)
                    .in(StringUtils.isNotBlank(tfIndexAllocation.getDeviceName()),"DEVICE_ID",deviceIds));
            List<TfIndexAllocation> indexAllocationList = indexAllocationPage.getRecords();
            for(TfIndexAllocation indexAllocation : indexAllocationList){
                if(StringUtils.isNotBlank(indexAllocation.getReportId())){
                    String[] reportIds = indexAllocation.getReportId().split(",");
                    StringBuilder reportLevels = new StringBuilder();
                    for (String reportId : reportIds){
                        reportLevels.append(tfIndexAllocationService.getReportLevel(Long.parseLong(reportId.trim())));
                        reportLevels.append(",");
                    }
                    reportLevels = new StringBuilder(reportLevels.substring(0, reportLevels.length() - 1));
                    indexAllocation.setReportLevel(reportLevels.toString());
                }
            }
            indexAllocationPage.setRecords(indexAllocationList);
            return ResultRespone.success(indexAllocationPage);
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed();
        }
    }


    /**
     * 通过id查询指标配置
     * @param deviceId id
     * @return ResultRespone
     */
    @GetMapping("/{deviceId}" )
    @ApiOperation(value = "通过ID查询")
    @ToFlyAppLog(title="通过ID查询" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("deviceId" ) Long deviceId) {
        try{
            return ResultRespone.success(tfIndexAllocationService.getById(deviceId));
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed();
        }
    }

    /**
     * 新增指标配置
     * @param tfIndexAllocation 指标配置
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增指标配置" )
    @ApiOperation(value = "新增指标配置")
    @PostMapping
    @Transactional
    public ResultRespone save(@RequestBody TfIndexAllocation tfIndexAllocation) {
        try{

            boolean flag=tfIndexAllocationService.save(tfIndexAllocation);
            String unit=tfIndexAllocation.getUnit();
            if(StringUtil.isNotEmpty(unit)){
                tfIndexAllocationService.updateUnitById(unit,tfIndexAllocation.getId());
            }
            return ResultRespone.success(flag);
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed();
        }
    }

    /**
     * 修改指标配置
     * @param tfIndexAllocation 指标配置
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改指标配置" )
    @ApiOperation(value = "修改指标配置")
    @PutMapping
    @Transactional
    public ResultRespone updateById(@RequestBody TfIndexAllocation tfIndexAllocation) {
        try{
            String unit=tfIndexAllocation.getUnit();
            if(StringUtil.isNotEmpty(unit)){
                tfIndexAllocationService.updateUnitById(unit,tfIndexAllocation.getId());
                tfIndexAllocation.setUnit(null);
            }
            return ResultRespone.success(tfIndexAllocationService.updateById(tfIndexAllocation));
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed();
        }
    }

    /**
     * 通过id删除指标配置
     * @param deviceId id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id删除指标配置" )
    @ApiOperation(value = "通过ID删除指标配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{deviceId}" )
    public ResultRespone removeById(@PathVariable Long deviceId) {
        try{
            return ResultRespone.success(tfIndexAllocationService.removeById(deviceId));
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed();
        }
    }

    /**
     * 通过id批量删除指标配置
     * @param ids id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id批量删除指标配置" )
    @ApiOperation(value = "通过ID批量删除指标配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds" )
    public ResultRespone removeById(String ids) {
        try{
            return ResultRespone.success(tfIndexAllocationService.removeByIds(Arrays.asList(ids.split(","))));
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed();
        }
    }


    @ApiOperation(value = "指标配置导出")
    @GetMapping("/export")
    public void monitorExport(@ModelAttribute TfIndexAllocation tfIndexAllocation) {
        List<Long> deviceIds = null;
        if(StringUtils.isNotBlank(tfIndexAllocation.getDeviceName())){
            deviceIds = tfDeviceArchiveManageService.getIdList(tfIndexAllocation.getDeviceName());
        }

        List<TfIndexAllocation>  list= tfIndexAllocationService.list(Wrappers.query(tfIndexAllocation)
                .in(StringUtils.isNotBlank(tfIndexAllocation.getDeviceName()),"DEVICE_ID",deviceIds));

        for(TfIndexAllocation indexAllocation : list){
            if(StringUtils.isNotBlank(indexAllocation.getReportId())){
                String[] reportIds = indexAllocation.getReportId().split(",");
                StringBuilder reportLevels = new StringBuilder();
                for (String reportId : reportIds){
                    reportLevels.append(tfIndexAllocationService.getReportLevel(Long.parseLong(reportId.trim())));
                    reportLevels.append(",");
                }
                reportLevels = new StringBuilder(reportLevels.substring(0, reportLevels.length() - 1));
                indexAllocation.setReportLevel(reportLevels.toString());
            }
        }

        if (!CollectionUtils.isEmpty(list)) {
            ExcelUtils.getListToExcel(TfIndexAllocation.class, list, "指标配置信息", "指标配置信息");
        }

    }
}
