package com.hw.mes.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.hw.common.core.domain.R;
import com.hw.common.core.utils.StringUtils;
import com.hw.common.core.utils.uuid.UUID;
import com.hw.system.api.RemoteFileService;
import com.hw.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import com.hw.common.log.annotation.Log;
import com.hw.common.log.enums.BusinessType;
import com.hw.common.security.annotation.RequiresPermissions;
import com.hw.mes.domain.MesBaseAttachInfo;
import com.hw.mes.service.IMesBaseAttachInfoService;
import com.hw.common.core.web.controller.BaseController;
import com.hw.common.core.web.domain.AjaxResult;
import com.hw.common.core.utils.poi.ExcelUtil;
import com.hw.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 附件信息Controller
 * 
 * @author Yinq
 * @date 2024-01-26
 */
@RestController
@RequestMapping("/baseAttachInfo")
public class MesBaseAttachInfoController extends BaseController
{
    @Autowired
    private IMesBaseAttachInfoService mesBaseAttachInfoService;

    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 查询附件信息列表
     */
    @RequiresPermissions("mes:baseAttachInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(MesBaseAttachInfo mesBaseAttachInfo)
    {
        startPage();
        List<MesBaseAttachInfo> list = mesBaseAttachInfoService.selectMesBaseAttachInfoList(mesBaseAttachInfo);
        return getDataTable(list);
    }

    /**
     * 导出附件信息列表
     */
    @RequiresPermissions("mes:baseAttachInfo:export")
    @Log(title = "附件信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesBaseAttachInfo mesBaseAttachInfo)
    {
        List<MesBaseAttachInfo> list = mesBaseAttachInfoService.selectMesBaseAttachInfoList(mesBaseAttachInfo);
        ExcelUtil<MesBaseAttachInfo> util = new ExcelUtil<MesBaseAttachInfo>(MesBaseAttachInfo.class);
        util.exportExcel(response, list, "附件信息数据");
    }

    /**
     * 获取附件信息详细信息
     */
    @RequiresPermissions("mes:baseAttachInfo:query")
    @GetMapping(value = "/{attachId}")
    public AjaxResult getInfo(@PathVariable("attachId") Long attachId)
    {
        return success(mesBaseAttachInfoService.selectMesBaseAttachInfoByAttachId(attachId));
    }

    /**
     * 新增附件信息
     */
    @RequiresPermissions("mes:baseAttachInfo:add")
    @Log(title = "附件信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesBaseAttachInfo mesBaseAttachInfo)
    {
        return toAjax(mesBaseAttachInfoService.insertMesBaseAttachInfo(mesBaseAttachInfo));
    }

    /**
     * 修改附件信息
     */
    @RequiresPermissions("mes:baseAttachInfo:edit")
    @Log(title = "附件信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesBaseAttachInfo mesBaseAttachInfo)
    {
        return toAjax(mesBaseAttachInfoService.updateMesBaseAttachInfo(mesBaseAttachInfo));
    }

    /**
     * 删除附件信息
     */
    @RequiresPermissions("mes:baseAttachInfo:remove")
    @Log(title = "附件信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{attachIds}")
    public AjaxResult remove(@PathVariable Long[] attachIds)
    {
        return toAjax(mesBaseAttachInfoService.deleteMesBaseAttachInfoByAttachIds(attachIds));
    }

    /**
     * MES附件上传
     * @param file 附件
     * @param processId 工序
     * @param attachType 附件类型
     * @return
     */
    @PostMapping(value = "/drawingFileUpload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AjaxResult drawingFileUpload(@RequestPart(value = "file") MultipartFile file, Long processId, String attachType){
        if (!file.isEmpty())
        {
            R<SysFile> fileResult = null;
            try {
                fileResult = remoteFileService.upload(file);
            } catch (Exception e) {
                return error("文件服务异常，请联系管理员");
            }
            String url = fileResult.getData().getUrl();
            String name = fileResult.getData().getName();
            MesBaseAttachInfo info = new MesBaseAttachInfo();
            info.setAttachCode(UUID.fastUUID().toString());
            info.setAttachName(name);
            info.setAttachType(attachType);
            // String fixedString = "/statics";
            // url = url.substring(url.indexOf(fixedString) + fixedString.length());
            info.setAttachPath(url);
            info.setProcessId(processId);
            int attachId = mesBaseAttachInfoService.insertMesBaseAttachInfo(info);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", url);
            ajax.put("attachId", attachId);
            ajax.put("fileName", name);
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }

}
