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.meapi.dto.SfcDto;
import com.foreverwin.mesnac.production.model.SfcDataAssemble;
import com.foreverwin.mesnac.production.service.SfcDataAssembleService;
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-19
 */
@RestController
@RequestMapping("/Z-SFC-DATA-ASSEMBLE")
public class SfcDataAssembleController {

    @Autowired
    public SfcDataAssembleService sfcDataAssembleService;


    /**
     * sfc回车
     * @param sfcDto
     * @return
     */
    @ResponseBody
    @GetMapping("/loadSfcAssemble")
    public R loadSfcAssemble(SfcDto sfcDto) {
        return R.ok(sfcDataAssembleService.loadSfcAssemble(sfcDto));
    }
    @ResponseBody
    @PostMapping("/assembleInventory")
    public R loadSfcAssemble(@RequestBody Map<String, Object> paramMap) {
        return R.ok(sfcDataAssembleService.assembleInventory(paramMap));
    }
    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public R getSfcDataAssembleById(@PathVariable String id) {
        return R.ok( sfcDataAssembleService.getById(id));
    }

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<SfcDataAssemble> frontPage, SfcDataAssemble sfcDataAssemble){
        IPage result;
        QueryWrapper<SfcDataAssemble> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(sfcDataAssemble);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(SfcDataAssemble::getHandle, frontPage.getGlobalQuery())
                .or().like(SfcDataAssemble::getSite, frontPage.getGlobalQuery())
                .or().like(SfcDataAssemble::getSfcDispatchBo, frontPage.getGlobalQuery())
                .or().like(SfcDataAssemble::getStatus, frontPage.getGlobalQuery())
                .or().like(SfcDataAssemble::getInventoryBo, frontPage.getGlobalQuery())
                .or().like(SfcDataAssemble::getItemBo, frontPage.getGlobalQuery())
                .or().like(SfcDataAssemble::getCreatedUser, frontPage.getGlobalQuery())
                .or().like(SfcDataAssemble::getModifyUser, frontPage.getGlobalQuery())
    );
        }
        result = sfcDataAssembleService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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