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.Tool;
import com.foreverwin.mesnac.common.service.ToolService;
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;

/**
 *
 * @author pavel.liu
 * @since 2021-07-14
 */
@RestController
@RequestMapping("/Z-TOOL")
public class ToolController {

    @Autowired
    public ToolService toolService;

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

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R getToolList(Tool tool){
        String site = CommonMethods.getSite();
        tool.setSite(site);
        List<Tool> result;
        QueryWrapper<Tool> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(tool);
        result = toolService.list(queryWrapper);
        return R.ok(result);
    }

    /**
     * 查询所有数据，状态为Y的刀具
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/getToolListByStatus")
    public R getToolListByStatus(Tool tool){
        String site = CommonMethods.getSite();
        tool.setSite(site);
        tool.setStatus(Constants.TOOL_STATUS_Y);
        List<Tool> result;
        QueryWrapper<Tool> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(tool);
        result = toolService.list(queryWrapper);
        return R.ok(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<Tool> frontPage, Tool tool){
        IPage result;
        QueryWrapper<Tool> queryWrapper = new QueryWrapper<>();
        tool.setSite(CommonMethods.getSite());
        queryWrapper.setEntity(tool);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(Tool::getHandle, frontPage.getGlobalQuery())
                .or().like(Tool::getSite, frontPage.getGlobalQuery())
                .or().like(Tool::getTool, frontPage.getGlobalQuery())
                .or().like(Tool::getDescription, frontPage.getGlobalQuery())
                    .or().like(Tool::getStatus, frontPage.getGlobalQuery())
                .or().like(Tool::getToolType, frontPage.getGlobalQuery())
                .or().like(Tool::getSpecification, frontPage.getGlobalQuery())
                .or().like(Tool::getBrand, frontPage.getGlobalQuery())
                .or().like(Tool::getRemark, frontPage.getGlobalQuery())
                .or().like(Tool::getCreateUser, frontPage.getGlobalQuery())
                .or().like(Tool::getModifyUser, frontPage.getGlobalQuery())
    );
        }
        result = toolService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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


    /**
     * 新增或者更新
     * @param tool  传递的实体
     * @return  null 失败  实体成功
     */
    @PostMapping(value =  "/saveOrUpdateOne")
    public R saveOrUpdateOne(@RequestBody Tool tool) {
        try{
            toolService.saveOrUpdateOne(tool);
        }catch (Exception e){
            return R.failed("数据保存失败" + e.getMessage());
        }
        return R.ok();
    }

    @ResponseBody
    @GetMapping("/getOneToolByToolNo")
    public R getOneToolByToolNo(Tool tool) {
        try{
            return R.ok(toolService.getOneToolByToolNo(tool));
        }catch (Exception e){
            return R.failed("数据查询失败：" + e.getMessage());
        }
    }


    @PostMapping("/deleteInspectionPlanByHandle")
    public R deleteInspectionPlanByHandle(@RequestBody String tool){
        try {
            toolService.deleteInspectionPlanByHandle(tool);
        } catch (Exception e) {
            R.failed("计划删除失败：" + e.getMessage());
        }
        return R.ok();
    }


}