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.model.NcCode;
import com.foreverwin.mesnac.meapi.model.NcGroup;
import com.foreverwin.mesnac.meapi.service.NcCodeService;
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-CODE")
public class NcCodeController {

    @Autowired
    public NcCodeService ncCodeService;

    @ResponseBody
    @GetMapping("/selectByNcGroup")
    public R selectPageByNcGroup(String ncGroup){
        List<NcCode> result;
        result = ncCodeService.selectByNcGroup(ncGroup);
        return R.ok(result);
    }
    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public R getNcCodeById(@PathVariable String id) {
        return R.ok( ncCodeService.getById(id));
    }

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

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

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

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

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

    /**
     * 根据不良代码组找出它下面的所有的不良代码，并且是主要代码
     */
    @GetMapping("/findNcByCategory")
    public R findNcByCategory(String category,String code){
        return R.ok(ncCodeService.findNcByCategory(category,code));
    }

    @GetMapping("/findSecondaryNcByNc")
    public R findSecondaryNcByNcI(String ncCode){
        return R.ok(ncCodeService.findSecondaryNcByNc(ncCode));
    }

    @GetMapping("/findNcCodeByNcGroup")
    public R findNcCodeByNcGroup(NcGroup ncGroup){
        return R.ok(ncCodeService.findNcCodeByNcGroup(ncGroup));
    }

    /**
     * 根据不合格代码组查询不合格代码
     * @param ncGroup
     * @return
     */
    @GetMapping("/findNcCodeByNcGroupList")
    public R findNcCodeByNcGroupList(String ncGroup){
        return R.ok(ncCodeService.findNcCodeByNcGroupList(ncGroup));
    }

}










