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.ServiceItem;
import com.ruoyi.dxp.dto.*;
import com.ruoyi.dxp.service.IServiceItemService;
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.ServiceShare;
import com.ruoyi.dxp.service.IServiceShareService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 服务共享Controller
 * 
 * @author jy
 * @date 2022-11-29
 */
@RestController
@RequestMapping("/system/share/service")
public class ServiceShareController extends BaseController
{
    @Autowired
    private IServiceShareService serviceShareService;

    @Autowired
    private IServiceItemService serviceItemService;

    /**
     * 查询服务共享列表
     */
    @PreAuthorize("@ss.hasPermi('system/share:service:list')")
    @GetMapping("/list")
    public TableDataInfo list(ServiceShare serviceShare)
    {
        startPage();
        List<ServiceShare> list = serviceShareService.selectServiceShareList(serviceShare);
        return getDataTable(list);
    }

    /**
     * 导出服务共享列表
     */
    @PreAuthorize("@ss.hasPermi('system/share:service:export')")
    @Log(title = "服务共享", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ServiceShare serviceShare)
    {
        List<ServiceShare> list = serviceShareService.selectServiceShareList(serviceShare);
        ExcelUtil<ServiceShare> util = new ExcelUtil<ServiceShare>(ServiceShare.class);
        util.exportExcel(response, list, "服务共享数据");
    }

    /**
     * 获取服务共享详细信息
     */
    @PreAuthorize("@ss.hasPermi('system/share:service:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(serviceShareService.selectServiceShareById(id));
    }

    /**
     * 查询服务参数
     * @param id 服务id
     * @return 参数列表
     */
    @PreAuthorize("@ss.hasPermi('system/integration:service:query')")
    @GetMapping(value = "/serviceitem")
    public AjaxResult getServiceItem(Long id, Integer checkChanged) {
        if (checkChanged != null && checkChanged == 1) {
            try {
                CompareShareServiceResultDto resultDto = serviceShareService.checkChange(id);
                return success(resultDto);
            } catch (Exception e) {
                return error(e.getMessage());
            }
        } else {
            ServiceItem req = new ServiceItem();
            req.setServiceType(DxpConstants.DATA_TYPE_SHARE);
            req.setServiceId(id);
            return success(serviceItemService.selectServiceItemList(req));
        }
    }

    /**
     * 新增服务共享
     */
    @PreAuthorize("@ss.hasPermi('system/share:service:add')")
    @Log(title = "服务共享", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ServiceShareSave serviceShare)
    {
        return serviceShareService.save(serviceShare);
    }

    /**
     * 挂载
     */
    @PreAuthorize("@ss.hasPermi('system/share:service:add')")
    @Log(title = "服务共享", businessType = BusinessType.INSERT)
    @PostMapping("/mount")
    public AjaxResult mount(@RequestBody IdsBodyDto dto)
    {
        try {
            serviceShareService.mount(dto.getIds());
            return success();
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改服务共享
     */
    @PreAuthorize("@ss.hasPermi('system/share:service:edit')")
    @Log(title = "服务共享", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ServiceShareSave serviceShare)
    {
        return serviceShareService.save(serviceShare);
    }

    /**
     * 删除服务共享
     */
    @PreAuthorize("@ss.hasPermi('system/share:service:remove')")
    @Log(title = "服务共享", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids)
    {
        return serviceShareService.deleteServiceShareById(ids);
    }

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