package com.bonus.sgzb.base.controller;

import com.bonus.sgzb.base.domain.MaPartType;
import com.bonus.sgzb.base.domain.MapType;
import com.bonus.sgzb.base.service.ExcelService;
import com.bonus.sgzb.base.service.IPartTypeService;
import com.bonus.sgzb.common.core.constant.Constants;
import com.bonus.sgzb.common.core.utils.poi.ExcelUtil;
import com.bonus.sgzb.common.core.web.controller.BaseController;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

;

/**
 * 配件类型管理ma_part_type(MaPartType)表控制层
 *
 * @author makejava
 * @since 2023-11-27 16:44:02
 */
@RestController
@RequestMapping("/base/maPartType")
public class MaPartTypeController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private IPartTypeService maPartTypeService;

    @Resource
    private ExcelService excelService;


    /**
     * 查询配件类型列表
     *
     * @param maPartType
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(MaPartType maPartType) {
        List<MaPartType> list = maPartTypeService.selectMaPartList(maPartType);
        return AjaxResult.success(list);
    }

    /**
     * 新增配件管理
     *
     * @param maPartType
     * @return
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody MaPartType maPartType) {
        System.out.println(maPartType);
        if (!maPartTypeService.checkPaNameUnique(maPartType)) {
            return error("新增配件名称'" + maPartType.getPaName() + "'失败，配件名称已存在");
        }
        maPartType.setCreateBy(SecurityUtils.getUsername());
        return toAjax(maPartTypeService.insertMaPart(maPartType));
    }

    /**
     * 导出配件类型管理
     *
     * @param response
     * @param maPartType
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaPartType maPartType) {
        List<MaPartType> list = maPartTypeService.selectMyPartTypeList(maPartType);
        ExcelUtil<MaPartType> util = new ExcelUtil<MaPartType>(MaPartType.class);
        util.exportExcel(response, list, "配件类型管理数据");
    }

    @PostMapping("/readExcel")
    public AjaxResult readExcelFile(@RequestParam("file") MultipartFile file) throws IOException {
        String os = System.getProperty("os.name").toLowerCase();
        boolean isLinux = false;
        if (os.contains("nix") || os.contains("nux") || os.contains("linux")) {
            isLinux = true;
        }
        // 创建上传目录，如果不存在的话
        File dir = null;
        if (isLinux){
            dir = new File(Constants.UPLOAD_DIR);
        }else {
            dir = new File(Constants.UPLOAD_DIR_WIN);
        }

        if (!dir.exists()) {
            dir.mkdirs();
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();
        Path filePath;
        // 定义文件路径
        if (isLinux){
            filePath = Paths.get(Constants.UPLOAD_DIR, fileName);
        }else {
            filePath = Paths.get(Constants.UPLOAD_DIR_WIN, fileName);
        }

        // 保存文件
        file.transferTo(filePath.toFile());
        Map<String, Map<String, List<MapType>>> stringMapMap = excelService.readExcelFile(filePath.toString());
        return AjaxResult.success();
    }

    @PostMapping("/downLoad")
    public void downLoadExcelFile() throws IOException {
        HttpServletResponse resp = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        maPartTypeService.downLoadTemplate(resp);
    }


    /**
     * poi
     * 删除配件管理类型
     *
     * @param paId
     * @return
     */
    @DeleteMapping("/{paId}")
    public AjaxResult delete (@PathVariable("paId") Long paId){
        if (maPartTypeService.hasChildBypaId(paId)) {
            return warn("存在下级仓库列表,不允许删除");
        }
        return toAjax(maPartTypeService.deletePaById(paId));
    }

    /**
     * 根据id获取数据
     *
     * @param paId
     * @return
     */
    @GetMapping("/{paId}")
    public AjaxResult getById (@PathVariable("paId") Long paId){
        MaPartType bean = maPartTypeService.getById(paId);
        return AjaxResult.success(bean);
    }

    /**
     * 根据id修改数据
     *
     * @param maPartType
     * @return
     */
    @PostMapping("/updateById")
    public AjaxResult updateById (@RequestBody MaPartType maPartType){
        maPartType.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(maPartTypeService.updateById(maPartType));
    }


}

