package com.yunfeiyun.agmp.pts.controller;

import com.yunfeiyun.agmp.pts.domain.PtsLink;
import com.yunfeiyun.agmp.pts.service.IPtsLinkService;
import com.yunfeiyun.agmp.common.constant.ErrorCode;
import com.yunfeiyun.agmp.common.constant.ResConstants;
import com.yunfeiyun.agmp.common.framework.manager.ResManager;
import com.yunfeiyun.agmp.common.framework.web.domain.UploadParam;
import com.yunfeiyun.agmp.common.web.system.domain.SysRes;
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.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.yunfeiyun.agmp.common.annotation.Log;
import com.yunfeiyun.agmp.common.core.controller.BaseController;
import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
import com.yunfeiyun.agmp.common.enums.BusinessType;
import com.yunfeiyun.agmp.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 链接Controller
 * 
 * @author 杨晓辉
 * @date 2023-05-23
 */
@RestController
@RequestMapping("/system/ptslink")
public class PtsLinkController extends BaseController
{
    @Autowired
    private IPtsLinkService ptsLinkService;

    @Autowired
    private ResManager resManager;
    /**
     * 查询链接列表
     */
    @PreAuthorize("@ss.hasPermi('system:ptslink:list')")
    @GetMapping("/list")
    public TableDataInfo list(PtsLink ptsLink)
    {
        startPage();
        List<PtsLink> list = ptsLinkService.selectPtsLinkList(ptsLink,0);
        return getDataTable(list);
    }

    /**
     * 查询链接列表
     */
    @PreAuthorize("@ss.hasPermi('system:ptslink:list')")
    @GetMapping("/all/list")
    public AjaxResult alllist(PtsLink ptsLink)
    {
        List<PtsLink> list = ptsLinkService.selectPtsLinkList(ptsLink,0);
        return success(list);
    }


    /**
     * 获取链接详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:ptslink:info')")
    @GetMapping(value = "/info")
    public AjaxResult getInfo(String linkId)
    {
        return success(ptsLinkService.selectPtsLinkByLinkId(linkId));
    }

    /**
     * 新增链接
     */
    @PreAuthorize("@ss.hasPermi('system:ptslink:add')")
    @Log(title = "链接", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PtsLink ptsLink)
    {
        return toAjax(ptsLinkService.insertPtsLink(ptsLink,getLoginUser()));
    }

    /**
     * 修改链接
     */
    @PreAuthorize("@ss.hasPermi('system:ptslink:edit')")
    @Log(title = "链接", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody PtsLink ptsLink)
    {
        try {
            ptsLinkService.updatePtsLink(ptsLink, getLoginUser());
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 删除链接
     */
    @PreAuthorize("@ss.hasPermi('system:ptslink:remove')")
    @Log(title = "链接", businessType = BusinessType.DELETE)
	@DeleteMapping("/delete/{linkIds}")
    public AjaxResult remove(@PathVariable String[] linkIds)
    {
        return toAjax(ptsLinkService.deletePtsLinkByLinkIds(linkIds));
    }


    /**
     * 子系统图片上传
     */
    @Log(title = "子系统图片上传", businessType = BusinessType.UPDATE)
    @PostMapping("/preview/upload")
    public AjaxResult uploadPtsLinkIcon(MultipartFile file, HttpServletRequest request) throws Exception
    {
        if (!file.isEmpty())
        {
            UploadParam uploadParam = UploadParam.parse(request);
            if(uploadParam == null) return error(ErrorCode.UPLOAD_FAIL.getCode());
            SysRes sysRes  = resManager.uploadRes( ResConstants.BizType.SYSLINK, ResConstants.mediaType.IMG,uploadParam.getResBusId(),file,getLoginUser());
            AjaxResult ajax = AjaxResult.success();
            ajax.put("data" ,sysRes );
            return ajax;
        }
        return error(ErrorCode.UPLOAD_FAIL.getCode());
    }

}
