package com.foreverwin.mesnac.meapi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.meapi.dto.OperationDto;
import com.foreverwin.mesnac.meapi.model.NcGroup;
import com.foreverwin.mesnac.meapi.model.Operation;
import com.foreverwin.mesnac.meapi.service.NcGroupService;
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 robert
 * @since 2021-06-23
 */
@RestController
@RequestMapping("/NC-GROUP")
public class NcGroupController {

    @Autowired
    public NcGroupService ncGroupService;

    @ResponseBody
    @GetMapping("/selectByOperation")
    public R selectPageByOperation(OperationDto dto){
        List<NcGroup> result;
        result = ncGroupService.selectPageByOperation(dto);
        return R.ok(result);
    }

    /**
     * 根据工序查询不良代码组，不分页
     * @param operation
     * @return
     */
    @ResponseBody
    @GetMapping("/selectPageByOperationNoPage")
    public R selectPageByOperationNoPage(String operation){
        List<NcGroup> result;
        try{
            result = ncGroupService.selectPageByOperationNoPage(operation);
        }catch (Exception e){
            return R.failed(e.getMessage());
        }
        return R.ok(result);
    }
    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public R getNcGroupById(@PathVariable String id) {
        return R.ok( ncGroupService.getById(id));
    }

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<NcGroup> frontPage, NcGroup ncGroup){
        IPage result;
        QueryWrapper<NcGroup> queryWrapper = new QueryWrapper<>();
        String site = CommonMethods.getSite();
        ncGroup.setSite(site);
        queryWrapper.setEntity(ncGroup);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(NcGroup::getHandle, frontPage.getGlobalQuery())
                .or().like(NcGroup::getSite, frontPage.getGlobalQuery())
                .or().like(NcGroup::getNcGroup, frontPage.getGlobalQuery())
                .or().like(NcGroup::getDescription, frontPage.getGlobalQuery())
                .or().like(NcGroup::getValidAtAllOperations, frontPage.getGlobalQuery())
    );
        }
        result = ncGroupService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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

    @GetMapping("/findNgGroupByOperationBo")
    public R findNgGroupByOperationBo(Operation operation){
        return R.ok(ncGroupService.findNgGroupByOperationBo(operation));
    }
}