package com.foreverwin.mesnac.anomaly.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.anomaly.model.AbnormalNcCode;
import com.foreverwin.mesnac.anomaly.service.AbnormalNcCodeService;
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-07-01
 */
@RestController
@RequestMapping("/Z-ABNORMAL-NC-CODE")
public class AbnormalNcCodeController {

    @Autowired
    public AbnormalNcCodeService abnormalNcCodeService;

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<AbnormalNcCode> frontPage, AbnormalNcCode abnormalNcCode){
        IPage result;
        QueryWrapper<AbnormalNcCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(abnormalNcCode);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(AbnormalNcCode::getHandle, frontPage.getGlobalQuery())
                .or().like(AbnormalNcCode::getSite, frontPage.getGlobalQuery())
                .or().like(AbnormalNcCode::getAbnormalBillBo, frontPage.getGlobalQuery())
                .or().like(AbnormalNcCode::getNcCodeGroup, frontPage.getGlobalQuery())
                .or().like(AbnormalNcCode::getNcCode, frontPage.getGlobalQuery())
                .or().like(AbnormalNcCode::getNcQty, frontPage.getGlobalQuery())
                .or().like(AbnormalNcCode::getCreatedUser, frontPage.getGlobalQuery())
                .or().like(AbnormalNcCode::getModifiedUser, frontPage.getGlobalQuery())
    );
        }
        result = abnormalNcCodeService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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