package com.steerinfo.ems.ifmesemsswapfile.controller;

import com.steerinfo.auth.utils.JwtUtil;
import com.steerinfo.ems.Utils.DateUtils;
import com.steerinfo.ems.ifmesemsswapfile.model.IfMesEmsSwapfile;
import com.steerinfo.ems.ifmesemsswapfile.service.IIfMesEmsSwapfileService;
import com.steerinfo.ems.trmworkprocmaterialvalue.service.ITRmWorkprocMaterialValueService;
import com.steerinfo.ems.trmworkprocproductvalue.service.ITRmWorkprocProductValueService;
import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageList;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/${api.version}/ifmesemsswapfile")
public class IfMesEmsSwapfileController extends BaseRESTfulController {

    @Autowired
    IIfMesEmsSwapfileService ifMesEmsSwapfileService;

    @ApiOperation(value="获取列表", notes="分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("trmunit:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<IfMesEmsSwapfile> list = ifMesEmsSwapfileService.queryForPage(parmas, pageNum, pageSize);
        return success(list);
    }
    
    @ApiOperation(value="获取列表", notes="分页模糊查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("tpmcaseact:view")
    @GetMapping(value = "/like/")
    public RESTfulResult listLike(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<IfMesEmsSwapfile> list = ifMesEmsSwapfileService.queryLikeForPage(parmas, pageNum, pageSize);
        return success(list);
    }


    @ApiOperation(value="立即从MES同步数据", notes="从MES同步投入产出数据")
    @PostMapping(value = "/synchronousMesAnEmsData")
    public RESTfulResult synchronousMesAnEmsData(@ModelAttribute IfMesEmsSwapfile model){
        String s = ifMesEmsSwapfileService.synchronousMesAnEmsData();
        return success(s);
    }

    @GetMapping("/getLgDateProd")
    public RESTfulResult getLgDateProd (){
        ifMesEmsSwapfileService.getLgDateProd();
        return success();
    }

    @ApiOperation(value="获取列表", notes="分页模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("tpmcaseact:view")
    @GetMapping(value = "/queryList/")
    public RESTfulResult queryList(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        if(parmas.containsKey("workprocid") && parmas.get("workprocid") != null && !"".equals(parmas.get("workprocid").toString())){
            PageList<Map<String, Object>> list = ifMesEmsSwapfileService.getQueryList(parmas, pageNum, pageSize);
            return success(list);
        }
        return failed(null, "菜单工序id为空，请联系管理员");
    }

    @ApiOperation(value="获取列表", notes="分页模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    @GetMapping(value = "/queryProductAndMaterial/")
    public RESTfulResult queryProductAndMaterial(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        if(parmas.containsKey("workprocid") && parmas.get("workprocid") != null && !"".equals(parmas.get("workprocid").toString())){
            if(parmas.containsKey("type") && parmas.get("type") != null && !"".equals(parmas.get("type").toString())) {
                Object type = parmas.get("type");
                if ("1".equals(type)) {
                    parmas.put("table_name","T_RM_WORKPROC_MATERIAL");
                    parmas.put("table_name2","T_RM_WORKPROC_MATERIAL_VALUE");
                    parmas.put("listing","MATERIALID");
                } else if ("2".equals(type)) {
                    parmas.put("table_name","T_RM_WORKPROC_PRODUCT");
                    parmas.put("table_name2","T_RM_WORKPROC_PRODUCT_VALUE");
                    parmas.put("listing","PRODUCTID");
                } else {
                    return failed(null, "数据类型有误");
                }
                PageList<Map<String, Object>> list = ifMesEmsSwapfileService.queryProductAndMaterial(parmas, pageNum, pageSize);
                return success(list);
            }
            return failed(null,"请选择数据类型");
        }
        return failed(null, "菜单工序id为空，请联系管理员");
    }

    @ApiOperation(value="批量更新详细信息", notes="根据传过来的parmas数组信息来更新详细信息")
    @ApiImplicitParam(name = "parmas", value = "详细实体usageData", required = true, dataType = "usageData")
    @PutMapping(value = "/updateProductAndMaterial", produces  = "application/json;charset=UTF-8")
    public RESTfulResult updateProductAndMaterial(@RequestBody HashMap[] parmas) {

        for (int i = 0; i < parmas.length; i++) {
            HashMap parma = parmas[i];
            Object type = parma.get("type");
            if (parma.containsKey("type") && parma.get("type") != null && !"".equals(parma.get("type").toString())) {
                if ("1".equals(type)) {
                    parma.put("table_name", "T_RM_WORKPROC_MATERIAL_VALUE");
                } else if ("2".equals(type)) {
                    parma.put("table_name", "T_RM_WORKPROC_PRODUCT_VALUE");
                } else {
                    return failed(null, "数据类型有误");
                }
                if (!parma.containsKey("itemid") || parma.get("itemid") == null || "".equals(parma.get("itemid").toString())) {
                    return failed(null, "缺少参数itemid");
                }
                if (!parma.containsKey("clock") || parma.get("clock") == null || "".equals(parma.get("clock").toString())) {
                    return failed(null, "缺少参数clock");
                }
                if (!parma.containsKey("timegranid") || parma.get("timegranid") == null || "".equals(parma.get("timegranid").toString())) {
                    return failed(null, "缺少参数timegranid");
                }
                if (!parma.containsKey("actualvalue") || parma.get("actualvalue") == null || "".equals(parma.get("actualvalue").toString())) {
                    return failed(null, "缺少参数actualvalue");
                }
                if (!parma.containsKey("correctvalue") || parma.get("correctvalue") == null || "".equals(parma.get("correctvalue").toString())) {
                    return failed(null, "缺少参数correctvalue");
                }
                if (!parma.containsKey("apportvalue") || parma.get("apportvalue") == null || "".equals(parma.get("apportvalue").toString())) {
                    return failed(null, "缺少参数apportvalue");
                }
                parma.put("upman", JwtUtil.getUseridByToken());
                parma.put("uptime", DateUtils.getCurrentTimetoDate("yyyy-MM-dd HH:mm:ss"));
                ifMesEmsSwapfileService.updateProductAndMaterial(parma);
            }
        }
        return success();
    }
    @Autowired
    ITRmWorkprocProductValueService tRmWorkprocProductValueService;

    @Autowired
    ITRmWorkprocMaterialValueService tRmWorkprocMaterialValueService;

    @ApiOperation(value="从T_RM_PRODUCT,T_RM_MATERIAL生成模板数据", notes="从生成投入产出数据模板")
    @GetMapping(value = "/generateProductAndMaterial")
    public RESTfulResult generateProductAndMaterial(@ModelAttribute IfMesEmsSwapfile model){
        String s = ifMesEmsSwapfileService.generateProductAndMaterial();
        tRmWorkprocProductValueService.synchronousData();
        tRmWorkprocMaterialValueService.synchronousData();
        return success(s);
    }
}
