package com.jeeplus.pubmodules.workCollectAccessory.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.pubmodules.workCollectAccessory.domain.WorkCollectAccessory;
import com.jeeplus.pubmodules.workCollectAccessory.service.WorkCollectAccessoryService;
import com.jeeplus.pubmodules.workCollectAccessory.service.dto.DictValue;
import com.jeeplus.pubmodules.workCollectAccessory.service.dto.WorkCollectAccessoryDTO;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * @Author hgc
 * @Date 2024/12/16/16:20
 * @ClassName WorkCollectAccessoryController
 * @Description
 */
@Slf4j
@Api(tags ="资料库管理")
@RestController
@RequestMapping(value = "/workCollectAccessory")
public class WorkCollectAccessoryController {

    @Autowired
    private WorkCollectAccessoryService workCollectAccessoryService;

    /**
     * 附件列表
     */
    @ApiOperation(value = "附件列表")
    @GetMapping(value = "/list")
    public ResponseEntity<IPage<WorkCollectAccessory>> findPageList(WorkCollectAccessory workCollectAccessory, Page<WorkCollectAccessory> page) throws Exception {
        IPage<WorkCollectAccessory> iPage= workCollectAccessoryService.findPageList(workCollectAccessory,page);
        return ResponseEntity.ok(iPage);
    }

    /**
     * 收藏附件
     */
    @ApiOperation(value = "收藏附件")
    @PostMapping(value = "/collection")
    public ResponseEntity collectionAttachment(@RequestBody WorkCollectAccessoryDTO workCollectAccessoryDTO) throws ParseException {
        String attachmentId = workCollectAccessoryDTO.getAttachmentId();
        UserDTO userDTO = SpringUtil.getBean(IUserApi.class).getByToken(TokenProvider.getCurrentToken());
        workCollectAccessoryService.collectionAttachment(userDTO.getId(),attachmentId);
        return ResponseEntity.ok("操作成功");
    }

    /**
     * 共享附件
     */
    @ApiOperation(value = "共享附件")
    @PostMapping(value = "/share")
    public ResponseEntity shareAttachment(@RequestBody WorkCollectAccessoryDTO workCollectAccessoryDTO) throws ParseException {
        String attachmentId = workCollectAccessoryDTO.getAttachmentId();
        String shareType = workCollectAccessoryDTO.getShareType();
        String componyId = workCollectAccessoryDTO.getCompanyId();
        UserDTO userDTO = SpringUtil.getBean(IUserApi.class).getByToken(TokenProvider.getCurrentToken());
        workCollectAccessoryService.shareAttachment(userDTO.getId(),attachmentId,shareType,componyId);
        return ResponseEntity.ok("操作成功");
    }


    /**
     * 上传资料库
     */
    @ApiOperation(value = "上传资料库")
    @PostMapping(value = "/saveForm")
    public ResponseEntity saveForm(@RequestBody WorkCollectAccessoryDTO workCollectAccessoryDTO) throws ParseException {
        workCollectAccessoryService.saveForm(workCollectAccessoryDTO);
        return ResponseEntity.ok("操作成功");
    }


    /**
     * 更新资料库
     */
    @ApiOperation(value = "更新资料库")
    @PostMapping(value = "/updateById")
    public ResponseEntity updateById(@RequestBody WorkCollectAccessory workCollectAccessory) throws ParseException {
        workCollectAccessoryService.updateById(workCollectAccessory);
        return ResponseEntity.ok("操作成功");
    }

    /**
     * 根据id查询当前附件信息
     */
    @ApiOperation(value = "根据id查询当前附件信息")
    @GetMapping(value = "/queryById")
    public ResponseEntity queryById( @RequestParam("id")String id) throws ParseException {
        WorkCollectAccessory workCollectAccessory = workCollectAccessoryService.queryById(id);
        return ResponseEntity.ok(workCollectAccessory);
    }

    /**
     * 删除附件
     */
    @ApiOperation(value = "删除附件")
    @PostMapping(value = "/del")
    public ResponseEntity delAttachment(@RequestBody WorkCollectAccessoryDTO workCollectAccessoryDTO) throws ParseException {
        workCollectAccessoryService.delAttachment(workCollectAccessoryDTO.getAttachmentId());
        return ResponseEntity.ok("操作成功");
    }

    /**
     * 根据字典值类型获取字典列表
     */
    @ApiOperation(value = "根据字典值类型获取字典列表")
    @GetMapping(value = "/queryDictListByName")
    public ResponseEntity queryDictListByName( @RequestParam("dictName")String dictName) throws ParseException {
        List<DictValue> dictList = workCollectAccessoryService.getDictValueByName(dictName);
        return ResponseEntity.ok(dictList);
    }

    /**
     * 根据分类去重查询
     */
    @ApiOperation(value = "根据分类去重查询")
    @GetMapping(value = "/getClassificationList")
    public ResponseEntity getClassificationList(WorkCollectAccessory workCollectAccessory ) throws Exception {
        List<WorkCollectAccessory> classificationList = workCollectAccessoryService.getClassificationList(workCollectAccessory);
        return ResponseEntity.ok(classificationList);
    }

}
