package com.foreverwin.mesnac.meapi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.meapi.model.DataField;
import com.foreverwin.mesnac.meapi.model.DataFieldList;
import com.foreverwin.mesnac.meapi.service.DataFieldListService;
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 robert
 * @since 2021-06-29
 */
@RestController
@RequestMapping("/DATA-FIELD-LIST")
public class DataFieldListController {

    @Autowired
    public DataFieldListService dataFieldListService;

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<DataFieldList> frontPage, DataFieldList dataFieldList){
        IPage result;
        QueryWrapper<DataFieldList> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(dataFieldList);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(DataFieldList::getHandle, frontPage.getGlobalQuery())
                .or().like(DataFieldList::getDataFieldBo, frontPage.getGlobalQuery())
                .or().like(DataFieldList::getDataValue, frontPage.getGlobalQuery())
                .or().like(DataFieldList::getIsDefault, frontPage.getGlobalQuery())
                .or().like(DataFieldList::getValuation, frontPage.getGlobalQuery())
                .or().like(DataFieldList::getErpCodeGroup, frontPage.getGlobalQuery())
    );
        }
        result = dataFieldListService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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

    @GetMapping("findDataFieldList")
    public R findDataFieldList(DataField dataField){
        return R.ok(dataFieldListService.findDataFieldList(dataField));
    }
}