package com.ruoyi.web.controller.dxp.system.integration;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.dxp.constant.DxpConstants;
import com.ruoyi.dxp.domain.DataItem;
import com.ruoyi.dxp.dto.DataIntegrationBatch;
import com.ruoyi.dxp.dto.IdBodyDto;
import com.ruoyi.dxp.service.IDataItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dxp.domain.DataIntegration;
import com.ruoyi.dxp.service.IDataIntegrationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 整合数据Controller
 * 
 * @author jy
 * @date 2022-11-12
 */
@RestController
@RequestMapping("/system/integration/data")
public class DataIntegrationController extends BaseController
{
    @Autowired
    private IDataIntegrationService dataIntegrationService;

    @Autowired
    private IDataItemService dataItemService;

    /**
     * 查询整合数据列表
     */
    @PreAuthorize("@ss.hasPermi('system/integration:data:list')")
    @GetMapping("/list")
    public TableDataInfo list(DataIntegration dataIntegration)
    {
        startPage();
//        dataIntegration.setIsMetadata(DxpConstants.YES);
        List<DataIntegration> list = dataIntegrationService.selectDataIntegrationList(dataIntegration);
        return getDataTable(list);
    }

    /**
     * 导出整合数据列表
     */
    @PreAuthorize("@ss.hasPermi('system/integration:data:export')")
    @Log(title = "整合数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataIntegration dataIntegration)
    {
        List<DataIntegration> list = dataIntegrationService.selectDataIntegrationList(dataIntegration);
        ExcelUtil<DataIntegration> util = new ExcelUtil<DataIntegration>(DataIntegration.class);
        util.exportExcel(response, list, "整合数据数据");
    }

    /**
     * 获取整合数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('system/integration:data:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dataIntegrationService.selectDataIntegrationById(id));
    }

    /**
     * 查询整合数据明细
     * @param id 整合数据id
     * @return 明细集合
     */
    @PreAuthorize("@ss.hasPermi('system/integration:data:query')")
    @GetMapping(value = "/dataitem")
    public AjaxResult getDataItem(Long id) {
        DataItem req = new DataItem();
        req.setDataType(DxpConstants.DATA_TYPE_INTEGRATION);
        req.setDataId(id);
        return success(dataItemService.selectDataItemList(req));
    }

    /**
     * 新增整合数据
     */
    @PreAuthorize("@ss.hasPermi('system/integration:data:add')")
    @Log(title = "整合数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DataIntegration dataIntegration)
    {
        dataIntegration.setIsMetadata(DxpConstants.NO);
        return dataIntegrationService.insertDataIntegration(dataIntegration);
    }

    /**
     * 从数据源挂载
     */
    @PreAuthorize("@ss.hasPermi('system/integration:data:add')")
    @Log(title = "整合数据", businessType = BusinessType.IMPORT)
    @PostMapping("/mount")
    public AjaxResult batch(@RequestBody DataIntegrationBatch dataIntegrationBatch) {
        return dataIntegrationService.batchDataIntegration(dataIntegrationBatch);
    }

    /**
     * 修改整合数据
     */
    @PreAuthorize("@ss.hasPermi('system/integration:data:edit')")
    @Log(title = "整合数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DataIntegration dataIntegration)
    {
        return toAjax(dataIntegrationService.updateDataIntegration(dataIntegration));
    }

    /**
     * 从数据源同步整合数据
     */
    @PreAuthorize("@ss.hasPermi('system/integration:data:edit')")
    @Log(title = "整合数据", businessType = BusinessType.UPDATE)
    @PostMapping("/sync")
    public AjaxResult syncFromDatabase(@RequestBody IdBodyDto dto) {
        try {
            dataIntegrationService.syncFromDatabase(dto.getId());
            return success();
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 删除整合数据
     */
    @PreAuthorize("@ss.hasPermi('system/integration:data:remove')")
    @Log(title = "整合数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return dataIntegrationService.deleteDataIntegrationById(id);
    }
}
