package com.ruoyi.web.controller.dxp.customer;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.dxp.constant.DxpConstants;
import com.ruoyi.dxp.domain.*;
import com.ruoyi.dxp.dto.CustomerServiceCallDto;
import com.ruoyi.dxp.dto.DataShareQueryParam;
import com.ruoyi.dxp.dto.customer.MyShareResourceDto;
import com.ruoyi.dxp.dto.customer.ShareResourceDto;
import com.ruoyi.dxp.dto.customer.ShareResourceParam;
import com.ruoyi.dxp.service.*;
import com.ruoyi.dxp.utils.ServiceUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 共享目录
 */
@RestController
@RequestMapping("/customer/resource")
public class ResourceCustomerController extends BaseController {

    @Autowired
    private ICatalogueService catalogueService;

    @Autowired
    private IResourceShareService resourceShareService;

    @Autowired
    private IDataItemService dataItemService;

    @Autowired
    private IServiceItemService serviceItemService;

    @Autowired
    private IUserResourceService userResourceService;

    @Autowired
    private IDataShareService dataShareService;

    @Autowired
    private IServiceShareService serviceShareService;

    @Autowired
    private IUserResourceItemService userResourceItemService;

    /**
     * 查询目录列表
     */
//    @Anonymous
    @GetMapping("/catalogue")
    public TableDataInfo catalogueList(Catalogue catalogue) {
        startPage();
        catalogue.setCatalogueType(DxpConstants.DATA_TYPE_SHARE);
        List<Catalogue> list = catalogueService.selectCatalogueList(catalogue);
        return getDataTable(list);
    }

    /**
     * 查询资源列表
     */
//    @Anonymous
    @GetMapping("/list")
    public AjaxResult resourceList(ShareResourceParam dto) {
        dto.setUserId(null);
        List<ShareResourceDto> shareResourceDtos = resourceShareService.selectList(dto);
        return AjaxResult.success(shareResourceDtos);
    }

    /**
     * 我的资源
     */
    @GetMapping("/my")
    public AjaxResult myResourceList(ShareResourceParam dto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        dto.setUserId(loginUser.getUserId());
        List<MyShareResourceDto> myShareResourceDtos = resourceShareService.selectMyList(dto);
        return AjaxResult.success(myShareResourceDtos);
    }

    /**
     * 资源详情
     */
//    @Anonymous
    @GetMapping("/detail")
    public AjaxResult resourceDetail(Long id, String resType) {
        if (resType.equals(DxpConstants.RESOURCE_TYPE_DATA)) {
            ShareResourceDto<DataShare> shareDataDto = new ShareResourceDto();
            DataShare dataShare = dataShareService.selectDataShareById(id);
            if (dataShare == null) {
                return success();
            }

            // 浏览器+1
            dataShareService.addViewCount(id);
            BeanUtils.copyProperties(dataShare, shareDataDto);
            shareDataDto.setResName(dataShare.getDataName());
            shareDataDto.setResType(resType);
            shareDataDto.setData(dataShare);
            return success(shareDataDto);
        } else if (resType.equals(DxpConstants.RESOURCE_TYPE_SERVICE)) {
            ShareResourceDto<ServiceShare> shareServiceDto = new ShareResourceDto();
            ServiceShare serviceShare = serviceShareService.selectServiceShareById(id);
            if (serviceShare == null) {
                return success();
            }

            serviceShareService.addViewCount(id);
            BeanUtils.copyProperties(serviceShare, shareServiceDto);
            shareServiceDto.setResName(serviceShare.getServiceName());
            shareServiceDto.setResType(resType);
            shareServiceDto.setData(serviceShare);
            return success(shareServiceDto);
        }

        return success();
    }

    /**
     * 查询资源明细
     */
//    @Anonymous
    @GetMapping("/outitem")
    public AjaxResult outItem(Long id, String resType, String callId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        // 查询当前用户是否已订阅该资源
        UserResource req = new UserResource();
        req.setResourceType(resType);
        req.setResourceId(id);
        req.setUserId(loginUser.getUserId());
        req.setCallId(callId);
        List<UserResource> userResources = userResourceService.selectUserResourceList(req);
        // 未订阅或未审核
        if (userResources == null || userResources.size() == 0 || !userResources.get(0).getStatus().equals("1")) {
            if (resType.equals(DxpConstants.RESOURCE_TYPE_DATA)) {
                DataItem dReq = new DataItem();
                dReq.setDataType(DxpConstants.DATA_TYPE_SHARE);
                dReq.setDataId(id);
                dReq.setIsVisable(DxpConstants.YES);
                List<DataItem> dataItems = dataItemService.selectDataItemList(dReq);
                return success(dataItems);
            } else if (resType.equals(DxpConstants.RESOURCE_TYPE_SERVICE)) {
                ServiceItem sReq = new ServiceItem();
                sReq.setServiceType(DxpConstants.DATA_TYPE_SHARE);
                sReq.setServiceId(id);
                sReq.setParamType(DxpConstants.PARAM_TYPE_OUT);
                sReq.setIsVisable(DxpConstants.YES);
                List<ServiceItem> serviceItems = serviceItemService.selectServiceItemList(sReq);
                return success(serviceItems);
            }
        } else {
            UserResourceItem iReq = new UserResourceItem();
            iReq.setUserResourceId(userResources.get(0).getId());
            return success(userResourceItemService.selectUserResourceItemList(iReq).get(0));
        }
        return success();
    }

    @GetMapping("/initem")
    public AjaxResult inItem(Long id, String resType) {
        if (resType.equals(DxpConstants.RESOURCE_TYPE_DATA)) {
            DataItem dReq = new DataItem();
            dReq.setDataType(DxpConstants.DATA_TYPE_SHARE);
            dReq.setDataId(id);
            List<DataItem> dataItems = dataItemService.selectDataItemList(dReq);
            return success(dataItems);
        } else if (resType.equals(DxpConstants.RESOURCE_TYPE_SERVICE)) {
            ServiceItem sReq = new ServiceItem();
            sReq.setServiceType(DxpConstants.DATA_TYPE_SHARE);
            sReq.setServiceId(id);
            sReq.setParamType(DxpConstants.PARAM_TYPE_IN);
            List<ServiceItem> serviceItems = serviceItemService.selectServiceItemList(sReq);
            return success(serviceItems);
        }

        return success();
    }

    @GetMapping("/serviceitem")
    public AjaxResult serviceItem(Long id, String resType) {
        if (resType.equals(DxpConstants.RESOURCE_TYPE_DATA)) {
            DataItem dReq = new DataItem();
            dReq.setDataType(DxpConstants.DATA_TYPE_SHARE);
            dReq.setDataId(id);
            List<DataItem> dataItems = dataItemService.selectDataItemList(dReq);
            return success(dataItems);
        } else if (resType.equals(DxpConstants.RESOURCE_TYPE_SERVICE)) {
            ServiceItem sReq = new ServiceItem();
            sReq.setServiceType(DxpConstants.DATA_TYPE_SHARE);
            sReq.setServiceId(id);
            sReq.setParamType(DxpConstants.PARAM_TYPE_OUT);
            List<ServiceItem> serviceItems = serviceItemService.selectServiceItemList(sReq);
            return success(serviceItems);
        }

        return success();
    }

    /**
     * 申请资源
     */
    @PostMapping("/apply")
    @Log(title = "资源", businessType = BusinessType.INSERT)
    public AjaxResult apply(@RequestBody UserResource userResource) {
        return resourceShareService.apply(userResource);
    }

    @PostMapping("/querydata")
    public AjaxResult queryData(@RequestBody DataShareQueryParam param) {
        return resourceShareService.queryData(param.getAccessId(), param.getPage(), param.getPageSize(), param.getQuery());
    }

    @PostMapping("/queryservice")
    public AjaxResult queryService(@RequestBody CustomerServiceCallDto req) {
        return resourceShareService.queryService(req);
    }
}
