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.InspectionTask;
import com.foreverwin.mesnac.common.service.InspectionTaskService;
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;
import java.util.Map;

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

    @Autowired
    public InspectionTaskService inspectionTaskService;

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
    public R getInspectionTaskList(@RequestBody Map<String, Object> paramMap){
        inspectionTaskService.saveTask(paramMap);
        return R.ok(null,"保存成功");
    }
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/createTask")
    public R createTask(@RequestBody Map<String, Object> paramMap) {
        Map<String, Object> map = inspectionTaskService.createTask(paramMap);
        return R.ok(map);
    }

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<InspectionTask> frontPage, InspectionTask inspectionTask){
        IPage result;
        QueryWrapper<InspectionTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(inspectionTask);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(InspectionTask::getHandle, frontPage.getGlobalQuery())
                .or().like(InspectionTask::getSite, frontPage.getGlobalQuery())
                .or().like(InspectionTask::getCategory, frontPage.getGlobalQuery())
                .or().like(InspectionTask::getTaskNo, frontPage.getGlobalQuery())
                .or().like(InspectionTask::getDescription, frontPage.getGlobalQuery())
                .or().like(InspectionTask::getStatus, frontPage.getGlobalQuery())
                .or().like(InspectionTask::getInspectionItemBo, frontPage.getGlobalQuery())
                .or().like(InspectionTask::getWorkCenter, frontPage.getGlobalQuery())
                .or().like(InspectionTask::getShopOrder, frontPage.getGlobalQuery())
                .or().like(InspectionTask::getSfc, frontPage.getGlobalQuery())
                .or().like(InspectionTask::getOperation, frontPage.getGlobalQuery())
                .or().like(InspectionTask::getStepId, frontPage.getGlobalQuery())
                .or().like(InspectionTask::getResrce, frontPage.getGlobalQuery())
                .or().like(InspectionTask::getResult, frontPage.getGlobalQuery())
                .or().like(InspectionTask::getComments, frontPage.getGlobalQuery())
                .or().like(InspectionTask::getCreateUser, frontPage.getGlobalQuery())
                .or().like(InspectionTask::getModifyUser, frontPage.getGlobalQuery())
                .or().like(InspectionTask::getSfcDispatchBo, frontPage.getGlobalQuery())
    );
        }
        result = inspectionTaskService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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