package com.foreverwin.mesnac.equip.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.equip.service.EdcDataResourceService;
import com.foreverwin.mesnac.equip.model.EdcDataResource;
import java.util.List;

/**
 *
 * @author pavel.liu
 * @since 2021-07-13
 */
@RestController
@RequestMapping("/Z-EDC-DATA-RESOURCE")
public class EdcDataResourceController {

    @Autowired
    public EdcDataResourceService edcDataResourceService;

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<EdcDataResource> frontPage, EdcDataResource edcDataResource){
        IPage result;
        QueryWrapper<EdcDataResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(edcDataResource);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(EdcDataResource::getHandle, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getSite, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getResrce, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getDcGroupBo, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getCreateUser, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS1, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS2, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS3, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS4, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS5, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS6, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS7, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS8, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS9, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS10, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS11, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS12, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS13, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS14, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS15, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS16, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS17, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS18, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS19, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS20, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS21, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS22, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS23, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS24, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS25, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS26, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS27, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS28, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS29, frontPage.getGlobalQuery())
                .or().like(EdcDataResource::getS30, frontPage.getGlobalQuery())
    );
        }
        result = edcDataResourceService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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