package com.foreverwin.mesnac.meapi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.meapi.model.ShopOrder;
import com.foreverwin.mesnac.meapi.service.ShopOrderService;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.FrontPage;
import com.foreverwin.modular.core.util.R;
import com.sap.me.status.StatusBOHandle;
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;

/**
 *
 * @author Leon.L
 * @since 2021-06-01
 */
@RestController
@RequestMapping("/SHOP-ORDER")
public class ShopOrderController {

    @Autowired
    public ShopOrderService shopOrderService;


    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/getShopOrderPageList")
    public R getShopOrderPageList(FrontPage<ShopOrder> frontPage, ShopOrder shopOrder){
        IPage result;
        try {
            String site = CommonMethods.getSite();
            //只查询 可下达 状态的
            String status501Bo = new StatusBOHandle(site, "501").getValue();

            shopOrder.setSite(site);
            shopOrder.setStatusBo(status501Bo);
            QueryWrapper<ShopOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.setEntity(shopOrder);
            if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
                queryWrapper
                        .like(ShopOrder.SHOP_ORDER, frontPage.getGlobalQuery())
                        .or().like(ShopOrder.STATUS_BO, frontPage.getGlobalQuery())
                        .or().like(ShopOrder.SHOP_ORDER_TYPE_BO, frontPage.getGlobalQuery())
                        .or().like(ShopOrder.PLANNED_WORK_CENTER_BO, frontPage.getGlobalQuery())
                        .or().like(ShopOrder.PLANNED_ITEM_BO, frontPage.getGlobalQuery())
                        .or().like(ShopOrder.PLANNED_BOM_BO, frontPage.getGlobalQuery())
                        .or().like(ShopOrder.PLANNED_ROUTER_BO, frontPage.getGlobalQuery())
                        .or().like(ShopOrder.ITEM_BO, frontPage.getGlobalQuery())
                        .or().like(ShopOrder.BOM_BO, frontPage.getGlobalQuery())
                        .or().like(ShopOrder.ROUTER_BO, frontPage.getGlobalQuery())
                        .or().like(ShopOrder.QTY_TO_BUILD, frontPage.getGlobalQuery())
                        .or().like(ShopOrder.CUSTOMER, frontPage.getGlobalQuery())
                        .or().like(ShopOrder.CUSTOMER_ORDER, frontPage.getGlobalQuery());
            }
            result = shopOrderService.page(frontPage.getPagePlus(), queryWrapper);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
        return R.ok(result);
    }

    /**
     * 根据handle找到对应的工单并且该工单是下达的
     * @param shopOrder
     * @param plannedItemBo
     * @return
     */
    @GetMapping("/findById")
    public R findById(String shopOrder,String plannedItemBo){
        return R.ok(shopOrderService.findById(shopOrder,plannedItemBo));
    }

    /**
     * 根据工单找出工作令号，项目号，产品类别
     */
    @GetMapping("/findByShopOrderBo")
    public R findByShopOrderBo(String shopOrder){
      return R.ok(shopOrderService.findByShopOrderBo(shopOrder));
    }

    /**
     * 查出只有异常的工单
     */
    @GetMapping("/findAbnormalShopOrder")
    public R findAbnormalShopOrder(ShopOrder shopOrder){
        return R.ok(shopOrderService.findAbnormalShopOrder(shopOrder));
    }


}










