package com.scm.contract.web.management;

import com.scm.contract.model.apply.LabelApplicationDataImport;
import com.scm.contract.model.management.Materiel;
import com.scm.contract.service.management.MaterielService;
import com.scm.contract.tools.HumpLineAdopter;
import com.set.direwolf.core.mybatis.Page;
import com.set.direwolf.core.web.OperationResult;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 物料标签信息
 *
 * @author zuo
 * @date 2024/6/7 11:09
 */

@Controller
@RequestMapping("${adminPath}/materiel/tag")
public class MateriaTaglController {

    private final MaterielService materielService;

    @Autowired
    public MateriaTaglController(MaterielService materielService) {
        this.materielService = materielService;
    }

    @PostMapping("/list")
    public @ResponseBody OperationResult List() {
        return OperationResult.buildSuccessResult("获取物料标签信息成功", materielService.findList(new Materiel()));
    }

    /**
     * 分页查询数据获取
     *
     * @param entity   参数实体
     * @param request  请求
     * @param response 响应
     * @return Page
     */
    @PostMapping("/page")
    public @ResponseBody OperationResult page(@RequestBody Materiel entity, HttpServletRequest request, HttpServletResponse response) {
        Page<Materiel> page = Page.bootstrapPage(request, response);
        List<List<String>> orderList = entity.getOrder();
        String defaultOrder = HumpLineAdopter.humpToLine(orderList);
        if (StringUtils.isNotEmpty(defaultOrder)) {
            page.setOrderBy(defaultOrder);
        }
        return OperationResult.buildSuccessResult("获取物料标签分页查询成功", materielService.findPage(page, entity));
    }


    @ApiOperation(value = "下载excel模版", notes = "下载excel模版", produces = "application/octet-stream")
    @GetMapping(value = "/download/template")
    public @ResponseBody void getTemplate(HttpServletResponse response) {
        try {
            Workbook wb = materielService.getTemplate();
            //设置要导出的文件的名字
            String fileName = "物料标签模版" + ".xlsx";
            //导出数据
            response.setContentType("application/octet-stream");
            //解决跨域文件名问题
            response.setHeader("Access-Control-Expose-Headers", "content-disposition");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            response.flushBuffer();
            wb.write(response.getOutputStream());
        } catch (IOException e) {
            //导出失败
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "数据上传", notes = "数据上传", produces = "multipart/form-data; charset=utf-8")
    @PostMapping("/data/import")
    public @ResponseBody OperationResult importData(LabelApplicationDataImport dataImport) {
        materielService.importData(dataImport);
        return OperationResult.buildSuccessResult("导入成功", true);
    }
}
