package com.foreverwin.mesnac.production.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.production.model.CutterLog;
import com.foreverwin.mesnac.production.service.CutterLogService;
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;
import java.util.Map;

/**
 *
 * @author Philip
 * @since 2021-07-14
 */
@RestController
@RequestMapping("/Z-CUTTER-LOG")
public class CutterLogController {

    @Autowired
    public CutterLogService cutterLogService;
    /**
     * 添加
     * @return
     */
    @ResponseBody
    @PostMapping("/doAdd")
    public R doAdd(@RequestBody Map<String,Object> map){
        cutterLogService.doAdd(map);
        return R.ok();
    }
    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public R getCutterLogById(@PathVariable String id) {
        return R.ok( cutterLogService.getById(id));
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R getCutterLogList(CutterLog cutterLog){
        List<CutterLog> result;
        QueryWrapper<CutterLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(cutterLog);
        result = cutterLogService.list(queryWrapper);
        return R.ok(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<CutterLog> frontPage, CutterLog cutterLog){
        IPage result;
        QueryWrapper<CutterLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(cutterLog);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(CutterLog::getHandle, frontPage.getGlobalQuery())
                .or().like(CutterLog::getSite, frontPage.getGlobalQuery())
                .or().like(CutterLog::getCutter, frontPage.getGlobalQuery())
                .or().like(CutterLog::getShopOrder, frontPage.getGlobalQuery())
                .or().like(CutterLog::getWorkCenter, frontPage.getGlobalQuery())
                .or().like(CutterLog::getSfc, frontPage.getGlobalQuery())
                .or().like(CutterLog::getType, frontPage.getGlobalQuery())
                .or().like(CutterLog::getOperation, frontPage.getGlobalQuery())
                .or().like(CutterLog::getStepId, frontPage.getGlobalQuery())
                .or().like(CutterLog::getItem, frontPage.getGlobalQuery())
                .or().like(CutterLog::getResrce, frontPage.getGlobalQuery())
                .or().like(CutterLog::getCreateUser, frontPage.getGlobalQuery())
    );
        }
        result = cutterLogService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

    /**
     * 新增
     * @param cutterLog  传递的实体
     * @return  null 失败  实体成功
     */
    @PostMapping
    public R save(@RequestBody CutterLog cutterLog) {
        return R.ok(cutterLogService.save(cutterLog));
    }

    /**
     * 修改
     * @param cutterLog  传递的实体
     * @return  null 失败  实体成功
     */
    @PutMapping
    public R updateById(@RequestBody CutterLog cutterLog) {
        return R.ok(cutterLogService.updateById(cutterLog));
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public R removeById(@PathVariable("id") String id){
        return R.ok(cutterLogService.removeById(id));
    }

    /**
     * 批量删除对象
     * @param ids 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/delete-batch")
    public R removeByIds(List<String> ids){
        return R.ok(cutterLogService.removeByIds(ids));
    }

    /**
     * 根据sfc和stepId查询刀具使用信息
     * @return
     */
    @ResponseBody
    @PostMapping("/findToolInfoBySfc")
    public R findToolInfoBySfc(@RequestBody CutterLog cutterLog){
        List<CutterLog> result;
        try{
            result = cutterLogService.findToolInfoBySfc(cutterLog);
            return R.ok(result);
        }catch (Exception e){
            return R.failed(e.getMessage());
        }
    }


}