package com.foreverwin.mesnac.common.controller;

import com.foreverwin.modular.core.util.R;
import com.foreverwin.modular.core.util.FrontPage;
import com.foreverwin.modular.core.util.CommonMethods;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mesnac.common.service.InspectionTaskDetailService;
import com.foreverwin.mesnac.common.model.InspectionTaskDetail;
import java.util.List;

/**
 *
 * @author Philip
 * @since 2021-07-05
 */
@RestController
@RequestMapping("/Z-INSPECTION-TASK-DETAIL")
public class InspectionTaskDetailController {

    @Autowired
    public InspectionTaskDetailService inspectionTaskDetailService;

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<InspectionTaskDetail> frontPage, InspectionTaskDetail inspectionTaskDetail){
        IPage result;
        QueryWrapper<InspectionTaskDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(inspectionTaskDetail);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(InspectionTaskDetail::getHandle, frontPage.getGlobalQuery())
                .or().like(InspectionTaskDetail::getSite, frontPage.getGlobalQuery())
                .or().like(InspectionTaskDetail::getTaskBo, frontPage.getGlobalQuery())
                .or().like(InspectionTaskDetail::getInspectionItemDetailBo, frontPage.getGlobalQuery())
                .or().like(InspectionTaskDetail::getCheckValues, frontPage.getGlobalQuery())
                .or().like(InspectionTaskDetail::getResult, frontPage.getGlobalQuery())
                .or().like(InspectionTaskDetail::getCheckUser, frontPage.getGlobalQuery())
                .or().like(InspectionTaskDetail::getRemark, frontPage.getGlobalQuery())
                .or().like(InspectionTaskDetail::getPictureNo, frontPage.getGlobalQuery())
                .or().like(InspectionTaskDetail::getSfc, frontPage.getGlobalQuery())
                .or().like(InspectionTaskDetail::getPictureQty, frontPage.getGlobalQuery())
    );
        }
        result = inspectionTaskDetailService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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