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.constant.Constants;
import com.foreverwin.mesnac.common.model.ProdReadyTask;
import com.foreverwin.mesnac.common.service.ProdReadyTaskService;
import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.modular.core.exception.BaseException;
import com.foreverwin.modular.core.util.CommonMethods;
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-06-23
 */
@RestController
@RequestMapping("/Z-PROD-READY-TASK")
public class ProdReadyTaskController {

    @Autowired
    public ProdReadyTaskService prodReadyTaskService;

    /**
     * 检索
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/doPrint")
    public R doPrint(@RequestBody List<ProdReadyTask> list){
        for (ProdReadyTask readyTask:list){
            ProdReadyTask prodReadyTask = prodReadyTaskService.getById(readyTask.getHandle());
            String result = prodReadyTask.getResult();
            if (StringUtil.isBlank(result) || !prodReadyTask.getResult().equals(Constants.RSESULT_OK)){
                throw new BaseException("未准备合格不能打印");
            }
            prodReadyTaskService.doPrint(prodReadyTask,readyTask.getStatus());
        }
        return R.ok();
    }
    /**
     * 检索
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/doListReady")
    public R doListReady(@RequestBody List<ProdReadyTask> list){
        prodReadyTaskService.doReadyList(list);
        return R.ok();
    }
    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R getProdReadyTaskList(ProdReadyTask prodReadyTask){
        List<ProdReadyTask> result;
        QueryWrapper<ProdReadyTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(prodReadyTask);
        result = prodReadyTaskService.list(queryWrapper);
        return R.ok(result);
    }
    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public R getProdReadyTaskById(@PathVariable String id) {
        return R.ok( prodReadyTaskService.getById(id));
    }

    /**
     *检验明细
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/getInspectionDetailList")
    public R getInspectionDetailList(@RequestBody Map<String,String> param){
        List<Map<String, Object>> result;
        result = prodReadyTaskService.getInspectionDetailList(param);
        return R.ok(result);
    }

    /**
     * 检索
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/getTaskList")
    public R getTaskList(ProdReadyTask prodReadyTask){
        List<ProdReadyTask> result;
        prodReadyTask.setSite(CommonMethods.getSite());
        result = prodReadyTaskService.getTaskList(prodReadyTask);
        return R.ok(result);
    }
    /**
     * 生产准备执行检索
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/loadProExecute")
    public R loadProExecute(String taskBo){
        ProdReadyTask result;
        result = prodReadyTaskService.loadProExecute(taskBo);
        return R.ok(result);
    }
    /**
     * 生产准备执行检索PDA
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/loadTaskExecute")
    public R loadTaskExecute(String taskBo){
        Map<String, Object> result;
        result = prodReadyTaskService.loadTaskExecute(taskBo);
        return R.ok(result);
    }
    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
    public R saveResult(@RequestBody ProdReadyTask prodReadyTask){
        boolean result=
         prodReadyTaskService.saveResult(prodReadyTask);
        return R.ok(result);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/pdaSave")
    public R savePdaResult(@RequestBody ProdReadyTask prodReadyTask){
        prodReadyTaskService.savePdaResult(prodReadyTask);
        return R.ok();
    }

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

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

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

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