package com.foreverwin.mesnac.dispatch.controller;

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.dispatch.service.CallItemService;
import com.foreverwin.mesnac.dispatch.model.CallItem;
import java.util.List;

/**
 *
 * @author Leon.L
 * @since 2021-06-24
 */
@RestController
@RequestMapping("/Z-CALL-ITEM")
public class CallItemController {

    @Autowired
    public CallItemService callItemService;

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<CallItem> frontPage, CallItem callItem){
        IPage result;
        QueryWrapper<CallItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(callItem);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(CallItem::getHandle, frontPage.getGlobalQuery())
                .or().like(CallItem::getSite, frontPage.getGlobalQuery())
                .or().like(CallItem::getCallItemNo, frontPage.getGlobalQuery())
                .or().like(CallItem::getShopOrder, frontPage.getGlobalQuery())
                .or().like(CallItem::getSfc, frontPage.getGlobalQuery())
                .or().like(CallItem::getDispatchNo, frontPage.getGlobalQuery())
                .or().like(CallItem::getOperation, frontPage.getGlobalQuery())
                .or().like(CallItem::getStepId, frontPage.getGlobalQuery())
                .or().like(CallItem::getResrce, frontPage.getGlobalQuery())
                .or().like(CallItem::getComponentBo, frontPage.getGlobalQuery())
                .or().like(CallItem::getCallType, frontPage.getGlobalQuery())
                .or().like(CallItem::getStatus, frontPage.getGlobalQuery())
                .or().like(CallItem::getCallStatus, frontPage.getGlobalQuery())
                .or().like(CallItem::getSendStatus, frontPage.getGlobalQuery())
                .or().like(CallItem::getCreateUserBo, frontPage.getGlobalQuery())
                .or().like(CallItem::getUpdateUserBo, frontPage.getGlobalQuery())
    );
        }
        result = callItemService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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