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

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.CompareShareDataResultDto;
import com.ruoyi.dxp.dto.AuditDto;
import com.ruoyi.dxp.dto.DataShareBatch;
import com.ruoyi.dxp.dto.ShareDataSaveDto;
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.DataShare;
import com.ruoyi.dxp.service.IDataShareService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 共享数据Controller
 *
 * @author jy
 * @date 2022-11-21
 */
@RestController
@RequestMapping("/system/share/data")
public class DataShareController extends BaseController {
    @Autowired
    private IDataShareService dataShareService;

    @Autowired
    private IDataItemService dataItemService;

    /**
     * 查询共享数据列表
     */
    @PreAuthorize("@ss.hasPermi('system/share:data:list')")
    @GetMapping("/list")
    public TableDataInfo list(DataShare dataShare) {
        startPage();
        List<DataShare> list = dataShareService.selectDataShareList(dataShare);
        return getDataTable(list);
    }

    /**
     * 导出共享数据列表
     */
    @PreAuthorize("@ss.hasPermi('system/share:data:export')")
    @Log(title = "共享数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataShare dataShare) {
        List<DataShare> list = dataShareService.selectDataShareList(dataShare);
        ExcelUtil<DataShare> util = new ExcelUtil<DataShare>(DataShare.class);
        util.exportExcel(response, list, "共享数据数据");
    }

    /**
     * 获取共享数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('system/share:data:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(dataShareService.selectDataShareById(id));
    }

    /**
     * 查询整合数据明细
     *
     * @param id           共享数据id
     * @param checkChanged 是否检查变化
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system/integration:data:query')")
    @GetMapping(value = "/dataitem")
    public AjaxResult getDataItem(Long id, Integer checkChanged) {
        if (checkChanged != null && checkChanged == 1) {
            try {
                CompareShareDataResultDto resultDto = dataShareService.checkChange(id);
                return success(resultDto);
            } catch (Exception e) {
                return error(e.getMessage());
            }
        } else {
            DataItem req = new DataItem();
            req.setDataType(DxpConstants.DATA_TYPE_SHARE);
            req.setDataId(id);
            return success(dataItemService.selectDataItemList(req));
        }
    }

    /**
     * 新增共享数据
     */
    @PreAuthorize("@ss.hasPermi('system/share:data:add')")
    @Log(title = "共享数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShareDataSaveDto dto) {
        return dataShareService.save(dto);
    }

    /**
     * 挂载（从整合数据生成）
     *
     * @param dataShareBatch
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system/share:data:add')")
    @Log(title = "共享数据", businessType = BusinessType.IMPORT)
    @PostMapping("/mount")
    public AjaxResult mount(@RequestBody DataShareBatch dataShareBatch) {
        return dataShareService.batchDataShare(dataShareBatch);
    }

    /**
     * 修改共享数据
     */
    @PreAuthorize("@ss.hasPermi('system/share:data:edit')")
    @Log(title = "共享数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShareDataSaveDto dto) {
        return dataShareService.save(dto);
    }

    /**
     * 删除共享数据
     */
    @PreAuthorize("@ss.hasPermi('system/share:data:remove')")
    @Log(title = "共享数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return dataShareService.deleteDataShareById(id);
    }

    /**
     * 审核
     * @param auditDto 审核对象
     * @return 审核结果
     */
    @PreAuthorize("@ss.hasPermi('system/share:data:audit')")
    @Log(title = "共享数据", businessType = BusinessType.AUDIT)
    @PostMapping("/audit")
    public AjaxResult audit(@RequestBody AuditDto auditDto) {
        return dataShareService.audit(auditDto);
    }
}
