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.AccessoryLog;
import com.foreverwin.mesnac.production.service.AccessoryLogService;
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 Philip
 * @since 2021-08-12
 */
@RestController
@RequestMapping("/Z-ACCESSORY-LOG")
public class AccessoryLogController {

    @Autowired
    public AccessoryLogService accessoryLogService;


    /**
     * 查询资源上料记录
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/loadList")
    public R getLoadList(AccessoryLog accessoryLog){
        List<AccessoryLog> result;
        result = accessoryLogService.getLoadLabelList(accessoryLog);
        return R.ok(result);
    }

    /**
     * 上料
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/loadConfirm")
    public R loadInventory(AccessoryLog accessoryLog){
        accessoryLogService.loadConfirm(accessoryLog);
        return R.ok("上料成功");
    }

    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public R getAccessoryLogById(@PathVariable String id) {
        return R.ok( accessoryLogService.getById(id));
    }

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

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

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

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

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public R removeById(@PathVariable("id") String id){
        return R.ok(accessoryLogService.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(accessoryLogService.removeByIds(ids));
    }
}