package com.foreverwin.mesnac.meapi.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.meapi.service.DcGroupService;
import com.foreverwin.mesnac.meapi.model.DcGroup;
import java.util.List;

/**
 *
 * @author pavel.liu
 * @since 2021-07-13
 */
@RestController
@RequestMapping("/DC-GROUP")
public class DcGroupController {

    @Autowired
    public DcGroupService dcGroupService;

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<DcGroup> frontPage, DcGroup dcGroup){
        IPage result;
        QueryWrapper<DcGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(dcGroup);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(DcGroup::getHandle, frontPage.getGlobalQuery())
                .or().like(DcGroup::getDcGroup, frontPage.getGlobalQuery())
                .or().like(DcGroup::getDescription, frontPage.getGlobalQuery())
                .or().like(DcGroup::getSite, frontPage.getGlobalQuery())
                .or().like(DcGroup::getCollectDataAt, frontPage.getGlobalQuery())
                .or().like(DcGroup::getCustomButtonId, frontPage.getGlobalQuery())
                .or().like(DcGroup::getPassFailGroup, frontPage.getGlobalQuery())
                .or().like(DcGroup::getRevision, frontPage.getGlobalQuery())
                .or().like(DcGroup::getCurrentRevision, frontPage.getGlobalQuery())
                .or().like(DcGroup::getStatusBo, frontPage.getGlobalQuery())
                .or().like(DcGroup::getAuthenticationRequired, frontPage.getGlobalQuery())
                .or().like(DcGroup::getErp, frontPage.getGlobalQuery())
                .or().like(DcGroup::getCollectMethod, frontPage.getGlobalQuery())
                .or().like(DcGroup::getCollectionType, frontPage.getGlobalQuery())
                .or().like(DcGroup::getErpInspection, frontPage.getGlobalQuery())
                .or().like(DcGroup::getErpWholeGroupInspection, frontPage.getGlobalQuery())
                .or().like(DcGroup::getMeEvalInspection, frontPage.getGlobalQuery())
    );
        }
        result = dcGroupService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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