package com.malk.kuaikeli.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.malk.kuaikeli.entity.SupplierMaterial;
import com.malk.kuaikeli.listener.SupplierMaterialDataListener;
import com.malk.kuaikeli.mapper.SupplierMaterialMapper;
import com.malk.kuaikeli.service.SupplierMaterialService;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.server.aliwork.YDSearch;
import com.malk.server.common.McR;
import com.malk.server.common.Page;
import com.malk.service.aliwork.YDClient;
import com.malk.utils.PublicUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/***
 * 供应商原材料
 */
@RestController
@RequestMapping("/suppliermaterial")
public class SupplierMaterialController {

    @Autowired
    private SupplierMaterialService supplierMaterialService;
    @Autowired
    private YDClient ydClient;


    @PostMapping("/upload")
    public McR upload(@RequestParam(value="file")MultipartFile file) throws IOException {
        if(file.isEmpty()||file.getSize()<1){
            return McR.errorParam("文件不存在！");
        }
        if(!file.getOriginalFilename().endsWith("xlsx")&&!file.getOriginalFilename().endsWith("xls")) {
            return McR.errorParam("文件格式错误！");
        }
        supplierMaterialService.uploadData(file);
        supplierMaterialService.sync();// 异步执行
        return McR.success();
    }

    @GetMapping("/sync")
    public McR sync(){
        supplierMaterialService.sync();
        return McR.success();
    }

    @PostMapping("/projectSupplierChange")
    public McR projectSupplierChange(@RequestBody JSONObject param) throws IOException {
        if(PublicUtil.isNull(param,"projectId","oldId","newId","newName")){
            return McR.errorNullPointer();
        }
        supplierMaterialService.projectSupplierChange(param.getString("projectId"),param.getString("oldId"),param.getString("newId"),param.getString("newName"));
        return McR.success();
    }

    @GetMapping("/getList")
    public McR selectList(@RequestParam(defaultValue = "1") Integer current,
                          @RequestParam(defaultValue = "10") Integer size){
        return supplierMaterialService.getList(current,size);
    }

    @GetMapping("/getYcl")
    public McR getYcl(@RequestParam() String yclId){
        YDSearch ydSearch=new YDSearch("textField_llkb7kd2",yclId,"原材料编号", YDSearch.Type.TEXT_FIELD,YDSearch.Operator.EQ);
        List<Map> list2=(List<Map>) ydClient.queryData(YDParam.builder().formUuid("FORM-E6766M811CKD2PIXEAKOM9S2DKBM2PBQU1MLL5").searchCondition(JSONObject.toJSONString(
                Arrays.asList(ydSearch)
        )).build(), YDConf.FORM_QUERY.retrieve_list).getData();
        return McR.success(list2);
    }


}
