package com.goldenwater.web.controller.pms;

import com.goldenwater.slgc.entity.pms.PmsAccessory;
import com.goldenwater.slgc.service.pms.IPmsAccessoryService;
import com.goldenwater.common.core.controller.BaseController;
import com.goldenwater.common.core.domain.AjaxResult;
import com.goldenwater.common.core.page.TableDataInfo;
import com.goldenwater.common.config.RuoYiConfig;
import com.goldenwater.common.utils.file.FileUtils;
import com.goldenwater.common.utils.file.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.goldenwater.common.annotation.Log;
import com.goldenwater.common.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@Validated
@RequestMapping("/pms/accessory")
public class PmsAccessoryController extends BaseController {
    @Autowired
    private IPmsAccessoryService pmsAccessoryService;

    @GetMapping("/list")
    public TableDataInfo list(PmsAccessory accessory) {
        startPage();
        List<PmsAccessory> list = pmsAccessoryService.selectList(accessory);
        return getDataTable(list);
    }

    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(pmsAccessoryService.selectById(id));
    }

    @Log(title = "附件管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmsAccessory accessory) {
        return toAjax(pmsAccessoryService.insert(accessory));
    }

    @Log(title = "附件管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmsAccessory accessory) {
        return toAjax(pmsAccessoryService.update(accessory));
    }

    @Log(title = "附件管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pmsAccessoryService.deleteByIds(ids));
    }

    @GetMapping("/tree")
    public AjaxResult tree(@RequestParam(required = false) String roleType) {
        List<PmsAccessory> list = pmsAccessoryService.selectFolderAll(0L, roleType);
        return success(list);
    }

    @PutMapping("/move")
    public AjaxResult move(@RequestBody PmsAccessory accessory) {
        return toAjax(pmsAccessoryService.update(accessory));
    }

    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file) {
        try {
            String url = FileUploadUtils.upload(RuoYiConfig.getProfile(), file);
            String absPath = RuoYiConfig.getProfile() + "/" + url.replace("/profile/", "");
            PmsAccessory accessory = new PmsAccessory();
            accessory.setName(file.getOriginalFilename());
            accessory.setFileType(FileUploadUtils.getExtension(file));
            accessory.setFileSize(file.getSize());
            accessory.setFilePath(absPath);
            pmsAccessoryService.insert(accessory);
            return success(url);
        } catch (IOException e) {
            return error(e.getMessage());
        }
    }

    @Log(title = "附件管理", businessType = BusinessType.UPDATE)
    @PutMapping("/seq")
    public AjaxResult updateSeq(@RequestBody PmsAccessory accessory) {
        return toAjax(pmsAccessoryService.updateSeq(accessory.getId(), accessory.getSeq()));
    }

    @Log(title = "附件管理", businessType = BusinessType.UPDATE)
    @PutMapping("/swapSeq")
    public AjaxResult swapSeq(@RequestParam Long id1, @RequestParam Long id2) {
        return toAjax(pmsAccessoryService.swapSeq(id1, id2));
    }

    @GetMapping("/download/{id}")
    public void download(@PathVariable Long id, HttpServletResponse response) {
        PmsAccessory accessory = pmsAccessoryService.selectById(id);
        if (accessory == null || accessory.getFilePath() == null) {
            return;
        }
        try {
            FileUtils.setAttachmentResponseHeader(response, accessory.getName());
            response.setContentType("application/octet-stream");
            FileUtils.writeBytes(accessory.getFilePath(), response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
