package com.bonus.zlpt.company.controller;
import com.bonus.zlpt.common.core.domain.equip.TypeInfo;
import com.bonus.zlpt.common.core.web.controller.BaseController;
import com.bonus.zlpt.common.core.web.domain.AjaxResult;
import com.bonus.zlpt.common.core.web.page.TableDataInfo;
import com.bonus.zlpt.company.domain.MaUpOff;
import com.bonus.zlpt.company.mapper.MaUpOffMapper;
import com.bonus.zlpt.company.service.impl.MaUpOffServiceImpl;
import com.bonus.zlpt.home.service.MaTypeInfoSevice;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

/**
 * 装备上下架管理
 (ma_up_off)表控制层
 * @author 阮世耀
*/
@RestController
@RequestMapping("/up_off")
public class MaUpOffController extends BaseController {

    /**
    * 服务对象
    */
    @Resource
    private MaUpOffServiceImpl maUpOffServiceImpl;

    @Resource
    private MaUpOffMapper maUpOffMapper;

    @Resource
    private MaTypeInfoSevice maTypeInfoSevice;

    /**
     * 获取所有数据
     * @param record 筛选条件
     */
    @PostMapping("getEquipmentOnShelfReviewList")
    public AjaxResult getEquipmentOnShelfReviewList(@RequestBody MaUpOff record) {
        try {
            return AjaxResult.success(maUpOffMapper.selectAll(record));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 装备上架审批
     * @param record 筛选条件
     */
    @PostMapping("checkEquipmentOnShelfInfo")
    public AjaxResult checkEquipmentOnShelfInfo(@RequestBody MaUpOff record) {
        try {
            return AjaxResult.success(maUpOffMapper.updateByPrimaryKeySelective(record));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 通过id下架装备
     */
    @PostMapping("withdrawEquipment")
    public AjaxResult withdrawEquipment(@RequestParam Integer equipmentID) {
        try {
            MaUpOff obj = new MaUpOff();
            obj.setId(equipmentID);
            obj.setType("2");
            return AjaxResult.success(maUpOffMapper.updateByPrimaryKeySelective(obj));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     * 批量上架设备
     */
    @PostMapping("batchUpOff")
    public AjaxResult batchUpOff(@RequestBody List<Integer> ids) {
        try {
            return AjaxResult.success(maUpOffMapper.batchUpOff(ids));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 搜索分类
     * @return 装备分类列表
     */
    @PostMapping("/getEquipmentType")
    public TableDataInfo getEquipmentType() {
        List<TypeInfo> list = maTypeInfoSevice.getMaTypeInfoList();
        return getDataTable(list);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("findEquipmentOnShelfInfo")
    public MaUpOff findEquipmentOnShelfInfo(Integer id) {
        return maUpOffServiceImpl.selectByPrimaryKey(id);
    }


    /**
     * 导出设备信息
     * @param record 筛选条件
     * @throws IOException 异常处理
     */
    @PostMapping("expEquipmentOnShelfInfo")
    public ResponseEntity<byte[]> expEquipmentOnShelfInfo(@RequestBody MaUpOff record) throws IOException {
        // 根据条件筛选获取设备信息
        List<MaUpOff> devices = maUpOffMapper.selectAll(record);

        // 创建一个临时文件，用于保存导出的设备信息
        Path tempFile = Files.createTempFile("device-export", ".csv");

        // 打开临时文件并写入设备信息
        try (FileWriter writer = new FileWriter(tempFile.toFile())) {
            writer.write("Device ID,Device Name,Device Type\n");
            for (MaUpOff device : devices) {
                writer.write(device.getId() + "," + device.getMaId() + "," + device.getApplyCompany() + "\n");
            }
        }

        // 将临时文件转换为字节数组，并设置响应头
        byte[] fileContent = Files.readAllBytes(tempFile);
        ResponseEntity<byte[]> response = ResponseEntity.ok()
                .header("Content-Disposition", "attachment; filename=device-export.csv")
                .header("Content-Type", "text/csv")
                .header("Content-Length", String.valueOf(fileContent.length))
                .body(fileContent);

        // 删除临时文件
        Files.deleteIfExists(tempFile);

        return response;
    }


}
