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.WorkCenter;
import com.foreverwin.mesnac.meapi.service.WorkCenterService;
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.*;

import java.util.List;

/**
 *
 * @author Leon.L
 * @since 2021-05-26
 */
@RestController
@RequestMapping("/WORK-CENTER")
public class WorkCenterController {

    @Autowired
    public WorkCenterService workCenterService;

    /**
     * 查询车间
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/getWorkShopList")
    public R getWorkShopList(){
        List<WorkCenter> result=workCenterService.getWorkShopList();
        return R.ok(result);
    }
    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R getWorkCenterList(WorkCenter workCenter){
        List<WorkCenter> result;
        try {
            String site = CommonMethods.getSite();
            workCenter.setSite(site);
            QueryWrapper<WorkCenter> queryWrapper = new QueryWrapper<>();
            queryWrapper.setEntity(workCenter);
            result = workCenterService.list(queryWrapper);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
        return R.ok(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<WorkCenter> frontPage, WorkCenter workCenter){
        IPage result;
        try {
            workCenter.setSite(CommonMethods.getSite());
            QueryWrapper<WorkCenter> queryWrapper = new QueryWrapper<>();
            queryWrapper.setEntity(workCenter);
            if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
                queryWrapper
                        .like(WorkCenter.WORK_CENTER, frontPage.getGlobalQuery())
                        .or().like(WorkCenter.ROUTER_BO, frontPage.getGlobalQuery())
                        .or().like(WorkCenter.CAN_BE_RELEASED_TO, frontPage.getGlobalQuery())
                        .or().like(WorkCenter.WC_CATEGORY, frontPage.getGlobalQuery())
                        .or().like(WorkCenter.STATUS_BO, frontPage.getGlobalQuery())
                        .or().like(WorkCenter.WC_TYPE, frontPage.getGlobalQuery())
                        .or().like("DESCRIPTION", frontPage.getGlobalQuery());
            }
            result = workCenterService.page(frontPage.getPagePlus(), queryWrapper);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }

    /**
     * 新增
     * @param workCenter  传递的实体
     * @return  null 失败  实体成功
     */
    @PostMapping
    public R save(@RequestBody WorkCenter workCenter) {

        return R.ok(workCenterService.save(workCenter));
    }

    /**
     * 修改
     * @param workCenter  传递的实体
     * @return  null 失败  实体成功
     */
    @PutMapping
    public R updateById(@RequestBody WorkCenter workCenter) {

        return R.ok(workCenterService.updateById(workCenter));
    }
}