package com.foreverwin.mesnac.production.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreverwin.mesnac.production.model.SfcHoldLog;
import com.foreverwin.mesnac.production.service.SfcHoldLogService;
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-08-19
 */
@RestController
@RequestMapping("/Z-SFC-HOLD-LOG")
public class SfcHoldLogController {

    @Autowired
    public SfcHoldLogService sfcHoldLogService;

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/sfcHold")
    public R sfcHold(@RequestBody Map<String,Object> map){
        List<SfcHoldLog> sfcHoldLogList = (List<SfcHoldLog>) map.get("sfcHoldLogList");
        ObjectMapper mapper = new ObjectMapper();
        sfcHoldLogList = mapper.convertValue(sfcHoldLogList, new TypeReference<List<SfcHoldLog>>() {
        });
        sfcHoldLogService.sfcHold(sfcHoldLogList);
        return R.ok();
    }
    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public R getSfcHoldLogById(@PathVariable String id) {
        return R.ok( sfcHoldLogService.getById(id));
    }

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<SfcHoldLog> frontPage, SfcHoldLog sfcHoldLog){
        IPage result;
        QueryWrapper<SfcHoldLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(sfcHoldLog);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(SfcHoldLog::getHandle, frontPage.getGlobalQuery())
                .or().like(SfcHoldLog::getSite, frontPage.getGlobalQuery())
                .or().like(SfcHoldLog::getSfcDispatchBo, frontPage.getGlobalQuery())
                .or().like(SfcHoldLog::getSfc, frontPage.getGlobalQuery())
                .or().like(SfcHoldLog::getStepId, frontPage.getGlobalQuery())
                .or().like(SfcHoldLog::getOperation, frontPage.getGlobalQuery())
                .or().like(SfcHoldLog::getType, frontPage.getGlobalQuery())
                .or().like(SfcHoldLog::getCreateUser, frontPage.getGlobalQuery())
    );
        }
        result = sfcHoldLogService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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