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.dto.WorkShopTransferDto;
import com.foreverwin.mesnac.production.mapper.WorkShopTransferMapper;
import com.foreverwin.mesnac.production.model.WorkShopTransfer;
import com.foreverwin.mesnac.production.service.WorkShopTransferService;
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 Philip
 * @since 2021-08-23
 */
@RestController
@RequestMapping("/Z-WORK-SHOP-TRANSFER")
public class WorkShopTransferController {

    @Autowired
    public WorkShopTransferService workShopTransferService;
    @Autowired
    public WorkShopTransferMapper workShopTransferMapper;
    /**
     * 查询转出数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("getOutList")
    public R getOutList(WorkShopTransferDto workShopTransferDto){
        List<WorkShopTransferDto> result;
        result = workShopTransferService.getOutList(workShopTransferDto);
        return R.ok(result);
    }


    /**
     * 查询转入数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("getInList")
    public R getInList(WorkShopTransferDto workShopTransferDto){
        List<WorkShopTransferDto> result;
        result = workShopTransferService.getInList(workShopTransferDto);
        return R.ok(result);
    }
    /**
     * 转出
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/transferOut")
    public R getList(String sfcs){
        return R.ok( workShopTransferService.transferOut(sfcs));
    }

    /**
     * 转入
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/transferIn")
    public R transferIn(@RequestBody List<String> handleList){
        workShopTransferService.transferIn(handleList);
        return R.ok();
    }
    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<WorkShopTransfer> frontPage, WorkShopTransfer workShopTransfer){
        IPage result;
        QueryWrapper<WorkShopTransfer> queryWrapper = new QueryWrapper<>();
        workShopTransfer.setSite(CommonMethods.getSite());
        queryWrapper.setEntity(workShopTransfer);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                    .like(WorkShopTransfer::getTransferNo, frontPage.getGlobalQuery())
            );
        }
        result = workShopTransferService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

    /**
     */
    @ResponseBody
    @GetMapping("/transferNo")
    public R getTransferNo(WorkShopTransfer workShopTransfer){
        List result;
        QueryWrapper<WorkShopTransfer> queryWrapper = new QueryWrapper<>();
        workShopTransfer.setSite(CommonMethods.getSite());
        queryWrapper.setEntity(workShopTransfer);
        result = workShopTransferMapper.getTransferNo(queryWrapper);
        return R.ok(result);
    }

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

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

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