package com.foreverwin.mesnac.dispatch.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.common.erp.ZprodordconfStruIn;
import com.foreverwin.mesnac.common.erp.ZprodordconfStruOut;
import com.foreverwin.mesnac.common.erp.holders.TableOfZprodordconfStruInHolder;
import com.foreverwin.mesnac.common.erp.holders.TableOfZprodordconfStruOutHolder;
import com.foreverwin.mesnac.common.util.ERPAPI;
import com.foreverwin.mesnac.dispatch.model.ItemBatch;
import com.foreverwin.mesnac.dispatch.service.ItemBatchService;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.FrontPage;
import com.foreverwin.modular.core.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 *
 * @author Leon.L
 * @since 2021-06-18
 */
@RestController
@RequestMapping("/Z-ITEM-BATCH")
public class ItemBatchController {

    @Autowired
    public ItemBatchService itemBatchService;

    @ResponseBody
    @GetMapping("/getItemBatch")
    public R getItemBatch(ItemBatch itemBatch){
        QueryWrapper<ItemBatch> queryWrapper=new QueryWrapper<>();
        itemBatch.setSite(CommonMethods.getSite());
        queryWrapper.setEntity(itemBatch);
        return R.ok(itemBatchService.list(queryWrapper));
    }
    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/getItemBatchPageList")
    public R getItemBatchPageList(FrontPage<ItemBatch> frontPage, ItemBatch itemBatch){
        IPage result;
        try {
            String site = CommonMethods.getSite();
            itemBatch.setSite(site);

            QueryWrapper<ItemBatch> queryWrapper = new QueryWrapper<>();
            queryWrapper.setEntity(itemBatch);
            if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
                queryWrapper
                        .like(ItemBatch.ITEM, frontPage.getGlobalQuery())
                        .or().like(ItemBatch.BATCH, frontPage.getGlobalQuery())
                        .or().like(ItemBatch.LABEL, frontPage.getGlobalQuery())
                        .or().like(ItemBatch.SUPPLIER, frontPage.getGlobalQuery());
            }

            result = itemBatchService.page(frontPage.getPagePlus(), queryWrapper);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }

    @GetMapping("/testErpJK")
    public R testErpJK (String shopOrder, String stepId) {

        try {
            //请求参数
            ZprodordconfStruIn[] ins = new ZprodordconfStruIn[1];

            ZprodordconfStruIn struIn = new ZprodordconfStruIn();
            //生产订单
            struIn.setAufnr(shopOrder);
            //步骤标示
            struIn.setVornr(stepId);
            struIn.setAueru("1");
            //良品数量
            struIn.setLmnga(new BigDecimal(10));
            //报废数量
            struIn.setXmnga(new BigDecimal(1));
            //机器工时
            struIn.setIsm01(new BigDecimal(1));
            //人工工时
            struIn.setIsm02(new BigDecimal(1));
            struIn.setIsm03(new BigDecimal(1));
            struIn.setIsm04(new BigDecimal(1));
            struIn.setIsm05(new BigDecimal(1));
            struIn.setIsm06(new BigDecimal(1));
            ins[0] = struIn;
            TableOfZprodordconfStruInHolder inHolder = new TableOfZprodordconfStruInHolder(ins);

            //返回对象
            ZprodordconfStruOut[] outs = new ZprodordconfStruOut[1];

            ZprodordconfStruOut struOut = new ZprodordconfStruOut();
            struOut.setAufnr(shopOrder);
            struOut.setVornr(stepId);
            struOut.setAueru("1");
            struOut.setLmnga(new BigDecimal(1));
            struOut.setRet("");
            struOut.setMsg("");
            outs[0] = struOut;
            TableOfZprodordconfStruOutHolder outHolder = new TableOfZprodordconfStruOutHolder(outs);

            //调用WS
            ERPAPI.erpWebService().zmesProdordconf(inHolder, outHolder);
            //状态： S|成功， E|失败
            String status = outHolder.value[1].getRet();
            String message = outHolder.value[1].getMsg();

            return R.failed("ERP接口返回：状态:" + status + ",消息:" + message);
        } catch (Exception e) {
            return R.ok(e.getMessage());
        }
    }
}