package com.foreverwin.mesnac.common.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.common.model.InspectionItemDetail;
import com.foreverwin.mesnac.common.service.InspectionItemDetailService;
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-06-09
 */
@RestController
@RequestMapping("/Z-INSPECTION-ITEM-DETAIL")
public class InspectionItemDetailController {

    @Autowired
    public InspectionItemDetailService inspectionItemDetailService;

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<InspectionItemDetail> frontPage, InspectionItemDetail inspectionItemDetail){
        IPage result;
        QueryWrapper<InspectionItemDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(inspectionItemDetail);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(InspectionItemDetail::getHandle, frontPage.getGlobalQuery())
                .or().like(InspectionItemDetail::getInspectionItemBo, frontPage.getGlobalQuery())
                .or().like(InspectionItemDetail::getParamNo, frontPage.getGlobalQuery())
                .or().like(InspectionItemDetail::getDescription, frontPage.getGlobalQuery())
                .or().like(InspectionItemDetail::getInspectionMethod, frontPage.getGlobalQuery())
                .or().like(InspectionItemDetail::getParamType, frontPage.getGlobalQuery())
                .or().like(InspectionItemDetail::getParamUnit, frontPage.getGlobalQuery())
                .or().like(InspectionItemDetail::getStandardValue, frontPage.getGlobalQuery())
                .or().like(InspectionItemDetail::getFalseValue, frontPage.getGlobalQuery())
                .or().like(InspectionItemDetail::getTrueValue, frontPage.getGlobalQuery())
                .or().like(InspectionItemDetail::getIsRequired, frontPage.getGlobalQuery())
                .or().like(InspectionItemDetail::getCreateUser, frontPage.getGlobalQuery())
                .or().like(InspectionItemDetail::getModifyUser, frontPage.getGlobalQuery())
    );
        }
        result = inspectionItemDetailService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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