package com.foreverwin.mesnac.equip.controller;

import com.foreverwin.mesnac.common.enums.HandleEnum;
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.ResourceRepairTaskService;
import com.foreverwin.mesnac.equip.model.ResourceRepairTask;
import java.util.List;

/**
 *
 * @author pavel.Liu
 * @since 2021-07-07
 */
@RestController
@RequestMapping("/Z-RESOURCE-REPAIR-TASK")
public class ResourceRepairTaskController {

    @Autowired
    public ResourceRepairTaskService resourceRepairTaskService;


    /**
     * 界面数据字段初始化
     * @param
     * @return
     */
    @GetMapping("/init")
    public R init(){
        try{
            return R.ok(resourceRepairTaskService.init());
        }catch (Exception e){
            return R.failed("" + e.getMessage());
        }
    }

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<ResourceRepairTask> frontPage, ResourceRepairTask resourceRepairTask){
        IPage result;
        QueryWrapper<ResourceRepairTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(resourceRepairTask);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(ResourceRepairTask::getHandle, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getSite, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getRepairNo, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getDescription, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getStatus, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getResourceNo, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getReportUser, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getNcCodeNo, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getRepairType, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getStopMachine, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getBreakDownDescription, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getFindLink, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getReportSource, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getObjectBo, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getRepairUsers, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getCancelCode, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getCancelDescription, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getCancelUser, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getDealWay, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getPreliminaryReason, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getDealWayDescription, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getWaitDescription, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getPrimaryReason, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getPrimaryReasonDescription, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getPrecautionMeasureDescription, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getRemark, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getOther, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getAnother, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getCreateUser, frontPage.getGlobalQuery())
                .or().like(ResourceRepairTask::getModifyUser, frontPage.getGlobalQuery())
    );
        }
        result = resourceRepairTaskService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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

    /**
     * 保存提报的维修单，返回维修单号
     * @param resourceRepairTask
     * @return
     */
    @ResponseBody
    @GetMapping("/saveRepairTask")
    public R saveRepairTask(ResourceRepairTask resourceRepairTask){
        try{
            resourceRepairTaskService.saveRepairTask(resourceRepairTask);
        }catch (Exception e){
            return R.failed("提报失败:"+e.getMessage());
        }
        return R.ok();
    }

    /**
     * 条件查询，查询维修任务
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/findResourceRepairTaskList")
    public R findResourceRepairTaskList(ResourceRepairTask resourceRepairTask){
        String site = CommonMethods.getSite();
        String workCenterNo = resourceRepairTask.getWorkCenterNo();
        String resourceNo = resourceRepairTask.getResourceNo();
        String resourceTypeNo = resourceRepairTask.getResourceTypeNo();
        if (workCenterNo != null){
            resourceRepairTask.setWorkCenterBo(HandleEnum.WORK_CENTER.getHandle(site,workCenterNo));
        }
        if (resourceNo != null){
            resourceRepairTask.setResourceBo(HandleEnum.RESOURCE.getHandle(site,resourceNo));
        }
        if (resourceTypeNo != null){
            resourceRepairTask.setResourceTypeBo(HandleEnum.RESOURCE_TYPE.getHandle(site,resourceTypeNo));
        }
        try {
            return R.ok(resourceRepairTaskService.findResourceRepairTaskList(resourceRepairTask));
        }catch (Exception e){
            return R.failed(""+e.getMessage());
        }
    }

    /**
     * 条件查询，查询维修任务
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/selOrStartRepairTask")
    public R selOrStartRepairTask(ResourceRepairTask resourceRepairTask){
        try {
            return R.ok(resourceRepairTaskService.selOrStartRepairTask(resourceRepairTask));
        }catch (Exception e){
            return R.failed(""+e.getMessage());
        }
    }

    /**
     * 条件查询，查询维修任务
     *
     * @return
     */
    @PostMapping("/saveOrUpdateTaskInfo")
    public R saveOrUpdateTaskInfo(@RequestBody ResourceRepairTask resourceRepairTask){
        try {
            return R.ok(resourceRepairTaskService.saveOrUpdateTaskInfo(resourceRepairTask));
        }catch (Exception e){
            return R.failed(""+e.getMessage());
        }
    }

    /**
     * 自定义sql，根据维修任务HANDLE，返回详细维修任务数据：
     *  1、维修任务纠正预防；
     *  2、维修任务数据修改。
     * @return
     */
    @ResponseBody
    @GetMapping("/findResourceRepairTaskByHandleAndSite")
    public R findResourceRepairTaskByHandleAndSite(String repairNo){
        String site = CommonMethods.getSite();
        String handle = HandleEnum.RESOURCE_REPAIR_TASK.getHandle(site, repairNo);
        try {
            return R.ok(resourceRepairTaskService.findResourceRepairTaskByHandleAndSite(site,handle));
        }catch (Exception e){
            return R.failed("查询失败："+e.getMessage());
        }
    }

    /**
     * 维修任务纠正预防，自定义sql，根据维修任务HANDLE，
     * 返回详细维修任务数据
     * @return
     */
    @PostMapping("/updateResourceRepairTaskByHandle")
    public R updateResourceRepairTaskByHandle(@RequestBody ResourceRepairTask resourceRepairTask){
        String handle = resourceRepairTask.getHandle();
        ResourceRepairTask repairTask = resourceRepairTaskService.getById(handle);
        repairTask.setPrimaryReason(resourceRepairTask.getPrimaryReason());
        repairTask.setPrecautionMeasureDescription(resourceRepairTask.getPrecautionMeasureDescription());
        repairTask.setPrimaryReasonDescription(resourceRepairTask.getPrimaryReasonDescription());
        try {
            return R.ok(resourceRepairTaskService.updateById(repairTask));
        }catch (Exception e){
            return R.failed("纠正预防更新失败："+e.getMessage());
        }
    }

    /**
     * 维修任务纠正预防，自定义sql，根据维修任务HANDLE，
     * 返回详细维修任务数据
     * @return
     */
    @PostMapping("/updateResourceRepairTaskInfoByHandle")
    public R updateResourceRepairTaskInfoByHandle(@RequestBody ResourceRepairTask resourceRepairTask){
        try {
            return R.ok(resourceRepairTaskService.updateResourceRepairTaskInfoByHandle(resourceRepairTask));
        }catch (Exception e){
            return R.failed("维修任务数据更改失败："+e.getMessage());
        }
    }


}