package com.foreverwin.mesnac.equip.controller;

import com.foreverwin.mesnac.common.enums.HandleEnum;
import com.foreverwin.mesnac.equip.dto.ResourceInspectTaskDto;
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.equip.service.ResourceInspectTaskService;
import com.foreverwin.mesnac.equip.model.ResourceInspectTask;
import java.util.List;

/**
 *
 * @author pavel_liu
 * @since 2021-07-20
 */
@RestController
@RequestMapping("/Z-RESOURCE-INSPECT-TASK")
public class ResourceInspectTaskController {

    @Autowired
    public ResourceInspectTaskService resourceInspectTaskService;

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<ResourceInspectTask> frontPage, ResourceInspectTask resourceInspectTask){
        IPage result;
        QueryWrapper<ResourceInspectTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(resourceInspectTask);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(ResourceInspectTask::getHandle, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTask::getSite, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTask::getTaskNo, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTask::getDescription, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTask::getStatus, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTask::getResourceInspectPlanBo, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTask::getInspectionItemBo, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTask::getResrce, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTask::getCategory, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTask::getResult, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTask::getReason, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTask::getExecutor, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTask::getRemark, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTask::getScheduleType, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTask::getFrequencyUnit, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTask::getCreateUser, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTask::getModifyUser, frontPage.getGlobalQuery())
    );
        }
        result = resourceInspectTaskService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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


    /**
     * 模糊查询检验任务
     * @param resourceInspectTaskDto
     * @return
     */
    @ResponseBody
    @GetMapping("/findResourceInspectTaskList")
    public R findResourceInspectTaskList(ResourceInspectTaskDto resourceInspectTaskDto){
        String site = CommonMethods.getSite();
        String planNo = resourceInspectTaskDto.getPlanNo();
        String resourceNo = resourceInspectTaskDto.getResrce();
        String resourceTypeNo = resourceInspectTaskDto.getResourceTypeNo();
        String workCenter = resourceInspectTaskDto.getWorkCenter();
        if (planNo != null){
            resourceInspectTaskDto.setResourceInspectPlanBo(HandleEnum.RESOURCE_INSPECT_PLAN.getHandle(site,planNo,resourceInspectTaskDto.getPlanRevision()));
        }
        if (resourceNo != null){
            resourceInspectTaskDto.setResourceBo(HandleEnum.RESOURCE.getHandle(site,resourceNo));
        }
        if (resourceTypeNo != null){
            resourceInspectTaskDto.setResourceTypeBo(HandleEnum.RESOURCE_TYPE.getHandle(site,resourceTypeNo));
        }
        if (workCenter != null){
            resourceInspectTaskDto.setWorkCenter(HandleEnum.WORK_CENTER.getHandle(site,workCenter));
        }
        try{
            return R.ok(resourceInspectTaskService.findResourceInspectTaskList(resourceInspectTaskDto));
        }catch (Exception e){
            return R.failed("检索失败：" + e.getMessage());
        }
    }


    /**
     * 任务总览下：点击任务
     *  1、非新建任务，直接返回数据
     *  2、新建任务，需比对检验项清单HANDLE，并创建检验项明细
     * @param resourceInspectTask
     * @return
     */
    @ResponseBody
    @GetMapping("/getAndCreateTaskParamList")
    public R getOrCreateTaskParamList(ResourceInspectTask resourceInspectTask){
        try {
            return R.ok(resourceInspectTaskService.getResourceInspectTaskAll(resourceInspectTask));
        }catch (Exception e){
            return R.failed("获取数据失败" + e.getMessage());
        }
    }

    /**
     * 任务执行：
     *  1、点击开始执行，仅修改任务状态、绑定执行人
     *  2、取消执行，仅修改任务状态
     * @param resourceInspectTask
     * @return
     */
    @ResponseBody
    @GetMapping("/startOrCancelTask")
    public R startOrCancelTask(ResourceInspectTask resourceInspectTask){
        try {
            return R.ok(resourceInspectTaskService.startOrCancelTask(resourceInspectTask));
        }catch (Exception e){
            return R.failed("数据更新失败" + e.getMessage());
        }
    }

    /***
     * 任务执行，状态为处理中/执行中时，点击保存、完成执行按钮：
     *  1、仅保存数据
     *  2、完成，保存数据
     * @param
     * @return
     */
    @PostMapping ("/saveOrUpdateTaskInfo")
    public R saveOrUpdateTaskInfo(@RequestBody ResourceInspectTaskDto resourceInspectTaskDto){
        try {
            resourceInspectTaskService.saveOrUpdateTaskInfo(resourceInspectTaskDto);
        }catch (Exception e){
            return R.failed("数据保存失败" + e.getMessage());
        }
        return R.ok();
    }

    /**
     * 扫码设备编号，获取当天的点检任务
     * @param resourceInspectTaskDto
     * @return
     */
    @ResponseBody
    @GetMapping("/getInspectTaskByResource")
    public R getInspectTaskByResource(ResourceInspectTaskDto resourceInspectTaskDto){
        try {
            return R.ok(resourceInspectTaskService.getInspectTaskByResource(resourceInspectTaskDto));
        }catch (Exception e){
            return R.failed("查询失败：" + e.getMessage());
        }
    }




}