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.SplitSfc;
import com.foreverwin.mesnac.production.service.SplitSfcService;
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-07-20
 */
@RestController
@RequestMapping("/Z-SPLIT-SFC")
public class SplitSfcController {

    @Autowired
    public SplitSfcService splitSfcService;

    @ResponseBody
    @GetMapping("/getSfcData")
    public R getSfcData(SfcDto dto){
        SfcDto result;
        result = splitSfcService.getSfcData(dto);
        return R.ok(result);
    }

    @ResponseBody
    @GetMapping("/getGoodSfcData")
    public R getGoodSfcData(String sfc){
        SfcDto result;
        result = splitSfcService.getGoodSfcData(sfc);
        return R.ok(result);
    }


    @ResponseBody
    @GetMapping("/goodSfcSplit")
    public R goodSfcSplit(SfcDto sfcDto){
        return R.ok(splitSfcService.goodSfcSplit(sfcDto));
    }

    @ResponseBody
    @GetMapping("/splitSfc")
    public R splitSfc(SfcDto sfcDto){
        return R.ok( splitSfcService.splitSfc(sfcDto));
    }

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<SplitSfc> frontPage, SplitSfc splitSfc){
        IPage result;
        QueryWrapper<SplitSfc> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(splitSfc);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(SplitSfc::getHandle, frontPage.getGlobalQuery())
                .or().like(SplitSfc::getSite, frontPage.getGlobalQuery())
                .or().like(SplitSfc::getSfc, frontPage.getGlobalQuery())
                .or().like(SplitSfc::getSplitSfc, frontPage.getGlobalQuery())
                .or().like(SplitSfc::getRouter, frontPage.getGlobalQuery())
                .or().like(SplitSfc::getCreateUser, frontPage.getGlobalQuery())
    );
        }
        result = splitSfcService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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